a_object.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright 1995-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 <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. i += BIO_dump(bp, (const char *)a->data, a->length);
  186. return i;
  187. }
  188. BIO_write(bp, p, i);
  189. if (p != buf)
  190. OPENSSL_free(p);
  191. return i;
  192. }
  193. ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
  194. long length)
  195. {
  196. const unsigned char *p;
  197. long len;
  198. int tag, xclass;
  199. int inf, i;
  200. ASN1_OBJECT *ret = NULL;
  201. p = *pp;
  202. inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
  203. if (inf & 0x80) {
  204. i = ASN1_R_BAD_OBJECT_HEADER;
  205. goto err;
  206. }
  207. if (tag != V_ASN1_OBJECT) {
  208. i = ASN1_R_EXPECTING_AN_OBJECT;
  209. goto err;
  210. }
  211. ret = ossl_c2i_ASN1_OBJECT(a, &p, len);
  212. if (ret)
  213. *pp = p;
  214. return ret;
  215. err:
  216. ERR_raise(ERR_LIB_ASN1, i);
  217. return NULL;
  218. }
  219. ASN1_OBJECT *ossl_c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
  220. long len)
  221. {
  222. ASN1_OBJECT *ret = NULL, tobj;
  223. const unsigned char *p;
  224. unsigned char *data;
  225. int i, length;
  226. /*
  227. * Sanity check OID encoding. Need at least one content octet. MSB must
  228. * be clear in the last octet. can't have leading 0x80 in subidentifiers,
  229. * see: X.690 8.19.2
  230. */
  231. if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
  232. p[len - 1] & 0x80) {
  233. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
  234. return NULL;
  235. }
  236. /* Now 0 < len <= INT_MAX, so the cast is safe. */
  237. length = (int)len;
  238. /*
  239. * Try to lookup OID in table: these are all valid encodings so if we get
  240. * a match we know the OID is valid.
  241. */
  242. tobj.nid = NID_undef;
  243. tobj.data = p;
  244. tobj.length = length;
  245. tobj.flags = 0;
  246. i = OBJ_obj2nid(&tobj);
  247. if (i != NID_undef) {
  248. /*
  249. * Return shared registered OID object: this improves efficiency
  250. * because we don't have to return a dynamically allocated OID
  251. * and NID lookups can use the cached value.
  252. */
  253. ret = OBJ_nid2obj(i);
  254. if (a) {
  255. ASN1_OBJECT_free(*a);
  256. *a = ret;
  257. }
  258. *pp += len;
  259. return ret;
  260. }
  261. for (i = 0; i < length; i++, p++) {
  262. if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
  263. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
  264. return NULL;
  265. }
  266. }
  267. if ((a == NULL) || ((*a) == NULL) ||
  268. !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
  269. if ((ret = ASN1_OBJECT_new()) == NULL)
  270. return NULL;
  271. } else {
  272. ret = (*a);
  273. }
  274. p = *pp;
  275. /* detach data from object */
  276. data = (unsigned char *)ret->data;
  277. ret->data = NULL;
  278. /* once detached we can change it */
  279. if ((data == NULL) || (ret->length < length)) {
  280. ret->length = 0;
  281. OPENSSL_free(data);
  282. data = OPENSSL_malloc(length);
  283. if (data == NULL)
  284. goto err;
  285. ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  286. }
  287. memcpy(data, p, length);
  288. /* If there are dynamic strings, free them here, and clear the flag */
  289. if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) {
  290. OPENSSL_free((char *)ret->sn);
  291. OPENSSL_free((char *)ret->ln);
  292. ret->flags &= ~ASN1_OBJECT_FLAG_DYNAMIC_STRINGS;
  293. }
  294. /* reattach data to object, after which it remains const */
  295. ret->data = data;
  296. ret->length = length;
  297. ret->sn = NULL;
  298. ret->ln = NULL;
  299. /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
  300. p += length;
  301. if (a != NULL)
  302. (*a) = ret;
  303. *pp = p;
  304. return ret;
  305. err:
  306. ERR_raise(ERR_LIB_ASN1, i);
  307. if ((a == NULL) || (*a != ret))
  308. ASN1_OBJECT_free(ret);
  309. return NULL;
  310. }
  311. ASN1_OBJECT *ASN1_OBJECT_new(void)
  312. {
  313. ASN1_OBJECT *ret;
  314. ret = OPENSSL_zalloc(sizeof(*ret));
  315. if (ret == NULL)
  316. return NULL;
  317. ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
  318. return ret;
  319. }
  320. void ASN1_OBJECT_free(ASN1_OBJECT *a)
  321. {
  322. if (a == NULL)
  323. return;
  324. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
  325. #ifndef CONST_STRICT
  326. /*
  327. * Disable purely for compile-time strict const checking. Doing this
  328. * on a "real" compile will cause memory leaks
  329. */
  330. OPENSSL_free((void*)a->sn);
  331. OPENSSL_free((void*)a->ln);
  332. #endif
  333. a->sn = a->ln = NULL;
  334. }
  335. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
  336. OPENSSL_free((void*)a->data);
  337. a->data = NULL;
  338. a->length = 0;
  339. }
  340. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
  341. OPENSSL_free(a);
  342. }
  343. ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
  344. const char *sn, const char *ln)
  345. {
  346. ASN1_OBJECT o;
  347. o.sn = sn;
  348. o.ln = ln;
  349. o.data = data;
  350. o.nid = nid;
  351. o.length = len;
  352. o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
  353. ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  354. return OBJ_dup(&o);
  355. }