You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

389 rivejä
12 KiB

  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. inc = (set == 0);
  218. name->modified = 1;
  219. if (set == -1) {
  220. if (loc == 0) {
  221. set = 0;
  222. inc = 1;
  223. } else {
  224. set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set;
  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. }
  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)->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. }