v3_lib.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright 1999-2025 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. /* X509 v3 extension utilities */
  10. #include <stdio.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/conf.h>
  13. #include <openssl/x509v3.h>
  14. #include "ext_dat.h"
  15. static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
  16. static int ext_cmp(const X509V3_EXT_METHOD *const *a,
  17. const X509V3_EXT_METHOD *const *b);
  18. static void ext_list_free(X509V3_EXT_METHOD *ext);
  19. int X509V3_EXT_add(X509V3_EXT_METHOD *ext)
  20. {
  21. if (ext_list == NULL
  22. && (ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp)) == NULL) {
  23. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  24. return 0;
  25. }
  26. if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
  27. ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
  28. return 0;
  29. }
  30. return 1;
  31. }
  32. static int ext_cmp(const X509V3_EXT_METHOD *const *a,
  33. const X509V3_EXT_METHOD *const *b)
  34. {
  35. return ((*a)->ext_nid - (*b)->ext_nid);
  36. }
  37. DECLARE_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *,
  38. const X509V3_EXT_METHOD *, ext);
  39. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *,
  40. const X509V3_EXT_METHOD *, ext);
  41. #include "standard_exts.h"
  42. const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid)
  43. {
  44. X509V3_EXT_METHOD tmp;
  45. const X509V3_EXT_METHOD *t = &tmp, *const *ret;
  46. int idx;
  47. if (nid < 0)
  48. return NULL;
  49. tmp.ext_nid = nid;
  50. ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT);
  51. if (ret)
  52. return *ret;
  53. if (!ext_list)
  54. return NULL;
  55. /* Ideally, this would be done under a lock */
  56. sk_X509V3_EXT_METHOD_sort(ext_list);
  57. idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp);
  58. /* A failure to locate the item is handled by the value method */
  59. return sk_X509V3_EXT_METHOD_value(ext_list, idx);
  60. }
  61. const X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext)
  62. {
  63. int nid;
  64. if ((nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext))) == NID_undef)
  65. return NULL;
  66. return X509V3_EXT_get_nid(nid);
  67. }
  68. int X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist)
  69. {
  70. for (; extlist->ext_nid != -1; extlist++)
  71. if (!X509V3_EXT_add(extlist))
  72. return 0;
  73. return 1;
  74. }
  75. int X509V3_EXT_add_alias(int nid_to, int nid_from)
  76. {
  77. const X509V3_EXT_METHOD *ext;
  78. X509V3_EXT_METHOD *tmpext;
  79. if ((ext = X509V3_EXT_get_nid(nid_from)) == NULL) {
  80. ERR_raise(ERR_LIB_X509V3, X509V3_R_EXTENSION_NOT_FOUND);
  81. return 0;
  82. }
  83. if ((tmpext = OPENSSL_malloc(sizeof(*tmpext))) == NULL)
  84. return 0;
  85. *tmpext = *ext;
  86. tmpext->ext_nid = nid_to;
  87. tmpext->ext_flags |= X509V3_EXT_DYNAMIC;
  88. if (!X509V3_EXT_add(tmpext)) {
  89. OPENSSL_free(tmpext);
  90. return 0;
  91. }
  92. return 1;
  93. }
  94. void X509V3_EXT_cleanup(void)
  95. {
  96. sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free);
  97. ext_list = NULL;
  98. }
  99. static void ext_list_free(X509V3_EXT_METHOD *ext)
  100. {
  101. if (ext->ext_flags & X509V3_EXT_DYNAMIC)
  102. OPENSSL_free(ext);
  103. }
  104. /*
  105. * Legacy function: we don't need to add standard extensions any more because
  106. * they are now kept in ext_dat.h.
  107. */
  108. int X509V3_add_standard_extensions(void)
  109. {
  110. return 1;
  111. }
  112. /* Return an extension internal structure */
  113. void *X509V3_EXT_d2i(X509_EXTENSION *ext)
  114. {
  115. const X509V3_EXT_METHOD *method;
  116. const unsigned char *p;
  117. ASN1_STRING *extvalue;
  118. int extlen;
  119. if ((method = X509V3_EXT_get(ext)) == NULL)
  120. return NULL;
  121. extvalue = X509_EXTENSION_get_data(ext);
  122. p = ASN1_STRING_get0_data(extvalue);
  123. extlen = ASN1_STRING_length(extvalue);
  124. if (method->it)
  125. return ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
  126. return method->d2i(NULL, &p, extlen);
  127. }
  128. /*-
  129. * Get critical flag and decoded version of extension from a NID.
  130. * The "idx" variable returns the last found extension and can
  131. * be used to retrieve multiple extensions of the same NID.
  132. * However multiple extensions with the same NID is usually
  133. * due to a badly encoded certificate so if idx is NULL we
  134. * choke if multiple extensions exist.
  135. * The "crit" variable is set to the critical value.
  136. * The return value is the decoded extension or NULL on
  137. * error. The actual error can have several different causes,
  138. * the value of *crit reflects the cause:
  139. * >= 0, extension found but not decoded (reflects critical value).
  140. * -1 extension not found.
  141. * -2 extension occurs more than once.
  142. */
  143. void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit,
  144. int *idx)
  145. {
  146. int lastpos, i;
  147. X509_EXTENSION *ex, *found_ex = NULL;
  148. if (!x) {
  149. if (idx)
  150. *idx = -1;
  151. if (crit)
  152. *crit = -1;
  153. return NULL;
  154. }
  155. if (idx)
  156. lastpos = *idx + 1;
  157. else
  158. lastpos = 0;
  159. if (lastpos < 0)
  160. lastpos = 0;
  161. for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) {
  162. ex = sk_X509_EXTENSION_value(x, i);
  163. if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) == nid) {
  164. if (idx) {
  165. *idx = i;
  166. found_ex = ex;
  167. break;
  168. } else if (found_ex) {
  169. /* Found more than one */
  170. if (crit)
  171. *crit = -2;
  172. return NULL;
  173. }
  174. found_ex = ex;
  175. }
  176. }
  177. if (found_ex) {
  178. /* Found it */
  179. if (crit)
  180. *crit = X509_EXTENSION_get_critical(found_ex);
  181. return X509V3_EXT_d2i(found_ex);
  182. }
  183. /* Extension not found */
  184. if (idx)
  185. *idx = -1;
  186. if (crit)
  187. *crit = -1;
  188. return NULL;
  189. }
  190. /*
  191. * This function is a general extension append, replace and delete utility.
  192. * The precise operation is governed by the 'flags' value. The 'crit' and
  193. * 'value' arguments (if relevant) are the extensions internal structure.
  194. */
  195. int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
  196. int crit, unsigned long flags)
  197. {
  198. int errcode, extidx = -1;
  199. X509_EXTENSION *ext = NULL, *extmp;
  200. STACK_OF(X509_EXTENSION) *ret = NULL;
  201. unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
  202. /*
  203. * If appending we don't care if it exists, otherwise look for existing
  204. * extension.
  205. */
  206. if (ext_op != X509V3_ADD_APPEND)
  207. extidx = X509v3_get_ext_by_NID(*x, nid, -1);
  208. /* See if extension exists */
  209. if (extidx >= 0) {
  210. /* If keep existing, nothing to do */
  211. if (ext_op == X509V3_ADD_KEEP_EXISTING)
  212. return 1;
  213. /* If default then its an error */
  214. if (ext_op == X509V3_ADD_DEFAULT) {
  215. errcode = X509V3_R_EXTENSION_EXISTS;
  216. goto err;
  217. }
  218. /* If delete, just delete it */
  219. if (ext_op == X509V3_ADD_DELETE) {
  220. extmp = sk_X509_EXTENSION_delete(*x, extidx);
  221. if (extmp == NULL)
  222. return -1;
  223. X509_EXTENSION_free(extmp);
  224. return 1;
  225. }
  226. } else {
  227. /*
  228. * If replace existing or delete, error since extension must exist
  229. */
  230. if ((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
  231. (ext_op == X509V3_ADD_DELETE)) {
  232. errcode = X509V3_R_EXTENSION_NOT_FOUND;
  233. goto err;
  234. }
  235. }
  236. /*
  237. * If we get this far then we have to create an extension: could have
  238. * some flags for alternative encoding schemes...
  239. */
  240. ext = X509V3_EXT_i2d(nid, crit, value);
  241. if (!ext) {
  242. ERR_raise(ERR_LIB_X509V3, X509V3_R_ERROR_CREATING_EXTENSION);
  243. return 0;
  244. }
  245. /* If extension exists replace it.. */
  246. if (extidx >= 0) {
  247. extmp = sk_X509_EXTENSION_value(*x, extidx);
  248. X509_EXTENSION_free(extmp);
  249. if (!sk_X509_EXTENSION_set(*x, extidx, ext))
  250. return -1;
  251. return 1;
  252. }
  253. ret = *x;
  254. if (*x == NULL
  255. && (ret = sk_X509_EXTENSION_new_null()) == NULL)
  256. goto m_fail;
  257. if (!sk_X509_EXTENSION_push(ret, ext))
  258. goto m_fail;
  259. *x = ret;
  260. return 1;
  261. m_fail:
  262. /* ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); */
  263. if (ret != *x)
  264. sk_X509_EXTENSION_free(ret);
  265. X509_EXTENSION_free(ext);
  266. return -1;
  267. err:
  268. if (!(flags & X509V3_ADD_SILENT))
  269. ERR_raise(ERR_LIB_X509V3, errcode);
  270. return 0;
  271. }