Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

a_object.c 8.9 KiB

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