25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

413 lines
10 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/asn1.h>
  57. #include <limits.h>
  58. #include <string.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/obj.h>
  62. int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp)
  63. {
  64. unsigned char *p;
  65. int objsize;
  66. if ((a == NULL) || (a->data == NULL)) return(0);
  67. objsize = ASN1_object_size(0,a->length,V_ASN1_OBJECT);
  68. if (pp == NULL) return objsize;
  69. p= *pp;
  70. ASN1_put_object(&p,0,a->length,V_ASN1_OBJECT,V_ASN1_UNIVERSAL);
  71. memcpy(p,a->data,a->length);
  72. p+=a->length;
  73. *pp=p;
  74. return(objsize);
  75. }
  76. int a2d_ASN1_OBJECT(unsigned char *out, int olen, const char *buf, int num)
  77. {
  78. int i,first,len=0,c, use_bn;
  79. char ftmp[24], *tmp = ftmp;
  80. int tmpsize = sizeof ftmp;
  81. const char *p;
  82. unsigned long l;
  83. BIGNUM *bl = NULL;
  84. if (num == 0)
  85. return(0);
  86. else if (num == -1)
  87. num=strlen(buf);
  88. p=buf;
  89. c= *(p++);
  90. num--;
  91. if ((c >= '0') && (c <= '2'))
  92. {
  93. first= c-'0';
  94. }
  95. else
  96. {
  97. OPENSSL_PUT_ERROR(ASN1, a2d_ASN1_OBJECT, ASN1_R_FIRST_NUM_TOO_LARGE);
  98. goto err;
  99. }
  100. if (num <= 0)
  101. {
  102. OPENSSL_PUT_ERROR(ASN1, a2d_ASN1_OBJECT, ASN1_R_MISSING_SECOND_NUMBER);
  103. goto err;
  104. }
  105. c= *(p++);
  106. num--;
  107. for (;;)
  108. {
  109. if (num <= 0) break;
  110. if ((c != '.') && (c != ' '))
  111. {
  112. OPENSSL_PUT_ERROR(ASN1, a2d_ASN1_OBJECT, ASN1_R_INVALID_SEPARATOR);
  113. goto err;
  114. }
  115. l=0;
  116. use_bn = 0;
  117. for (;;)
  118. {
  119. if (num <= 0) break;
  120. num--;
  121. c= *(p++);
  122. if ((c == ' ') || (c == '.'))
  123. break;
  124. if ((c < '0') || (c > '9'))
  125. {
  126. OPENSSL_PUT_ERROR(ASN1, a2d_ASN1_OBJECT, ASN1_R_INVALID_DIGIT);
  127. goto err;
  128. }
  129. if (!use_bn && l >= ((ULONG_MAX - 80) / 10L))
  130. {
  131. use_bn = 1;
  132. if (!bl)
  133. bl = BN_new();
  134. if (!bl || !BN_set_word(bl, l))
  135. goto err;
  136. }
  137. if (use_bn)
  138. {
  139. if (!BN_mul_word(bl, 10L)
  140. || !BN_add_word(bl, c-'0'))
  141. goto err;
  142. }
  143. else
  144. l=l*10L+(long)(c-'0');
  145. }
  146. if (len == 0)
  147. {
  148. if ((first < 2) && (l >= 40))
  149. {
  150. OPENSSL_PUT_ERROR(ASN1, a2d_ASN1_OBJECT, ASN1_R_SECOND_NUMBER_TOO_LARGE);
  151. goto err;
  152. }
  153. if (use_bn)
  154. {
  155. if (!BN_add_word(bl, first * 40))
  156. goto err;
  157. }
  158. else
  159. l+=(long)first*40;
  160. }
  161. i=0;
  162. if (use_bn)
  163. {
  164. int blsize;
  165. blsize = BN_num_bits(bl);
  166. blsize = (blsize + 6)/7;
  167. if (blsize > tmpsize)
  168. {
  169. if (tmp != ftmp)
  170. OPENSSL_free(tmp);
  171. tmpsize = blsize + 32;
  172. tmp = OPENSSL_malloc(tmpsize);
  173. if (!tmp)
  174. goto err;
  175. }
  176. while(blsize--)
  177. tmp[i++] = (unsigned char)BN_div_word(bl, 0x80L);
  178. }
  179. else
  180. {
  181. for (;;)
  182. {
  183. tmp[i++]=(unsigned char)l&0x7f;
  184. l>>=7L;
  185. if (l == 0L) break;
  186. }
  187. }
  188. if (out != NULL)
  189. {
  190. if (len+i > olen)
  191. {
  192. OPENSSL_PUT_ERROR(ASN1, a2d_ASN1_OBJECT, ASN1_R_BUFFER_TOO_SMALL);
  193. goto err;
  194. }
  195. while (--i > 0)
  196. out[len++]=tmp[i]|0x80;
  197. out[len++]=tmp[0];
  198. }
  199. else
  200. len+=i;
  201. }
  202. if (tmp != ftmp)
  203. OPENSSL_free(tmp);
  204. if (bl)
  205. BN_free(bl);
  206. return(len);
  207. err:
  208. if (tmp != ftmp)
  209. OPENSSL_free(tmp);
  210. if (bl)
  211. BN_free(bl);
  212. return(0);
  213. }
  214. int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)
  215. {
  216. return OBJ_obj2txt(buf, buf_len, a, 0);
  217. }
  218. int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
  219. {
  220. char buf[80], *p = buf;
  221. int i;
  222. if ((a == NULL) || (a->data == NULL))
  223. return(BIO_write(bp,"NULL",4));
  224. i=i2t_ASN1_OBJECT(buf,sizeof buf,a);
  225. if (i > (int)(sizeof(buf) - 1))
  226. {
  227. p = OPENSSL_malloc(i + 1);
  228. if (!p)
  229. return -1;
  230. i2t_ASN1_OBJECT(p,i + 1,a);
  231. }
  232. if (i <= 0)
  233. return BIO_write(bp, "<INVALID>", 9);
  234. BIO_write(bp,p,i);
  235. if (p != buf)
  236. OPENSSL_free(p);
  237. return(i);
  238. }
  239. ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
  240. long length)
  241. {
  242. const unsigned char *p;
  243. long len;
  244. int tag,xclass;
  245. int inf,i;
  246. ASN1_OBJECT *ret = NULL;
  247. p= *pp;
  248. inf=ASN1_get_object(&p,&len,&tag,&xclass,length);
  249. if (inf & 0x80)
  250. {
  251. i=ASN1_R_BAD_OBJECT_HEADER;
  252. goto err;
  253. }
  254. if (tag != V_ASN1_OBJECT)
  255. {
  256. i=ASN1_R_EXPECTING_AN_OBJECT;
  257. goto err;
  258. }
  259. ret = c2i_ASN1_OBJECT(a, &p, len);
  260. if(ret) *pp = p;
  261. return ret;
  262. err:
  263. OPENSSL_PUT_ERROR(ASN1, d2i_ASN1_OBJECT, i);
  264. return(NULL);
  265. }
  266. ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
  267. long len)
  268. {
  269. ASN1_OBJECT *ret=NULL;
  270. const unsigned char *p;
  271. unsigned char *data;
  272. int i, length;
  273. /* Sanity check OID encoding.
  274. * Need at least one content octet.
  275. * MSB must be clear in the last octet.
  276. * can't have leading 0x80 in subidentifiers, see: X.690 8.19.2
  277. */
  278. if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
  279. p[len - 1] & 0x80)
  280. {
  281. OPENSSL_PUT_ERROR(ASN1, c2i_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
  282. return NULL;
  283. }
  284. /* Now 0 < len <= INT_MAX, so the cast is safe. */
  285. length = (int)len;
  286. for (i = 0; i < length; i++, p++)
  287. {
  288. if (*p == 0x80 && (!i || !(p[-1] & 0x80)))
  289. {
  290. OPENSSL_PUT_ERROR(ASN1, c2i_ASN1_OBJECT, ASN1_R_INVALID_OBJECT_ENCODING);
  291. return NULL;
  292. }
  293. }
  294. /* only the ASN1_OBJECTs from the 'table' will have values
  295. * for ->sn or ->ln */
  296. if ((a == NULL) || ((*a) == NULL) ||
  297. !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC))
  298. {
  299. if ((ret=ASN1_OBJECT_new()) == NULL) return(NULL);
  300. }
  301. else ret=(*a);
  302. p= *pp;
  303. /* detach data from object */
  304. data = (unsigned char *)ret->data;
  305. ret->data = NULL;
  306. /* once detached we can change it */
  307. if ((data == NULL) || (ret->length < length))
  308. {
  309. ret->length=0;
  310. if (data != NULL) OPENSSL_free(data);
  311. data=(unsigned char *)OPENSSL_malloc(length);
  312. if (data == NULL)
  313. { i=ERR_R_MALLOC_FAILURE; goto err; }
  314. ret->flags|=ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  315. }
  316. memcpy(data,p,length);
  317. /* reattach data to object, after which it remains const */
  318. ret->data =data;
  319. ret->length=length;
  320. ret->sn=NULL;
  321. ret->ln=NULL;
  322. /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
  323. p+=length;
  324. if (a != NULL) (*a)=ret;
  325. *pp=p;
  326. return(ret);
  327. err:
  328. OPENSSL_PUT_ERROR(ASN1, c2i_ASN1_OBJECT, i);
  329. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  330. ASN1_OBJECT_free(ret);
  331. return(NULL);
  332. }
  333. ASN1_OBJECT *ASN1_OBJECT_new(void)
  334. {
  335. ASN1_OBJECT *ret;
  336. ret=(ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT));
  337. if (ret == NULL)
  338. {
  339. OPENSSL_PUT_ERROR(ASN1, ASN1_OBJECT_new, ERR_R_MALLOC_FAILURE);
  340. return(NULL);
  341. }
  342. ret->length=0;
  343. ret->data=NULL;
  344. ret->nid=0;
  345. ret->sn=NULL;
  346. ret->ln=NULL;
  347. ret->flags=ASN1_OBJECT_FLAG_DYNAMIC;
  348. return(ret);
  349. }
  350. void ASN1_OBJECT_free(ASN1_OBJECT *a)
  351. {
  352. if (a == NULL) return;
  353. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS)
  354. {
  355. #ifndef CONST_STRICT /* disable purely for compile-time strict const checking. Doing this on a "real" compile will cause memory leaks */
  356. if (a->sn != NULL) OPENSSL_free((void *)a->sn);
  357. if (a->ln != NULL) OPENSSL_free((void *)a->ln);
  358. #endif
  359. a->sn=a->ln=NULL;
  360. }
  361. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA)
  362. {
  363. if (a->data != NULL) OPENSSL_free((void *)a->data);
  364. a->data=NULL;
  365. a->length=0;
  366. }
  367. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
  368. OPENSSL_free(a);
  369. }
  370. ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
  371. const char *sn, const char *ln)
  372. {
  373. ASN1_OBJECT o;
  374. o.sn=sn;
  375. o.ln=ln;
  376. o.data=data;
  377. o.nid=nid;
  378. o.length=len;
  379. o.flags=ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS|
  380. ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  381. return(OBJ_dup(&o));
  382. }