x_int64.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright 2017-2021 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 "internal/numbers.h"
  12. #include <openssl/asn1t.h>
  13. #include <openssl/bn.h>
  14. #include "asn1_local.h"
  15. /*
  16. * Custom primitive types for handling int32_t, int64_t, uint32_t, uint64_t.
  17. * This converts between an ASN1_INTEGER and those types directly.
  18. * This is preferred to using the LONG / ZLONG primitives.
  19. */
  20. /*
  21. * We abuse the ASN1_ITEM fields |size| as a flags field
  22. */
  23. #define INTxx_FLAG_ZERO_DEFAULT (1<<0)
  24. #define INTxx_FLAG_SIGNED (1<<1)
  25. static int uint64_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  26. {
  27. if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint64_t))) == NULL)
  28. return 0;
  29. return 1;
  30. }
  31. static void uint64_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  32. {
  33. OPENSSL_free(*pval);
  34. *pval = NULL;
  35. }
  36. static void uint64_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  37. {
  38. **(uint64_t **)pval = 0;
  39. }
  40. static int uint64_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
  41. const ASN1_ITEM *it)
  42. {
  43. uint64_t utmp;
  44. int neg = 0;
  45. /* this exists to bypass broken gcc optimization */
  46. char *cp = (char *)*pval;
  47. /* use memcpy, because we may not be uint64_t aligned */
  48. memcpy(&utmp, cp, sizeof(utmp));
  49. if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
  50. && utmp == 0)
  51. return -1;
  52. if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
  53. && (int64_t)utmp < 0) {
  54. /* ossl_i2c_uint64_int() assumes positive values */
  55. utmp = 0 - utmp;
  56. neg = 1;
  57. }
  58. return ossl_i2c_uint64_int(cont, utmp, neg);
  59. }
  60. static int uint64_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  61. int utype, char *free_cont, const ASN1_ITEM *it)
  62. {
  63. uint64_t utmp = 0;
  64. char *cp;
  65. int neg = 0;
  66. if (*pval == NULL && !uint64_new(pval, it))
  67. return 0;
  68. cp = (char *)*pval;
  69. /*
  70. * Strictly speaking, zero length is malformed. However, long_c2i
  71. * (x_long.c) encodes 0 as a zero length INTEGER (wrongly, of course),
  72. * so for the sake of backward compatibility, we still decode zero
  73. * length INTEGERs as the number zero.
  74. */
  75. if (len == 0)
  76. goto long_compat;
  77. if (!ossl_c2i_uint64_int(&utmp, &neg, &cont, len))
  78. return 0;
  79. if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
  80. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
  81. return 0;
  82. }
  83. if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
  84. && !neg && utmp > INT64_MAX) {
  85. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
  86. return 0;
  87. }
  88. if (neg)
  89. /* ossl_c2i_uint64_int() returns positive values */
  90. utmp = 0 - utmp;
  91. long_compat:
  92. memcpy(cp, &utmp, sizeof(utmp));
  93. return 1;
  94. }
  95. static int uint64_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
  96. int indent, const ASN1_PCTX *pctx)
  97. {
  98. if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
  99. return BIO_printf(out, "%jd\n", **(int64_t **)pval);
  100. return BIO_printf(out, "%ju\n", **(uint64_t **)pval);
  101. }
  102. /* 32-bit variants */
  103. static int uint32_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  104. {
  105. if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint32_t))) == NULL)
  106. return 0;
  107. return 1;
  108. }
  109. static void uint32_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  110. {
  111. OPENSSL_free(*pval);
  112. *pval = NULL;
  113. }
  114. static void uint32_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  115. {
  116. **(uint32_t **)pval = 0;
  117. }
  118. static int uint32_i2c(const ASN1_VALUE **pval, unsigned char *cont, int *putype,
  119. const ASN1_ITEM *it)
  120. {
  121. uint32_t utmp;
  122. int neg = 0;
  123. /* this exists to bypass broken gcc optimization */
  124. char *cp = (char *)*pval;
  125. /* use memcpy, because we may not be uint32_t aligned */
  126. memcpy(&utmp, cp, sizeof(utmp));
  127. if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
  128. && utmp == 0)
  129. return -1;
  130. if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
  131. && (int32_t)utmp < 0) {
  132. /* ossl_i2c_uint64_int() assumes positive values */
  133. utmp = 0 - utmp;
  134. neg = 1;
  135. }
  136. return ossl_i2c_uint64_int(cont, (uint64_t)utmp, neg);
  137. }
  138. /*
  139. * Absolute value of INT32_MIN: we can't just use -INT32_MIN as it produces
  140. * overflow warnings.
  141. */
  142. #define ABS_INT32_MIN ((uint32_t)INT32_MAX + 1)
  143. static int uint32_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  144. int utype, char *free_cont, const ASN1_ITEM *it)
  145. {
  146. uint64_t utmp = 0;
  147. uint32_t utmp2 = 0;
  148. char *cp;
  149. int neg = 0;
  150. if (*pval == NULL && !uint64_new(pval, it))
  151. return 0;
  152. cp = (char *)*pval;
  153. /*
  154. * Strictly speaking, zero length is malformed. However, long_c2i
  155. * (x_long.c) encodes 0 as a zero length INTEGER (wrongly, of course),
  156. * so for the sake of backward compatibility, we still decode zero
  157. * length INTEGERs as the number zero.
  158. */
  159. if (len == 0)
  160. goto long_compat;
  161. if (!ossl_c2i_uint64_int(&utmp, &neg, &cont, len))
  162. return 0;
  163. if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
  164. ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
  165. return 0;
  166. }
  167. if (neg) {
  168. if (utmp > ABS_INT32_MIN) {
  169. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
  170. return 0;
  171. }
  172. utmp = 0 - utmp;
  173. } else {
  174. if (((it->size & INTxx_FLAG_SIGNED) != 0 && utmp > INT32_MAX)
  175. || ((it->size & INTxx_FLAG_SIGNED) == 0 && utmp > UINT32_MAX)) {
  176. ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE);
  177. return 0;
  178. }
  179. }
  180. long_compat:
  181. utmp2 = (uint32_t)utmp;
  182. memcpy(cp, &utmp2, sizeof(utmp2));
  183. return 1;
  184. }
  185. static int uint32_print(BIO *out, const ASN1_VALUE **pval, const ASN1_ITEM *it,
  186. int indent, const ASN1_PCTX *pctx)
  187. {
  188. if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
  189. return BIO_printf(out, "%d\n", (int)**(int32_t **)pval);
  190. return BIO_printf(out, "%u\n", (unsigned int)**(uint32_t **)pval);
  191. }
  192. /* Define the primitives themselves */
  193. static ASN1_PRIMITIVE_FUNCS uint32_pf = {
  194. NULL, 0,
  195. uint32_new,
  196. uint32_free,
  197. uint32_clear,
  198. uint32_c2i,
  199. uint32_i2c,
  200. uint32_print
  201. };
  202. static ASN1_PRIMITIVE_FUNCS uint64_pf = {
  203. NULL, 0,
  204. uint64_new,
  205. uint64_free,
  206. uint64_clear,
  207. uint64_c2i,
  208. uint64_i2c,
  209. uint64_print
  210. };
  211. ASN1_ITEM_start(INT32)
  212. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
  213. INTxx_FLAG_SIGNED, "INT32"
  214. ASN1_ITEM_end(INT32)
  215. ASN1_ITEM_start(UINT32)
  216. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, 0, "UINT32"
  217. ASN1_ITEM_end(UINT32)
  218. ASN1_ITEM_start(INT64)
  219. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
  220. INTxx_FLAG_SIGNED, "INT64"
  221. ASN1_ITEM_end(INT64)
  222. ASN1_ITEM_start(UINT64)
  223. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, 0, "UINT64"
  224. ASN1_ITEM_end(UINT64)
  225. ASN1_ITEM_start(ZINT32)
  226. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
  227. INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT32"
  228. ASN1_ITEM_end(ZINT32)
  229. ASN1_ITEM_start(ZUINT32)
  230. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
  231. INTxx_FLAG_ZERO_DEFAULT, "ZUINT32"
  232. ASN1_ITEM_end(ZUINT32)
  233. ASN1_ITEM_start(ZINT64)
  234. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
  235. INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT64"
  236. ASN1_ITEM_end(ZINT64)
  237. ASN1_ITEM_start(ZUINT64)
  238. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
  239. INTxx_FLAG_ZERO_DEFAULT, "ZUINT64"
  240. ASN1_ITEM_end(ZUINT64)