x509_att.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright 1995-2024 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 <openssl/x509v3.h>
  17. #include "crypto/x509.h"
  18. #include "x509_local.h"
  19. int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)
  20. {
  21. return sk_X509_ATTRIBUTE_num(x);
  22. }
  23. int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid,
  24. int lastpos)
  25. {
  26. const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  27. if (obj == NULL)
  28. return -2;
  29. return X509at_get_attr_by_OBJ(x, obj, lastpos);
  30. }
  31. int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk,
  32. const ASN1_OBJECT *obj, int lastpos)
  33. {
  34. int n;
  35. X509_ATTRIBUTE *ex;
  36. if (sk == NULL)
  37. return -1;
  38. lastpos++;
  39. if (lastpos < 0)
  40. lastpos = 0;
  41. n = sk_X509_ATTRIBUTE_num(sk);
  42. for (; lastpos < n; lastpos++) {
  43. ex = sk_X509_ATTRIBUTE_value(sk, lastpos);
  44. if (OBJ_cmp(ex->object, obj) == 0)
  45. return lastpos;
  46. }
  47. return -1;
  48. }
  49. X509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc)
  50. {
  51. if (x == NULL) {
  52. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  53. return NULL;
  54. }
  55. if (sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0) {
  56. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_INVALID_ARGUMENT);
  57. return NULL;
  58. }
  59. return sk_X509_ATTRIBUTE_value(x, loc);
  60. }
  61. X509_ATTRIBUTE *X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc)
  62. {
  63. if (x == NULL) {
  64. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  65. return NULL;
  66. }
  67. if (sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0) {
  68. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_INVALID_ARGUMENT);
  69. return NULL;
  70. }
  71. return sk_X509_ATTRIBUTE_delete(x, loc);
  72. }
  73. STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
  74. X509_ATTRIBUTE *attr)
  75. {
  76. X509_ATTRIBUTE *new_attr = NULL;
  77. STACK_OF(X509_ATTRIBUTE) *sk = NULL;
  78. if (x == NULL || attr == NULL) {
  79. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  80. return NULL;
  81. }
  82. if (*x == NULL) {
  83. if ((sk = sk_X509_ATTRIBUTE_new_null()) == NULL) {
  84. ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
  85. goto err;
  86. }
  87. } else {
  88. sk = *x;
  89. }
  90. if ((new_attr = X509_ATTRIBUTE_dup(attr)) == NULL)
  91. goto err;
  92. if (!sk_X509_ATTRIBUTE_push(sk, new_attr)) {
  93. ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
  94. goto err;
  95. }
  96. if (*x == NULL)
  97. *x = sk;
  98. return sk;
  99. err:
  100. X509_ATTRIBUTE_free(new_attr);
  101. if (*x == NULL)
  102. sk_X509_ATTRIBUTE_free(sk);
  103. return NULL;
  104. }
  105. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
  106. X509_ATTRIBUTE *attr)
  107. {
  108. if (x == NULL || attr == NULL) {
  109. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  110. return NULL;
  111. }
  112. if (*x != NULL && X509at_get_attr_by_OBJ(*x, attr->object, -1) != -1) {
  113. ERR_raise(ERR_LIB_X509, X509_R_DUPLICATE_ATTRIBUTE);
  114. return NULL;
  115. }
  116. return ossl_x509at_add1_attr(x, attr);
  117. }
  118. STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE) **x,
  119. const ASN1_OBJECT *obj,
  120. int type,
  121. const unsigned char *bytes,
  122. int len)
  123. {
  124. X509_ATTRIBUTE *attr;
  125. STACK_OF(X509_ATTRIBUTE) *ret;
  126. attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
  127. if (attr == NULL)
  128. return 0;
  129. ret = ossl_x509at_add1_attr(x, attr);
  130. X509_ATTRIBUTE_free(attr);
  131. return ret;
  132. }
  133. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE)
  134. **x, const ASN1_OBJECT *obj,
  135. int type,
  136. const unsigned char *bytes,
  137. int len)
  138. {
  139. if (x == NULL || obj == NULL) {
  140. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  141. return NULL;
  142. }
  143. if (*x != NULL && X509at_get_attr_by_OBJ(*x, obj, -1) != -1) {
  144. ERR_raise(ERR_LIB_X509, X509_R_DUPLICATE_ATTRIBUTE);
  145. return NULL;
  146. }
  147. return ossl_x509at_add1_attr_by_OBJ(x, obj, type, bytes, len);
  148. }
  149. STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE) **x,
  150. int nid, int type,
  151. const unsigned char *bytes,
  152. int len)
  153. {
  154. X509_ATTRIBUTE *attr;
  155. STACK_OF(X509_ATTRIBUTE) *ret;
  156. attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
  157. if (attr == NULL)
  158. return 0;
  159. ret = ossl_x509at_add1_attr(x, attr);
  160. X509_ATTRIBUTE_free(attr);
  161. return ret;
  162. }
  163. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE)
  164. **x, int nid, int type,
  165. const unsigned char *bytes,
  166. int len)
  167. {
  168. if (x == NULL) {
  169. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  170. return NULL;
  171. }
  172. if (*x != NULL && X509at_get_attr_by_NID(*x, nid, -1) != -1) {
  173. ERR_raise(ERR_LIB_X509, X509_R_DUPLICATE_ATTRIBUTE);
  174. return NULL;
  175. }
  176. return ossl_x509at_add1_attr_by_NID(x, nid, type, bytes, len);
  177. }
  178. STACK_OF(X509_ATTRIBUTE) *ossl_x509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE) **x,
  179. const char *attrname,
  180. int type,
  181. const unsigned char *bytes,
  182. int len)
  183. {
  184. X509_ATTRIBUTE *attr;
  185. STACK_OF(X509_ATTRIBUTE) *ret;
  186. attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
  187. if (attr == NULL)
  188. return 0;
  189. ret = ossl_x509at_add1_attr(x, attr);
  190. X509_ATTRIBUTE_free(attr);
  191. return ret;
  192. }
  193. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE)
  194. **x, const char *attrname,
  195. int type,
  196. const unsigned char *bytes,
  197. int len)
  198. {
  199. X509_ATTRIBUTE *attr;
  200. STACK_OF(X509_ATTRIBUTE) *ret;
  201. attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
  202. if (attr == NULL)
  203. return 0;
  204. ret = X509at_add1_attr(x, attr);
  205. X509_ATTRIBUTE_free(attr);
  206. return ret;
  207. }
  208. void *X509at_get0_data_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *x,
  209. const ASN1_OBJECT *obj, int lastpos, int type)
  210. {
  211. int i = X509at_get_attr_by_OBJ(x, obj, lastpos);
  212. X509_ATTRIBUTE *at;
  213. if (i == -1)
  214. return NULL;
  215. if (lastpos <= -2 && X509at_get_attr_by_OBJ(x, obj, i) != -1)
  216. return NULL;
  217. at = X509at_get_attr(x, i);
  218. if (lastpos <= -3 && X509_ATTRIBUTE_count(at) != 1)
  219. return NULL;
  220. return X509_ATTRIBUTE_get0_data(at, 0, type, NULL);
  221. }
  222. STACK_OF(X509_ATTRIBUTE) *ossl_x509at_dup(const STACK_OF(X509_ATTRIBUTE) *x)
  223. {
  224. int i, n = sk_X509_ATTRIBUTE_num(x);
  225. STACK_OF(X509_ATTRIBUTE) *sk = NULL;
  226. for (i = 0; i < n; ++i) {
  227. if (X509at_add1_attr(&sk, sk_X509_ATTRIBUTE_value(x, i)) == NULL) {
  228. sk_X509_ATTRIBUTE_pop_free(sk, X509_ATTRIBUTE_free);
  229. return NULL;
  230. }
  231. }
  232. return sk;
  233. }
  234. X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
  235. int atrtype, const void *data,
  236. int len)
  237. {
  238. ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  239. X509_ATTRIBUTE *ret;
  240. if (obj == NULL) {
  241. ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);
  242. return NULL;
  243. }
  244. ret = X509_ATTRIBUTE_create_by_OBJ(attr, obj, atrtype, data, len);
  245. if (ret == NULL)
  246. ASN1_OBJECT_free(obj);
  247. return ret;
  248. }
  249. X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
  250. const ASN1_OBJECT *obj,
  251. int atrtype, const void *data,
  252. int len)
  253. {
  254. X509_ATTRIBUTE *ret;
  255. if (attr == NULL || *attr == NULL) {
  256. if ((ret = X509_ATTRIBUTE_new()) == NULL) {
  257. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  258. return NULL;
  259. }
  260. } else {
  261. ret = *attr;
  262. }
  263. if (!X509_ATTRIBUTE_set1_object(ret, obj))
  264. goto err;
  265. if (!X509_ATTRIBUTE_set1_data(ret, atrtype, data, len))
  266. goto err;
  267. if (attr != NULL && *attr == NULL)
  268. *attr = ret;
  269. return ret;
  270. err:
  271. if (attr == NULL || ret != *attr)
  272. X509_ATTRIBUTE_free(ret);
  273. return NULL;
  274. }
  275. X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
  276. const char *atrname, int type,
  277. const unsigned char *bytes,
  278. int len)
  279. {
  280. ASN1_OBJECT *obj = OBJ_txt2obj(atrname, 0);
  281. X509_ATTRIBUTE *nattr;
  282. if (obj == NULL) {
  283. ERR_raise_data(ERR_LIB_X509, X509_R_INVALID_FIELD_NAME,
  284. "name=%s", atrname);
  285. return NULL;
  286. }
  287. nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len);
  288. ASN1_OBJECT_free(obj);
  289. return nattr;
  290. }
  291. int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
  292. {
  293. if (attr == NULL || obj == NULL) {
  294. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  295. return 0;
  296. }
  297. ASN1_OBJECT_free(attr->object);
  298. attr->object = OBJ_dup(obj);
  299. return attr->object != NULL;
  300. }
  301. int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
  302. const void *data, int len)
  303. {
  304. ASN1_TYPE *ttmp = NULL;
  305. ASN1_STRING *stmp = NULL;
  306. int atype = 0;
  307. if (attr == NULL) {
  308. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  309. return 0;
  310. }
  311. if ((attrtype & MBSTRING_FLAG) != 0) {
  312. stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
  313. OBJ_obj2nid(attr->object));
  314. if (stmp == NULL) {
  315. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  316. return 0;
  317. }
  318. atype = stmp->type;
  319. } else if (len != -1) {
  320. if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL
  321. || !ASN1_STRING_set(stmp, data, len)) {
  322. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  323. goto err;
  324. }
  325. atype = attrtype;
  326. }
  327. /*
  328. * This is a bit naughty because the attribute should really have at
  329. * least one value but some types use and zero length SET and require
  330. * this.
  331. */
  332. if (attrtype == 0) {
  333. ASN1_STRING_free(stmp);
  334. return 1;
  335. }
  336. if ((ttmp = ASN1_TYPE_new()) == NULL) {
  337. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  338. goto err;
  339. }
  340. if (len == -1 && (attrtype & MBSTRING_FLAG) == 0) {
  341. if (!ASN1_TYPE_set1(ttmp, attrtype, data)) {
  342. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  343. goto err;
  344. }
  345. } else {
  346. ASN1_TYPE_set(ttmp, atype, stmp);
  347. stmp = NULL;
  348. }
  349. if (!sk_ASN1_TYPE_push(attr->set, ttmp)) {
  350. ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
  351. goto err;
  352. }
  353. return 1;
  354. err:
  355. ASN1_TYPE_free(ttmp);
  356. ASN1_STRING_free(stmp);
  357. return 0;
  358. }
  359. int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr)
  360. {
  361. if (attr == NULL)
  362. return 0;
  363. return sk_ASN1_TYPE_num(attr->set);
  364. }
  365. ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
  366. {
  367. if (attr == NULL) {
  368. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  369. return NULL;
  370. }
  371. return attr->object;
  372. }
  373. void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
  374. int atrtype, void *data)
  375. {
  376. ASN1_TYPE *ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
  377. if (ttmp == NULL)
  378. return NULL;
  379. if (atrtype == V_ASN1_BOOLEAN
  380. || atrtype == V_ASN1_NULL
  381. || atrtype != ASN1_TYPE_get(ttmp)) {
  382. ERR_raise(ERR_LIB_X509, X509_R_WRONG_TYPE);
  383. return NULL;
  384. }
  385. return ttmp->value.ptr;
  386. }
  387. ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
  388. {
  389. if (attr == NULL) {
  390. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  391. return NULL;
  392. }
  393. return sk_ASN1_TYPE_value(attr->set, idx);
  394. }