tb_asnmth.c 6.1 KB

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