cryptoutil.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Package cryptoutil provides small helpers for inspecting and encoding the
  2. // key and certificate types used across the CA.
  3. package cryptoutil
  4. import (
  5. "crypto"
  6. "crypto/ecdsa"
  7. "crypto/ed25519"
  8. "crypto/rsa"
  9. "crypto/x509"
  10. "encoding/pem"
  11. "fmt"
  12. "go.step.sm/crypto/mldsa"
  13. )
  14. // IsSupportedPublicKey reports whether pub is a public key type that the CA
  15. // knows how to work with. Anything else, including a nil key, is rejected.
  16. func IsSupportedPublicKey(pub crypto.PublicKey) bool {
  17. switch pub.(type) {
  18. case *rsa.PublicKey, *ecdsa.PublicKey, ed25519.PublicKey, *mldsa.PublicKey:
  19. return true
  20. default:
  21. return false
  22. }
  23. }
  24. // IsSupportedPrivateKey reports whether priv is a private key type that the CA
  25. // knows how to work with. Anything else, including a nil key, is rejected.
  26. //
  27. // Note that keys living outside the process, like the ones backed by a KMS,
  28. // are not reported as supported here; they only satisfy crypto.Signer.
  29. func IsSupportedPrivateKey(priv crypto.PrivateKey) bool {
  30. switch priv.(type) {
  31. case *rsa.PrivateKey, *ecdsa.PrivateKey, ed25519.PrivateKey, *mldsa.PrivateKey:
  32. return true
  33. default:
  34. return false
  35. }
  36. }
  37. // PEMEncode encodes a key, a certificate or a certificate request using PEM.
  38. //
  39. // Public keys are marshaled using PKIX and private keys using PKCS #8, so the
  40. // resulting blocks are always "PUBLIC KEY" and "PRIVATE KEY"; the legacy
  41. // "RSA PRIVATE KEY" and "EC PRIVATE KEY" forms are never produced. Certificates
  42. // and certificate requests are encoded from their raw DER bytes, which means
  43. // they must have been parsed or created beforehand.
  44. func PEMEncode(key any) ([]byte, error) {
  45. switch k := key.(type) {
  46. case *rsa.PublicKey, *ecdsa.PublicKey, ed25519.PublicKey, *mldsa.PublicKey:
  47. b, err := x509.MarshalPKIXPublicKey(key)
  48. if err != nil {
  49. return nil, fmt.Errorf("error marshaling public key: %w", err)
  50. }
  51. return pem.EncodeToMemory(&pem.Block{
  52. Type: "PUBLIC KEY",
  53. Bytes: b,
  54. }), nil
  55. case *rsa.PrivateKey, *ecdsa.PrivateKey, ed25519.PrivateKey, *mldsa.PrivateKey:
  56. b, err := x509.MarshalPKCS8PrivateKey(key)
  57. if err != nil {
  58. return nil, fmt.Errorf("error marshaling private key: %w", err)
  59. }
  60. return pem.EncodeToMemory(&pem.Block{
  61. Type: "PRIVATE KEY",
  62. Bytes: b,
  63. }), nil
  64. case *x509.Certificate:
  65. return pem.EncodeToMemory(&pem.Block{
  66. Type: "CERTIFICATE",
  67. Bytes: k.Raw,
  68. }), nil
  69. case *x509.CertificateRequest:
  70. return pem.EncodeToMemory(&pem.Block{
  71. Type: "CERTIFICATE REQUEST",
  72. Bytes: k.Raw,
  73. }), nil
  74. default:
  75. return nil, fmt.Errorf("error PEM encoding: unsupported type %T", key)
  76. }
  77. }