x_long.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1t.h>
  12. #define COPY_SIZE(a, b) (sizeof(a) < sizeof(b) ? sizeof(a) : sizeof(b))
  13. /*
  14. * Custom primitive type for long handling. This converts between an
  15. * ASN1_INTEGER and a long directly.
  16. */
  17. static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
  18. static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
  19. static int long_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
  20. const ASN1_ITEM *it);
  21. static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  22. int utype, char *free_cont, const ASN1_ITEM *it);
  23. static int long_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
  24. int indent, const ASN1_PCTX *pctx);
  25. static ASN1_PRIMITIVE_FUNCS long_pf = {
  26. NULL, 0,
  27. long_new,
  28. long_free,
  29. long_free, /* Clear should set to initial value */
  30. long_c2i,
  31. long_i2c,
  32. long_print
  33. };
  34. ASN1_ITEM_start(LONG)
  35. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, ASN1_LONG_UNDEF, "LONG"
  36. ASN1_ITEM_end(LONG)
  37. ASN1_ITEM_start(ZLONG)
  38. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &long_pf, 0, "ZLONG"
  39. ASN1_ITEM_end(ZLONG)
  40. static int long_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  41. {
  42. memcpy(pval, &it->size, COPY_SIZE(*pval, it->size));
  43. return 1;
  44. }
  45. static void long_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  46. {
  47. memcpy(pval, &it->size, COPY_SIZE(*pval, it->size));
  48. }
  49. /*
  50. * Originally BN_num_bits_word was called to perform this operation, but
  51. * trouble is that there is no guarantee that sizeof(long) equals to
  52. * sizeof(BN_ULONG). BN_ULONG is a configurable type that can be as wide
  53. * as long, but also double or half...
  54. */
  55. static int num_bits_ulong(unsigned long value)
  56. {
  57. size_t i;
  58. unsigned long ret = 0;
  59. /*
  60. * It is argued that *on average* constant counter loop performs
  61. * not worse [if not better] than one with conditional break or
  62. * mask-n-table-lookup-style, because of branch misprediction
  63. * penalties.
  64. */
  65. for (i = 0; i < sizeof(value) * 8; i++) {
  66. ret += (value != 0);
  67. value >>= 1;
  68. }
  69. return (int)ret;
  70. }
  71. static int long_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
  72. const ASN1_ITEM *it)
  73. {
  74. long ltmp;
  75. unsigned long utmp, sign;
  76. int clen, pad, i;
  77. memcpy(&ltmp, pval, COPY_SIZE(*pval, ltmp));
  78. if (ltmp == it->size)
  79. return -1;
  80. /*
  81. * Convert the long to positive: we subtract one if negative so we can
  82. * cleanly handle the padding if only the MSB of the leading octet is
  83. * set.
  84. */
  85. if (ltmp < 0) {
  86. sign = 0xff;
  87. utmp = 0 - (unsigned long)ltmp - 1;
  88. } else {
  89. sign = 0;
  90. utmp = ltmp;
  91. }
  92. clen = num_bits_ulong(utmp);
  93. /* If MSB of leading octet set we need to pad */
  94. if (!(clen & 0x7))
  95. pad = 1;
  96. else
  97. pad = 0;
  98. /* Convert number of bits to number of octets */
  99. clen = (clen + 7) >> 3;
  100. if (cont != NULL) {
  101. if (pad)
  102. *cont++ = (unsigned char)sign;
  103. for (i = clen - 1; i >= 0; i--) {
  104. cont[i] = (unsigned char)(utmp ^ sign);
  105. utmp >>= 8;
  106. }
  107. }
  108. return clen + pad;
  109. }
  110. static int long_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  111. int utype, char *free_cont, const ASN1_ITEM *it)
  112. {
  113. int i;
  114. long ltmp;
  115. unsigned long utmp = 0, sign = 0x100;
  116. if (len > 1) {
  117. /*
  118. * Check possible pad byte. Worst case, we're skipping past actual
  119. * content, but since that's only with 0x00 and 0xff and we set neg
  120. * accordingly, the result will be correct in the end anyway.
  121. */
  122. switch (cont[0]) {
  123. case 0xff:
  124. cont++;
  125. len--;
  126. sign = 0xff;
  127. break;
  128. case 0:
  129. cont++;
  130. len--;
  131. sign = 0;
  132. break;
  133. }
  134. }
  135. if (len > (int)sizeof(long)) {
  136. ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
  137. return 0;
  138. }
  139. if (sign == 0x100) {
  140. /* Is it negative? */
  141. if (len && (cont[0] & 0x80))
  142. sign = 0xff;
  143. else
  144. sign = 0;
  145. } else if (((sign ^ cont[0]) & 0x80) == 0) { /* same sign bit? */
  146. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_PADDING);
  147. return 0;
  148. }
  149. utmp = 0;
  150. for (i = 0; i < len; i++) {
  151. utmp <<= 8;
  152. utmp |= cont[i] ^ sign;
  153. }
  154. ltmp = (long)utmp;
  155. if (ltmp < 0) {
  156. ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
  157. return 0;
  158. }
  159. if (sign)
  160. ltmp = -ltmp - 1;
  161. if (ltmp == it->size) {
  162. ERR_raise(ERR_LIB_ASN1, ASN1_R_INTEGER_TOO_LARGE_FOR_LONG);
  163. return 0;
  164. }
  165. memcpy(pval, &ltmp, COPY_SIZE(*pval, ltmp));
  166. return 1;
  167. }
  168. static int long_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
  169. int indent, const ASN1_PCTX *pctx)
  170. {
  171. long l;
  172. memcpy(&l, pval, COPY_SIZE(*pval, l));
  173. return BIO_printf(out, "%ld\n", l);
  174. }