x509name.c 11 KB

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