x509name.c 12 KB

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