a_bytes.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* crypto/asn1/a_bytes.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/asn1.h>
  61. static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c,
  62. int depth);
  63. static ASN1_STRING *int_d2i_ASN1_bytes(ASN1_STRING **a,
  64. const unsigned char **pp, long length,
  65. int Ptag, int Pclass, int depth,
  66. int *perr);
  67. /*
  68. * type is a 'bitmap' of acceptable string types.
  69. */
  70. ASN1_STRING *d2i_ASN1_type_bytes(ASN1_STRING **a, const unsigned char **pp,
  71. long length, int type)
  72. {
  73. ASN1_STRING *ret = NULL;
  74. const unsigned char *p;
  75. unsigned char *s;
  76. long len;
  77. int inf, tag, xclass;
  78. int i = 0;
  79. p = *pp;
  80. inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
  81. if (inf & 0x80)
  82. goto err;
  83. if (tag >= 32) {
  84. i = ASN1_R_TAG_VALUE_TOO_HIGH;
  85. goto err;
  86. }
  87. if (!(ASN1_tag2bit(tag) & type)) {
  88. i = ASN1_R_WRONG_TYPE;
  89. goto err;
  90. }
  91. /* If a bit-string, exit early */
  92. if (tag == V_ASN1_BIT_STRING)
  93. return (d2i_ASN1_BIT_STRING(a, pp, length));
  94. if ((a == NULL) || ((*a) == NULL)) {
  95. if ((ret = ASN1_STRING_new()) == NULL)
  96. return (NULL);
  97. } else
  98. ret = (*a);
  99. if (len != 0) {
  100. s = OPENSSL_malloc((int)len + 1);
  101. if (s == NULL) {
  102. i = ERR_R_MALLOC_FAILURE;
  103. goto err;
  104. }
  105. memcpy(s, p, (int)len);
  106. s[len] = '\0';
  107. p += len;
  108. } else
  109. s = NULL;
  110. if (ret->data != NULL)
  111. OPENSSL_free(ret->data);
  112. ret->length = (int)len;
  113. ret->data = s;
  114. ret->type = tag;
  115. if (a != NULL)
  116. (*a) = ret;
  117. *pp = p;
  118. return (ret);
  119. err:
  120. ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES, i);
  121. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  122. ASN1_STRING_free(ret);
  123. return (NULL);
  124. }
  125. int i2d_ASN1_bytes(ASN1_STRING *a, unsigned char **pp, int tag, int xclass)
  126. {
  127. int ret, r, constructed;
  128. unsigned char *p;
  129. if (a == NULL)
  130. return (0);
  131. if (tag == V_ASN1_BIT_STRING)
  132. return (i2d_ASN1_BIT_STRING(a, pp));
  133. ret = a->length;
  134. r = ASN1_object_size(0, ret, tag);
  135. if (pp == NULL)
  136. return (r);
  137. p = *pp;
  138. if ((tag == V_ASN1_SEQUENCE) || (tag == V_ASN1_SET))
  139. constructed = 1;
  140. else
  141. constructed = 0;
  142. ASN1_put_object(&p, constructed, ret, tag, xclass);
  143. memcpy(p, a->data, a->length);
  144. p += a->length;
  145. *pp = p;
  146. return (r);
  147. }
  148. /*
  149. * Maximum recursion depth of d2i_ASN1_bytes(): much more than should be
  150. * encountered in pratice.
  151. */
  152. #define ASN1_BYTES_MAXDEPTH 20
  153. ASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, const unsigned char **pp,
  154. long length, int Ptag, int Pclass)
  155. {
  156. int err = 0;
  157. ASN1_STRING *s = int_d2i_ASN1_bytes(a, pp, length, Ptag, Pclass, 0, &err);
  158. if (err != 0)
  159. ASN1err(ASN1_F_D2I_ASN1_BYTES, err);
  160. return s;
  161. }
  162. static ASN1_STRING *int_d2i_ASN1_bytes(ASN1_STRING **a,
  163. const unsigned char **pp, long length,
  164. int Ptag, int Pclass,
  165. int depth, int *perr)
  166. {
  167. ASN1_STRING *ret = NULL;
  168. const unsigned char *p;
  169. unsigned char *s;
  170. long len;
  171. int inf, tag, xclass;
  172. if (depth > ASN1_BYTES_MAXDEPTH) {
  173. *perr = ASN1_R_NESTED_ASN1_STRING;
  174. return NULL;
  175. }
  176. if ((a == NULL) || ((*a) == NULL)) {
  177. if ((ret = ASN1_STRING_new()) == NULL)
  178. return (NULL);
  179. } else
  180. ret = (*a);
  181. p = *pp;
  182. inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
  183. if (inf & 0x80) {
  184. *perr = ASN1_R_BAD_OBJECT_HEADER;
  185. goto err;
  186. }
  187. if (tag != Ptag) {
  188. *perr = ASN1_R_WRONG_TAG;
  189. goto err;
  190. }
  191. if (inf & V_ASN1_CONSTRUCTED) {
  192. ASN1_const_CTX c;
  193. c.error = 0;
  194. c.pp = pp;
  195. c.p = p;
  196. c.inf = inf;
  197. c.slen = len;
  198. c.tag = Ptag;
  199. c.xclass = Pclass;
  200. c.max = (length == 0) ? 0 : (p + length);
  201. if (!asn1_collate_primitive(ret, &c, depth)) {
  202. *perr = c.error;
  203. goto err;
  204. } else {
  205. p = c.p;
  206. }
  207. } else {
  208. if (len != 0) {
  209. if ((ret->length < len) || (ret->data == NULL)) {
  210. s = OPENSSL_malloc((int)len + 1);
  211. if (s == NULL) {
  212. *perr = ERR_R_MALLOC_FAILURE;
  213. goto err;
  214. }
  215. if (ret->data != NULL)
  216. OPENSSL_free(ret->data);
  217. } else
  218. s = ret->data;
  219. memcpy(s, p, (int)len);
  220. s[len] = '\0';
  221. p += len;
  222. } else {
  223. s = NULL;
  224. if (ret->data != NULL)
  225. OPENSSL_free(ret->data);
  226. }
  227. ret->length = (int)len;
  228. ret->data = s;
  229. ret->type = Ptag;
  230. }
  231. if (a != NULL)
  232. (*a) = ret;
  233. *pp = p;
  234. return (ret);
  235. err:
  236. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  237. ASN1_STRING_free(ret);
  238. return (NULL);
  239. }
  240. /*
  241. * We are about to parse 0..n d2i_ASN1_bytes objects, we are to collapse them
  242. * into the one structure that is then returned
  243. */
  244. /*
  245. * There have been a few bug fixes for this function from Paul Keogh
  246. * <paul.keogh@sse.ie>, many thanks to him
  247. */
  248. static int asn1_collate_primitive(ASN1_STRING *a, ASN1_const_CTX *c,
  249. int depth)
  250. {
  251. ASN1_STRING *os = NULL;
  252. BUF_MEM b;
  253. int num;
  254. b.length = 0;
  255. b.max = 0;
  256. b.data = NULL;
  257. if (a == NULL) {
  258. c->error = ERR_R_PASSED_NULL_PARAMETER;
  259. goto err;
  260. }
  261. num = 0;
  262. for (;;) {
  263. if (c->inf & 1) {
  264. c->eos = ASN1_const_check_infinite_end(&c->p,
  265. (long)(c->max - c->p));
  266. if (c->eos)
  267. break;
  268. } else {
  269. if (c->slen <= 0)
  270. break;
  271. }
  272. c->q = c->p;
  273. if (int_d2i_ASN1_bytes(&os, &c->p, c->max - c->p, c->tag, c->xclass,
  274. depth + 1, &c->error) == NULL) {
  275. goto err;
  276. }
  277. if (!BUF_MEM_grow_clean(&b, num + os->length)) {
  278. c->error = ERR_R_BUF_LIB;
  279. goto err;
  280. }
  281. memcpy(&(b.data[num]), os->data, os->length);
  282. if (!(c->inf & 1))
  283. c->slen -= (c->p - c->q);
  284. num += os->length;
  285. }
  286. if (!asn1_const_Finish(c))
  287. goto err;
  288. a->length = num;
  289. if (a->data != NULL)
  290. OPENSSL_free(a->data);
  291. a->data = (unsigned char *)b.data;
  292. if (os != NULL)
  293. ASN1_STRING_free(os);
  294. return (1);
  295. err:
  296. if (os != NULL)
  297. ASN1_STRING_free(os);
  298. if (b.data != NULL)
  299. OPENSSL_free(b.data);
  300. return (0);
  301. }