tb_asnmth.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 2006-2018 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. ENGINEerr(ENGINE_F_ENGINE_GET_PKEY_ASN1_METH,
  80. ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD);
  81. return NULL;
  82. }
  83. return ret;
  84. }
  85. /* Gets the pkey_asn1_meth callback from an ENGINE structure */
  86. ENGINE_PKEY_ASN1_METHS_PTR ENGINE_get_pkey_asn1_meths(const ENGINE *e)
  87. {
  88. return e->pkey_asn1_meths;
  89. }
  90. /* Sets the pkey_asn1_meth callback in an ENGINE structure */
  91. int ENGINE_set_pkey_asn1_meths(ENGINE *e, ENGINE_PKEY_ASN1_METHS_PTR f)
  92. {
  93. e->pkey_asn1_meths = f;
  94. return 1;
  95. }
  96. /*
  97. * Internal function to free up EVP_PKEY_ASN1_METHOD structures before an
  98. * ENGINE is destroyed
  99. */
  100. void engine_pkey_asn1_meths_free(ENGINE *e)
  101. {
  102. int i;
  103. EVP_PKEY_ASN1_METHOD *pkm;
  104. if (e->pkey_asn1_meths) {
  105. const int *pknids;
  106. int npknids;
  107. npknids = e->pkey_asn1_meths(e, NULL, &pknids, 0);
  108. for (i = 0; i < npknids; i++) {
  109. if (e->pkey_asn1_meths(e, &pkm, NULL, pknids[i])) {
  110. EVP_PKEY_asn1_free(pkm);
  111. }
  112. }
  113. }
  114. }
  115. /*
  116. * Find a method based on a string. This does a linear search through all
  117. * implemented algorithms. This is OK in practice because only a small number
  118. * of algorithms are likely to be implemented in an engine and it is not used
  119. * for speed critical operations.
  120. */
  121. const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth_str(ENGINE *e,
  122. const char *str,
  123. int len)
  124. {
  125. int i, nidcount;
  126. const int *nids;
  127. EVP_PKEY_ASN1_METHOD *ameth;
  128. if (!e->pkey_asn1_meths)
  129. return NULL;
  130. if (len == -1)
  131. len = strlen(str);
  132. nidcount = e->pkey_asn1_meths(e, NULL, &nids, 0);
  133. for (i = 0; i < nidcount; i++) {
  134. e->pkey_asn1_meths(e, &ameth, NULL, nids[i]);
  135. if (ameth != NULL
  136. && ((int)strlen(ameth->pem_str) == len)
  137. && strncasecmp(ameth->pem_str, str, len) == 0)
  138. return ameth;
  139. }
  140. return NULL;
  141. }
  142. typedef struct {
  143. ENGINE *e;
  144. const EVP_PKEY_ASN1_METHOD *ameth;
  145. const char *str;
  146. int len;
  147. } ENGINE_FIND_STR;
  148. static void look_str_cb(int nid, STACK_OF(ENGINE) *sk, ENGINE *def, void *arg)
  149. {
  150. ENGINE_FIND_STR *lk = arg;
  151. int i;
  152. if (lk->ameth)
  153. return;
  154. for (i = 0; i < sk_ENGINE_num(sk); i++) {
  155. ENGINE *e = sk_ENGINE_value(sk, i);
  156. EVP_PKEY_ASN1_METHOD *ameth;
  157. e->pkey_asn1_meths(e, &ameth, NULL, nid);
  158. if (ameth != NULL
  159. && ((int)strlen(ameth->pem_str) == lk->len)
  160. && strncasecmp(ameth->pem_str, lk->str, lk->len) == 0) {
  161. lk->e = e;
  162. lk->ameth = ameth;
  163. return;
  164. }
  165. }
  166. }
  167. const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe,
  168. const char *str,
  169. int len)
  170. {
  171. ENGINE_FIND_STR fstr;
  172. fstr.e = NULL;
  173. fstr.ameth = NULL;
  174. fstr.str = str;
  175. fstr.len = len;
  176. if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
  177. ENGINEerr(ENGINE_F_ENGINE_PKEY_ASN1_FIND_STR, ERR_R_MALLOC_FAILURE);
  178. return NULL;
  179. }
  180. CRYPTO_THREAD_write_lock(global_engine_lock);
  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. }