tb_asnmth.c 8.0 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 <openssl/evp.h>
  56. #include "internal/asn1_int.h"
  57. /*
  58. * If this symbol is defined then ENGINE_get_pkey_asn1_meth_engine(), the
  59. * function that is used by EVP to hook in pkey_asn1_meth code and cache
  60. * defaults (etc), will display brief debugging summaries to stderr with the
  61. * 'nid'.
  62. */
  63. /* #define ENGINE_PKEY_ASN1_METH_DEBUG */
  64. static ENGINE_TABLE *pkey_asn1_meth_table = NULL;
  65. void ENGINE_unregister_pkey_asn1_meths(ENGINE *e)
  66. {
  67. engine_table_unregister(&pkey_asn1_meth_table, e);
  68. }
  69. static void engine_unregister_all_pkey_asn1_meths(void)
  70. {
  71. engine_table_cleanup(&pkey_asn1_meth_table);
  72. }
  73. int ENGINE_register_pkey_asn1_meths(ENGINE *e)
  74. {
  75. if (e->pkey_asn1_meths) {
  76. const int *nids;
  77. int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0);
  78. if (num_nids > 0)
  79. return engine_table_register(&pkey_asn1_meth_table,
  80. engine_unregister_all_pkey_asn1_meths,
  81. e, nids, num_nids, 0);
  82. }
  83. return 1;
  84. }
  85. void ENGINE_register_all_pkey_asn1_meths(void)
  86. {
  87. ENGINE *e;
  88. for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
  89. ENGINE_register_pkey_asn1_meths(e);
  90. }
  91. int ENGINE_set_default_pkey_asn1_meths(ENGINE *e)
  92. {
  93. if (e->pkey_asn1_meths) {
  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,
  99. e, nids, num_nids, 1);
  100. }
  101. return 1;
  102. }
  103. /*
  104. * Exposed API function to get a functional reference from the implementation
  105. * table (ie. try to get a functional reference from the tabled structural
  106. * references) for a given pkey_asn1_meth 'nid'
  107. */
  108. ENGINE *ENGINE_get_pkey_asn1_meth_engine(int nid)
  109. {
  110. return engine_table_select(&pkey_asn1_meth_table, nid);
  111. }
  112. /*
  113. * Obtains a pkey_asn1_meth implementation from an ENGINE functional
  114. * reference
  115. */
  116. const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth(ENGINE *e, int nid)
  117. {
  118. EVP_PKEY_ASN1_METHOD *ret;
  119. ENGINE_PKEY_ASN1_METHS_PTR fn = ENGINE_get_pkey_asn1_meths(e);
  120. if (!fn || !fn(e, &ret, NULL, nid)) {
  121. ENGINEerr(ENGINE_F_ENGINE_GET_PKEY_ASN1_METH,
  122. ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD);
  123. return NULL;
  124. }
  125. return ret;
  126. }
  127. /* Gets the pkey_asn1_meth callback from an ENGINE structure */
  128. ENGINE_PKEY_ASN1_METHS_PTR ENGINE_get_pkey_asn1_meths(const ENGINE *e)
  129. {
  130. return e->pkey_asn1_meths;
  131. }
  132. /* Sets the pkey_asn1_meth callback in an ENGINE structure */
  133. int ENGINE_set_pkey_asn1_meths(ENGINE *e, ENGINE_PKEY_ASN1_METHS_PTR f)
  134. {
  135. e->pkey_asn1_meths = f;
  136. return 1;
  137. }
  138. /*
  139. * Internal function to free up EVP_PKEY_ASN1_METHOD structures before an
  140. * ENGINE is destroyed
  141. */
  142. void engine_pkey_asn1_meths_free(ENGINE *e)
  143. {
  144. int i;
  145. EVP_PKEY_ASN1_METHOD *pkm;
  146. if (e->pkey_asn1_meths) {
  147. const int *pknids;
  148. int npknids;
  149. npknids = e->pkey_asn1_meths(e, NULL, &pknids, 0);
  150. for (i = 0; i < npknids; i++) {
  151. if (e->pkey_asn1_meths(e, &pkm, NULL, pknids[i])) {
  152. EVP_PKEY_asn1_free(pkm);
  153. }
  154. }
  155. }
  156. }
  157. /*
  158. * Find a method based on a string. This does a linear search through all
  159. * implemented algorithms. This is OK in practice because only a small number
  160. * of algorithms are likely to be implemented in an engine and it is not used
  161. * for speed critical operations.
  162. */
  163. const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth_str(ENGINE *e,
  164. const char *str,
  165. int len)
  166. {
  167. int i, nidcount;
  168. const int *nids;
  169. EVP_PKEY_ASN1_METHOD *ameth;
  170. if (!e->pkey_asn1_meths)
  171. return NULL;
  172. if (len == -1)
  173. len = strlen(str);
  174. nidcount = e->pkey_asn1_meths(e, NULL, &nids, 0);
  175. for (i = 0; i < nidcount; i++) {
  176. e->pkey_asn1_meths(e, &ameth, NULL, nids[i]);
  177. if (((int)strlen(ameth->pem_str) == len)
  178. && strncasecmp(ameth->pem_str, str, len) == 0)
  179. return ameth;
  180. }
  181. return NULL;
  182. }
  183. typedef struct {
  184. ENGINE *e;
  185. const EVP_PKEY_ASN1_METHOD *ameth;
  186. const char *str;
  187. int len;
  188. } ENGINE_FIND_STR;
  189. static void look_str_cb(int nid, STACK_OF(ENGINE) *sk, ENGINE *def, void *arg)
  190. {
  191. ENGINE_FIND_STR *lk = arg;
  192. int i;
  193. if (lk->ameth)
  194. return;
  195. for (i = 0; i < sk_ENGINE_num(sk); i++) {
  196. ENGINE *e = sk_ENGINE_value(sk, i);
  197. EVP_PKEY_ASN1_METHOD *ameth;
  198. e->pkey_asn1_meths(e, &ameth, NULL, nid);
  199. if (((int)strlen(ameth->pem_str) == lk->len)
  200. && strncasecmp(ameth->pem_str, lk->str, lk->len) == 0) {
  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,
  209. int len)
  210. {
  211. ENGINE_FIND_STR fstr;
  212. fstr.e = NULL;
  213. fstr.ameth = NULL;
  214. fstr.str = str;
  215. fstr.len = len;
  216. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  217. engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr);
  218. /* If found obtain a structural reference to engine */
  219. if (fstr.e) {
  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. }