v3_lib.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright 1999-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. /* 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. return X509V3_EXT_add(tmpext);
  89. }
  90. void X509V3_EXT_cleanup(void)
  91. {
  92. sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free);
  93. ext_list = NULL;
  94. }
  95. static void ext_list_free(X509V3_EXT_METHOD *ext)
  96. {
  97. if (ext->ext_flags & X509V3_EXT_DYNAMIC)
  98. OPENSSL_free(ext);
  99. }
  100. /*
  101. * Legacy function: we don't need to add standard extensions any more because
  102. * they are now kept in ext_dat.h.
  103. */
  104. int X509V3_add_standard_extensions(void)
  105. {
  106. return 1;
  107. }
  108. /* Return an extension internal structure */
  109. void *X509V3_EXT_d2i(X509_EXTENSION *ext)
  110. {
  111. const X509V3_EXT_METHOD *method;
  112. const unsigned char *p;
  113. ASN1_STRING *extvalue;
  114. int extlen;
  115. if ((method = X509V3_EXT_get(ext)) == NULL)
  116. return NULL;
  117. extvalue = X509_EXTENSION_get_data(ext);
  118. p = ASN1_STRING_get0_data(extvalue);
  119. extlen = ASN1_STRING_length(extvalue);
  120. if (method->it)
  121. return ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
  122. return method->d2i(NULL, &p, extlen);
  123. }
  124. /*-
  125. * Get critical flag and decoded version of extension from a NID.
  126. * The "idx" variable returns the last found extension and can
  127. * be used to retrieve multiple extensions of the same NID.
  128. * However multiple extensions with the same NID is usually
  129. * due to a badly encoded certificate so if idx is NULL we
  130. * choke if multiple extensions exist.
  131. * The "crit" variable is set to the critical value.
  132. * The return value is the decoded extension or NULL on
  133. * error. The actual error can have several different causes,
  134. * the value of *crit reflects the cause:
  135. * >= 0, extension found but not decoded (reflects critical value).
  136. * -1 extension not found.
  137. * -2 extension occurs more than once.
  138. */
  139. void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit,
  140. int *idx)
  141. {
  142. int lastpos, i;
  143. X509_EXTENSION *ex, *found_ex = NULL;
  144. if (!x) {
  145. if (idx)
  146. *idx = -1;
  147. if (crit)
  148. *crit = -1;
  149. return NULL;
  150. }
  151. if (idx)
  152. lastpos = *idx + 1;
  153. else
  154. lastpos = 0;
  155. if (lastpos < 0)
  156. lastpos = 0;
  157. for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) {
  158. ex = sk_X509_EXTENSION_value(x, i);
  159. if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) == nid) {
  160. if (idx) {
  161. *idx = i;
  162. found_ex = ex;
  163. break;
  164. } else if (found_ex) {
  165. /* Found more than one */
  166. if (crit)
  167. *crit = -2;
  168. return NULL;
  169. }
  170. found_ex = ex;
  171. }
  172. }
  173. if (found_ex) {
  174. /* Found it */
  175. if (crit)
  176. *crit = X509_EXTENSION_get_critical(found_ex);
  177. return X509V3_EXT_d2i(found_ex);
  178. }
  179. /* Extension not found */
  180. if (idx)
  181. *idx = -1;
  182. if (crit)
  183. *crit = -1;
  184. return NULL;
  185. }
  186. /*
  187. * This function is a general extension append, replace and delete utility.
  188. * The precise operation is governed by the 'flags' value. The 'crit' and
  189. * 'value' arguments (if relevant) are the extensions internal structure.
  190. */
  191. int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
  192. int crit, unsigned long flags)
  193. {
  194. int errcode, extidx = -1;
  195. X509_EXTENSION *ext = NULL, *extmp;
  196. STACK_OF(X509_EXTENSION) *ret = NULL;
  197. unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
  198. /*
  199. * If appending we don't care if it exists, otherwise look for existing
  200. * extension.
  201. */
  202. if (ext_op != X509V3_ADD_APPEND)
  203. extidx = X509v3_get_ext_by_NID(*x, nid, -1);
  204. /* See if extension exists */
  205. if (extidx >= 0) {
  206. /* If keep existing, nothing to do */
  207. if (ext_op == X509V3_ADD_KEEP_EXISTING)
  208. return 1;
  209. /* If default then its an error */
  210. if (ext_op == X509V3_ADD_DEFAULT) {
  211. errcode = X509V3_R_EXTENSION_EXISTS;
  212. goto err;
  213. }
  214. /* If delete, just delete it */
  215. if (ext_op == X509V3_ADD_DELETE) {
  216. extmp = sk_X509_EXTENSION_delete(*x, extidx);
  217. if (extmp == NULL)
  218. return -1;
  219. X509_EXTENSION_free(extmp);
  220. return 1;
  221. }
  222. } else {
  223. /*
  224. * If replace existing or delete, error since extension must exist
  225. */
  226. if ((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
  227. (ext_op == X509V3_ADD_DELETE)) {
  228. errcode = X509V3_R_EXTENSION_NOT_FOUND;
  229. goto err;
  230. }
  231. }
  232. /*
  233. * If we get this far then we have to create an extension: could have
  234. * some flags for alternative encoding schemes...
  235. */
  236. ext = X509V3_EXT_i2d(nid, crit, value);
  237. if (!ext) {
  238. ERR_raise(ERR_LIB_X509V3, X509V3_R_ERROR_CREATING_EXTENSION);
  239. return 0;
  240. }
  241. /* If extension exists replace it.. */
  242. if (extidx >= 0) {
  243. extmp = sk_X509_EXTENSION_value(*x, extidx);
  244. X509_EXTENSION_free(extmp);
  245. if (!sk_X509_EXTENSION_set(*x, extidx, ext))
  246. return -1;
  247. return 1;
  248. }
  249. ret = *x;
  250. if (*x == NULL
  251. && (ret = sk_X509_EXTENSION_new_null()) == NULL)
  252. goto m_fail;
  253. if (!sk_X509_EXTENSION_push(ret, ext))
  254. goto m_fail;
  255. *x = ret;
  256. return 1;
  257. m_fail:
  258. /* ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); */
  259. if (ret != *x)
  260. sk_X509_EXTENSION_free(ret);
  261. X509_EXTENSION_free(ext);
  262. return -1;
  263. err:
  264. if (!(flags & X509V3_ADD_SILENT))
  265. ERR_raise(ERR_LIB_X509V3, errcode);
  266. return 0;
  267. }