x509name.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 <string.h>
  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 X509_NAME_get_text_by_NID(X509_NAME *name, int nid, char *buf, int len)
  64. {
  65. const ASN1_OBJECT *obj;
  66. obj = OBJ_nid2obj(nid);
  67. if (obj == NULL)
  68. return (-1);
  69. return (X509_NAME_get_text_by_OBJ(name, obj, buf, len));
  70. }
  71. int X509_NAME_get_text_by_OBJ(X509_NAME *name, const ASN1_OBJECT *obj,
  72. char *buf, int len)
  73. {
  74. int i;
  75. ASN1_STRING *data;
  76. i = X509_NAME_get_index_by_OBJ(name, obj, -1);
  77. if (i < 0)
  78. return (-1);
  79. data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i));
  80. i = (data->length > (len - 1)) ? (len - 1) : data->length;
  81. if (buf == NULL)
  82. return (data->length);
  83. memcpy(buf, data->data, i);
  84. buf[i] = '\0';
  85. return (i);
  86. }
  87. int X509_NAME_entry_count(X509_NAME *name)
  88. {
  89. if (name == NULL)
  90. return (0);
  91. return (sk_X509_NAME_ENTRY_num(name->entries));
  92. }
  93. int X509_NAME_get_index_by_NID(X509_NAME *name, int nid, int lastpos)
  94. {
  95. const ASN1_OBJECT *obj;
  96. obj = OBJ_nid2obj(nid);
  97. if (obj == NULL)
  98. return (-2);
  99. return (X509_NAME_get_index_by_OBJ(name, obj, lastpos));
  100. }
  101. /* NOTE: you should be passsing -1, not 0 as lastpos */
  102. int X509_NAME_get_index_by_OBJ(X509_NAME *name, const ASN1_OBJECT *obj,
  103. int lastpos)
  104. {
  105. int n;
  106. X509_NAME_ENTRY *ne;
  107. STACK_OF(X509_NAME_ENTRY) *sk;
  108. if (name == NULL)
  109. return (-1);
  110. if (lastpos < 0)
  111. lastpos = -1;
  112. sk = name->entries;
  113. n = sk_X509_NAME_ENTRY_num(sk);
  114. for (lastpos++; lastpos < n; lastpos++) {
  115. ne = sk_X509_NAME_ENTRY_value(sk, lastpos);
  116. if (OBJ_cmp(ne->object, obj) == 0)
  117. return (lastpos);
  118. }
  119. return (-1);
  120. }
  121. X509_NAME_ENTRY *X509_NAME_get_entry(X509_NAME *name, int loc)
  122. {
  123. if (name == NULL || loc < 0
  124. || sk_X509_NAME_ENTRY_num(name->entries) <= (size_t)loc)
  125. return (NULL);
  126. else
  127. return (sk_X509_NAME_ENTRY_value(name->entries, loc));
  128. }
  129. X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc)
  130. {
  131. X509_NAME_ENTRY *ret;
  132. int i, n, set_prev, set_next;
  133. STACK_OF(X509_NAME_ENTRY) *sk;
  134. if (name == NULL || loc < 0
  135. || sk_X509_NAME_ENTRY_num(name->entries) <= (size_t)loc)
  136. return (NULL);
  137. sk = name->entries;
  138. ret = sk_X509_NAME_ENTRY_delete(sk, loc);
  139. n = sk_X509_NAME_ENTRY_num(sk);
  140. name->modified = 1;
  141. if (loc == n)
  142. return (ret);
  143. /* else we need to fixup the set field */
  144. if (loc != 0)
  145. set_prev = (sk_X509_NAME_ENTRY_value(sk, loc - 1))->set;
  146. else
  147. set_prev = ret->set - 1;
  148. set_next = sk_X509_NAME_ENTRY_value(sk, loc)->set;
  149. /*
  150. * set_prev is the previous set set is the current set set_next is the
  151. * following prev 1 1 1 1 1 1 1 1 set 1 1 2 2 next 1 1 2 2 2 2 3 2 so
  152. * basically only if prev and next differ by 2, then re-number down by 1
  153. */
  154. if (set_prev + 1 < set_next)
  155. for (i = loc; i < n; i++)
  156. sk_X509_NAME_ENTRY_value(sk, i)->set--;
  157. return (ret);
  158. }
  159. int X509_NAME_add_entry_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, int type,
  160. unsigned char *bytes, int len, int loc,
  161. int set)
  162. {
  163. X509_NAME_ENTRY *ne;
  164. int ret;
  165. ne = X509_NAME_ENTRY_create_by_OBJ(NULL, obj, type, bytes, len);
  166. if (!ne)
  167. return 0;
  168. ret = X509_NAME_add_entry(name, ne, loc, set);
  169. X509_NAME_ENTRY_free(ne);
  170. return ret;
  171. }
  172. int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid, int type,
  173. unsigned char *bytes, int len, int loc,
  174. int set)
  175. {
  176. X509_NAME_ENTRY *ne;
  177. int ret;
  178. ne = X509_NAME_ENTRY_create_by_NID(NULL, nid, type, bytes, len);
  179. if (!ne)
  180. return 0;
  181. ret = X509_NAME_add_entry(name, ne, loc, set);
  182. X509_NAME_ENTRY_free(ne);
  183. return ret;
  184. }
  185. int X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type,
  186. const unsigned char *bytes, int len, int loc,
  187. int set)
  188. {
  189. X509_NAME_ENTRY *ne;
  190. int ret;
  191. ne = X509_NAME_ENTRY_create_by_txt(NULL, field, type, bytes, len);
  192. if (!ne)
  193. return 0;
  194. ret = X509_NAME_add_entry(name, ne, loc, set);
  195. X509_NAME_ENTRY_free(ne);
  196. return ret;
  197. }
  198. /*
  199. * if set is -1, append to previous set, 0 'a new one', and 1, prepend to the
  200. * guy we are about to stomp on.
  201. */
  202. int X509_NAME_add_entry(X509_NAME *name, X509_NAME_ENTRY *ne, int loc,
  203. int set)
  204. {
  205. X509_NAME_ENTRY *new_name = NULL;
  206. int n, i, inc;
  207. STACK_OF(X509_NAME_ENTRY) *sk;
  208. if (name == NULL)
  209. return (0);
  210. sk = name->entries;
  211. n = sk_X509_NAME_ENTRY_num(sk);
  212. if (loc > n)
  213. loc = n;
  214. else if (loc < 0)
  215. loc = n;
  216. name->modified = 1;
  217. if (set == -1) {
  218. if (loc == 0) {
  219. set = 0;
  220. inc = 1;
  221. } else {
  222. set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set;
  223. inc = 0;
  224. }
  225. } else { /* if (set >= 0) */
  226. if (loc >= n) {
  227. if (loc != 0)
  228. set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set + 1;
  229. else
  230. set = 0;
  231. } else
  232. set = sk_X509_NAME_ENTRY_value(sk, loc)->set;
  233. inc = (set == 0) ? 1 : 0;
  234. }
  235. if ((new_name = X509_NAME_ENTRY_dup(ne)) == NULL)
  236. goto err;
  237. new_name->set = set;
  238. if (!sk_X509_NAME_ENTRY_insert(sk, new_name, loc)) {
  239. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  240. goto err;
  241. }
  242. if (inc) {
  243. n = sk_X509_NAME_ENTRY_num(sk);
  244. for (i = loc + 1; i < n; i++)
  245. sk_X509_NAME_ENTRY_value(sk, i - 1)->set += 1;
  246. }
  247. return (1);
  248. err:
  249. if (new_name != NULL)
  250. X509_NAME_ENTRY_free(new_name);
  251. return (0);
  252. }
  253. X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_txt(X509_NAME_ENTRY **ne,
  254. const char *field, int type,
  255. const unsigned char *bytes,
  256. int len)
  257. {
  258. ASN1_OBJECT *obj;
  259. X509_NAME_ENTRY *nentry;
  260. obj = OBJ_txt2obj(field, 0);
  261. if (obj == NULL) {
  262. OPENSSL_PUT_ERROR(X509, X509_R_INVALID_FIELD_NAME);
  263. ERR_add_error_data(2, "name=", field);
  264. return (NULL);
  265. }
  266. nentry = X509_NAME_ENTRY_create_by_OBJ(ne, obj, type, bytes, len);
  267. ASN1_OBJECT_free(obj);
  268. return nentry;
  269. }
  270. X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid,
  271. int type, unsigned char *bytes,
  272. int len)
  273. {
  274. const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  275. if (obj == NULL) {
  276. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_NID);
  277. return NULL;
  278. }
  279. return X509_NAME_ENTRY_create_by_OBJ(ne, obj, type, bytes, len);
  280. }
  281. X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne,
  282. const ASN1_OBJECT *obj,
  283. int type,
  284. const unsigned char *bytes,
  285. int len)
  286. {
  287. X509_NAME_ENTRY *ret;
  288. if ((ne == NULL) || (*ne == NULL)) {
  289. if ((ret = X509_NAME_ENTRY_new()) == NULL)
  290. return (NULL);
  291. } else
  292. ret = *ne;
  293. if (!X509_NAME_ENTRY_set_object(ret, obj))
  294. goto err;
  295. if (!X509_NAME_ENTRY_set_data(ret, type, bytes, len))
  296. goto err;
  297. if ((ne != NULL) && (*ne == NULL))
  298. *ne = ret;
  299. return (ret);
  300. err:
  301. if ((ne == NULL) || (ret != *ne))
  302. X509_NAME_ENTRY_free(ret);
  303. return (NULL);
  304. }
  305. int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne, const ASN1_OBJECT *obj)
  306. {
  307. if ((ne == NULL) || (obj == NULL)) {
  308. OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER);
  309. return (0);
  310. }
  311. ASN1_OBJECT_free(ne->object);
  312. ne->object = OBJ_dup(obj);
  313. return ((ne->object == NULL) ? 0 : 1);
  314. }
  315. int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne, int type,
  316. const unsigned char *bytes, int len)
  317. {
  318. int i;
  319. if ((ne == NULL) || ((bytes == NULL) && (len != 0)))
  320. return (0);
  321. if ((type > 0) && (type & MBSTRING_FLAG))
  322. return ASN1_STRING_set_by_NID(&ne->value, bytes,
  323. len, type,
  324. OBJ_obj2nid(ne->object)) ? 1 : 0;
  325. if (len < 0)
  326. len = strlen((const char *)bytes);
  327. i = ASN1_STRING_set(ne->value, bytes, len);
  328. if (!i)
  329. return (0);
  330. if (type != V_ASN1_UNDEF) {
  331. if (type == V_ASN1_APP_CHOOSE)
  332. ne->value->type = ASN1_PRINTABLE_type(bytes, len);
  333. else
  334. ne->value->type = type;
  335. }
  336. return (1);
  337. }
  338. ASN1_OBJECT *X509_NAME_ENTRY_get_object(X509_NAME_ENTRY *ne)
  339. {
  340. if (ne == NULL)
  341. return (NULL);
  342. return (ne->object);
  343. }
  344. ASN1_STRING *X509_NAME_ENTRY_get_data(X509_NAME_ENTRY *ne)
  345. {
  346. if (ne == NULL)
  347. return (NULL);
  348. return (ne->value);
  349. }