x_int64.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. if (!c2i_uint64_int(&utmp, &neg, &cont, len))
  72. return 0;
  73. if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
  74. ASN1err(ASN1_F_UINT64_C2I, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
  75. return 0;
  76. }
  77. if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
  78. && !neg && utmp > INT64_MAX) {
  79. ASN1err(ASN1_F_UINT64_C2I, ASN1_R_TOO_LARGE);
  80. return 0;
  81. }
  82. if (neg)
  83. /* c2i_uint64_int() returns positive values */
  84. utmp = 0 - utmp;
  85. memcpy(cp, &utmp, sizeof(utmp));
  86. return 1;
  87. }
  88. static int uint64_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
  89. int indent, const ASN1_PCTX *pctx)
  90. {
  91. if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
  92. return BIO_printf(out, "%jd\n", **(int64_t **)pval);
  93. return BIO_printf(out, "%ju\n", **(uint64_t **)pval);
  94. }
  95. /* 32-bit variants */
  96. static int uint32_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
  97. {
  98. if ((*pval = (ASN1_VALUE *)OPENSSL_zalloc(sizeof(uint32_t))) == NULL) {
  99. ASN1err(ASN1_F_UINT32_NEW, ERR_R_MALLOC_FAILURE);
  100. return 0;
  101. }
  102. return 1;
  103. }
  104. static void uint32_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  105. {
  106. OPENSSL_free(*pval);
  107. *pval = NULL;
  108. }
  109. static void uint32_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
  110. {
  111. **(uint32_t **)pval = 0;
  112. }
  113. static int uint32_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype,
  114. const ASN1_ITEM *it)
  115. {
  116. uint32_t utmp;
  117. int neg = 0;
  118. /* this exists to bypass broken gcc optimization */
  119. char *cp = (char *)*pval;
  120. /* use memcpy, because we may not be uint32_t aligned */
  121. memcpy(&utmp, cp, sizeof(utmp));
  122. if ((it->size & INTxx_FLAG_ZERO_DEFAULT) == INTxx_FLAG_ZERO_DEFAULT
  123. && utmp == 0)
  124. return -1;
  125. if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED
  126. && (int32_t)utmp < 0) {
  127. /* i2c_uint64_int() assumes positive values */
  128. utmp = 0 - utmp;
  129. neg = 1;
  130. }
  131. return i2c_uint64_int(cont, (uint64_t)utmp, neg);
  132. }
  133. /*
  134. * Absolute value of INT32_MIN: we can't just use -INT32_MIN as it produces
  135. * overflow warnings.
  136. */
  137. #define ABS_INT32_MIN ((uint32_t)INT32_MAX + 1)
  138. static int uint32_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
  139. int utype, char *free_cont, const ASN1_ITEM *it)
  140. {
  141. uint64_t utmp = 0;
  142. uint32_t utmp2 = 0;
  143. char *cp;
  144. int neg = 0;
  145. if (*pval == NULL && !uint64_new(pval, it))
  146. return 0;
  147. cp = (char *)*pval;
  148. if (!c2i_uint64_int(&utmp, &neg, &cont, len))
  149. return 0;
  150. if ((it->size & INTxx_FLAG_SIGNED) == 0 && neg) {
  151. ASN1err(ASN1_F_UINT32_C2I, ASN1_R_ILLEGAL_NEGATIVE_VALUE);
  152. return 0;
  153. }
  154. if (neg) {
  155. if (utmp > ABS_INT32_MIN) {
  156. ASN1err(ASN1_F_UINT32_C2I, ASN1_R_TOO_SMALL);
  157. return 0;
  158. }
  159. utmp = 0 - utmp;
  160. } else {
  161. if (((it->size & INTxx_FLAG_SIGNED) != 0 && utmp > INT32_MAX)
  162. || ((it->size & INTxx_FLAG_SIGNED) == 0 && utmp > UINT32_MAX)) {
  163. ASN1err(ASN1_F_UINT32_C2I, ASN1_R_TOO_LARGE);
  164. return 0;
  165. }
  166. }
  167. utmp2 = (uint32_t)utmp;
  168. memcpy(cp, &utmp2, sizeof(utmp2));
  169. return 1;
  170. }
  171. static int uint32_print(BIO *out, ASN1_VALUE **pval, const ASN1_ITEM *it,
  172. int indent, const ASN1_PCTX *pctx)
  173. {
  174. if ((it->size & INTxx_FLAG_SIGNED) == INTxx_FLAG_SIGNED)
  175. return BIO_printf(out, "%d\n", **(int32_t **)pval);
  176. return BIO_printf(out, "%u\n", **(uint32_t **)pval);
  177. }
  178. /* Define the primitives themselves */
  179. static ASN1_PRIMITIVE_FUNCS uint32_pf = {
  180. NULL, 0,
  181. uint32_new,
  182. uint32_free,
  183. uint32_clear,
  184. uint32_c2i,
  185. uint32_i2c,
  186. uint32_print
  187. };
  188. static ASN1_PRIMITIVE_FUNCS uint64_pf = {
  189. NULL, 0,
  190. uint64_new,
  191. uint64_free,
  192. uint64_clear,
  193. uint64_c2i,
  194. uint64_i2c,
  195. uint64_print
  196. };
  197. ASN1_ITEM_start(INT32)
  198. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
  199. INTxx_FLAG_SIGNED, "INT32"
  200. ASN1_ITEM_end(INT32)
  201. ASN1_ITEM_start(UINT32)
  202. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf, 0, "UINT32"
  203. ASN1_ITEM_end(UINT32)
  204. ASN1_ITEM_start(INT64)
  205. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
  206. INTxx_FLAG_SIGNED, "INT64"
  207. ASN1_ITEM_end(INT64)
  208. ASN1_ITEM_start(UINT64)
  209. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf, 0, "UINT64"
  210. ASN1_ITEM_end(UINT64)
  211. ASN1_ITEM_start(ZINT32)
  212. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
  213. INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT32"
  214. ASN1_ITEM_end(ZINT32)
  215. ASN1_ITEM_start(ZUINT32)
  216. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint32_pf,
  217. INTxx_FLAG_ZERO_DEFAULT, "ZUINT32"
  218. ASN1_ITEM_end(ZUINT32)
  219. ASN1_ITEM_start(ZINT64)
  220. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
  221. INTxx_FLAG_ZERO_DEFAULT|INTxx_FLAG_SIGNED, "ZINT64"
  222. ASN1_ITEM_end(ZINT64)
  223. ASN1_ITEM_start(ZUINT64)
  224. ASN1_ITYPE_PRIMITIVE, V_ASN1_INTEGER, NULL, 0, &uint64_pf,
  225. INTxx_FLAG_ZERO_DEFAULT, "ZUINT64"
  226. ASN1_ITEM_end(ZUINT64)