encoders.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 ENCODER_PROVIDER
  10. # error Macro ENCODER_PROVIDER undefined
  11. #endif
  12. #define ENCODER_STRUCTURE_type_specific_keypair "type-specific"
  13. #define ENCODER_STRUCTURE_type_specific_params "type-specific"
  14. #define ENCODER_STRUCTURE_type_specific "type-specific"
  15. #define ENCODER_STRUCTURE_type_specific_no_pub "type-specific"
  16. #define ENCODER_STRUCTURE_PKCS8 "pkcs8"
  17. #define ENCODER_STRUCTURE_SubjectPublicKeyInfo "SubjectPublicKeyInfo"
  18. #define ENCODER_STRUCTURE_DH "dh"
  19. #define ENCODER_STRUCTURE_DHX "dhx"
  20. #define ENCODER_STRUCTURE_DSA "dsa"
  21. #define ENCODER_STRUCTURE_EC "ec"
  22. #define ENCODER_STRUCTURE_RSA "rsa"
  23. #define ENCODER_STRUCTURE_PKCS1 "pkcs1"
  24. #define ENCODER_STRUCTURE_PKCS3 "pkcs3"
  25. #define ENCODER_STRUCTURE_X9_42 "X9.42"
  26. #define ENCODER_STRUCTURE_X9_62 "X9.62"
  27. /* Arguments are prefixed with '_' to avoid build breaks on certain platforms */
  28. #define ENCODER_TEXT(_name, _sym, _fips) \
  29. { _name, \
  30. "provider=" ENCODER_PROVIDER ",fips=" #_fips ",output=text", \
  31. (ossl_##_sym##_to_text_encoder_functions) }
  32. #define ENCODER(_name, _sym, _fips, _output, _structure) \
  33. { _name, \
  34. "provider=" ENCODER_PROVIDER ",fips=" #_fips ",output=" #_output \
  35. ",structure=" ENCODER_STRUCTURE_##_structure, \
  36. (ossl_##_sym##_to_##_structure##_##_output##_encoder_functions) }
  37. /*
  38. * Entries for human text "encoders"
  39. */
  40. ENCODER_TEXT("RSA", rsa, yes),
  41. ENCODER_TEXT("RSA-PSS", rsapss, yes),
  42. #ifndef OPENSSL_NO_DH
  43. ENCODER_TEXT("DH", dh, yes),
  44. ENCODER_TEXT("DHX", dhx, yes),
  45. #endif
  46. #ifndef OPENSSL_NO_DSA
  47. ENCODER_TEXT("DSA", dsa, yes),
  48. #endif
  49. #ifndef OPENSSL_NO_EC
  50. ENCODER_TEXT("EC", ec, yes),
  51. ENCODER_TEXT("ED25519", ed25519, yes),
  52. ENCODER_TEXT("ED448", ed448, yes),
  53. ENCODER_TEXT("X25519", x25519, yes),
  54. ENCODER_TEXT("X448", x448, yes),
  55. #endif
  56. /*
  57. * Entries for key type specific output formats. The structure name on these
  58. * is the same as the key type name. This allows us to say something like:
  59. *
  60. * To replace i2d_{TYPE}PrivateKey(), i2d_{TYPE}PublicKey() and
  61. * i2d_{TYPE}Params(), use OSSL_ENCODER functions with an OSSL_ENCODER_CTX
  62. * created like this:
  63. *
  64. * OSSL_ENCODER_CTX *ctx =
  65. * OSSL_ENCODER_CTX_new_by_EVP_PKEY(pkey, selection, "DER", "type-specific",
  66. * NULL, NULL);
  67. *
  68. * To replace PEM_write_bio_{TYPE}PrivateKey(), PEM_write_bio_{TYPE}PublicKey()
  69. * and PEM_write_bio_{TYPE}Params(), use OSSL_ENCODER functions with an
  70. * OSSL_ENCODER_CTX created like this:
  71. *
  72. * OSSL_ENCODER_CTX *ctx =
  73. * OSSL_ENCODER_CTX_new_by_EVP_PKEY(pkey, selection, "PEM", "type-specific",
  74. * NULL, NULL);
  75. *
  76. * We only implement those for which there are current i2d_ and PEM_write_bio
  77. * implementations.
  78. */
  79. /* The RSA encoders only support private key and public key output */
  80. ENCODER("RSA", rsa, yes, der, type_specific_keypair),
  81. ENCODER("RSA", rsa, yes, pem, type_specific_keypair),
  82. #ifndef OPENSSL_NO_DH
  83. /* DH and X9.42 DH only support key parameters output. */
  84. ENCODER("DH", dh, yes, der, type_specific_params),
  85. ENCODER("DH", dh, yes, pem, type_specific_params),
  86. ENCODER("DHX", dhx, yes, der, type_specific_params),
  87. ENCODER("DHX", dhx, yes, pem, type_specific_params),
  88. #endif
  89. #ifndef OPENSSL_NO_DSA
  90. ENCODER("DSA", dsa, yes, der, type_specific),
  91. ENCODER("DSA", dsa, yes, pem, type_specific),
  92. #endif
  93. #ifndef OPENSSL_NO_EC
  94. /* EC only supports keypair and parameters output. */
  95. ENCODER("EC", ec, yes, der, type_specific_no_pub),
  96. ENCODER("EC", ec, yes, pem, type_specific_no_pub),
  97. #endif
  98. /*
  99. * Entries for PKCS#8 and SubjectPublicKeyInfo.
  100. * The "der" ones are added convenience for any user that wants to use
  101. * OSSL_ENCODER directly.
  102. * The "pem" ones also support PEM_write_bio_PrivateKey() and
  103. * PEM_write_bio_PUBKEY().
  104. */
  105. ENCODER("RSA", rsa, yes, der, PKCS8),
  106. ENCODER("RSA", rsa, yes, pem, PKCS8),
  107. ENCODER("RSA", rsa, yes, der, SubjectPublicKeyInfo),
  108. ENCODER("RSA", rsa, yes, pem, SubjectPublicKeyInfo),
  109. ENCODER("RSA-PSS", rsapss, yes, der, PKCS8),
  110. ENCODER("RSA-PSS", rsapss, yes, pem, PKCS8),
  111. ENCODER("RSA-PSS", rsapss, yes, der, SubjectPublicKeyInfo),
  112. ENCODER("RSA-PSS", rsapss, yes, pem, SubjectPublicKeyInfo),
  113. #ifndef OPENSSL_NO_DH
  114. ENCODER("DH", dh, yes, der, PKCS8),
  115. ENCODER("DH", dh, yes, pem, PKCS8),
  116. ENCODER("DH", dh, yes, der, SubjectPublicKeyInfo),
  117. ENCODER("DH", dh, yes, pem, SubjectPublicKeyInfo),
  118. ENCODER("DHX", dhx, yes, der, PKCS8),
  119. ENCODER("DHX", dhx, yes, pem, PKCS8),
  120. ENCODER("DHX", dhx, yes, der, SubjectPublicKeyInfo),
  121. ENCODER("DHX", dhx, yes, pem, SubjectPublicKeyInfo),
  122. #endif
  123. #ifndef OPENSSL_NO_DSA
  124. ENCODER("DSA", dsa, yes, der, PKCS8),
  125. ENCODER("DSA", dsa, yes, pem, PKCS8),
  126. ENCODER("DSA", dsa, yes, der, SubjectPublicKeyInfo),
  127. ENCODER("DSA", dsa, yes, pem, SubjectPublicKeyInfo),
  128. #endif
  129. #ifndef OPENSSL_NO_EC
  130. ENCODER("EC", ec, yes, der, PKCS8),
  131. ENCODER("EC", ec, yes, pem, PKCS8),
  132. ENCODER("EC", ec, yes, der, SubjectPublicKeyInfo),
  133. ENCODER("EC", ec, yes, pem, SubjectPublicKeyInfo),
  134. ENCODER("X25519", x25519, yes, der, PKCS8),
  135. ENCODER("X25519", x25519, yes, pem, PKCS8),
  136. ENCODER("X25519", x25519, yes, der, SubjectPublicKeyInfo),
  137. ENCODER("X25519", x25519, yes, pem, SubjectPublicKeyInfo),
  138. ENCODER("X448", x448, yes, der, PKCS8),
  139. ENCODER("X448", x448, yes, pem, PKCS8),
  140. ENCODER("X448", x448, yes, der, SubjectPublicKeyInfo),
  141. ENCODER("X448", x448, yes, pem, SubjectPublicKeyInfo),
  142. ENCODER("ED25519", ed25519, yes, der, PKCS8),
  143. ENCODER("ED25519", ed25519, yes, pem, PKCS8),
  144. ENCODER("ED25519", ed25519, yes, der, SubjectPublicKeyInfo),
  145. ENCODER("ED25519", ed25519, yes, pem, SubjectPublicKeyInfo),
  146. ENCODER("ED448", ed448, yes, der, PKCS8),
  147. ENCODER("ED448", ed448, yes, pem, PKCS8),
  148. ENCODER("ED448", ed448, yes, der, SubjectPublicKeyInfo),
  149. ENCODER("ED448", ed448, yes, pem, SubjectPublicKeyInfo),
  150. #endif
  151. /*
  152. * Entries for key type specific output formats. These are exactly the
  153. * same as the type specific above, except that they use the key type
  154. * name as structure name instead of "type-specific", in the call on
  155. * OSSL_ENCODER_CTX_new_by_EVP_PKEY().
  156. */
  157. /* The RSA encoders only support private key and public key output */
  158. ENCODER("RSA", rsa, yes, der, RSA),
  159. ENCODER("RSA", rsa, yes, pem, RSA),
  160. #ifndef OPENSSL_NO_DH
  161. /* DH and X9.42 DH only support key parameters output. */
  162. ENCODER("DH", dh, yes, der, DH),
  163. ENCODER("DH", dh, yes, pem, DH),
  164. ENCODER("DHX", dhx, yes, der, DHX),
  165. ENCODER("DHX", dhx, yes, pem, DHX),
  166. #endif
  167. #ifndef OPENSSL_NO_DSA
  168. ENCODER("DSA", dsa, yes, der, DSA),
  169. ENCODER("DSA", dsa, yes, pem, DSA),
  170. #endif
  171. #ifndef OPENSSL_NO_EC
  172. ENCODER("EC", ec, yes, der, EC),
  173. ENCODER("EC", ec, yes, pem, EC),
  174. #endif
  175. /*
  176. * Additional entries with structure names being the standard name.
  177. * This is entirely for the convenience of the user that wants to use
  178. * OSSL_ENCODER directly with names they may fancy. These do not impact
  179. * on libcrypto functionality in any way.
  180. */
  181. /* PKCS#1 is a well known for plain RSA keys, so we add that too */
  182. ENCODER("RSA", rsa, yes, der, PKCS1),
  183. ENCODER("RSA", rsa, yes, pem, PKCS1),
  184. ENCODER("RSA-PSS", rsapss, yes, der, PKCS1),
  185. ENCODER("RSA-PSS", rsapss, yes, pem, PKCS1),
  186. #ifndef OPENSSL_NO_DH
  187. /* PKCS#3 defines the format for DH parameters */
  188. ENCODER("DH", dh, yes, der, PKCS3),
  189. ENCODER("DH", dh, yes, pem, PKCS3),
  190. /* X9.42 defines the format for DHX parameters */
  191. ENCODER("DHX", dhx, yes, der, X9_42),
  192. ENCODER("DHX", dhx, yes, pem, X9_42),
  193. #endif
  194. #ifndef OPENSSL_NO_EC
  195. /* RFC 5915 defines the format for EC keys and parameters */
  196. ENCODER("EC", ec, yes, der, X9_62),
  197. ENCODER("EC", ec, yes, pem, X9_62),
  198. #endif