a_object.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright 1995-2024 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 <limits.h>
  11. #include "crypto/ctype.h"
  12. #include "internal/cryptlib.h"
  13. #include <openssl/buffer.h>
  14. #include <openssl/asn1.h>
  15. #include <openssl/objects.h>
  16. #include <openssl/bn.h>
  17. #include "crypto/asn1.h"
  18. #include "asn1_local.h"
  19. int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
  20. {
  21. unsigned char *p, *allocated = NULL;
  22. int objsize;
  23. if ((a == NULL) || (a->data == NULL))
  24. return 0;
  25. objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
  26. if (pp == NULL || objsize == -1)
  27. return objsize;
  28. if (*pp == NULL) {
  29. if ((p = allocated = OPENSSL_malloc(objsize)) == NULL)
  30. return 0;
  31. } else {
  32. p = *pp;
  33. }
  34. ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
  35. memcpy(p, a->data, a->length);
  36. /*
  37. * If a new buffer was allocated, just return it back.
  38. * If not, return the incremented buffer pointer.
  39. */
  40. *pp = allocated != NULL ? allocated : p + a->length;
  41. return objsize;
  42. }
  43. int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
  44. {
  45. int i, first, len = 0, c, use_bn;
  46. char ftmp[24], *tmp = ftmp;
  47. int tmpsize = sizeof(ftmp);
  48. const char *p;
  49. unsigned long l;
  50. BIGNUM *bl = NULL;
  51. if (num == 0)
  52. return 0;
  53. else if (num == -1)
  54. num = strlen(buf);
  55. p = buf;
  56. c = *(p++);
  57. num--;
  58. if ((c >= '0') && (c <= '2')) {
  59. first = c - '0';
  60. } else {
  61. ERR_raise(ERR_LIB_ASN1, ASN1_R_FIRST_NUM_TOO_LARGE);
  62. goto err;
  63. }
  64. if (num <= 0) {
  65. ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_SECOND_NUMBER);
  66. goto err;
  67. }
  68. c = *(p++);
  69. num--;
  70. for (;;) {
  71. if (num <= 0)
  72. break;
  73. if ((c != '.') && (c != ' ')) {
  74. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_SEPARATOR);
  75. goto err;
  76. }
  77. l = 0;
  78. use_bn = 0;
  79. for (;;) {
  80. if (num <= 0)
  81. break;
  82. num--;
  83. c = *(p++);
  84. if ((c == ' ') || (c == '.'))
  85. break;
  86. if (!ossl_isdigit(c)) {
  87. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_DIGIT);
  88. goto err;
  89. }
  90. if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) {
  91. use_bn = 1;
  92. if (bl == NULL)
  93. bl = BN_new();
  94. if (bl == NULL || !BN_set_word(bl, l))
  95. goto err;
  96. }
  97. if (use_bn) {
  98. if (!BN_mul_word(bl, 10L)
  99. || !BN_add_word(bl, c - '0'))
  100. goto err;
  101. } else
  102. l = l * 10L + (long)(c - '0');
  103. }
  104. if (len == 0) {
  105. if ((first < 2) && (l >= 40)) {
  106. ERR_raise(ERR_LIB_ASN1, ASN1_R_SECOND_NUMBER_TOO_LARGE);
  107. goto err;
  108. }
  109. if (use_bn) {
  110. if (!BN_add_word(bl, first * 40))
  111. goto err;
  112. } else
  113. l += (long)first *40;
  114. }
  115. i = 0;
  116. if (use_bn) {
  117. int blsize;
  118. blsize = BN_num_bits(bl);
  119. blsize = (blsize + 6) / 7;
  120. if (blsize > tmpsize) {
  121. if (tmp != ftmp)
  122. OPENSSL_free(tmp);
  123. tmpsize = blsize + 32;
  124. tmp = OPENSSL_malloc(tmpsize);
  125. if (tmp == NULL)
  126. goto err;
  127. }
  128. while (blsize--) {
  129. BN_ULONG t = BN_div_word(bl, 0x80L);
  130. if (t == (BN_ULONG)-1)
  131. goto err;
  132. tmp[i++] = (unsigned char)t;
  133. }
  134. } else {
  135. for (;;) {
  136. tmp[i++] = (unsigned char)l & 0x7f;
  137. l >>= 7L;
  138. if (l == 0L)
  139. break;
  140. }
  141. }
  142. if (out != NULL) {
  143. if (len + i > olen) {
  144. ERR_raise(ERR_LIB_ASN1, ASN1_R_BUFFER_TOO_SMALL);
  145. goto err;
  146. }
  147. while (--i > 0)
  148. out[len++] = tmp[i] | 0x80;
  149. out[len++] = tmp[0];
  150. } else
  151. len += i;
  152. }
  153. if (tmp != ftmp)
  154. OPENSSL_free(tmp);
  155. BN_free(bl);
  156. return len;
  157. err:
  158. if (tmp != ftmp)
  159. OPENSSL_free(tmp);
  160. BN_free(bl);
  161. return 0;
  162. }
  163. int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
  164. {
  165. return OBJ_obj2txt(buf, buf_len, a, 0);
  166. }
  167. int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
  168. {
  169. char buf[80], *p = buf;
  170. int i;
  171. if ((a == NULL) || (a->data == NULL))
  172. return BIO_write(bp, "NULL", 4);
  173. i = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
  174. if (i > (int)(sizeof(buf) - 1)) {
  175. if (i > INT_MAX - 1) { /* catch an integer overflow */
  176. ERR_raise(ERR_LIB_ASN1, ASN1_R_LENGTH_TOO_LONG);
  177. return -1;
  178. }
  179. if ((p = OPENSSL_malloc(i + 1)) == NULL)
  180. return -1;
  181. i2t_ASN1_OBJECT(p, i + 1, a);
  182. }
  183. if (i <= 0) {
  184. i = BIO_write(bp, "<INVALID>", 9);
  185. if (i > 0)
  186. i += BIO_dump(bp, (const char *)a->data, a->length);
  187. return i;
  188. }
  189. BIO_write(bp, p, i);
  190. if (p != buf)
  191. OPENSSL_free(p);
  192. return i;
  193. }
  194. ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
  195. long length)
  196. {
  197. const unsigned char *p;
  198. long len;
  199. int tag, xclass;
  200. int inf, i;
  201. ASN1_OBJECT *ret = NULL;
  202. p = *pp;
  203. inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
  204. if (inf & 0x80) {
  205. i = ASN1_R_BAD_OBJECT_HEADER;
  206. goto err;
  207. }
  208. if (tag != V_ASN1_OBJECT) {
  209. i = ASN1_R_EXPECTING_AN_OBJECT;
  210. goto err;
  211. }
  212. ret = ossl_c2i_ASN1_OBJECT(a, &p, len);
  213. if (ret)
  214. *pp = p;
  215. return ret;
  216. err:
  217. ERR_raise(ERR_LIB_ASN1, i);
  218. return NULL;
  219. }
  220. ASN1_OBJECT *ossl_c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
  221. long len)
  222. {
  223. ASN1_OBJECT *ret = NULL, tobj;
  224. const unsigned char *p;
  225. unsigned char *data;
  226. int i, length;
  227. /*
  228. * Sanity check OID encoding. Need at least one content octet. MSB must
  229. * be clear in the last octet. can't have leading 0x80 in subidentifiers,
  230. * see: X.690 8.19.2
  231. */
  232. if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
  233. p[len - 1] & 0x80) {
  234. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
  235. return NULL;
  236. }
  237. /* Now 0 < len <= INT_MAX, so the cast is safe. */
  238. length = (int)len;
  239. /*
  240. * Try to lookup OID in table: these are all valid encodings so if we get
  241. * a match we know the OID is valid.
  242. */
  243. tobj.nid = NID_undef;
  244. tobj.data = p;
  245. tobj.length = length;
  246. tobj.flags = 0;
  247. i = OBJ_obj2nid(&tobj);
  248. if (i != NID_undef) {
  249. /*
  250. * Return shared registered OID object: this improves efficiency
  251. * because we don't have to return a dynamically allocated OID
  252. * and NID lookups can use the cached value.
  253. */
  254. ret = OBJ_nid2obj(i);
  255. if (a) {
  256. ASN1_OBJECT_free(*a);
  257. *a = ret;
  258. }
  259. *pp += len;
  260. return ret;
  261. }
  262. for (i = 0; i < length; i++, p++) {
  263. if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
  264. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
  265. return NULL;
  266. }
  267. }
  268. if ((a == NULL) || ((*a) == NULL) ||
  269. !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
  270. if ((ret = ASN1_OBJECT_new()) == NULL)
  271. return NULL;
  272. } else {
  273. ret = (*a);
  274. }
  275. p = *pp;
  276. /* detach data from object */
  277. data = (unsigned char *)ret->data;
  278. ret->data = NULL;
  279. /* once detached we can change it */
  280. if ((data == NULL) || (ret->length < length)) {
  281. ret->length = 0;
  282. OPENSSL_free(data);
  283. data = OPENSSL_malloc(length);
  284. if (data == NULL)
  285. goto err;
  286. ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  287. }
  288. memcpy(data, p, length);
  289. /* If there are dynamic strings, free them here, and clear the flag */
  290. if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) {
  291. OPENSSL_free((char *)ret->sn);
  292. OPENSSL_free((char *)ret->ln);
  293. ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
  294. }
  295. /* reattach data to object, after which it remains const */
  296. ret->data = data;
  297. ret->length = length;
  298. ret->sn = NULL;
  299. ret->ln = NULL;
  300. /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
  301. p += length;
  302. if (a != NULL)
  303. (*a) = ret;
  304. *pp = p;
  305. return ret;
  306. err:
  307. ERR_raise(ERR_LIB_ASN1, i);
  308. if ((a == NULL) || (*a != ret))
  309. ASN1_OBJECT_free(ret);
  310. return NULL;
  311. }
  312. ASN1_OBJECT *ASN1_OBJECT_new(void)
  313. {
  314. ASN1_OBJECT *ret;
  315. ret = OPENSSL_zalloc(sizeof(*ret));
  316. if (ret == NULL)
  317. return NULL;
  318. ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
  319. return ret;
  320. }
  321. void ASN1_OBJECT_free(ASN1_OBJECT *a)
  322. {
  323. if (a == NULL)
  324. return;
  325. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
  326. #ifndef CONST_STRICT
  327. /*
  328. * Disable purely for compile-time strict const checking. Doing this
  329. * on a "real" compile will cause memory leaks
  330. */
  331. OPENSSL_free((void*)a->sn);
  332. OPENSSL_free((void*)a->ln);
  333. #endif
  334. a->sn = a->ln = NULL;
  335. }
  336. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
  337. OPENSSL_free((void*)a->data);
  338. a->data = NULL;
  339. a->length = 0;
  340. }
  341. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
  342. OPENSSL_free(a);
  343. }
  344. ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
  345. const char *sn, const char *ln)
  346. {
  347. ASN1_OBJECT o;
  348. o.sn = sn;
  349. o.ln = ln;
  350. o.data = data;
  351. o.nid = nid;
  352. o.length = len;
  353. o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
  354. ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  355. return OBJ_dup(&o);
  356. }