x509name.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/safestack.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/x509.h>
  16. #include "crypto/x509.h"
  17. int X509_NAME_get_text_by_NID(const X509_NAME *name, int nid,
  18. char *buf, int len)
  19. {
  20. ASN1_OBJECT *obj;
  21. obj = OBJ_nid2obj(nid);
  22. if (obj == NULL)
  23. return -1;
  24. return X509_NAME_get_text_by_OBJ(name, obj, buf, len);
  25. }
  26. int X509_NAME_get_text_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj,
  27. char *buf, int len)
  28. {
  29. int i;
  30. const ASN1_STRING *data;
  31. i = X509_NAME_get_index_by_OBJ(name, obj, -1);
  32. if (i < 0)
  33. return -1;
  34. data = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i));
  35. if (buf == NULL)
  36. return data->length;
  37. if (len <= 0)
  38. return 0;
  39. i = (data->length > (len - 1)) ? (len - 1) : data->length;
  40. memcpy(buf, data->data, i);
  41. buf[i] = '\0';
  42. return i;
  43. }
  44. int X509_NAME_entry_count(const X509_NAME *name)
  45. {
  46. if (name == NULL)
  47. return 0;
  48. return sk_X509_NAME_ENTRY_num(name->entries);
  49. }
  50. int X509_NAME_get_index_by_NID(const X509_NAME *name, int nid, int lastpos)
  51. {
  52. ASN1_OBJECT *obj;
  53. obj = OBJ_nid2obj(nid);
  54. if (obj == NULL)
  55. return -2;
  56. return X509_NAME_get_index_by_OBJ(name, obj, lastpos);
  57. }
  58. /* NOTE: you should be passing -1, not 0 as lastpos */
  59. int X509_NAME_get_index_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj,
  60. int lastpos)
  61. {
  62. int n;
  63. X509_NAME_ENTRY *ne;
  64. STACK_OF(X509_NAME_ENTRY) *sk;
  65. if (name == NULL)
  66. return -1;
  67. if (lastpos < 0)
  68. lastpos = -1;
  69. sk = name->entries;
  70. n = sk_X509_NAME_ENTRY_num(sk);
  71. for (lastpos++; lastpos < n; lastpos++) {
  72. ne = sk_X509_NAME_ENTRY_value(sk, lastpos);
  73. if (OBJ_cmp(ne->object, obj) == 0)
  74. return lastpos;
  75. }
  76. return -1;
  77. }
  78. X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name, int loc)
  79. {
  80. if (name == NULL || sk_X509_NAME_ENTRY_num(name->entries) <= loc
  81. || loc < 0)
  82. return NULL;
  83. return sk_X509_NAME_ENTRY_value(name->entries, loc);
  84. }
  85. X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc)
  86. {
  87. X509_NAME_ENTRY *ret;
  88. int i, n, set_prev, set_next;
  89. STACK_OF(X509_NAME_ENTRY) *sk;
  90. if (name == NULL || sk_X509_NAME_ENTRY_num(name->entries) <= loc
  91. || loc < 0)
  92. return NULL;
  93. sk = name->entries;
  94. ret = sk_X509_NAME_ENTRY_delete(sk, loc);
  95. n = sk_X509_NAME_ENTRY_num(sk);
  96. name->modified = 1;
  97. if (loc == n)
  98. return ret;
  99. /* else we need to fixup the set field */
  100. if (loc != 0)
  101. set_prev = (sk_X509_NAME_ENTRY_value(sk, loc - 1))->set;
  102. else
  103. set_prev = ret->set - 1;
  104. set_next = sk_X509_NAME_ENTRY_value(sk, loc)->set;
  105. /*-
  106. * set_prev is the previous set
  107. * set is the current set
  108. * set_next is the following
  109. * prev 1 1 1 1 1 1 1 1
  110. * set 1 1 2 2
  111. * next 1 1 2 2 2 2 3 2
  112. * so basically only if prev and next differ by 2, then
  113. * re-number down by 1
  114. */
  115. if (set_prev + 1 < set_next)
  116. for (i = loc; i < n; i++)
  117. sk_X509_NAME_ENTRY_value(sk, i)->set--;
  118. return ret;
  119. }
  120. int X509_NAME_add_entry_by_OBJ(X509_NAME *name, const ASN1_OBJECT *obj, int type,
  121. const unsigned char *bytes, int len, int loc,
  122. int set)
  123. {
  124. X509_NAME_ENTRY *ne;
  125. int ret;
  126. ne = X509_NAME_ENTRY_create_by_OBJ(NULL, obj, type, bytes, len);
  127. if (!ne)
  128. return 0;
  129. ret = X509_NAME_add_entry(name, ne, loc, set);
  130. X509_NAME_ENTRY_free(ne);
  131. return ret;
  132. }
  133. int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid, int type,
  134. const unsigned char *bytes, int len, int loc,
  135. int set)
  136. {
  137. X509_NAME_ENTRY *ne;
  138. int ret;
  139. ne = X509_NAME_ENTRY_create_by_NID(NULL, nid, type, bytes, len);
  140. if (!ne)
  141. return 0;
  142. ret = X509_NAME_add_entry(name, ne, loc, set);
  143. X509_NAME_ENTRY_free(ne);
  144. return ret;
  145. }
  146. int X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type,
  147. const unsigned char *bytes, int len, int loc,
  148. int set)
  149. {
  150. X509_NAME_ENTRY *ne;
  151. int ret;
  152. ne = X509_NAME_ENTRY_create_by_txt(NULL, field, type, bytes, len);
  153. if (!ne)
  154. return 0;
  155. ret = X509_NAME_add_entry(name, ne, loc, set);
  156. X509_NAME_ENTRY_free(ne);
  157. return ret;
  158. }
  159. /*
  160. * if set is -1, append to previous set, 0 'a new one', and 1, prepend to the
  161. * guy we are about to stomp on.
  162. */
  163. int X509_NAME_add_entry(X509_NAME *name, const X509_NAME_ENTRY *ne, int loc,
  164. int set)
  165. {
  166. X509_NAME_ENTRY *new_name = NULL;
  167. int n, i, inc;
  168. STACK_OF(X509_NAME_ENTRY) *sk;
  169. if (name == NULL)
  170. return 0;
  171. sk = name->entries;
  172. n = sk_X509_NAME_ENTRY_num(sk);
  173. if (loc > n)
  174. loc = n;
  175. else if (loc < 0)
  176. loc = n;
  177. inc = (set == 0);
  178. name->modified = 1;
  179. if (set == -1) {
  180. if (loc == 0) {
  181. set = 0;
  182. inc = 1;
  183. } else {
  184. set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set;
  185. }
  186. } else { /* if (set >= 0) */
  187. if (loc >= n) {
  188. if (loc != 0)
  189. set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set + 1;
  190. else
  191. set = 0;
  192. } else
  193. set = sk_X509_NAME_ENTRY_value(sk, loc)->set;
  194. }
  195. if ((new_name = X509_NAME_ENTRY_dup(ne)) == NULL)
  196. goto err;
  197. new_name->set = set;
  198. if (!sk_X509_NAME_ENTRY_insert(sk, new_name, loc)) {
  199. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  200. goto err;
  201. }
  202. if (inc) {
  203. n = sk_X509_NAME_ENTRY_num(sk);
  204. for (i = loc + 1; i < n; i++)
  205. sk_X509_NAME_ENTRY_value(sk, i)->set += 1;
  206. }
  207. return 1;
  208. err:
  209. X509_NAME_ENTRY_free(new_name);
  210. return 0;
  211. }
  212. X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_txt(X509_NAME_ENTRY **ne,
  213. const char *field, int type,
  214. const unsigned char *bytes,
  215. int len)
  216. {
  217. ASN1_OBJECT *obj;
  218. X509_NAME_ENTRY *nentry;
  219. obj = OBJ_txt2obj(field, 0);
  220. if (obj == NULL) {
  221. ERR_raise_data(ERR_LIB_X509, X509_R_INVALID_FIELD_NAME,
  222. "name=%s", field);
  223. return NULL;
  224. }
  225. nentry = X509_NAME_ENTRY_create_by_OBJ(ne, obj, type, bytes, len);
  226. ASN1_OBJECT_free(obj);
  227. return nentry;
  228. }
  229. X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid,
  230. int type,
  231. const unsigned char *bytes,
  232. int len)
  233. {
  234. ASN1_OBJECT *obj;
  235. X509_NAME_ENTRY *nentry;
  236. obj = OBJ_nid2obj(nid);
  237. if (obj == NULL) {
  238. ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);
  239. return NULL;
  240. }
  241. nentry = X509_NAME_ENTRY_create_by_OBJ(ne, obj, type, bytes, len);
  242. ASN1_OBJECT_free(obj);
  243. return nentry;
  244. }
  245. X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne,
  246. const ASN1_OBJECT *obj, int type,
  247. const unsigned char *bytes,
  248. int len)
  249. {
  250. X509_NAME_ENTRY *ret;
  251. if ((ne == NULL) || (*ne == NULL)) {
  252. if ((ret = X509_NAME_ENTRY_new()) == NULL)
  253. return NULL;
  254. } else
  255. ret = *ne;
  256. if (!X509_NAME_ENTRY_set_object(ret, obj))
  257. goto err;
  258. if (!X509_NAME_ENTRY_set_data(ret, type, bytes, len))
  259. goto err;
  260. if ((ne != NULL) && (*ne == NULL))
  261. *ne = ret;
  262. return ret;
  263. err:
  264. if ((ne == NULL) || (ret != *ne))
  265. X509_NAME_ENTRY_free(ret);
  266. return NULL;
  267. }
  268. int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne, const ASN1_OBJECT *obj)
  269. {
  270. if ((ne == NULL) || (obj == NULL)) {
  271. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  272. return 0;
  273. }
  274. ASN1_OBJECT_free(ne->object);
  275. ne->object = OBJ_dup(obj);
  276. return ((ne->object == NULL) ? 0 : 1);
  277. }
  278. int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne, int type,
  279. const unsigned char *bytes, int len)
  280. {
  281. int i;
  282. if ((ne == NULL) || ((bytes == NULL) && (len != 0)))
  283. return 0;
  284. if ((type > 0) && (type & MBSTRING_FLAG))
  285. return ASN1_STRING_set_by_NID(&ne->value, bytes,
  286. len, type,
  287. OBJ_obj2nid(ne->object)) ? 1 : 0;
  288. if (len < 0)
  289. len = strlen((const char *)bytes);
  290. i = ASN1_STRING_set(ne->value, bytes, len);
  291. if (!i)
  292. return 0;
  293. if (type != V_ASN1_UNDEF) {
  294. if (type == V_ASN1_APP_CHOOSE)
  295. ne->value->type = ASN1_PRINTABLE_type(bytes, len);
  296. else
  297. ne->value->type = type;
  298. }
  299. return 1;
  300. }
  301. ASN1_OBJECT *X509_NAME_ENTRY_get_object(const X509_NAME_ENTRY *ne)
  302. {
  303. if (ne == NULL)
  304. return NULL;
  305. return ne->object;
  306. }
  307. ASN1_STRING *X509_NAME_ENTRY_get_data(const X509_NAME_ENTRY *ne)
  308. {
  309. if (ne == NULL)
  310. return NULL;
  311. return ne->value;
  312. }
  313. int X509_NAME_ENTRY_set(const X509_NAME_ENTRY *ne)
  314. {
  315. return ne->set;
  316. }