2
0

a_object.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright 1995-2020 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. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  31. return 0;
  32. }
  33. } else {
  34. p = *pp;
  35. }
  36. ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
  37. memcpy(p, a->data, a->length);
  38. /*
  39. * If a new buffer was allocated, just return it back.
  40. * If not, return the incremented buffer pointer.
  41. */
  42. *pp = allocated != NULL ? allocated : p + a->length;
  43. return objsize;
  44. }
  45. int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
  46. {
  47. int i, first, len = 0, c, use_bn;
  48. char ftmp[24], *tmp = ftmp;
  49. int tmpsize = sizeof(ftmp);
  50. const char *p;
  51. unsigned long l;
  52. BIGNUM *bl = NULL;
  53. if (num == 0)
  54. return 0;
  55. else if (num == -1)
  56. num = strlen(buf);
  57. p = buf;
  58. c = *(p++);
  59. num--;
  60. if ((c >= '0') && (c <= '2')) {
  61. first = c - '0';
  62. } else {
  63. ERR_raise(ERR_LIB_ASN1, ASN1_R_FIRST_NUM_TOO_LARGE);
  64. goto err;
  65. }
  66. if (num <= 0) {
  67. ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_SECOND_NUMBER);
  68. goto err;
  69. }
  70. c = *(p++);
  71. num--;
  72. for (;;) {
  73. if (num <= 0)
  74. break;
  75. if ((c != '.') && (c != ' ')) {
  76. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_SEPARATOR);
  77. goto err;
  78. }
  79. l = 0;
  80. use_bn = 0;
  81. for (;;) {
  82. if (num <= 0)
  83. break;
  84. num--;
  85. c = *(p++);
  86. if ((c == ' ') || (c == '.'))
  87. break;
  88. if (!ossl_isdigit(c)) {
  89. ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_DIGIT);
  90. goto err;
  91. }
  92. if (!use_bn && l >= ((ULONG_MAX - 80) / 10L)) {
  93. use_bn = 1;
  94. if (bl == NULL)
  95. bl = BN_new();
  96. if (bl == NULL || !BN_set_word(bl, l))
  97. goto err;
  98. }
  99. if (use_bn) {
  100. if (!BN_mul_word(bl, 10L)
  101. || !BN_add_word(bl, c - '0'))
  102. goto err;
  103. } else
  104. l = l * 10L + (long)(c - '0');
  105. }
  106. if (len == 0) {
  107. if ((first < 2) && (l >= 40)) {
  108. ERR_raise(ERR_LIB_ASN1, ASN1_R_SECOND_NUMBER_TOO_LARGE);
  109. goto err;
  110. }
  111. if (use_bn) {
  112. if (!BN_add_word(bl, first * 40))
  113. goto err;
  114. } else
  115. l += (long)first *40;
  116. }
  117. i = 0;
  118. if (use_bn) {
  119. int blsize;
  120. blsize = BN_num_bits(bl);
  121. blsize = (blsize + 6) / 7;
  122. if (blsize > tmpsize) {
  123. if (tmp != ftmp)
  124. OPENSSL_free(tmp);
  125. tmpsize = blsize + 32;
  126. tmp = OPENSSL_malloc(tmpsize);
  127. if (tmp == NULL)
  128. goto err;
  129. }
  130. while (blsize--) {
  131. BN_ULONG t = BN_div_word(bl, 0x80L);
  132. if (t == (BN_ULONG)-1)
  133. goto err;
  134. tmp[i++] = (unsigned char)t;
  135. }
  136. } else {
  137. for (;;) {
  138. tmp[i++] = (unsigned char)l & 0x7f;
  139. l >>= 7L;
  140. if (l == 0L)
  141. break;
  142. }
  143. }
  144. if (out != NULL) {
  145. if (len + i > olen) {
  146. ERR_raise(ERR_LIB_ASN1, ASN1_R_BUFFER_TOO_SMALL);
  147. goto err;
  148. }
  149. while (--i > 0)
  150. out[len++] = tmp[i] | 0x80;
  151. out[len++] = tmp[0];
  152. } else
  153. len += i;
  154. }
  155. if (tmp != ftmp)
  156. OPENSSL_free(tmp);
  157. BN_free(bl);
  158. return len;
  159. err:
  160. if (tmp != ftmp)
  161. OPENSSL_free(tmp);
  162. BN_free(bl);
  163. return 0;
  164. }
  165. int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
  166. {
  167. return OBJ_obj2txt(buf, buf_len, a, 0);
  168. }
  169. int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
  170. {
  171. char buf[80], *p = buf;
  172. int i;
  173. if ((a == NULL) || (a->data == NULL))
  174. return BIO_write(bp, "NULL", 4);
  175. i = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
  176. if (i > (int)(sizeof(buf) - 1)) {
  177. if ((p = OPENSSL_malloc(i + 1)) == NULL) {
  178. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  179. return -1;
  180. }
  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 = 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 *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. /*
  268. * only the ASN1_OBJECTs from the 'table' will have values for ->sn or
  269. * ->ln
  270. */
  271. if ((a == NULL) || ((*a) == NULL) ||
  272. !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
  273. if ((ret = ASN1_OBJECT_new()) == NULL)
  274. return NULL;
  275. } else
  276. ret = (*a);
  277. p = *pp;
  278. /* detach data from object */
  279. data = (unsigned char *)ret->data;
  280. ret->data = NULL;
  281. /* once detached we can change it */
  282. if ((data == NULL) || (ret->length < length)) {
  283. ret->length = 0;
  284. OPENSSL_free(data);
  285. data = OPENSSL_malloc(length);
  286. if (data == NULL) {
  287. i = ERR_R_MALLOC_FAILURE;
  288. goto err;
  289. }
  290. ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  291. }
  292. memcpy(data, p, length);
  293. /* reattach data to object, after which it remains const */
  294. ret->data = data;
  295. ret->length = length;
  296. ret->sn = NULL;
  297. ret->ln = NULL;
  298. /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
  299. p += length;
  300. if (a != NULL)
  301. (*a) = ret;
  302. *pp = p;
  303. return ret;
  304. err:
  305. ERR_raise(ERR_LIB_ASN1, i);
  306. if ((a == NULL) || (*a != ret))
  307. ASN1_OBJECT_free(ret);
  308. return NULL;
  309. }
  310. ASN1_OBJECT *ASN1_OBJECT_new(void)
  311. {
  312. ASN1_OBJECT *ret;
  313. ret = OPENSSL_zalloc(sizeof(*ret));
  314. if (ret == NULL) {
  315. ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
  316. return NULL;
  317. }
  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 /* disable purely for compile-time strict
  327. * const checking. Doing this on a "real"
  328. * compile will cause memory leaks */
  329. OPENSSL_free((void*)a->sn);
  330. OPENSSL_free((void*)a->ln);
  331. #endif
  332. a->sn = a->ln = NULL;
  333. }
  334. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
  335. OPENSSL_free((void*)a->data);
  336. a->data = NULL;
  337. a->length = 0;
  338. }
  339. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
  340. OPENSSL_free(a);
  341. }
  342. ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
  343. const char *sn, const char *ln)
  344. {
  345. ASN1_OBJECT o;
  346. o.sn = sn;
  347. o.ln = ln;
  348. o.data = data;
  349. o.nid = nid;
  350. o.length = len;
  351. o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
  352. ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  353. return OBJ_dup(&o);
  354. }