encoders.inc 12 KB

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