pem_local.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 EVP_PKEY_PUBLIC_KEY
  29. # define PEM_SELECTION_PrivateKey EVP_PKEY_KEYPAIR
  30. # define PEM_SELECTION_Parameters EVP_PKEY_KEY_PARAMETERS
  31. /*
  32. * Properties, named according to the ASN.1 names used throughout libcrypto.
  33. */
  34. # define PEM_STRUCTURE_PUBKEY "SubjectPublicKeyInfo"
  35. # define PEM_STRUCTURE_PrivateKey "pkcs8"
  36. # define PEM_STRUCTURE_Parameters "type-specific"
  37. # define PEM_STRUCTURE_RSAPrivateKey "type-specific"
  38. # define PEM_STRUCTURE_RSAPublicKey "type-specific"
  39. /* Alternative IMPLEMENT macros for provided encoders */
  40. # define IMPLEMENT_PEM_provided_write_body_vars(type, asn1, pq) \
  41. int ret = 0; \
  42. OSSL_ENCODER_CTX *ctx = \
  43. OSSL_ENCODER_CTX_new_by_##type(x, PEM_SELECTION_##asn1, \
  44. "PEM", PEM_STRUCTURE_##asn1, \
  45. (pq)); \
  46. \
  47. if (OSSL_ENCODER_CTX_get_num_encoders(ctx) == 0) { \
  48. OSSL_ENCODER_CTX_free(ctx); \
  49. goto legacy; \
  50. }
  51. # define IMPLEMENT_PEM_provided_write_body_pass() \
  52. ret = 1; \
  53. if (kstr == NULL && cb == NULL) { \
  54. if (u != NULL) { \
  55. kstr = u; \
  56. klen = strlen(u); \
  57. } else { \
  58. cb = PEM_def_callback; \
  59. } \
  60. } \
  61. if (enc != NULL) { \
  62. ret = 0; \
  63. if (OSSL_ENCODER_CTX_set_cipher(ctx, EVP_CIPHER_name(enc), \
  64. NULL)) { \
  65. ret = 1; \
  66. if (kstr != NULL \
  67. && !OSSL_ENCODER_CTX_set_passphrase(ctx, kstr, klen)) \
  68. ret = 0; \
  69. else if (cb != NULL \
  70. && !OSSL_ENCODER_CTX_set_pem_password_cb(ctx, \
  71. cb, u)) \
  72. ret = 0; \
  73. } \
  74. } \
  75. if (!ret) { \
  76. OSSL_ENCODER_CTX_free(ctx); \
  77. return 0; \
  78. }
  79. # define IMPLEMENT_PEM_provided_write_body_main(type, outtype) \
  80. ret = OSSL_ENCODER_to_##outtype(ctx, out); \
  81. OSSL_ENCODER_CTX_free(ctx); \
  82. return ret
  83. # define IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
  84. writename) \
  85. legacy: \
  86. return PEM_ASN1_##writename((i2d_of_void *)i2d_##asn1, str, out, \
  87. x, NULL, NULL, 0, NULL, NULL)
  88. # define IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \
  89. writename) \
  90. legacy: \
  91. return PEM_ASN1_##writename##((i2d_of_void *)i2d_##asn1, str, out, \
  92. x, enc, kstr, klen, cb, u)
  93. # define IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, \
  94. OUTTYPE, outtype, writename) \
  95. PEM_write_fnsig(name, type, OUTTYPE, writename) \
  96. { \
  97. IMPLEMENT_PEM_provided_write_body_vars(type, asn1, NULL); \
  98. IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
  99. IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
  100. writename); \
  101. } \
  102. PEM_write_ex_fnsig(name, type, OUTTYPE, writename) \
  103. { \
  104. IMPLEMENT_PEM_provided_write_body_vars(type, asn1, propq); \
  105. IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
  106. IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
  107. writename); \
  108. }
  109. # define IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, \
  110. OUTTYPE, outtype, writename) \
  111. PEM_write_cb_fnsig(name, type, OUTTYPE, writename) \
  112. { \
  113. IMPLEMENT_PEM_provided_write_body_vars(type, asn1, NULL); \
  114. IMPLEMENT_PEM_provided_write_body_pass(); \
  115. IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
  116. IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \
  117. writename); \
  118. } \
  119. PEM_write_ex_cb_fnsig(name, type, OUTTYPE, writename) \
  120. { \
  121. IMPLEMENT_PEM_provided_write_body_vars(type, asn1, propq); \
  122. IMPLEMENT_PEM_provided_write_body_pass(); \
  123. IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
  124. IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
  125. writename); \
  126. }
  127. # ifdef OPENSSL_NO_STDIO
  128. # define IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1)
  129. # define IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1)
  130. # else
  131. # define IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1) \
  132. IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, FILE, fp, write)
  133. # define IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1) \
  134. IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, FILE, fp, write)
  135. # endif
  136. # define IMPLEMENT_PEM_provided_write_bio(name, type, str, asn1) \
  137. IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, BIO, bio, write_bio)
  138. # define IMPLEMENT_PEM_provided_write_cb_bio(name, type, str, asn1) \
  139. IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, BIO, bio, write_bio)
  140. # define IMPLEMENT_PEM_provided_write(name, type, str, asn1) \
  141. IMPLEMENT_PEM_provided_write_bio(name, type, str, asn1) \
  142. IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1)
  143. # define IMPLEMENT_PEM_provided_write_cb(name, type, str, asn1) \
  144. IMPLEMENT_PEM_provided_write_cb_bio(name, type, str, asn1) \
  145. IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1)
  146. # define IMPLEMENT_PEM_provided_rw(name, type, str, asn1) \
  147. IMPLEMENT_PEM_read(name, type, str, asn1) \
  148. IMPLEMENT_PEM_provided_write(name, type, str, asn1)
  149. # define IMPLEMENT_PEM_provided_rw_cb(name, type, str, asn1) \
  150. IMPLEMENT_PEM_read(name, type, str, asn1) \
  151. IMPLEMENT_PEM_provided_write_cb(name, type, str, asn1)