tb_asnmth.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include "e_os.h"
  10. #include "eng_local.h"
  11. #include <openssl/evp.h>
  12. #include "crypto/asn1.h"
  13. /*
  14. * If this symbol is defined then ENGINE_get_pkey_asn1_meth_engine(), the
  15. * function that is used by EVP to hook in pkey_asn1_meth code and cache
  16. * defaults (etc), will display brief debugging summaries to stderr with the
  17. * 'nid'.
  18. */
  19. /* #define ENGINE_PKEY_ASN1_METH_DEBUG */
  20. static ENGINE_TABLE *pkey_asn1_meth_table = NULL;
  21. void ENGINE_unregister_pkey_asn1_meths(ENGINE *e)
  22. {
  23. engine_table_unregister(&pkey_asn1_meth_table, e);
  24. }
  25. static void engine_unregister_all_pkey_asn1_meths(void)
  26. {
  27. engine_table_cleanup(&pkey_asn1_meth_table);
  28. }
  29. int ENGINE_register_pkey_asn1_meths(ENGINE *e)
  30. {
  31. if (e->pkey_asn1_meths) {
  32. const int *nids;
  33. int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0);
  34. if (num_nids > 0)
  35. return engine_table_register(&pkey_asn1_meth_table,
  36. engine_unregister_all_pkey_asn1_meths,
  37. e, nids, num_nids, 0);
  38. }
  39. return 1;
  40. }
  41. void ENGINE_register_all_pkey_asn1_meths(void)
  42. {
  43. ENGINE *e;
  44. for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
  45. ENGINE_register_pkey_asn1_meths(e);
  46. }
  47. int ENGINE_set_default_pkey_asn1_meths(ENGINE *e)
  48. {
  49. if (e->pkey_asn1_meths) {
  50. const int *nids;
  51. int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0);
  52. if (num_nids > 0)
  53. return engine_table_register(&pkey_asn1_meth_table,
  54. engine_unregister_all_pkey_asn1_meths,
  55. e, nids, num_nids, 1);
  56. }
  57. return 1;
  58. }
  59. /*
  60. * Exposed API function to get a functional reference from the implementation
  61. * table (ie. try to get a functional reference from the tabled structural
  62. * references) for a given pkey_asn1_meth 'nid'
  63. */
  64. ENGINE *ENGINE_get_pkey_asn1_meth_engine(int nid)
  65. {
  66. return engine_table_select(&pkey_asn1_meth_table, nid);
  67. }
  68. /*
  69. * Obtains a pkey_asn1_meth implementation from an ENGINE functional
  70. * reference
  71. */
  72. const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth(ENGINE *e, int nid)
  73. {
  74. EVP_PKEY_ASN1_METHOD *ret;
  75. ENGINE_PKEY_ASN1_METHS_PTR fn = ENGINE_get_pkey_asn1_meths(e);
  76. if (!fn || !fn(e, &ret, NULL, nid)) {
  77. ENGINEerr(ENGINE_F_ENGINE_GET_PKEY_ASN1_METH,
  78. ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD);
  79. return NULL;
  80. }
  81. return ret;
  82. }
  83. /* Gets the pkey_asn1_meth callback from an ENGINE structure */
  84. ENGINE_PKEY_ASN1_METHS_PTR ENGINE_get_pkey_asn1_meths(const ENGINE *e)
  85. {
  86. return e->pkey_asn1_meths;
  87. }
  88. /* Sets the pkey_asn1_meth callback in an ENGINE structure */
  89. int ENGINE_set_pkey_asn1_meths(ENGINE *e, ENGINE_PKEY_ASN1_METHS_PTR f)
  90. {
  91. e->pkey_asn1_meths = f;
  92. return 1;
  93. }
  94. /*
  95. * Internal function to free up EVP_PKEY_ASN1_METHOD structures before an
  96. * ENGINE is destroyed
  97. */
  98. void engine_pkey_asn1_meths_free(ENGINE *e)
  99. {
  100. int i;
  101. EVP_PKEY_ASN1_METHOD *pkm;
  102. if (e->pkey_asn1_meths) {
  103. const int *pknids;
  104. int npknids;
  105. npknids = e->pkey_asn1_meths(e, NULL, &pknids, 0);
  106. for (i = 0; i < npknids; i++) {
  107. if (e->pkey_asn1_meths(e, &pkm, NULL, pknids[i])) {
  108. EVP_PKEY_asn1_free(pkm);
  109. }
  110. }
  111. }
  112. }
  113. /*
  114. * Find a method based on a string. This does a linear search through all
  115. * implemented algorithms. This is OK in practice because only a small number
  116. * of algorithms are likely to be implemented in an engine and it is not used
  117. * for speed critical operations.
  118. */
  119. const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth_str(ENGINE *e,
  120. const char *str,
  121. int len)
  122. {
  123. int i, nidcount;
  124. const int *nids;
  125. EVP_PKEY_ASN1_METHOD *ameth;
  126. if (!e->pkey_asn1_meths)
  127. return NULL;
  128. if (len == -1)
  129. len = strlen(str);
  130. nidcount = e->pkey_asn1_meths(e, NULL, &nids, 0);
  131. for (i = 0; i < nidcount; i++) {
  132. e->pkey_asn1_meths(e, &ameth, NULL, nids[i]);
  133. if (ameth != NULL
  134. && ((int)strlen(ameth->pem_str) == len)
  135. && strncasecmp(ameth->pem_str, str, len) == 0)
  136. return ameth;
  137. }
  138. return NULL;
  139. }
  140. typedef struct {
  141. ENGINE *e;
  142. const EVP_PKEY_ASN1_METHOD *ameth;
  143. const char *str;
  144. int len;
  145. } ENGINE_FIND_STR;
  146. static void look_str_cb(int nid, STACK_OF(ENGINE) *sk, ENGINE *def, void *arg)
  147. {
  148. ENGINE_FIND_STR *lk = arg;
  149. int i;
  150. if (lk->ameth)
  151. return;
  152. for (i = 0; i < sk_ENGINE_num(sk); i++) {
  153. ENGINE *e = sk_ENGINE_value(sk, i);
  154. EVP_PKEY_ASN1_METHOD *ameth;
  155. e->pkey_asn1_meths(e, &ameth, NULL, nid);
  156. if (ameth != NULL
  157. && ((int)strlen(ameth->pem_str) == lk->len)
  158. && strncasecmp(ameth->pem_str, lk->str, lk->len) == 0) {
  159. lk->e = e;
  160. lk->ameth = ameth;
  161. return;
  162. }
  163. }
  164. }
  165. const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe,
  166. const char *str,
  167. int len)
  168. {
  169. ENGINE_FIND_STR fstr;
  170. fstr.e = NULL;
  171. fstr.ameth = NULL;
  172. fstr.str = str;
  173. fstr.len = len;
  174. if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
  175. ENGINEerr(ENGINE_F_ENGINE_PKEY_ASN1_FIND_STR, ERR_R_MALLOC_FAILURE);
  176. return NULL;
  177. }
  178. CRYPTO_THREAD_write_lock(global_engine_lock);
  179. engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr);
  180. /* If found obtain a structural reference to engine */
  181. if (fstr.e) {
  182. fstr.e->struct_ref++;
  183. engine_ref_debug(fstr.e, 0, 1);
  184. }
  185. *pe = fstr.e;
  186. CRYPTO_THREAD_unlock(global_engine_lock);
  187. return fstr.ameth;
  188. }