a_object.c 9.8 KB

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