No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

x509_att.c 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* crypto/x509/x509_att.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. #include <openssl/asn1.h>
  58. #include <openssl/err.h>
  59. #include <openssl/evp.h>
  60. #include <openssl/obj.h>
  61. #include <openssl/stack.h>
  62. #include <openssl/x509.h>
  63. int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)
  64. {
  65. return sk_X509_ATTRIBUTE_num(x);
  66. }
  67. int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid,
  68. int lastpos)
  69. {
  70. const ASN1_OBJECT *obj;
  71. obj = OBJ_nid2obj(nid);
  72. if (obj == NULL)
  73. return (-2);
  74. return (X509at_get_attr_by_OBJ(x, obj, lastpos));
  75. }
  76. int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk,
  77. const ASN1_OBJECT *obj, int lastpos)
  78. {
  79. int n;
  80. X509_ATTRIBUTE *ex;
  81. if (sk == NULL)
  82. return (-1);
  83. lastpos++;
  84. if (lastpos < 0)
  85. lastpos = 0;
  86. n = sk_X509_ATTRIBUTE_num(sk);
  87. for (; lastpos < n; lastpos++) {
  88. ex = sk_X509_ATTRIBUTE_value(sk, lastpos);
  89. if (OBJ_cmp(ex->object, obj) == 0)
  90. return (lastpos);
  91. }
  92. return (-1);
  93. }
  94. X509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc)
  95. {
  96. if (x == NULL || loc < 0 || sk_X509_ATTRIBUTE_num(x) <= (size_t)loc)
  97. return NULL;
  98. else
  99. return sk_X509_ATTRIBUTE_value(x, loc);
  100. }
  101. X509_ATTRIBUTE *X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc)
  102. {
  103. X509_ATTRIBUTE *ret;
  104. if (x == NULL || loc < 0 || sk_X509_ATTRIBUTE_num(x) <= (size_t)loc)
  105. return (NULL);
  106. ret = sk_X509_ATTRIBUTE_delete(x, loc);
  107. return (ret);
  108. }
  109. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
  110. X509_ATTRIBUTE *attr)
  111. {
  112. X509_ATTRIBUTE *new_attr = NULL;
  113. STACK_OF(X509_ATTRIBUTE) *sk = NULL;
  114. if (x == NULL) {
  115. OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER);
  116. goto err2;
  117. }
  118. if (*x == NULL) {
  119. if ((sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
  120. goto err;
  121. } else
  122. sk = *x;
  123. if ((new_attr = X509_ATTRIBUTE_dup(attr)) == NULL)
  124. goto err2;
  125. if (!sk_X509_ATTRIBUTE_push(sk, new_attr))
  126. goto err;
  127. if (*x == NULL)
  128. *x = sk;
  129. return (sk);
  130. err:
  131. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  132. err2:
  133. if (new_attr != NULL)
  134. X509_ATTRIBUTE_free(new_attr);
  135. if (sk != NULL)
  136. sk_X509_ATTRIBUTE_free(sk);
  137. return (NULL);
  138. }
  139. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE)
  140. **x, const ASN1_OBJECT *obj,
  141. int type,
  142. const unsigned char *bytes,
  143. int len)
  144. {
  145. X509_ATTRIBUTE *attr;
  146. STACK_OF(X509_ATTRIBUTE) *ret;
  147. attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
  148. if (!attr)
  149. return 0;
  150. ret = X509at_add1_attr(x, attr);
  151. X509_ATTRIBUTE_free(attr);
  152. return ret;
  153. }
  154. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE)
  155. **x, int nid, int type,
  156. const unsigned char *bytes,
  157. int len)
  158. {
  159. X509_ATTRIBUTE *attr;
  160. STACK_OF(X509_ATTRIBUTE) *ret;
  161. attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
  162. if (!attr)
  163. return 0;
  164. ret = X509at_add1_attr(x, attr);
  165. X509_ATTRIBUTE_free(attr);
  166. return ret;
  167. }
  168. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE)
  169. **x, const char *attrname,
  170. int type,
  171. const unsigned char *bytes,
  172. int len)
  173. {
  174. X509_ATTRIBUTE *attr;
  175. STACK_OF(X509_ATTRIBUTE) *ret;
  176. attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
  177. if (!attr)
  178. return 0;
  179. ret = X509at_add1_attr(x, attr);
  180. X509_ATTRIBUTE_free(attr);
  181. return ret;
  182. }
  183. void *X509at_get0_data_by_OBJ(STACK_OF(X509_ATTRIBUTE) *x,
  184. ASN1_OBJECT *obj, int lastpos, int type)
  185. {
  186. int i;
  187. X509_ATTRIBUTE *at;
  188. i = X509at_get_attr_by_OBJ(x, obj, lastpos);
  189. if (i == -1)
  190. return NULL;
  191. if ((lastpos <= -2) && (X509at_get_attr_by_OBJ(x, obj, i) != -1))
  192. return NULL;
  193. at = X509at_get_attr(x, i);
  194. if (lastpos <= -3 && (X509_ATTRIBUTE_count(at) != 1))
  195. return NULL;
  196. return X509_ATTRIBUTE_get0_data(at, 0, type, NULL);
  197. }
  198. X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
  199. int atrtype, const void *data,
  200. int len)
  201. {
  202. const ASN1_OBJECT *obj;
  203. obj = OBJ_nid2obj(nid);
  204. if (obj == NULL) {
  205. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_NID);
  206. return (NULL);
  207. }
  208. return X509_ATTRIBUTE_create_by_OBJ(attr, obj, atrtype, data, len);
  209. }
  210. X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
  211. const ASN1_OBJECT *obj,
  212. int atrtype, const void *data,
  213. int len)
  214. {
  215. X509_ATTRIBUTE *ret;
  216. if ((attr == NULL) || (*attr == NULL)) {
  217. if ((ret = X509_ATTRIBUTE_new()) == NULL) {
  218. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  219. return (NULL);
  220. }
  221. } else
  222. ret = *attr;
  223. if (!X509_ATTRIBUTE_set1_object(ret, obj))
  224. goto err;
  225. if (!X509_ATTRIBUTE_set1_data(ret, atrtype, data, len))
  226. goto err;
  227. if ((attr != NULL) && (*attr == NULL))
  228. *attr = ret;
  229. return (ret);
  230. err:
  231. if ((attr == NULL) || (ret != *attr))
  232. X509_ATTRIBUTE_free(ret);
  233. return (NULL);
  234. }
  235. X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
  236. const char *atrname, int type,
  237. const unsigned char *bytes,
  238. int len)
  239. {
  240. ASN1_OBJECT *obj;
  241. X509_ATTRIBUTE *nattr;
  242. obj = OBJ_txt2obj(atrname, 0);
  243. if (obj == NULL) {
  244. OPENSSL_PUT_ERROR(X509, X509_R_INVALID_FIELD_NAME);
  245. ERR_add_error_data(2, "name=", atrname);
  246. return (NULL);
  247. }
  248. nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len);
  249. ASN1_OBJECT_free(obj);
  250. return nattr;
  251. }
  252. int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
  253. {
  254. if ((attr == NULL) || (obj == NULL))
  255. return (0);
  256. ASN1_OBJECT_free(attr->object);
  257. attr->object = OBJ_dup(obj);
  258. return attr->object != NULL;
  259. }
  260. int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
  261. const void *data, int len)
  262. {
  263. ASN1_TYPE *ttmp = NULL;
  264. ASN1_STRING *stmp = NULL;
  265. int atype = 0;
  266. if (!attr)
  267. return 0;
  268. if (attrtype & MBSTRING_FLAG) {
  269. stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
  270. OBJ_obj2nid(attr->object));
  271. if (!stmp) {
  272. OPENSSL_PUT_ERROR(X509, ERR_R_ASN1_LIB);
  273. return 0;
  274. }
  275. atype = stmp->type;
  276. } else if (len != -1) {
  277. if (!(stmp = ASN1_STRING_type_new(attrtype)))
  278. goto err;
  279. if (!ASN1_STRING_set(stmp, data, len))
  280. goto err;
  281. atype = attrtype;
  282. }
  283. if (!(attr->value.set = sk_ASN1_TYPE_new_null()))
  284. goto err;
  285. attr->single = 0;
  286. /*
  287. * This is a bit naughty because the attribute should really have at
  288. * least one value but some types use and zero length SET and require
  289. * this.
  290. */
  291. if (attrtype == 0) {
  292. ASN1_STRING_free(stmp);
  293. return 1;
  294. }
  295. if (!(ttmp = ASN1_TYPE_new()))
  296. goto err;
  297. if ((len == -1) && !(attrtype & MBSTRING_FLAG)) {
  298. if (!ASN1_TYPE_set1(ttmp, attrtype, data))
  299. goto err;
  300. } else {
  301. ASN1_TYPE_set(ttmp, atype, stmp);
  302. stmp = NULL;
  303. }
  304. if (!sk_ASN1_TYPE_push(attr->value.set, ttmp))
  305. goto err;
  306. return 1;
  307. err:
  308. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  309. ASN1_TYPE_free(ttmp);
  310. ASN1_STRING_free(stmp);
  311. return 0;
  312. }
  313. int X509_ATTRIBUTE_count(X509_ATTRIBUTE *attr)
  314. {
  315. if (!attr->single)
  316. return sk_ASN1_TYPE_num(attr->value.set);
  317. if (attr->value.single)
  318. return 1;
  319. return 0;
  320. }
  321. ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
  322. {
  323. if (attr == NULL)
  324. return (NULL);
  325. return (attr->object);
  326. }
  327. void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
  328. int atrtype, void *data)
  329. {
  330. ASN1_TYPE *ttmp;
  331. ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
  332. if (!ttmp)
  333. return NULL;
  334. if (atrtype != ASN1_TYPE_get(ttmp)) {
  335. OPENSSL_PUT_ERROR(X509, X509_R_WRONG_TYPE);
  336. return NULL;
  337. }
  338. return ttmp->value.ptr;
  339. }
  340. ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
  341. {
  342. if (attr == NULL)
  343. return (NULL);
  344. if (idx >= X509_ATTRIBUTE_count(attr))
  345. return NULL;
  346. if (!attr->single)
  347. return sk_ASN1_TYPE_value(attr->value.set, idx);
  348. else
  349. return attr->value.single;
  350. }