pem_local.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2019-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. /*
  10. * TODO(v3.0): the IMPLEMENT macros in include/openssl/pem.h should be
  11. * moved here.
  12. */
  13. #include <openssl/core_dispatch.h>
  14. #include <openssl/pem.h>
  15. #include <openssl/encoder.h>
  16. /*
  17. * Selectors, named according to the ASN.1 names used throughout libcrypto.
  18. *
  19. * Note that these are not absolutely mandatory, they are rather a wishlist
  20. * of sorts. The provider implementations are free to make choices that
  21. * make sense for them, based on these selectors.
  22. * For example, the EC backend is likely to really just output the private
  23. * key to a PKCS#8 structure, even thought PEM_SELECTION_PrivateKey specifies
  24. * the public key as well. This is fine, as long as the corresponding
  25. * decoding operation can return an object that contains what libcrypto
  26. * expects.
  27. */
  28. # define PEM_SELECTION_PUBKEY \
  29. (OSSL_KEYMGMT_SELECT_ALL_PARAMETERS | OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
  30. # define PEM_SELECTION_PrivateKey \
  31. (OSSL_KEYMGMT_SELECT_ALL_PARAMETERS | OSSL_KEYMGMT_SELECT_KEYPAIR)
  32. # define PEM_SELECTION_Parameters OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
  33. /* Alternative IMPLEMENT macros for provided encoders */
  34. # define IMPLEMENT_PEM_provided_write_body_vars(type, asn1) \
  35. int ret = 0; \
  36. OSSL_ENCODER_CTX *ctx = \
  37. OSSL_ENCODER_CTX_new_by_##type(x, "PEM", PEM_SELECTION_##asn1, \
  38. NULL, NULL); \
  39. \
  40. if (OSSL_ENCODER_CTX_get_num_encoders(ctx) == 0) { \
  41. OSSL_ENCODER_CTX_free(ctx); \
  42. goto legacy; \
  43. }
  44. # define IMPLEMENT_PEM_provided_write_body_pass() \
  45. ret = 1; \
  46. if (kstr == NULL && cb == NULL) { \
  47. if (u != NULL) { \
  48. kstr = u; \
  49. klen = strlen(u); \
  50. } else { \
  51. cb = PEM_def_callback; \
  52. } \
  53. } \
  54. if (enc != NULL) { \
  55. ret = 0; \
  56. if (OSSL_ENCODER_CTX_set_cipher(ctx, EVP_CIPHER_name(enc), \
  57. NULL)) { \
  58. ret = 1; \
  59. if (kstr != NULL \
  60. && !OSSL_ENCODER_CTX_set_passphrase(ctx, kstr, klen)) \
  61. ret = 0; \
  62. else if (cb != NULL \
  63. && !OSSL_ENCODER_CTX_set_pem_password_cb(ctx, \
  64. cb, u)) \
  65. ret = 0; \
  66. } \
  67. } \
  68. if (!ret) { \
  69. OSSL_ENCODER_CTX_free(ctx); \
  70. return 0; \
  71. }
  72. # define IMPLEMENT_PEM_provided_write_body_main(type, outtype) \
  73. ret = OSSL_ENCODER_to_##outtype(ctx, out); \
  74. OSSL_ENCODER_CTX_free(ctx); \
  75. return ret
  76. # define IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
  77. writename) \
  78. legacy: \
  79. return PEM_ASN1_##writename((i2d_of_void *)i2d_##asn1, str, out, \
  80. x, NULL, NULL, 0, NULL, NULL)
  81. # define IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \
  82. writename) \
  83. legacy: \
  84. return PEM_ASN1_##writename((i2d_of_void *)i2d_##asn1, str, out, \
  85. x, enc, kstr, klen, cb, u)
  86. # define IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, \
  87. OUTTYPE, outtype, writename) \
  88. PEM_write_fnsig(name, type, OUTTYPE, writename) \
  89. { \
  90. IMPLEMENT_PEM_provided_write_body_vars(type, asn1); \
  91. IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
  92. IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
  93. writename); \
  94. }
  95. # define IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, \
  96. OUTTYPE, outtype, writename) \
  97. PEM_write_cb_fnsig(name, type, OUTTYPE, writename) \
  98. { \
  99. IMPLEMENT_PEM_provided_write_body_vars(type, asn1); \
  100. IMPLEMENT_PEM_provided_write_body_pass(); \
  101. IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
  102. IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \
  103. writename); \
  104. }
  105. # ifdef OPENSSL_NO_STDIO
  106. # define IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1)
  107. # define IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1)
  108. # else
  109. # define IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1) \
  110. IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, FILE, fp, write)
  111. # define IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1) \
  112. IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, FILE, fp, write)
  113. # endif
  114. # define IMPLEMENT_PEM_provided_write_bio(name, type, str, asn1) \
  115. IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, BIO, bio, write_bio)
  116. # define IMPLEMENT_PEM_provided_write_cb_bio(name, type, str, asn1) \
  117. IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, BIO, bio, write_bio)
  118. # define IMPLEMENT_PEM_provided_write(name, type, str, asn1) \
  119. IMPLEMENT_PEM_provided_write_bio(name, type, str, asn1) \
  120. IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1)
  121. # define IMPLEMENT_PEM_provided_write_cb(name, type, str, asn1) \
  122. IMPLEMENT_PEM_provided_write_cb_bio(name, type, str, asn1) \
  123. IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1)
  124. # define IMPLEMENT_PEM_provided_rw(name, type, str, asn1) \
  125. IMPLEMENT_PEM_read(name, type, str, asn1) \
  126. IMPLEMENT_PEM_provided_write(name, type, str, asn1)
  127. # define IMPLEMENT_PEM_provided_rw_cb(name, type, str, asn1) \
  128. IMPLEMENT_PEM_read(name, type, str, asn1) \
  129. IMPLEMENT_PEM_provided_write_cb(name, type, str, asn1)