v3_lib.c 9.0 KB

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