2
0

pem_local.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2019 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/pem.h>
  14. #include <openssl/serializer.h>
  15. /* Alternative IMPLEMENT macros for provided serializers */
  16. # define IMPLEMENT_PEM_provided_write_body_vars(type, asn1) \
  17. int ret = 0; \
  18. const char *pq = OSSL_SERIALIZER_##asn1##_TO_PEM_PQ; \
  19. OSSL_SERIALIZER_CTX *ctx = OSSL_SERIALIZER_CTX_new_by_##type(x, pq); \
  20. \
  21. if (ctx != NULL && OSSL_SERIALIZER_CTX_get_serializer(ctx) == NULL) { \
  22. OSSL_SERIALIZER_CTX_free(ctx); \
  23. goto legacy; \
  24. }
  25. # define IMPLEMENT_PEM_provided_write_body_pass() \
  26. ret = 1; \
  27. if (kstr == NULL && cb == NULL) { \
  28. if (u != NULL) { \
  29. kstr = u; \
  30. klen = strlen(u); \
  31. } else { \
  32. cb = PEM_def_callback; \
  33. } \
  34. } \
  35. if (enc != NULL) { \
  36. ret = 0; \
  37. if (OSSL_SERIALIZER_CTX_set_cipher(ctx, EVP_CIPHER_name(enc), \
  38. NULL)) { \
  39. ret = 1; \
  40. if (kstr != NULL \
  41. && !OSSL_SERIALIZER_CTX_set_passphrase(ctx, kstr, klen)) \
  42. ret = 0; \
  43. else if (cb != NULL \
  44. && !OSSL_SERIALIZER_CTX_set_passphrase_cb(ctx, 1, \
  45. cb, u)) \
  46. ret = 0; \
  47. } \
  48. } \
  49. if (!ret) { \
  50. OSSL_SERIALIZER_CTX_free(ctx); \
  51. return 0; \
  52. }
  53. # define IMPLEMENT_PEM_provided_write_body_main(type, outtype) \
  54. ret = OSSL_SERIALIZER_to_##outtype(ctx, out); \
  55. OSSL_SERIALIZER_CTX_free(ctx); \
  56. return ret
  57. # define IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
  58. writename) \
  59. legacy: \
  60. return PEM_ASN1_##writename((i2d_of_void *)i2d_##asn1, str, out, \
  61. x, NULL, NULL, 0, NULL, NULL)
  62. # define IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \
  63. writename) \
  64. legacy: \
  65. return PEM_ASN1_##writename((i2d_of_void *)i2d_##asn1, str, out, \
  66. x, enc, kstr, klen, cb, u)
  67. # define IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, \
  68. OUTTYPE, outtype, writename) \
  69. PEM_write_fnsig(name, type, OUTTYPE, writename) \
  70. { \
  71. IMPLEMENT_PEM_provided_write_body_vars(type, asn1); \
  72. IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
  73. IMPLEMENT_PEM_provided_write_body_fallback(str, asn1, \
  74. writename); \
  75. }
  76. # define IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, \
  77. OUTTYPE, outtype, writename) \
  78. PEM_write_cb_fnsig(name, type, OUTTYPE, writename) \
  79. { \
  80. IMPLEMENT_PEM_provided_write_body_vars(type, asn1); \
  81. IMPLEMENT_PEM_provided_write_body_pass(); \
  82. IMPLEMENT_PEM_provided_write_body_main(type, outtype); \
  83. IMPLEMENT_PEM_provided_write_body_fallback_cb(str, asn1, \
  84. writename); \
  85. }
  86. # ifdef OPENSSL_NO_STDIO
  87. # define IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1)
  88. # define IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1)
  89. # else
  90. # define IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1) \
  91. IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, FILE, fp, write)
  92. # define IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1) \
  93. IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, FILE, fp, write)
  94. # endif
  95. # define IMPLEMENT_PEM_provided_write_bio(name, type, str, asn1) \
  96. IMPLEMENT_PEM_provided_write_to(name, type, str, asn1, BIO, bio, write_bio)
  97. # define IMPLEMENT_PEM_provided_write_cb_bio(name, type, str, asn1) \
  98. IMPLEMENT_PEM_provided_write_cb_to(name, type, str, asn1, BIO, bio, write_bio)
  99. # define IMPLEMENT_PEM_provided_write(name, type, str, asn1) \
  100. IMPLEMENT_PEM_provided_write_bio(name, type, str, asn1) \
  101. IMPLEMENT_PEM_provided_write_fp(name, type, str, asn1)
  102. # define IMPLEMENT_PEM_provided_write_cb(name, type, str, asn1) \
  103. IMPLEMENT_PEM_provided_write_cb_bio(name, type, str, asn1) \
  104. IMPLEMENT_PEM_provided_write_cb_fp(name, type, str, asn1)
  105. # define IMPLEMENT_PEM_provided_rw(name, type, str, asn1) \
  106. IMPLEMENT_PEM_read(name, type, str, asn1) \
  107. IMPLEMENT_PEM_provided_write(name, type, str, asn1)
  108. # define IMPLEMENT_PEM_provided_rw_cb(name, type, str, asn1) \
  109. IMPLEMENT_PEM_read(name, type, str, asn1) \
  110. IMPLEMENT_PEM_provided_write_cb(name, type, str, asn1)