v3_lib.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* v3_lib.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project 1999.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. /* X509 v3 extension utilities */
  59. #include <stdio.h>
  60. #include "cryptlib.h"
  61. #include <openssl/conf.h>
  62. #include <openssl/x509v3.h>
  63. #include "ext_dat.h"
  64. static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
  65. static int ext_cmp(const X509V3_EXT_METHOD * const *a,
  66. const X509V3_EXT_METHOD * const *b);
  67. static void ext_list_free(X509V3_EXT_METHOD *ext);
  68. int X509V3_EXT_add(X509V3_EXT_METHOD *ext)
  69. {
  70. if(!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp))) {
  71. X509V3err(X509V3_F_X509V3_EXT_ADD,ERR_R_MALLOC_FAILURE);
  72. return 0;
  73. }
  74. if(!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
  75. X509V3err(X509V3_F_X509V3_EXT_ADD,ERR_R_MALLOC_FAILURE);
  76. return 0;
  77. }
  78. return 1;
  79. }
  80. static int ext_cmp(const X509V3_EXT_METHOD * const *a,
  81. const X509V3_EXT_METHOD * const *b)
  82. {
  83. return ((*a)->ext_nid - (*b)->ext_nid);
  84. }
  85. DECLARE_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *, const X509V3_EXT_METHOD *,
  86. ext);
  87. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *,
  88. const X509V3_EXT_METHOD *, ext);
  89. const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid)
  90. {
  91. X509V3_EXT_METHOD tmp;
  92. const X509V3_EXT_METHOD *t = &tmp, * const *ret;
  93. int idx;
  94. if(nid < 0) return NULL;
  95. tmp.ext_nid = nid;
  96. ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT);
  97. if(ret) return *ret;
  98. if(!ext_list) return NULL;
  99. idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp);
  100. if(idx == -1) return NULL;
  101. return sk_X509V3_EXT_METHOD_value(ext_list, idx);
  102. }
  103. const X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext)
  104. {
  105. int nid;
  106. if((nid = OBJ_obj2nid(ext->object)) == NID_undef) return NULL;
  107. return X509V3_EXT_get_nid(nid);
  108. }
  109. int X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist)
  110. {
  111. for(;extlist->ext_nid!=-1;extlist++)
  112. if(!X509V3_EXT_add(extlist)) return 0;
  113. return 1;
  114. }
  115. int X509V3_EXT_add_alias(int nid_to, int nid_from)
  116. {
  117. const X509V3_EXT_METHOD *ext;
  118. X509V3_EXT_METHOD *tmpext;
  119. if(!(ext = X509V3_EXT_get_nid(nid_from))) {
  120. X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS,X509V3_R_EXTENSION_NOT_FOUND);
  121. return 0;
  122. }
  123. if(!(tmpext = (X509V3_EXT_METHOD *)OPENSSL_malloc(sizeof(X509V3_EXT_METHOD)))) {
  124. X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS,ERR_R_MALLOC_FAILURE);
  125. return 0;
  126. }
  127. *tmpext = *ext;
  128. tmpext->ext_nid = nid_to;
  129. tmpext->ext_flags |= X509V3_EXT_DYNAMIC;
  130. return X509V3_EXT_add(tmpext);
  131. }
  132. void X509V3_EXT_cleanup(void)
  133. {
  134. sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free);
  135. ext_list = NULL;
  136. }
  137. static void ext_list_free(X509V3_EXT_METHOD *ext)
  138. {
  139. if(ext->ext_flags & X509V3_EXT_DYNAMIC) OPENSSL_free(ext);
  140. }
  141. /* Legacy function: we don't need to add standard extensions
  142. * any more because they are now kept in ext_dat.h.
  143. */
  144. int X509V3_add_standard_extensions(void)
  145. {
  146. return 1;
  147. }
  148. /* Return an extension internal structure */
  149. void *X509V3_EXT_d2i(X509_EXTENSION *ext)
  150. {
  151. const X509V3_EXT_METHOD *method;
  152. const unsigned char *p;
  153. if(!(method = X509V3_EXT_get(ext))) return NULL;
  154. p = ext->value->data;
  155. if(method->it) return ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it));
  156. return method->d2i(NULL, &p, ext->value->length);
  157. }
  158. /* Get critical flag and decoded version of extension from a NID.
  159. * The "idx" variable returns the last found extension and can
  160. * be used to retrieve multiple extensions of the same NID.
  161. * However multiple extensions with the same NID is usually
  162. * due to a badly encoded certificate so if idx is NULL we
  163. * choke if multiple extensions exist.
  164. * The "crit" variable is set to the critical value.
  165. * The return value is the decoded extension or NULL on
  166. * error. The actual error can have several different causes,
  167. * the value of *crit reflects the cause:
  168. * >= 0, extension found but not decoded (reflects critical value).
  169. * -1 extension not found.
  170. * -2 extension occurs more than once.
  171. */
  172. void *X509V3_get_d2i(STACK_OF(X509_EXTENSION) *x, int nid, int *crit, int *idx)
  173. {
  174. int lastpos, i;
  175. X509_EXTENSION *ex, *found_ex = NULL;
  176. if(!x) {
  177. if(idx) *idx = -1;
  178. if(crit) *crit = -1;
  179. return NULL;
  180. }
  181. if(idx) lastpos = *idx + 1;
  182. else lastpos = 0;
  183. if(lastpos < 0) lastpos = 0;
  184. for(i = lastpos; i < sk_X509_EXTENSION_num(x); i++)
  185. {
  186. ex = sk_X509_EXTENSION_value(x, i);
  187. if(OBJ_obj2nid(ex->object) == nid) {
  188. if(idx) {
  189. *idx = i;
  190. found_ex = ex;
  191. break;
  192. } else if(found_ex) {
  193. /* Found more than one */
  194. if(crit) *crit = -2;
  195. return NULL;
  196. }
  197. found_ex = ex;
  198. }
  199. }
  200. if(found_ex) {
  201. /* Found it */
  202. if(crit) *crit = X509_EXTENSION_get_critical(found_ex);
  203. return X509V3_EXT_d2i(found_ex);
  204. }
  205. /* Extension not found */
  206. if(idx) *idx = -1;
  207. if(crit) *crit = -1;
  208. return NULL;
  209. }
  210. /* This function is a general extension append, replace and delete utility.
  211. * The precise operation is governed by the 'flags' value. The 'crit' and
  212. * 'value' arguments (if relevant) are the extensions internal structure.
  213. */
  214. int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
  215. int crit, unsigned long flags)
  216. {
  217. int extidx = -1;
  218. int errcode;
  219. X509_EXTENSION *ext, *extmp;
  220. unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
  221. /* If appending we don't care if it exists, otherwise
  222. * look for existing extension.
  223. */
  224. if(ext_op != X509V3_ADD_APPEND)
  225. extidx = X509v3_get_ext_by_NID(*x, nid, -1);
  226. /* See if extension exists */
  227. if(extidx >= 0) {
  228. /* If keep existing, nothing to do */
  229. if(ext_op == X509V3_ADD_KEEP_EXISTING)
  230. return 1;
  231. /* If default then its an error */
  232. if(ext_op == X509V3_ADD_DEFAULT) {
  233. errcode = X509V3_R_EXTENSION_EXISTS;
  234. goto err;
  235. }
  236. /* If delete, just delete it */
  237. if(ext_op == X509V3_ADD_DELETE) {
  238. if(!sk_X509_EXTENSION_delete(*x, extidx)) return -1;
  239. return 1;
  240. }
  241. } else {
  242. /* If replace existing or delete, error since
  243. * extension must exist
  244. */
  245. if((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
  246. (ext_op == X509V3_ADD_DELETE)) {
  247. errcode = X509V3_R_EXTENSION_NOT_FOUND;
  248. goto err;
  249. }
  250. }
  251. /* If we get this far then we have to create an extension:
  252. * could have some flags for alternative encoding schemes...
  253. */
  254. ext = X509V3_EXT_i2d(nid, crit, value);
  255. if(!ext) {
  256. X509V3err(X509V3_F_X509V3_ADD1_I2D, X509V3_R_ERROR_CREATING_EXTENSION);
  257. return 0;
  258. }
  259. /* If extension exists replace it.. */
  260. if(extidx >= 0) {
  261. extmp = sk_X509_EXTENSION_value(*x, extidx);
  262. X509_EXTENSION_free(extmp);
  263. if(!sk_X509_EXTENSION_set(*x, extidx, ext)) return -1;
  264. return 1;
  265. }
  266. if(!*x && !(*x = sk_X509_EXTENSION_new_null())) return -1;
  267. if(!sk_X509_EXTENSION_push(*x, ext)) return -1;
  268. return 1;
  269. err:
  270. if(!(flags & X509V3_ADD_SILENT))
  271. X509V3err(X509V3_F_X509V3_ADD1_I2D, errcode);
  272. return 0;
  273. }
  274. IMPLEMENT_STACK_OF(X509V3_EXT_METHOD)