encoders.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2020-2021 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) \
  33. { _name, \
  34. "provider=" ENCODER_PROVIDER ",fips=" #_fips ",output=" #_output, \
  35. (ossl_##_sym##_to_##_output##_encoder_functions) }
  36. #define ENCODER_w_structure(_name, _sym, _fips, _output, _structure) \
  37. { _name, \
  38. "provider=" ENCODER_PROVIDER ",fips=" #_fips ",output=" #_output \
  39. ",structure=" ENCODER_STRUCTURE_##_structure, \
  40. (ossl_##_sym##_to_##_structure##_##_output##_encoder_functions) }
  41. /*
  42. * Entries for human text "encoders"
  43. */
  44. ENCODER_TEXT("RSA", rsa, yes),
  45. ENCODER_TEXT("RSA-PSS", rsapss, yes),
  46. #ifndef OPENSSL_NO_DH
  47. ENCODER_TEXT("DH", dh, yes),
  48. ENCODER_TEXT("DHX", dhx, yes),
  49. #endif
  50. #ifndef OPENSSL_NO_DSA
  51. ENCODER_TEXT("DSA", dsa, yes),
  52. #endif
  53. #ifndef OPENSSL_NO_EC
  54. ENCODER_TEXT("EC", ec, yes),
  55. ENCODER_TEXT("ED25519", ed25519, yes),
  56. ENCODER_TEXT("ED448", ed448, yes),
  57. ENCODER_TEXT("X25519", x25519, yes),
  58. ENCODER_TEXT("X448", x448, yes),
  59. # ifndef OPENSSL_NO_SM2
  60. ENCODER_TEXT("SM2", sm2, yes),
  61. # endif
  62. #endif
  63. /*
  64. * Entries for key type specific output formats. The structure name on these
  65. * is the same as the key type name. This allows us to say something like:
  66. *
  67. * To replace i2d_{TYPE}PrivateKey(), i2d_{TYPE}PublicKey() and
  68. * i2d_{TYPE}Params(), use OSSL_ENCODER functions with an OSSL_ENCODER_CTX
  69. * created like this:
  70. *
  71. * OSSL_ENCODER_CTX *ctx =
  72. * OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "DER", "type-specific",
  73. * NULL, NULL);
  74. *
  75. * To replace PEM_write_bio_{TYPE}PrivateKey(), PEM_write_bio_{TYPE}PublicKey()
  76. * and PEM_write_bio_{TYPE}Params(), use OSSL_ENCODER functions with an
  77. * OSSL_ENCODER_CTX created like this:
  78. *
  79. * OSSL_ENCODER_CTX *ctx =
  80. * OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "PEM", "type-specific",
  81. * NULL, NULL);
  82. *
  83. * We only implement those for which there are current i2d_ and PEM_write_bio
  84. * implementations.
  85. */
  86. /* The RSA encoders only support private key and public key output */
  87. ENCODER_w_structure("RSA", rsa, yes, der, type_specific_keypair),
  88. ENCODER_w_structure("RSA", rsa, yes, pem, type_specific_keypair),
  89. #ifndef OPENSSL_NO_DH
  90. /* DH and X9.42 DH only support key parameters output. */
  91. ENCODER_w_structure("DH", dh, yes, der, type_specific_params),
  92. ENCODER_w_structure("DH", dh, yes, pem, type_specific_params),
  93. ENCODER_w_structure("DHX", dhx, yes, der, type_specific_params),
  94. ENCODER_w_structure("DHX", dhx, yes, pem, type_specific_params),
  95. #endif
  96. #ifndef OPENSSL_NO_DSA
  97. ENCODER_w_structure("DSA", dsa, yes, der, type_specific),
  98. ENCODER_w_structure("DSA", dsa, yes, pem, type_specific),
  99. #endif
  100. #ifndef OPENSSL_NO_EC
  101. /* EC only supports keypair and parameters DER and PEM output. */
  102. ENCODER_w_structure("EC", ec, yes, der, type_specific_no_pub),
  103. ENCODER_w_structure("EC", ec, yes, pem, type_specific_no_pub),
  104. /* EC supports blob output for the public key */
  105. ENCODER("EC", ec, yes, blob),
  106. # ifndef OPENSSL_NO_SM2
  107. ENCODER_w_structure("SM2", sm2, yes, der, type_specific_no_pub),
  108. ENCODER_w_structure("SM2", sm2, yes, pem, type_specific_no_pub),
  109. ENCODER("SM2", sm2, yes, blob),
  110. # endif
  111. #endif
  112. /*
  113. * Entries for the output formats MSBLOB and PVK
  114. */
  115. ENCODER("RSA", rsa, yes, msblob),
  116. ENCODER("RSA", rsa, yes, pvk),
  117. #ifndef OPENSSL_NO_DSA
  118. ENCODER("DSA", dsa, yes, msblob),
  119. ENCODER("DSA", dsa, yes, pvk),
  120. #endif
  121. /*
  122. * Entries for PKCS#8 and SubjectPublicKeyInfo.
  123. * The "der" ones are added convenience for any user that wants to use
  124. * OSSL_ENCODER directly.
  125. * The "pem" ones also support PEM_write_bio_PrivateKey() and
  126. * PEM_write_bio_PUBKEY().
  127. */
  128. ENCODER_w_structure("RSA", rsa, yes, der, PKCS8),
  129. ENCODER_w_structure("RSA", rsa, yes, pem, PKCS8),
  130. ENCODER_w_structure("RSA", rsa, yes, der, SubjectPublicKeyInfo),
  131. ENCODER_w_structure("RSA", rsa, yes, pem, SubjectPublicKeyInfo),
  132. ENCODER_w_structure("RSA-PSS", rsapss, yes, der, PKCS8),
  133. ENCODER_w_structure("RSA-PSS", rsapss, yes, pem, PKCS8),
  134. ENCODER_w_structure("RSA-PSS", rsapss, yes, der, SubjectPublicKeyInfo),
  135. ENCODER_w_structure("RSA-PSS", rsapss, yes, pem, SubjectPublicKeyInfo),
  136. #ifndef OPENSSL_NO_DH
  137. ENCODER_w_structure("DH", dh, yes, der, PKCS8),
  138. ENCODER_w_structure("DH", dh, yes, pem, PKCS8),
  139. ENCODER_w_structure("DH", dh, yes, der, SubjectPublicKeyInfo),
  140. ENCODER_w_structure("DH", dh, yes, pem, SubjectPublicKeyInfo),
  141. ENCODER_w_structure("DHX", dhx, yes, der, PKCS8),
  142. ENCODER_w_structure("DHX", dhx, yes, pem, PKCS8),
  143. ENCODER_w_structure("DHX", dhx, yes, der, SubjectPublicKeyInfo),
  144. ENCODER_w_structure("DHX", dhx, yes, pem, SubjectPublicKeyInfo),
  145. #endif
  146. #ifndef OPENSSL_NO_DSA
  147. ENCODER_w_structure("DSA", dsa, yes, der, PKCS8),
  148. ENCODER_w_structure("DSA", dsa, yes, pem, PKCS8),
  149. ENCODER_w_structure("DSA", dsa, yes, der, SubjectPublicKeyInfo),
  150. ENCODER_w_structure("DSA", dsa, yes, pem, SubjectPublicKeyInfo),
  151. #endif
  152. #ifndef OPENSSL_NO_EC
  153. ENCODER_w_structure("EC", ec, yes, der, PKCS8),
  154. ENCODER_w_structure("EC", ec, yes, pem, PKCS8),
  155. ENCODER_w_structure("EC", ec, yes, der, SubjectPublicKeyInfo),
  156. ENCODER_w_structure("EC", ec, yes, pem, SubjectPublicKeyInfo),
  157. ENCODER_w_structure("X25519", x25519, yes, der, PKCS8),
  158. ENCODER_w_structure("X25519", x25519, yes, pem, PKCS8),
  159. ENCODER_w_structure("X25519", x25519, yes, der, SubjectPublicKeyInfo),
  160. ENCODER_w_structure("X25519", x25519, yes, pem, SubjectPublicKeyInfo),
  161. ENCODER_w_structure("X448", x448, yes, der, PKCS8),
  162. ENCODER_w_structure("X448", x448, yes, pem, PKCS8),
  163. ENCODER_w_structure("X448", x448, yes, der, SubjectPublicKeyInfo),
  164. ENCODER_w_structure("X448", x448, yes, pem, SubjectPublicKeyInfo),
  165. ENCODER_w_structure("ED25519", ed25519, yes, der, PKCS8),
  166. ENCODER_w_structure("ED25519", ed25519, yes, pem, PKCS8),
  167. ENCODER_w_structure("ED25519", ed25519, yes, der, SubjectPublicKeyInfo),
  168. ENCODER_w_structure("ED25519", ed25519, yes, pem, SubjectPublicKeyInfo),
  169. ENCODER_w_structure("ED448", ed448, yes, der, PKCS8),
  170. ENCODER_w_structure("ED448", ed448, yes, pem, PKCS8),
  171. ENCODER_w_structure("ED448", ed448, yes, der, SubjectPublicKeyInfo),
  172. ENCODER_w_structure("ED448", ed448, yes, pem, SubjectPublicKeyInfo),
  173. # ifndef OPENSSL_NO_SM2
  174. ENCODER_w_structure("SM2", sm2, yes, der, PKCS8),
  175. ENCODER_w_structure("SM2", sm2, yes, pem, PKCS8),
  176. ENCODER_w_structure("SM2", sm2, yes, der, SubjectPublicKeyInfo),
  177. ENCODER_w_structure("SM2", sm2, yes, pem, SubjectPublicKeyInfo),
  178. # endif
  179. #endif
  180. /*
  181. * Entries for key type specific output formats. These are exactly the
  182. * same as the type specific above, except that they use the key type
  183. * name as structure name instead of "type-specific", in the call on
  184. * OSSL_ENCODER_CTX_new_for_pkey().
  185. */
  186. /* The RSA encoders only support private key and public key output */
  187. ENCODER_w_structure("RSA", rsa, yes, der, RSA),
  188. ENCODER_w_structure("RSA", rsa, yes, pem, RSA),
  189. #ifndef OPENSSL_NO_DH
  190. /* DH and X9.42 DH only support key parameters output. */
  191. ENCODER_w_structure("DH", dh, yes, der, DH),
  192. ENCODER_w_structure("DH", dh, yes, pem, DH),
  193. ENCODER_w_structure("DHX", dhx, yes, der, DHX),
  194. ENCODER_w_structure("DHX", dhx, yes, pem, DHX),
  195. #endif
  196. #ifndef OPENSSL_NO_DSA
  197. ENCODER_w_structure("DSA", dsa, yes, der, DSA),
  198. ENCODER_w_structure("DSA", dsa, yes, pem, DSA),
  199. #endif
  200. #ifndef OPENSSL_NO_EC
  201. ENCODER_w_structure("EC", ec, yes, der, EC),
  202. ENCODER_w_structure("EC", ec, yes, pem, EC),
  203. #endif
  204. /*
  205. * Additional entries with structure names being the standard name.
  206. * This is entirely for the convenience of the user that wants to use
  207. * OSSL_ENCODER directly with names they may fancy. These do not impact
  208. * on libcrypto functionality in any way.
  209. */
  210. /* PKCS#1 is a well known for plain RSA keys, so we add that too */
  211. ENCODER_w_structure("RSA", rsa, yes, der, PKCS1),
  212. ENCODER_w_structure("RSA", rsa, yes, pem, PKCS1),
  213. ENCODER_w_structure("RSA-PSS", rsapss, yes, der, PKCS1),
  214. ENCODER_w_structure("RSA-PSS", rsapss, yes, pem, PKCS1),
  215. #ifndef OPENSSL_NO_DH
  216. /* PKCS#3 defines the format for DH parameters */
  217. ENCODER_w_structure("DH", dh, yes, der, PKCS3),
  218. ENCODER_w_structure("DH", dh, yes, pem, PKCS3),
  219. /* X9.42 defines the format for DHX parameters */
  220. ENCODER_w_structure("DHX", dhx, yes, der, X9_42),
  221. ENCODER_w_structure("DHX", dhx, yes, pem, X9_42),
  222. #endif
  223. #ifndef OPENSSL_NO_EC
  224. /* RFC 5915 defines the format for EC keys and parameters */
  225. ENCODER_w_structure("EC", ec, yes, der, X9_62),
  226. ENCODER_w_structure("EC", ec, yes, pem, X9_62),
  227. #endif