github.com/jasoet/gopki@v1.15.0/encryption/cms_generic_test.go (about) 1 package encryption 2 3 import ( 4 "crypto/ed25519" 5 "crypto/rand" 6 "crypto/rsa" 7 "crypto/x509" 8 "crypto/x509/pkix" 9 "math/big" 10 "testing" 11 "time" 12 13 "github.com/jasoet/gopki/keypair/algo" 14 ) 15 16 // TestGenericPrivateKeyTypes demonstrates type safety with different private key types 17 func TestGenericPrivateKeyTypes(t *testing.T) { 18 testData := []byte("generic test data") 19 20 t.Run("RSAPrivateKey", func(t *testing.T) { 21 // Generate RSA key pair 22 rsaKeys, err := algo.GenerateRSAKeyPair(algo.KeySize2048) 23 if err != nil { 24 t.Fatalf("Failed to generate RSA key pair: %v", err) 25 } 26 27 // Create certificate 28 cert := createTestCertificate(t, rsaKeys.PublicKey, rsaKeys.PrivateKey) 29 30 // Create encrypted data 31 encData := createTestEncryptedData(testData, cert) 32 33 // Encode 34 encoded, err := EncodeToCMS(encData) 35 if err != nil { 36 t.Fatalf("Failed to encode: %v", err) 37 } 38 39 // Decode with RSA private key (type inferred as *rsa.PrivateKey) 40 decoded, err := DecodeFromCMS(encoded, cert, rsaKeys.PrivateKey) 41 if err != nil { 42 t.Fatalf("Failed to decode with RSA private key: %v", err) 43 } 44 45 if string(decoded.Data) != string(testData) { 46 t.Errorf("Data mismatch") 47 } 48 49 // Also test with explicit type parameter 50 decoded2, err := DecodeFromCMS[*rsa.PrivateKey](encoded, cert, rsaKeys.PrivateKey) 51 if err != nil { 52 t.Fatalf("Failed to decode with explicit RSA type: %v", err) 53 } 54 55 if string(decoded2.Data) != string(testData) { 56 t.Errorf("Data mismatch with explicit type") 57 } 58 }) 59 60 t.Run("TypeSafetyDemo", func(t *testing.T) { 61 // Demonstrate that the generic function accepts different private key types 62 // Note: We only test with RSA for actual encryption since PKCS7 library 63 // primarily supports RSA for envelope encryption 64 65 // Generate RSA key pair for actual testing 66 rsaKeys, err := algo.GenerateRSAKeyPair(algo.KeySize2048) 67 if err != nil { 68 t.Fatalf("Failed to generate RSA key pair: %v", err) 69 } 70 71 cert := createTestCertificate(t, rsaKeys.PublicKey, rsaKeys.PrivateKey) 72 encData := createTestEncryptedData(testData, cert) 73 encoded, err := EncodeToCMS(encData) 74 if err != nil { 75 t.Fatalf("Failed to encode: %v", err) 76 } 77 78 // Demonstrate type safety with different ways to call the generic function 79 t.Run("TypeInference", func(t *testing.T) { 80 // Type inferred automatically 81 decoded, err := DecodeFromCMS(encoded, cert, rsaKeys.PrivateKey) 82 if err != nil { 83 t.Fatalf("Failed with type inference: %v", err) 84 } 85 if string(decoded.Data) != string(testData) { 86 t.Error("Data mismatch") 87 } 88 }) 89 90 t.Run("ExplicitType", func(t *testing.T) { 91 // Explicit type parameter 92 decoded, err := DecodeFromCMS[*rsa.PrivateKey](encoded, cert, rsaKeys.PrivateKey) 93 if err != nil { 94 t.Fatalf("Failed with explicit type: %v", err) 95 } 96 if string(decoded.Data) != string(testData) { 97 t.Error("Data mismatch") 98 } 99 }) 100 101 t.Run("TypeCompatibility", func(t *testing.T) { 102 // Generate other key types just to show they compile 103 ecdsaKeys, err := algo.GenerateECDSAKeyPair(algo.P256) 104 if err != nil { 105 t.Fatalf("Failed to generate ECDSA key pair: %v", err) 106 } 107 108 _, ed25519Priv, err := ed25519.GenerateKey(rand.Reader) 109 if err != nil { 110 t.Fatalf("Failed to generate Ed25519 key pair: %v", err) 111 } 112 113 // These demonstrate type safety - they compile with different key types 114 dummyData := []byte("dummy") 115 dummyCert := createTestCertificate(t, rsaKeys.PublicKey, rsaKeys.PrivateKey) 116 117 // These would compile but may fail at runtime due to PKCS7 limitations 118 _, _ = DecodeFromCMS(dummyData, dummyCert, rsaKeys.PrivateKey) // *rsa.PrivateKey 119 _, _ = DecodeFromCMS(dummyData, dummyCert, ecdsaKeys.PrivateKey) // *ecdsa.PrivateKey 120 _, _ = DecodeFromCMS(dummyData, dummyCert, ed25519Priv) // ed25519.PrivateKey 121 122 t.Log("Generic function accepts multiple private key types at compile time") 123 }) 124 }) 125 } 126 127 // Helper function to create a test certificate 128 func createTestCertificate(t *testing.T, publicKey interface{}, privateKey interface{}) *x509.Certificate { 129 template := x509.Certificate{ 130 SerialNumber: big.NewInt(1), 131 Subject: pkix.Name{ 132 CommonName: "Generic Test Certificate", 133 Organization: []string{"Test Org"}, 134 }, 135 NotBefore: time.Now(), 136 NotAfter: time.Now().Add(365 * 24 * time.Hour), 137 KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, 138 ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageEmailProtection}, 139 BasicConstraintsValid: true, 140 } 141 142 certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey, privateKey) 143 if err != nil { 144 t.Fatalf("Failed to create certificate: %v", err) 145 } 146 147 cert, err := x509.ParseCertificate(certDER) 148 if err != nil { 149 t.Fatalf("Failed to parse certificate: %v", err) 150 } 151 152 return cert 153 } 154 155 // Helper function to create test encrypted data 156 func createTestEncryptedData(data []byte, cert *x509.Certificate) *EncryptedData { 157 return &EncryptedData{ 158 Algorithm: AlgorithmEnvelope, 159 Format: FormatCMS, 160 Data: data, 161 Recipients: []*RecipientInfo{ 162 { 163 Certificate: cert, 164 KeyEncryptionAlgorithm: AlgorithmRSAOAEP, 165 }, 166 }, 167 Timestamp: time.Now(), 168 Metadata: make(map[string]any), 169 } 170 }