passphrase.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2020 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. #ifndef OSSL_INTERNAL_PASSPHRASE_H
  10. # define OSSL_INTERNAL_PASSPHRASE_H
  11. /*
  12. * This is a passphrase reader bridge with bells and whistles.
  13. *
  14. * On one hand, an API may wish to offer all sorts of passphrase callback
  15. * possibilities to users, or may have to do so for historical reasons.
  16. * On the other hand, that same API may have demands from other interfaces,
  17. * notably from the libcrypto <-> provider interface, which uses
  18. * OSSL_PASSPHRASE_CALLBACK consistently.
  19. *
  20. * The structure and functions below are the fundaments for bridging one
  21. * passphrase callback form to another.
  22. *
  23. * In addition, extra features are included (this may be a growing list):
  24. *
  25. * - password caching. This is to be used by APIs where it's likely
  26. * that the same passphrase may be asked for more than once, but the
  27. * user shouldn't get prompted more than once. For example, this is
  28. * useful for OSSL_DECODER, which may have to use a passphrase while
  29. * trying to find out what input it has.
  30. */
  31. /*
  32. * Structure to hold whatever the calling user may specify. This structure
  33. * is intended to be integrated into API specific structures or to be used
  34. * as a local on-stack variable type. Therefore, no functions to allocate
  35. * or freed it on the heap is offered.
  36. */
  37. struct ossl_passphrase_data_st {
  38. enum {
  39. is_expl_passphrase = 1, /* Explicit passphrase given by user */
  40. is_pem_password, /* pem_password_cb given by user */
  41. is_ossl_passphrase, /* OSSL_PASSPHRASE_CALLBACK given by user */
  42. is_ui_method /* UI_METHOD given by user */
  43. } type;
  44. union {
  45. struct {
  46. char *passphrase_copy;
  47. size_t passphrase_len;
  48. } expl_passphrase;
  49. struct {
  50. pem_password_cb *password_cb;
  51. void *password_cbarg;
  52. } pem_password;
  53. struct {
  54. OSSL_PASSPHRASE_CALLBACK *passphrase_cb;
  55. void *passphrase_cbarg;
  56. } ossl_passphrase;
  57. struct {
  58. const UI_METHOD *ui_method;
  59. void *ui_method_data;
  60. } ui_method;
  61. } _;
  62. /*-
  63. * Flags section
  64. */
  65. /* Set to indicate that caching should be done */
  66. unsigned int flag_cache_passphrase:1;
  67. /*-
  68. * Misc section: caches and other
  69. */
  70. char *cached_passphrase;
  71. size_t cached_passphrase_len;
  72. };
  73. /* Structure manipulation */
  74. void ossl_pw_clear_passphrase_data(struct ossl_passphrase_data_st *data);
  75. void ossl_pw_clear_passphrase_cache(struct ossl_passphrase_data_st *data);
  76. int ossl_pw_set_passphrase(struct ossl_passphrase_data_st *data,
  77. const unsigned char *passphrase,
  78. size_t passphrase_len);
  79. int ossl_pw_set_pem_password_cb(struct ossl_passphrase_data_st *data,
  80. pem_password_cb *cb, void *cbarg);
  81. int ossl_pw_set_ossl_passphrase_cb(struct ossl_passphrase_data_st *data,
  82. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg);
  83. int ossl_pw_set_ui_method(struct ossl_passphrase_data_st *data,
  84. const UI_METHOD *ui_method, void *ui_data);
  85. int ossl_pw_enable_passphrase_caching(struct ossl_passphrase_data_st *data);
  86. int ossl_pw_disable_passphrase_caching(struct ossl_passphrase_data_st *data);
  87. /* Central function for direct calls */
  88. int ossl_pw_get_passphrase(char *pass, size_t pass_size, size_t *pass_len,
  89. const OSSL_PARAM params[], int verify,
  90. struct ossl_passphrase_data_st *data);
  91. /* Callback functions */
  92. /*
  93. * All of these callback expect that the callback argument is a
  94. * struct ossl_passphrase_data_st
  95. */
  96. pem_password_cb ossl_pw_pem_password;
  97. /* One callback for encoding (verification prompt) and one for decoding */
  98. OSSL_PASSPHRASE_CALLBACK ossl_pw_passphrase_callback_enc;
  99. OSSL_PASSPHRASE_CALLBACK ossl_pw_passphrase_callback_dec;
  100. #endif