x509name.c 12 KB

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