v3_lib.c 8.3 KB

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