x509_att.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 <openssl/x509v3.h>
  17. #include "x509_local.h"
  18. DEFINE_STACK_OF(X509_ATTRIBUTE)
  19. DEFINE_STACK_OF(ASN1_TYPE)
  20. int X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)
  21. {
  22. return sk_X509_ATTRIBUTE_num(x);
  23. }
  24. int X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid,
  25. int lastpos)
  26. {
  27. const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
  28. if (obj == NULL)
  29. return -2;
  30. return X509at_get_attr_by_OBJ(x, obj, lastpos);
  31. }
  32. int X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk,
  33. const ASN1_OBJECT *obj, int lastpos)
  34. {
  35. int n;
  36. X509_ATTRIBUTE *ex;
  37. if (sk == NULL)
  38. return -1;
  39. lastpos++;
  40. if (lastpos < 0)
  41. lastpos = 0;
  42. n = sk_X509_ATTRIBUTE_num(sk);
  43. for (; lastpos < n; lastpos++) {
  44. ex = sk_X509_ATTRIBUTE_value(sk, lastpos);
  45. if (OBJ_cmp(ex->object, obj) == 0)
  46. return lastpos;
  47. }
  48. return -1;
  49. }
  50. X509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc)
  51. {
  52. if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
  53. return NULL;
  54. return sk_X509_ATTRIBUTE_value(x, loc);
  55. }
  56. X509_ATTRIBUTE *X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc)
  57. {
  58. X509_ATTRIBUTE *ret;
  59. if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
  60. return NULL;
  61. ret = sk_X509_ATTRIBUTE_delete(x, loc);
  62. return ret;
  63. }
  64. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
  65. X509_ATTRIBUTE *attr)
  66. {
  67. X509_ATTRIBUTE *new_attr = NULL;
  68. STACK_OF(X509_ATTRIBUTE) *sk = NULL;
  69. if (x == NULL) {
  70. X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_PASSED_NULL_PARAMETER);
  71. goto err2;
  72. }
  73. if (*x == NULL) {
  74. if ((sk = sk_X509_ATTRIBUTE_new_null()) == NULL)
  75. goto err;
  76. } else
  77. sk = *x;
  78. if ((new_attr = X509_ATTRIBUTE_dup(attr)) == NULL)
  79. goto err2;
  80. if (!sk_X509_ATTRIBUTE_push(sk, new_attr))
  81. goto err;
  82. if (*x == NULL)
  83. *x = sk;
  84. return sk;
  85. err:
  86. X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_MALLOC_FAILURE);
  87. err2:
  88. X509_ATTRIBUTE_free(new_attr);
  89. sk_X509_ATTRIBUTE_free(sk);
  90. return NULL;
  91. }
  92. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE)
  93. **x, const ASN1_OBJECT *obj,
  94. int type,
  95. const unsigned char *bytes,
  96. int len)
  97. {
  98. X509_ATTRIBUTE *attr;
  99. STACK_OF(X509_ATTRIBUTE) *ret;
  100. attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
  101. if (!attr)
  102. return 0;
  103. ret = X509at_add1_attr(x, attr);
  104. X509_ATTRIBUTE_free(attr);
  105. return ret;
  106. }
  107. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE)
  108. **x, int nid, int type,
  109. const unsigned char *bytes,
  110. int len)
  111. {
  112. X509_ATTRIBUTE *attr;
  113. STACK_OF(X509_ATTRIBUTE) *ret;
  114. attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
  115. if (!attr)
  116. return 0;
  117. ret = X509at_add1_attr(x, attr);
  118. X509_ATTRIBUTE_free(attr);
  119. return ret;
  120. }
  121. STACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE)
  122. **x, const char *attrname,
  123. int type,
  124. const unsigned char *bytes,
  125. int len)
  126. {
  127. X509_ATTRIBUTE *attr;
  128. STACK_OF(X509_ATTRIBUTE) *ret;
  129. attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
  130. if (!attr)
  131. return 0;
  132. ret = X509at_add1_attr(x, attr);
  133. X509_ATTRIBUTE_free(attr);
  134. return ret;
  135. }
  136. void *X509at_get0_data_by_OBJ(STACK_OF(X509_ATTRIBUTE) *x,
  137. const ASN1_OBJECT *obj, int lastpos, int type)
  138. {
  139. int i;
  140. X509_ATTRIBUTE *at;
  141. i = X509at_get_attr_by_OBJ(x, obj, lastpos);
  142. if (i == -1)
  143. return NULL;
  144. if ((lastpos <= -2) && (X509at_get_attr_by_OBJ(x, obj, i) != -1))
  145. return NULL;
  146. at = X509at_get_attr(x, i);
  147. if (lastpos <= -3 && (X509_ATTRIBUTE_count(at) != 1))
  148. return NULL;
  149. return X509_ATTRIBUTE_get0_data(at, 0, type, NULL);
  150. }
  151. X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
  152. int atrtype, const void *data,
  153. int len)
  154. {
  155. ASN1_OBJECT *obj;
  156. X509_ATTRIBUTE *ret;
  157. obj = OBJ_nid2obj(nid);
  158. if (obj == NULL) {
  159. X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_NID, X509_R_UNKNOWN_NID);
  160. return NULL;
  161. }
  162. ret = X509_ATTRIBUTE_create_by_OBJ(attr, obj, atrtype, data, len);
  163. if (ret == NULL)
  164. ASN1_OBJECT_free(obj);
  165. return ret;
  166. }
  167. X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
  168. const ASN1_OBJECT *obj,
  169. int atrtype, const void *data,
  170. int len)
  171. {
  172. X509_ATTRIBUTE *ret;
  173. if ((attr == NULL) || (*attr == NULL)) {
  174. if ((ret = X509_ATTRIBUTE_new()) == NULL) {
  175. X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_OBJ,
  176. ERR_R_MALLOC_FAILURE);
  177. return NULL;
  178. }
  179. } else
  180. ret = *attr;
  181. if (!X509_ATTRIBUTE_set1_object(ret, obj))
  182. goto err;
  183. if (!X509_ATTRIBUTE_set1_data(ret, atrtype, data, len))
  184. goto err;
  185. if ((attr != NULL) && (*attr == NULL))
  186. *attr = ret;
  187. return ret;
  188. err:
  189. if ((attr == NULL) || (ret != *attr))
  190. X509_ATTRIBUTE_free(ret);
  191. return NULL;
  192. }
  193. X509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
  194. const char *atrname, int type,
  195. const unsigned char *bytes,
  196. int len)
  197. {
  198. ASN1_OBJECT *obj;
  199. X509_ATTRIBUTE *nattr;
  200. obj = OBJ_txt2obj(atrname, 0);
  201. if (obj == NULL) {
  202. X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_TXT,
  203. X509_R_INVALID_FIELD_NAME);
  204. ERR_add_error_data(2, "name=", atrname);
  205. return NULL;
  206. }
  207. nattr = X509_ATTRIBUTE_create_by_OBJ(attr, obj, type, bytes, len);
  208. ASN1_OBJECT_free(obj);
  209. return nattr;
  210. }
  211. int X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
  212. {
  213. if ((attr == NULL) || (obj == NULL))
  214. return 0;
  215. ASN1_OBJECT_free(attr->object);
  216. attr->object = OBJ_dup(obj);
  217. return attr->object != NULL;
  218. }
  219. int X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype,
  220. const void *data, int len)
  221. {
  222. ASN1_TYPE *ttmp = NULL;
  223. ASN1_STRING *stmp = NULL;
  224. int atype = 0;
  225. if (!attr)
  226. return 0;
  227. if (attrtype & MBSTRING_FLAG) {
  228. stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
  229. OBJ_obj2nid(attr->object));
  230. if (!stmp) {
  231. X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_ASN1_LIB);
  232. return 0;
  233. }
  234. atype = stmp->type;
  235. } else if (len != -1) {
  236. if ((stmp = ASN1_STRING_type_new(attrtype)) == NULL)
  237. goto err;
  238. if (!ASN1_STRING_set(stmp, data, len))
  239. goto err;
  240. atype = attrtype;
  241. }
  242. /*
  243. * This is a bit naughty because the attribute should really have at
  244. * least one value but some types use and zero length SET and require
  245. * this.
  246. */
  247. if (attrtype == 0) {
  248. ASN1_STRING_free(stmp);
  249. return 1;
  250. }
  251. if ((ttmp = ASN1_TYPE_new()) == NULL)
  252. goto err;
  253. if ((len == -1) && !(attrtype & MBSTRING_FLAG)) {
  254. if (!ASN1_TYPE_set1(ttmp, attrtype, data))
  255. goto err;
  256. } else {
  257. ASN1_TYPE_set(ttmp, atype, stmp);
  258. stmp = NULL;
  259. }
  260. if (!sk_ASN1_TYPE_push(attr->set, ttmp))
  261. goto err;
  262. return 1;
  263. err:
  264. X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE);
  265. ASN1_TYPE_free(ttmp);
  266. ASN1_STRING_free(stmp);
  267. return 0;
  268. }
  269. int X509_ATTRIBUTE_count(const X509_ATTRIBUTE *attr)
  270. {
  271. if (attr == NULL)
  272. return 0;
  273. return sk_ASN1_TYPE_num(attr->set);
  274. }
  275. ASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
  276. {
  277. if (attr == NULL)
  278. return NULL;
  279. return attr->object;
  280. }
  281. void *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
  282. int atrtype, void *data)
  283. {
  284. ASN1_TYPE *ttmp;
  285. ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
  286. if (!ttmp)
  287. return NULL;
  288. if (atrtype == V_ASN1_BOOLEAN
  289. || atrtype == V_ASN1_NULL
  290. || atrtype != ASN1_TYPE_get(ttmp)) {
  291. X509err(X509_F_X509_ATTRIBUTE_GET0_DATA, X509_R_WRONG_TYPE);
  292. return NULL;
  293. }
  294. return ttmp->value.ptr;
  295. }
  296. ASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
  297. {
  298. if (attr == NULL)
  299. return NULL;
  300. return sk_ASN1_TYPE_value(attr->set, idx);
  301. }