tb_asnmth.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* ====================================================================
  2. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * licensing@OpenSSL.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com).
  52. *
  53. */
  54. #include "eng_int.h"
  55. #include "asn1_locl.h"
  56. #include <openssl/evp.h>
  57. /* If this symbol is defined then ENGINE_get_pkey_asn1_meth_engine(), the
  58. * function that is used by EVP to hook in pkey_asn1_meth code and cache
  59. * defaults (etc), will display brief debugging summaries to stderr with the
  60. * 'nid'. */
  61. /* #define ENGINE_PKEY_ASN1_METH_DEBUG */
  62. static ENGINE_TABLE *pkey_asn1_meth_table = NULL;
  63. void ENGINE_unregister_pkey_asn1_meths(ENGINE *e)
  64. {
  65. engine_table_unregister(&pkey_asn1_meth_table, e);
  66. }
  67. static void engine_unregister_all_pkey_asn1_meths(void)
  68. {
  69. engine_table_cleanup(&pkey_asn1_meth_table);
  70. }
  71. int ENGINE_register_pkey_asn1_meths(ENGINE *e)
  72. {
  73. if(e->pkey_asn1_meths)
  74. {
  75. const int *nids;
  76. int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0);
  77. if(num_nids > 0)
  78. return engine_table_register(&pkey_asn1_meth_table,
  79. engine_unregister_all_pkey_asn1_meths, e, nids,
  80. num_nids, 0);
  81. }
  82. return 1;
  83. }
  84. void ENGINE_register_all_pkey_asn1_meths(void)
  85. {
  86. ENGINE *e;
  87. for(e=ENGINE_get_first() ; e ; e=ENGINE_get_next(e))
  88. ENGINE_register_pkey_asn1_meths(e);
  89. }
  90. int ENGINE_set_default_pkey_asn1_meths(ENGINE *e)
  91. {
  92. if(e->pkey_asn1_meths)
  93. {
  94. const int *nids;
  95. int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0);
  96. if(num_nids > 0)
  97. return engine_table_register(&pkey_asn1_meth_table,
  98. engine_unregister_all_pkey_asn1_meths, e, nids,
  99. num_nids, 1);
  100. }
  101. return 1;
  102. }
  103. /* Exposed API function to get a functional reference from the implementation
  104. * table (ie. try to get a functional reference from the tabled structural
  105. * references) for a given pkey_asn1_meth 'nid' */
  106. ENGINE *ENGINE_get_pkey_asn1_meth_engine(int nid)
  107. {
  108. return engine_table_select(&pkey_asn1_meth_table, nid);
  109. }
  110. /* Obtains a pkey_asn1_meth implementation from an ENGINE functional reference */
  111. const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth(ENGINE *e, int nid)
  112. {
  113. EVP_PKEY_ASN1_METHOD *ret;
  114. ENGINE_PKEY_ASN1_METHS_PTR fn = ENGINE_get_pkey_asn1_meths(e);
  115. if(!fn || !fn(e, &ret, NULL, nid))
  116. {
  117. ENGINEerr(ENGINE_F_ENGINE_GET_PKEY_ASN1_METH,
  118. ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD);
  119. return NULL;
  120. }
  121. return ret;
  122. }
  123. /* Gets the pkey_asn1_meth callback from an ENGINE structure */
  124. ENGINE_PKEY_ASN1_METHS_PTR ENGINE_get_pkey_asn1_meths(const ENGINE *e)
  125. {
  126. return e->pkey_asn1_meths;
  127. }
  128. /* Sets the pkey_asn1_meth callback in an ENGINE structure */
  129. int ENGINE_set_pkey_asn1_meths(ENGINE *e, ENGINE_PKEY_ASN1_METHS_PTR f)
  130. {
  131. e->pkey_asn1_meths = f;
  132. return 1;
  133. }
  134. /* Internal function to free up EVP_PKEY_ASN1_METHOD structures before an
  135. * ENGINE is destroyed
  136. */
  137. void engine_pkey_asn1_meths_free(ENGINE *e)
  138. {
  139. int i;
  140. EVP_PKEY_ASN1_METHOD *pkm;
  141. if (e->pkey_asn1_meths)
  142. {
  143. const int *pknids;
  144. int npknids;
  145. npknids = e->pkey_asn1_meths(e, NULL, &pknids, 0);
  146. for (i = 0; i < npknids; i++)
  147. {
  148. if (e->pkey_asn1_meths(e, &pkm, NULL, pknids[i]))
  149. {
  150. EVP_PKEY_asn1_free(pkm);
  151. }
  152. }
  153. }
  154. }
  155. /* Find a method based on a string. This does a linear search through
  156. * all implemented algorithms. This is OK in practice because only
  157. * a small number of algorithms are likely to be implemented in an engine
  158. * and it is not used for speed critical operations.
  159. */
  160. const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth_str(ENGINE *e,
  161. const char *str, int len)
  162. {
  163. int i, nidcount;
  164. const int *nids;
  165. EVP_PKEY_ASN1_METHOD *ameth;
  166. if (!e->pkey_asn1_meths)
  167. return NULL;
  168. if (len == -1)
  169. len = strlen(str);
  170. nidcount = e->pkey_asn1_meths(e, NULL, &nids, 0);
  171. for (i = 0; i < nidcount; i++)
  172. {
  173. e->pkey_asn1_meths(e, &ameth, NULL, nids[i]);
  174. if (((int)strlen(ameth->pem_str) == len) &&
  175. !strncasecmp(ameth->pem_str, str, len))
  176. return ameth;
  177. }
  178. return NULL;
  179. }
  180. typedef struct
  181. {
  182. ENGINE *e;
  183. const EVP_PKEY_ASN1_METHOD *ameth;
  184. const char *str;
  185. int len;
  186. } ENGINE_FIND_STR;
  187. static void look_str_cb(int nid, STACK_OF(ENGINE) *sk, ENGINE *def, void *arg)
  188. {
  189. ENGINE_FIND_STR *lk = arg;
  190. int i;
  191. if (lk->ameth)
  192. return;
  193. for (i = 0; i < sk_ENGINE_num(sk); i++)
  194. {
  195. ENGINE *e = sk_ENGINE_value(sk, i);
  196. EVP_PKEY_ASN1_METHOD *ameth;
  197. e->pkey_asn1_meths(e, &ameth, NULL, nid);
  198. if (((int)strlen(ameth->pem_str) == lk->len) &&
  199. !strncasecmp(ameth->pem_str, lk->str, lk->len))
  200. {
  201. lk->e = e;
  202. lk->ameth = ameth;
  203. return;
  204. }
  205. }
  206. }
  207. const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe,
  208. const char *str, int len)
  209. {
  210. ENGINE_FIND_STR fstr;
  211. fstr.e = NULL;
  212. fstr.ameth = NULL;
  213. fstr.str = str;
  214. fstr.len = len;
  215. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  216. engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr);
  217. /* If found obtain a structural reference to engine */
  218. if (fstr.e)
  219. {
  220. fstr.e->struct_ref++;
  221. engine_ref_debug(fstr.e, 0, 1)
  222. }
  223. *pe = fstr.e;
  224. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  225. return fstr.ameth;
  226. }