x_int64.c 7.8 KB

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