encoders.inc 12 KB

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