cryptoutil_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package cryptoutil
  2. import (
  3. "crypto"
  4. "crypto/dsa" //nolint:staticcheck // DSA is only used to build an unsupported key
  5. "crypto/ecdsa"
  6. "crypto/ed25519"
  7. "crypto/elliptic"
  8. "crypto/rand"
  9. "crypto/rsa"
  10. "crypto/x509"
  11. "crypto/x509/pkix"
  12. "encoding/pem"
  13. "math/big"
  14. "testing"
  15. "time"
  16. "github.com/stretchr/testify/assert"
  17. "github.com/stretchr/testify/require"
  18. "go.step.sm/crypto/mldsa"
  19. )
  20. func TestIsSupportedPublicKey(t *testing.T) {
  21. tests := []struct {
  22. name string
  23. pub crypto.PublicKey
  24. want bool
  25. }{
  26. {"rsa", &rsa.PublicKey{}, true},
  27. {"ecdsa", &ecdsa.PublicKey{}, true},
  28. {"ed25519", ed25519.PublicKey{}, true},
  29. {"mldsa", &mldsa.PublicKey{}, true},
  30. {"dsa", &dsa.PublicKey{}, false},
  31. {"rsa value", rsa.PublicKey{}, false},
  32. {"ecdsa value", ecdsa.PublicKey{}, false},
  33. {"ed25519 pointer", &ed25519.PublicKey{}, false},
  34. {"private key", &rsa.PrivateKey{}, false},
  35. {"bytes", []byte("not a key"), false},
  36. {"nil", nil, false},
  37. }
  38. for _, tt := range tests {
  39. t.Run(tt.name, func(t *testing.T) {
  40. assert.Equal(t, tt.want, IsSupportedPublicKey(tt.pub))
  41. })
  42. }
  43. }
  44. func TestIsSupportedPrivateKey(t *testing.T) {
  45. tests := []struct {
  46. name string
  47. priv crypto.PrivateKey
  48. want bool
  49. }{
  50. {"rsa", &rsa.PrivateKey{}, true},
  51. {"ecdsa", &ecdsa.PrivateKey{}, true},
  52. {"ed25519", ed25519.PrivateKey{}, true},
  53. {"mldsa", &mldsa.PrivateKey{}, true},
  54. {"dsa", &dsa.PrivateKey{}, false},
  55. {"rsa value", rsa.PrivateKey{}, false},
  56. {"ecdsa value", ecdsa.PrivateKey{}, false},
  57. {"ed25519 pointer", &ed25519.PrivateKey{}, false},
  58. {"public key", &rsa.PublicKey{}, false},
  59. {"bytes", []byte("not a key"), false},
  60. {"nil", nil, false},
  61. }
  62. for _, tt := range tests {
  63. t.Run(tt.name, func(t *testing.T) {
  64. assert.Equal(t, tt.want, IsSupportedPrivateKey(tt.priv))
  65. })
  66. }
  67. }
  68. func TestPEMEncode(t *testing.T) {
  69. rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
  70. require.NoError(t, err)
  71. ecdsaKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  72. require.NoError(t, err)
  73. _, ed25519Key, err := ed25519.GenerateKey(rand.Reader)
  74. require.NoError(t, err)
  75. cert := mustCertificate(t, ecdsaKey)
  76. csr := mustCertificateRequest(t, ecdsaKey)
  77. type test struct {
  78. name string
  79. key any
  80. want *pem.Block
  81. wantErr string
  82. }
  83. tests := []test{
  84. {"rsa public key", rsaKey.Public(), &pem.Block{
  85. Type: "PUBLIC KEY", Bytes: mustPKIX(t, rsaKey.Public()),
  86. }, ""},
  87. {"ecdsa public key", ecdsaKey.Public(), &pem.Block{
  88. Type: "PUBLIC KEY", Bytes: mustPKIX(t, ecdsaKey.Public()),
  89. }, ""},
  90. {"ed25519 public key", ed25519Key.Public(), &pem.Block{
  91. Type: "PUBLIC KEY", Bytes: mustPKIX(t, ed25519Key.Public()),
  92. }, ""},
  93. {"rsa private key", rsaKey, &pem.Block{
  94. Type: "PRIVATE KEY", Bytes: mustPKCS8(t, rsaKey),
  95. }, ""},
  96. {"ecdsa private key", ecdsaKey, &pem.Block{
  97. Type: "PRIVATE KEY", Bytes: mustPKCS8(t, ecdsaKey),
  98. }, ""},
  99. {"ed25519 private key", ed25519Key, &pem.Block{
  100. Type: "PRIVATE KEY", Bytes: mustPKCS8(t, ed25519Key),
  101. }, ""},
  102. {"certificate", cert, &pem.Block{
  103. Type: "CERTIFICATE", Bytes: cert.Raw,
  104. }, ""},
  105. {"certificate request", csr, &pem.Block{
  106. Type: "CERTIFICATE REQUEST", Bytes: csr.Raw,
  107. }, ""},
  108. {"fail public key", &ecdsa.PublicKey{}, nil, "error marshaling public key"},
  109. {"fail private key", &ecdsa.PrivateKey{}, nil, "error marshaling private key"},
  110. {"fail dsa public key", &dsa.PublicKey{}, nil, "unsupported type *dsa.PublicKey"},
  111. {"fail bytes", []byte("not a key"), nil, "unsupported type []uint8"},
  112. {"fail nil", nil, nil, "unsupported type <nil>"},
  113. }
  114. // ML-DSA is only available on Go 1.27 and later. On older toolchains the
  115. // mldsa package is a stub that cannot generate keys, so there is nothing
  116. // to encode.
  117. if mldsa.Supported {
  118. mldsaKey, err := mldsa.GenerateKey(mldsa.MLDSA44())
  119. require.NoError(t, err)
  120. tests = append(tests,
  121. test{"mldsa public key", mldsaKey.Public(), &pem.Block{
  122. Type: "PUBLIC KEY", Bytes: mustPKIX(t, mldsaKey.Public()),
  123. }, ""},
  124. test{"mldsa private key", mldsaKey, &pem.Block{
  125. Type: "PRIVATE KEY", Bytes: mustPKCS8(t, mldsaKey),
  126. }, ""},
  127. )
  128. }
  129. for _, tt := range tests {
  130. t.Run(tt.name, func(t *testing.T) {
  131. got, err := PEMEncode(tt.key)
  132. if tt.wantErr != "" {
  133. assert.ErrorContains(t, err, tt.wantErr)
  134. assert.Nil(t, got)
  135. return
  136. }
  137. require.NoError(t, err)
  138. block, rest := pem.Decode(got)
  139. require.NotNil(t, block)
  140. assert.Empty(t, rest)
  141. assert.Equal(t, tt.want.Type, block.Type)
  142. assert.Equal(t, tt.want.Bytes, block.Bytes)
  143. assert.Empty(t, block.Headers)
  144. })
  145. }
  146. }
  147. func mustPKIX(t *testing.T, pub crypto.PublicKey) []byte {
  148. t.Helper()
  149. b, err := x509.MarshalPKIXPublicKey(pub)
  150. require.NoError(t, err)
  151. return b
  152. }
  153. func mustPKCS8(t *testing.T, priv crypto.PrivateKey) []byte {
  154. t.Helper()
  155. b, err := x509.MarshalPKCS8PrivateKey(priv)
  156. require.NoError(t, err)
  157. return b
  158. }
  159. func mustCertificate(t *testing.T, signer crypto.Signer) *x509.Certificate {
  160. t.Helper()
  161. template := &x509.Certificate{
  162. SerialNumber: big.NewInt(1234),
  163. Subject: pkix.Name{CommonName: "test.smallstep.com"},
  164. NotBefore: time.Now(),
  165. NotAfter: time.Now().Add(time.Hour),
  166. }
  167. der, err := x509.CreateCertificate(rand.Reader, template, template, signer.Public(), signer)
  168. require.NoError(t, err)
  169. cert, err := x509.ParseCertificate(der)
  170. require.NoError(t, err)
  171. return cert
  172. }
  173. func mustCertificateRequest(t *testing.T, signer crypto.Signer) *x509.CertificateRequest {
  174. t.Helper()
  175. der, err := x509.CreateCertificateRequest(rand.Reader, &x509.CertificateRequest{
  176. Subject: pkix.Name{CommonName: "test.smallstep.com"},
  177. }, signer)
  178. require.NoError(t, err)
  179. csr, err := x509.ParseCertificateRequest(der)
  180. require.NoError(t, err)
  181. return csr
  182. }