engine_loader.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 2018-2022 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. /*
  10. * Here is an STORE loader for ENGINE backed keys. It relies on deprecated
  11. * functions, and therefore need to have deprecation warnings suppressed.
  12. * This file is not compiled at all in a '--api=3 no-deprecated' configuration.
  13. */
  14. #define OPENSSL_SUPPRESS_DEPRECATED
  15. #include "internal/e_os.h"
  16. #include "apps.h"
  17. #ifndef OPENSSL_NO_ENGINE
  18. # include <stdarg.h>
  19. # include <string.h>
  20. # include <openssl/engine.h>
  21. # include <openssl/store.h>
  22. /*
  23. * Support for legacy private engine keys via the 'org.openssl.engine:' scheme
  24. *
  25. * org.openssl.engine:{engineid}:{keyid}
  26. *
  27. * Note: we ONLY support ENGINE_load_private_key() and ENGINE_load_public_key()
  28. * Note 2: This scheme has a precedent in code in PKIX-SSH. for exactly
  29. * this sort of purpose.
  30. */
  31. /* Local definition of OSSL_STORE_LOADER_CTX */
  32. struct ossl_store_loader_ctx_st {
  33. ENGINE *e; /* Structural reference */
  34. char *keyid;
  35. int expected;
  36. int loaded; /* 0 = key not loaded yet, 1 = key loaded */
  37. };
  38. static OSSL_STORE_LOADER_CTX *OSSL_STORE_LOADER_CTX_new(ENGINE *e, char *keyid)
  39. {
  40. OSSL_STORE_LOADER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  41. if (ctx != NULL) {
  42. ctx->e = e;
  43. ctx->keyid = keyid;
  44. }
  45. return ctx;
  46. }
  47. static void OSSL_STORE_LOADER_CTX_free(OSSL_STORE_LOADER_CTX *ctx)
  48. {
  49. if (ctx != NULL) {
  50. ENGINE_free(ctx->e);
  51. OPENSSL_free(ctx->keyid);
  52. OPENSSL_free(ctx);
  53. }
  54. }
  55. static OSSL_STORE_LOADER_CTX *engine_open(const OSSL_STORE_LOADER *loader,
  56. const char *uri,
  57. const UI_METHOD *ui_method,
  58. void *ui_data)
  59. {
  60. const char *p = uri, *q;
  61. ENGINE *e = NULL;
  62. char *keyid = NULL;
  63. OSSL_STORE_LOADER_CTX *ctx = NULL;
  64. if (!CHECK_AND_SKIP_CASE_PREFIX(p, ENGINE_SCHEME_COLON))
  65. return NULL;
  66. /* Look for engine ID */
  67. q = strchr(p, ':');
  68. if (q != NULL /* There is both an engine ID and a key ID */
  69. && p[0] != ':' /* The engine ID is at least one character */
  70. && q[1] != '\0') { /* The key ID is at least one character */
  71. char engineid[256];
  72. size_t engineid_l = q - p;
  73. strncpy(engineid, p, engineid_l);
  74. engineid[engineid_l] = '\0';
  75. e = ENGINE_by_id(engineid);
  76. keyid = OPENSSL_strdup(q + 1);
  77. }
  78. if (e != NULL && keyid != NULL)
  79. ctx = OSSL_STORE_LOADER_CTX_new(e, keyid);
  80. if (ctx == NULL) {
  81. OPENSSL_free(keyid);
  82. ENGINE_free(e);
  83. }
  84. return ctx;
  85. }
  86. static int engine_expect(OSSL_STORE_LOADER_CTX *ctx, int expected)
  87. {
  88. if (expected == 0
  89. || expected == OSSL_STORE_INFO_PUBKEY
  90. || expected == OSSL_STORE_INFO_PKEY) {
  91. ctx->expected = expected;
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. static OSSL_STORE_INFO *engine_load(OSSL_STORE_LOADER_CTX *ctx,
  97. const UI_METHOD *ui_method, void *ui_data)
  98. {
  99. EVP_PKEY *pkey = NULL, *pubkey = NULL;
  100. OSSL_STORE_INFO *info = NULL;
  101. if (ctx->loaded == 0) {
  102. if (ENGINE_init(ctx->e)) {
  103. if (ctx->expected == 0
  104. || ctx->expected == OSSL_STORE_INFO_PKEY)
  105. pkey =
  106. ENGINE_load_private_key(ctx->e, ctx->keyid,
  107. (UI_METHOD *)ui_method, ui_data);
  108. if ((pkey == NULL && ctx->expected == 0)
  109. || ctx->expected == OSSL_STORE_INFO_PUBKEY)
  110. pubkey =
  111. ENGINE_load_public_key(ctx->e, ctx->keyid,
  112. (UI_METHOD *)ui_method, ui_data);
  113. ENGINE_finish(ctx->e);
  114. }
  115. }
  116. ctx->loaded = 1;
  117. if (pubkey != NULL)
  118. info = OSSL_STORE_INFO_new_PUBKEY(pubkey);
  119. else if (pkey != NULL)
  120. info = OSSL_STORE_INFO_new_PKEY(pkey);
  121. if (info == NULL) {
  122. EVP_PKEY_free(pkey);
  123. EVP_PKEY_free(pubkey);
  124. }
  125. return info;
  126. }
  127. static int engine_eof(OSSL_STORE_LOADER_CTX *ctx)
  128. {
  129. return ctx->loaded != 0;
  130. }
  131. static int engine_error(OSSL_STORE_LOADER_CTX *ctx)
  132. {
  133. return 0;
  134. }
  135. static int engine_close(OSSL_STORE_LOADER_CTX *ctx)
  136. {
  137. OSSL_STORE_LOADER_CTX_free(ctx);
  138. return 1;
  139. }
  140. int setup_engine_loader(void)
  141. {
  142. OSSL_STORE_LOADER *loader = NULL;
  143. if ((loader = OSSL_STORE_LOADER_new(NULL, ENGINE_SCHEME)) == NULL
  144. || !OSSL_STORE_LOADER_set_open(loader, engine_open)
  145. || !OSSL_STORE_LOADER_set_expect(loader, engine_expect)
  146. || !OSSL_STORE_LOADER_set_load(loader, engine_load)
  147. || !OSSL_STORE_LOADER_set_eof(loader, engine_eof)
  148. || !OSSL_STORE_LOADER_set_error(loader, engine_error)
  149. || !OSSL_STORE_LOADER_set_close(loader, engine_close)
  150. || !OSSL_STORE_register_loader(loader)) {
  151. OSSL_STORE_LOADER_free(loader);
  152. loader = NULL;
  153. }
  154. return loader != NULL;
  155. }
  156. void destroy_engine_loader(void)
  157. {
  158. OSSL_STORE_LOADER *loader = OSSL_STORE_unregister_loader(ENGINE_SCHEME);
  159. OSSL_STORE_LOADER_free(loader);
  160. }
  161. #else /* !OPENSSL_NO_ENGINE */
  162. int setup_engine_loader(void)
  163. {
  164. return 0;
  165. }
  166. void destroy_engine_loader(void)
  167. {
  168. }
  169. #endif