You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

276 lines
8.6 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. #include "../internal.h"
  63. int i2d_ASN1_OBJECT(ASN1_OBJECT *a, unsigned char **pp)
  64. {
  65. unsigned char *p;
  66. int objsize;
  67. if ((a == NULL) || (a->data == NULL))
  68. return (0);
  69. objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
  70. if (pp == NULL || objsize == -1)
  71. return objsize;
  72. p = *pp;
  73. ASN1_put_object(&p, 0, a->length, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
  74. OPENSSL_memcpy(p, a->data, a->length);
  75. p += a->length;
  76. *pp = p;
  77. return (objsize);
  78. }
  79. int i2t_ASN1_OBJECT(char *buf, int buf_len, ASN1_OBJECT *a)
  80. {
  81. return OBJ_obj2txt(buf, buf_len, a, 0);
  82. }
  83. int i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
  84. {
  85. char buf[80], *p = buf;
  86. int i;
  87. if ((a == NULL) || (a->data == NULL))
  88. return (BIO_write(bp, "NULL", 4));
  89. i = i2t_ASN1_OBJECT(buf, sizeof buf, a);
  90. if (i > (int)(sizeof(buf) - 1)) {
  91. p = OPENSSL_malloc(i + 1);
  92. if (!p)
  93. return -1;
  94. i2t_ASN1_OBJECT(p, i + 1, a);
  95. }
  96. if (i <= 0)
  97. return BIO_write(bp, "<INVALID>", 9);
  98. BIO_write(bp, p, i);
  99. if (p != buf)
  100. OPENSSL_free(p);
  101. return (i);
  102. }
  103. ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
  104. long length)
  105. {
  106. const unsigned char *p;
  107. long len;
  108. int tag, xclass;
  109. int inf, i;
  110. ASN1_OBJECT *ret = NULL;
  111. p = *pp;
  112. inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
  113. if (inf & 0x80) {
  114. i = ASN1_R_BAD_OBJECT_HEADER;
  115. goto err;
  116. }
  117. if (tag != V_ASN1_OBJECT) {
  118. i = ASN1_R_EXPECTING_AN_OBJECT;
  119. goto err;
  120. }
  121. ret = c2i_ASN1_OBJECT(a, &p, len);
  122. if (ret)
  123. *pp = p;
  124. return ret;
  125. err:
  126. OPENSSL_PUT_ERROR(ASN1, i);
  127. return (NULL);
  128. }
  129. ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
  130. long len)
  131. {
  132. ASN1_OBJECT *ret = NULL;
  133. const unsigned char *p;
  134. unsigned char *data;
  135. int i, length;
  136. /*
  137. * Sanity check OID encoding. Need at least one content octet. MSB must
  138. * be clear in the last octet. can't have leading 0x80 in subidentifiers,
  139. * see: X.690 8.19.2
  140. */
  141. if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
  142. p[len - 1] & 0x80) {
  143. OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
  144. return NULL;
  145. }
  146. /* Now 0 < len <= INT_MAX, so the cast is safe. */
  147. length = (int)len;
  148. for (i = 0; i < length; i++, p++) {
  149. if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
  150. OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
  151. return NULL;
  152. }
  153. }
  154. /*
  155. * only the ASN1_OBJECTs from the 'table' will have values for ->sn or
  156. * ->ln
  157. */
  158. if ((a == NULL) || ((*a) == NULL) ||
  159. !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
  160. if ((ret = ASN1_OBJECT_new()) == NULL)
  161. return (NULL);
  162. } else
  163. ret = (*a);
  164. p = *pp;
  165. /* detach data from object */
  166. data = (unsigned char *)ret->data;
  167. ret->data = NULL;
  168. /* once detached we can change it */
  169. if ((data == NULL) || (ret->length < length)) {
  170. ret->length = 0;
  171. if (data != NULL)
  172. OPENSSL_free(data);
  173. data = (unsigned char *)OPENSSL_malloc(length);
  174. if (data == NULL) {
  175. i = ERR_R_MALLOC_FAILURE;
  176. goto err;
  177. }
  178. ret->flags |= ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  179. }
  180. OPENSSL_memcpy(data, p, length);
  181. /* reattach data to object, after which it remains const */
  182. ret->data = data;
  183. ret->length = length;
  184. ret->sn = NULL;
  185. ret->ln = NULL;
  186. /* ret->flags=ASN1_OBJECT_FLAG_DYNAMIC; we know it is dynamic */
  187. p += length;
  188. if (a != NULL)
  189. (*a) = ret;
  190. *pp = p;
  191. return (ret);
  192. err:
  193. OPENSSL_PUT_ERROR(ASN1, i);
  194. if ((ret != NULL) && ((a == NULL) || (*a != ret)))
  195. ASN1_OBJECT_free(ret);
  196. return (NULL);
  197. }
  198. ASN1_OBJECT *ASN1_OBJECT_new(void)
  199. {
  200. ASN1_OBJECT *ret;
  201. ret = (ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT));
  202. if (ret == NULL) {
  203. OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
  204. return (NULL);
  205. }
  206. ret->length = 0;
  207. ret->data = NULL;
  208. ret->nid = 0;
  209. ret->sn = NULL;
  210. ret->ln = NULL;
  211. ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
  212. return (ret);
  213. }
  214. void ASN1_OBJECT_free(ASN1_OBJECT *a)
  215. {
  216. if (a == NULL)
  217. return;
  218. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
  219. #ifndef CONST_STRICT /* disable purely for compile-time strict
  220. * const checking. Doing this on a "real"
  221. * compile will cause memory leaks */
  222. if (a->sn != NULL)
  223. OPENSSL_free((void *)a->sn);
  224. if (a->ln != NULL)
  225. OPENSSL_free((void *)a->ln);
  226. #endif
  227. a->sn = a->ln = NULL;
  228. }
  229. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
  230. if (a->data != NULL)
  231. OPENSSL_free((void *)a->data);
  232. a->data = NULL;
  233. a->length = 0;
  234. }
  235. if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
  236. OPENSSL_free(a);
  237. }
  238. ASN1_OBJECT *ASN1_OBJECT_create(int nid, unsigned char *data, int len,
  239. const char *sn, const char *ln)
  240. {
  241. ASN1_OBJECT o;
  242. o.sn = sn;
  243. o.ln = ln;
  244. o.data = data;
  245. o.nid = nid;
  246. o.length = len;
  247. o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
  248. ASN1_OBJECT_FLAG_DYNAMIC_DATA;
  249. return (OBJ_dup(&o));
  250. }