x509_att.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright 1995-2023 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) *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 && X509at_get_attr_by_OBJ(*x, attr->object, -1) != -1) {
  83. ERR_raise(ERR_LIB_X509, X509_R_DUPLICATE_ATTRIBUTE);
  84. return NULL;
  85. }
  86. if (*x == NULL) {
  87. if ((sk = sk_X509_ATTRIBUTE_new_null()) == NULL) {
  88. ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
  89. goto err;
  90. }
  91. } else {
  92. sk = *x;
  93. }
  94. if ((new_attr = X509_ATTRIBUTE_dup(attr)) == NULL)
  95. goto err;
  96. if (!sk_X509_ATTRIBUTE_push(sk, new_attr)) {
  97. ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
  98. goto err;
  99. }
  100. if (*x == NULL)
  101. *x = sk;
  102. return sk;
  103. err:
  104. X509_ATTRIBUTE_free(new_attr);
  105. if (*x == NULL)
  106. sk_X509_ATTRIBUTE_free(sk);
  107. return NULL;
  108. }
  109. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE)
  110. **x, const ASN1_OBJECT *obj,
  111. int type,
  112. const unsigned char *bytes,
  113. int len)
  114. {
  115. X509_ATTRIBUTE *attr;
  116. STACK_OF(X509_ATTRIBUTE) *ret;
  117. attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
  118. if (attr == NULL)
  119. return 0;
  120. ret = X509at_add1_attr(x, attr);
  121. X509_ATTRIBUTE_free(attr);
  122. return ret;
  123. }
  124. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE)
  125. **x, int nid, int type,
  126. const unsigned char *bytes,
  127. int len)
  128. {
  129. X509_ATTRIBUTE *attr;
  130. STACK_OF(X509_ATTRIBUTE) *ret;
  131. attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
  132. if (attr == NULL)
  133. return 0;
  134. ret = X509at_add1_attr(x, attr);
  135. X509_ATTRIBUTE_free(attr);
  136. return ret;
  137. }
  138. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE)
  139. **x, const char *attrname,
  140. int type,
  141. const unsigned char *bytes,
  142. int len)
  143. {
  144. X509_ATTRIBUTE *attr;
  145. STACK_OF(X509_ATTRIBUTE) *ret;
  146. attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
  147. if (attr == NULL)
  148. return 0;
  149. ret = X509at_add1_attr(x, attr);
  150. X509_ATTRIBUTE_free(attr);
  151. return ret;
  152. }
  153. void *X509at_get0_data_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *x,
  154. const ASN1_OBJECT *obj, int lastpos, int type)
  155. {
  156. int i = X509at_get_attr_by_OBJ(x, obj, lastpos);
  157. X509_ATTRIBUTE *at;
  158. if (i == -1)
  159. return NULL;
  160. if (lastpos <= -2 && X509at_get_attr_by_OBJ(x, obj, i) != -1)
  161. return NULL;
  162. at = X509at_get_attr(x, i);
  163. if (lastpos <= -3 && X509_ATTRIBUTE_count(at) != 1)
  164. return NULL;
  165. return X509_ATTRIBUTE_get0_data(at, 0, type, NULL);
  166. }
  167. STACK_OF(X509_ATTRIBUTE) *ossl_x509at_dup(const STACK_OF(X509_ATTRIBUTE) *x)
  168. {
  169. int i, n = sk_X509_ATTRIBUTE_num(x);
  170. STACK_OF(X509_ATTRIBUTE) *sk = NULL;
  171. for (i = 0; i < n; ++i) {
  172. if (X509at_add1_attr(&sk, sk_X509_ATTRIBUTE_value(x, i)) == NULL) {
  173. sk_X509_ATTRIBUTE_pop_free(sk, X509_ATTRIBUTE_free);
  174. return NULL;
  175. }
  176. }
  177. return sk;
  178. }
  179. X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
  180. int atrtype, const void *data,
  181. int len)
  182. {
  183. ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  184. X509_ATTRIBUTE *ret;
  185. if (obj == NULL) {
  186. ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_NID);
  187. return NULL;
  188. }
  189. ret = X509_ATTRIBUTE_create_by_OBJ(attr, obj, atrtype, data, len);
  190. if (ret == NULL)
  191. ASN1_OBJECT_free(obj);
  192. return ret;
  193. }
  194. X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
  195. const ASN1_OBJECT *obj,
  196. int atrtype, const void *data,
  197. int len)
  198. {
  199. X509_ATTRIBUTE *ret;
  200. if (attr == NULL || *attr == NULL) {
  201. if ((ret = X509_ATTRIBUTE_new()) == NULL) {
  202. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  203. return NULL;
  204. }
  205. } else {
  206. ret = *attr;
  207. }
  208. if (!X509_ATTRIBUTE_set1_object(ret, obj))
  209. goto err;
  210. if (!X509_ATTRIBUTE_set1_data(ret, atrtype, data, len))
  211. goto err;
  212. if (attr != NULL && *attr == NULL)
  213. *attr = ret;
  214. return ret;
  215. err:
  216. if (attr == NULL || ret != *attr)
  217. X509_ATTRIBUTE_free(ret);
  218. return NULL;
  219. }
  220. X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
  221. const char *atrname, int type,
  222. const unsigned char *bytes,
  223. int len)
  224. {
  225. ASN1_OBJECT *obj = OBJ_txt2obj(atrname, 0);
  226. X509_ATTRIBUTE *nattr;
  227. if (obj == NULL) {
  228. ERR_raise_data(ERR_LIB_X509, X509_R_INVALID_FIELD_NAME,
  229. "name=%s", atrname);
  230. return NULL;
  231. }
  232. nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len);
  233. ASN1_OBJECT_free(obj);
  234. return nattr;
  235. }
  236. int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
  237. {
  238. if (attr == NULL || obj == NULL) {
  239. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  240. return 0;
  241. }
  242. ASN1_OBJECT_free(attr->object);
  243. attr->object = OBJ_dup(obj);
  244. return attr->object != NULL;
  245. }
  246. int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
  247. const void *data, int len)
  248. {
  249. ASN1_TYPE *ttmp = NULL;
  250. ASN1_STRING *stmp = NULL;
  251. int atype = 0;
  252. if (attr == NULL) {
  253. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  254. return 0;
  255. }
  256. if ((attrtype & MBSTRING_FLAG) != 0) {
  257. stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
  258. OBJ_obj2nid(attr->object));
  259. if (stmp == NULL) {
  260. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  261. return 0;
  262. }
  263. atype = stmp->type;
  264. } else if (len != -1) {
  265. if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL
  266. || !ASN1_STRING_set(stmp, data, len)) {
  267. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  268. goto err;
  269. }
  270. atype = attrtype;
  271. }
  272. /*
  273. * This is a bit naughty because the attribute should really have at
  274. * least one value but some types use and zero length SET and require
  275. * this.
  276. */
  277. if (attrtype == 0) {
  278. ASN1_STRING_free(stmp);
  279. return 1;
  280. }
  281. if ((ttmp = ASN1_TYPE_new()) == NULL) {
  282. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  283. goto err;
  284. }
  285. if (len == -1 && (attrtype & MBSTRING_FLAG) == 0) {
  286. if (!ASN1_TYPE_set1(ttmp, attrtype, data)) {
  287. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  288. goto err;
  289. }
  290. } else {
  291. ASN1_TYPE_set(ttmp, atype, stmp);
  292. stmp = NULL;
  293. }
  294. if (!sk_ASN1_TYPE_push(attr->set, ttmp)) {
  295. ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB);
  296. goto err;
  297. }
  298. return 1;
  299. err:
  300. ASN1_TYPE_free(ttmp);
  301. ASN1_STRING_free(stmp);
  302. return 0;
  303. }
  304. int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr)
  305. {
  306. if (attr == NULL)
  307. return 0;
  308. return sk_ASN1_TYPE_num(attr->set);
  309. }
  310. ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
  311. {
  312. if (attr == NULL) {
  313. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  314. return NULL;
  315. }
  316. return attr->object;
  317. }
  318. void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
  319. int atrtype, void *data)
  320. {
  321. ASN1_TYPE *ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
  322. if (ttmp == NULL)
  323. return NULL;
  324. if (atrtype == V_ASN1_BOOLEAN
  325. || atrtype == V_ASN1_NULL
  326. || atrtype != ASN1_TYPE_get(ttmp)) {
  327. ERR_raise(ERR_LIB_X509, X509_R_WRONG_TYPE);
  328. return NULL;
  329. }
  330. return ttmp->value.ptr;
  331. }
  332. ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
  333. {
  334. if (attr == NULL) {
  335. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  336. return NULL;
  337. }
  338. return sk_ASN1_TYPE_value(attr->set, idx);
  339. }