store.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright 2016-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. #ifndef HEADER_OSSL_STORE_H
  10. # define HEADER_OSSL_STORE_H
  11. # include <stdarg.h>
  12. # include <openssl/ossl_typ.h>
  13. # include <openssl/pem.h>
  14. # include <openssl/storeerr.h>
  15. # ifdef __cplusplus
  16. extern "C" {
  17. # endif
  18. /*-
  19. * The main OSSL_STORE functions.
  20. * ------------------------------
  21. *
  22. * These allow applications to open a channel to a resource with supported
  23. * data (keys, certs, crls, ...), read the data a piece at a time and decide
  24. * what to do with it, and finally close.
  25. */
  26. typedef struct ossl_store_ctx_st OSSL_STORE_CTX;
  27. /*
  28. * Typedef for the OSSL_STORE_INFO post processing callback. This can be used
  29. * to massage the given OSSL_STORE_INFO, or to drop it entirely (by returning
  30. * NULL).
  31. */
  32. typedef OSSL_STORE_INFO *(*OSSL_STORE_post_process_info_fn)(OSSL_STORE_INFO *,
  33. void *);
  34. /*
  35. * Open a channel given a URI. The given UI method will be used any time the
  36. * loader needs extra input, for example when a password or pin is needed, and
  37. * will be passed the same user data every time it's needed in this context.
  38. *
  39. * Returns a context reference which represents the channel to communicate
  40. * through.
  41. */
  42. OSSL_STORE_CTX *OSSL_STORE_open(const char *uri, const UI_METHOD *ui_method,
  43. void *ui_data,
  44. OSSL_STORE_post_process_info_fn post_process,
  45. void *post_process_data);
  46. /*
  47. * Control / fine tune the OSSL_STORE channel. |cmd| determines what is to be
  48. * done, and depends on the underlying loader (use OSSL_STORE_get0_scheme to
  49. * determine which loader is used), except for common commands (see below).
  50. * Each command takes different arguments.
  51. */
  52. int OSSL_STORE_ctrl(OSSL_STORE_CTX *ctx, int cmd, ... /* args */);
  53. /*
  54. * Common ctrl commands that different loaders may choose to support.
  55. */
  56. /* int on = 0 or 1; STORE_ctrl(ctx, STORE_C_USE_SECMEM, &on); */
  57. # define OSSL_STORE_C_USE_SECMEM 1
  58. /* Where custom commands start */
  59. # define OSSL_STORE_C_CUSTOM_START 100
  60. /*
  61. * Read one data item (a key, a cert, a CRL) that is supported by the OSSL_STORE
  62. * functionality, given a context.
  63. * Returns a OSSL_STORE_INFO pointer, from which OpenSSL typed data can be
  64. * extracted with OSSL_STORE_INFO_get0_PKEY(), OSSL_STORE_INFO_get0_CERT(), ...
  65. * NULL is returned on error, which may include that the data found at the URI
  66. * can't be figured out for certain or is ambiguous.
  67. */
  68. OSSL_STORE_INFO *OSSL_STORE_load(OSSL_STORE_CTX *ctx);
  69. /*
  70. * Check if end of data (end of file) is reached
  71. * Returns 1 on end, 0 otherwise.
  72. */
  73. int OSSL_STORE_eof(OSSL_STORE_CTX *ctx);
  74. /*
  75. * Check if an error occured
  76. * Returns 1 if it did, 0 otherwise.
  77. */
  78. int OSSL_STORE_error(OSSL_STORE_CTX *ctx);
  79. /*
  80. * Close the channel
  81. * Returns 1 on success, 0 on error.
  82. */
  83. int OSSL_STORE_close(OSSL_STORE_CTX *ctx);
  84. /*-
  85. * Extracting OpenSSL types from and creating new OSSL_STORE_INFOs
  86. * ---------------------------------------------------------------
  87. */
  88. /*
  89. * Types of data that can be ossl_stored in a OSSL_STORE_INFO.
  90. * OSSL_STORE_INFO_NAME is typically found when getting a listing of
  91. * available "files" / "tokens" / what have you.
  92. */
  93. # define OSSL_STORE_INFO_NAME 1 /* char * */
  94. # define OSSL_STORE_INFO_PARAMS 2 /* EVP_PKEY * */
  95. # define OSSL_STORE_INFO_PKEY 3 /* EVP_PKEY * */
  96. # define OSSL_STORE_INFO_CERT 4 /* X509 * */
  97. # define OSSL_STORE_INFO_CRL 5 /* X509_CRL * */
  98. /*
  99. * Functions to generate OSSL_STORE_INFOs, one function for each type we
  100. * support having in them, as well as a generic constructor.
  101. *
  102. * In all cases, ownership of the object is transfered to the OSSL_STORE_INFO
  103. * and will therefore be freed when the OSSL_STORE_INFO is freed.
  104. */
  105. OSSL_STORE_INFO *OSSL_STORE_INFO_new_NAME(char *name);
  106. int OSSL_STORE_INFO_set0_NAME_description(OSSL_STORE_INFO *info, char *desc);
  107. OSSL_STORE_INFO *OSSL_STORE_INFO_new_PARAMS(EVP_PKEY *params);
  108. OSSL_STORE_INFO *OSSL_STORE_INFO_new_PKEY(EVP_PKEY *pkey);
  109. OSSL_STORE_INFO *OSSL_STORE_INFO_new_CERT(X509 *x509);
  110. OSSL_STORE_INFO *OSSL_STORE_INFO_new_CRL(X509_CRL *crl);
  111. /*
  112. * Functions to try to extract data from a OSSL_STORE_INFO.
  113. */
  114. int OSSL_STORE_INFO_get_type(const OSSL_STORE_INFO *info);
  115. const char *OSSL_STORE_INFO_get0_NAME(const OSSL_STORE_INFO *info);
  116. char *OSSL_STORE_INFO_get1_NAME(const OSSL_STORE_INFO *info);
  117. const char *OSSL_STORE_INFO_get0_NAME_description(const OSSL_STORE_INFO *info);
  118. char *OSSL_STORE_INFO_get1_NAME_description(const OSSL_STORE_INFO *info);
  119. EVP_PKEY *OSSL_STORE_INFO_get0_PARAMS(const OSSL_STORE_INFO *info);
  120. EVP_PKEY *OSSL_STORE_INFO_get1_PARAMS(const OSSL_STORE_INFO *info);
  121. EVP_PKEY *OSSL_STORE_INFO_get0_PKEY(const OSSL_STORE_INFO *info);
  122. EVP_PKEY *OSSL_STORE_INFO_get1_PKEY(const OSSL_STORE_INFO *info);
  123. X509 *OSSL_STORE_INFO_get0_CERT(const OSSL_STORE_INFO *info);
  124. X509 *OSSL_STORE_INFO_get1_CERT(const OSSL_STORE_INFO *info);
  125. X509_CRL *OSSL_STORE_INFO_get0_CRL(const OSSL_STORE_INFO *info);
  126. X509_CRL *OSSL_STORE_INFO_get1_CRL(const OSSL_STORE_INFO *info);
  127. const char *OSSL_STORE_INFO_type_string(int type);
  128. /*
  129. * Free the OSSL_STORE_INFO
  130. */
  131. void OSSL_STORE_INFO_free(OSSL_STORE_INFO *info);
  132. /*-
  133. * Function to register a loader for the given URI scheme.
  134. * -------------------------------------------------------
  135. *
  136. * The loader receives all the main components of an URI except for the
  137. * scheme.
  138. */
  139. typedef struct ossl_store_loader_st OSSL_STORE_LOADER;
  140. OSSL_STORE_LOADER *OSSL_STORE_LOADER_new(ENGINE *e, const char *scheme);
  141. const ENGINE *OSSL_STORE_LOADER_get0_engine(const OSSL_STORE_LOADER *loader);
  142. const char *OSSL_STORE_LOADER_get0_scheme(const OSSL_STORE_LOADER *loader);
  143. /* struct ossl_store_loader_ctx_st is defined differently by each loader */
  144. typedef struct ossl_store_loader_ctx_st OSSL_STORE_LOADER_CTX;
  145. typedef OSSL_STORE_LOADER_CTX *(*OSSL_STORE_open_fn)(const OSSL_STORE_LOADER
  146. *loader,
  147. const char *uri,
  148. const UI_METHOD *ui_method,
  149. void *ui_data);
  150. int OSSL_STORE_LOADER_set_open(OSSL_STORE_LOADER *loader,
  151. OSSL_STORE_open_fn open_function);
  152. typedef int (*OSSL_STORE_ctrl_fn)(OSSL_STORE_LOADER_CTX *ctx, int cmd,
  153. va_list args);
  154. int OSSL_STORE_LOADER_set_ctrl(OSSL_STORE_LOADER *loader,
  155. OSSL_STORE_ctrl_fn ctrl_function);
  156. typedef OSSL_STORE_INFO *(*OSSL_STORE_load_fn)(OSSL_STORE_LOADER_CTX *ctx,
  157. const UI_METHOD *ui_method,
  158. void *ui_data);
  159. int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER *loader,
  160. OSSL_STORE_load_fn load_function);
  161. typedef int (*OSSL_STORE_eof_fn)(OSSL_STORE_LOADER_CTX *ctx);
  162. int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER *loader,
  163. OSSL_STORE_eof_fn eof_function);
  164. typedef int (*OSSL_STORE_error_fn)(OSSL_STORE_LOADER_CTX *ctx);
  165. int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER *loader,
  166. OSSL_STORE_error_fn error_function);
  167. typedef int (*OSSL_STORE_close_fn)(OSSL_STORE_LOADER_CTX *ctx);
  168. int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER *loader,
  169. OSSL_STORE_close_fn close_function);
  170. void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER *loader);
  171. int OSSL_STORE_register_loader(OSSL_STORE_LOADER *loader);
  172. OSSL_STORE_LOADER *OSSL_STORE_unregister_loader(const char *scheme);
  173. /*-
  174. * Functions to list STORE loaders
  175. * -------------------------------
  176. */
  177. int OSSL_STORE_do_all_loaders(void (*do_function) (const OSSL_STORE_LOADER
  178. *loader, void *do_arg),
  179. void *do_arg);
  180. # ifdef __cplusplus
  181. }
  182. # endif
  183. #endif