| 1 | package pkcs7 |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto" |
| 6 | "crypto/x509" |
| 7 | "crypto/x509/pkix" |
| 8 | "encoding/asn1" |
| 9 | "encoding/pem" |
| 10 | "io/ioutil" |
| 11 | "os" |
| 12 | "os/exec" |
| 13 | "reflect" |
| 14 | "testing" |
| 15 | ) |
| 16 | |
| 17 | func TestEncrypt(t *testing.T) { |
| 18 | modes := []int{ |
| 19 | EncryptionAlgorithmDESCBC, |
| 20 | EncryptionAlgorithmAES128CBC, |
| 21 | EncryptionAlgorithmAES256CBC, |
| 22 | EncryptionAlgorithmAES128GCM, |
| 23 | EncryptionAlgorithmAES256GCM, |
| 24 | } |
| 25 | sigalgs := []x509.SignatureAlgorithm{ |
| 26 | x509.SHA1WithRSA, |
| 27 | x509.SHA256WithRSA, |
| 28 | x509.SHA512WithRSA, |
| 29 | } |
| 30 | for _, mode := range modes { |
| 31 | for _, sigalg := range sigalgs { |
| 32 | ContentEncryptionAlgorithm = mode |
| 33 | |
| 34 | plaintext := []byte("Hello Secret World!") |
| 35 | cert, err := createTestCertificate(sigalg) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | encrypted, err := Encrypt(plaintext, []*x509.Certificate{cert.Certificate}) |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | p7, err := Parse(encrypted) |
| 44 | if err != nil { |
| 45 | t.Fatalf("cannot Parse encrypted result: %s", err) |
| 46 | } |
| 47 | result, err := p7.Decrypt(cert.Certificate, *cert.PrivateKey) |
| 48 | if err != nil { |
| 49 | t.Fatalf("cannot Decrypt encrypted result: %s", err) |
| 50 | } |
| 51 | if !bytes.Equal(plaintext, result) { |
| 52 | t.Errorf("encrypted data does not match plaintext:\n\tExpected: %s\n\tActual: %s", plaintext, result) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestEncryptUsingPSK(t *testing.T) { |
| 59 | modes := []int{ |
| 60 | EncryptionAlgorithmDESCBC, |
| 61 | EncryptionAlgorithmAES128GCM, |
| 62 | } |
| 63 | |
| 64 | for _, mode := range modes { |
| 65 | ContentEncryptionAlgorithm = mode |
| 66 | plaintext := []byte("Hello Secret World!") |
| 67 | var key []byte |
| 68 | |
| 69 | switch mode { |
| 70 | case EncryptionAlgorithmDESCBC: |
| 71 | key = []byte("64BitKey") |
| 72 | case EncryptionAlgorithmAES128GCM: |
| 73 | key = []byte("128BitKey4AESGCM") |
| 74 | } |
| 75 | ciphertext, err := EncryptUsingPSK(plaintext, key) |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | |
| 80 | p7, _ := Parse(ciphertext) |
| 81 | result, err := p7.DecryptUsingPSK(key) |
| 82 | if err != nil { |
| 83 | t.Fatalf("cannot Decrypt encrypted result: %s", err) |
| 84 | } |
| 85 | if !bytes.Equal(plaintext, result) { |
| 86 | t.Errorf("encrypted data does not match plaintext:\n\tExpected: %s\n\tActual: %s", plaintext, result) |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func TestPad(t *testing.T) { |
| 92 | tests := []struct { |
| 93 | Original []byte |
| 94 | Expected []byte |
| 95 | BlockSize int |
| 96 | }{ |
| 97 | {[]byte{0x1, 0x2, 0x3, 0x10}, []byte{0x1, 0x2, 0x3, 0x10, 0x4, 0x4, 0x4, 0x4}, 8}, |
| 98 | {[]byte{0x1, 0x2, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0}, []byte{0x1, 0x2, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8}, 8}, |
| 99 | } |
| 100 | for _, test := range tests { |
| 101 | padded, err := pad(test.Original, test.BlockSize) |
| 102 | if err != nil { |
| 103 | t.Errorf("pad encountered error: %s", err) |
| 104 | continue |
| 105 | } |
| 106 | if !bytes.Equal(test.Expected, padded) { |
| 107 | t.Errorf("pad results mismatch:\n\tExpected: %X\n\tActual: %X", test.Expected, padded) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func Test_getParametersForKeyEncryptionAlgorithm(t *testing.T) { |
| 113 | type args struct { |
| 114 | algorithm asn1.ObjectIdentifier |
| 115 | hash crypto.Hash |
| 116 | } |
| 117 | tests := []struct { |
| 118 | name string |
| 119 | args args |
| 120 | expErr error |
| 121 | }{ |
| 122 | {name: "sha256", args: args{algorithm: OIDEncryptionAlgorithmRSAESOAEP, hash: crypto.SHA256}}, |
| 123 | {name: "sha512", args: args{algorithm: OIDEncryptionAlgorithmRSAESOAEP, hash: crypto.SHA512}}, |
| 124 | } |
| 125 | for _, tt := range tests { |
| 126 | t.Run(tt.name, func(t *testing.T) { |
| 127 | got, err := getParametersForKeyEncryptionAlgorithm(tt.args.algorithm, tt.args.hash) |
| 128 | if tt.expErr != nil { |
| 129 | if err == nil { |
| 130 | t.Errorf("getParametersForKeyEncryptionAlgorithm() error = %v, expErr %v", err, tt.expErr) |
| 131 | return |
| 132 | } |
| 133 | if err.Error() != tt.expErr.Error() { |
| 134 | t.Errorf("getParametersForKeyEncryptionAlgorithm() = %v, want %v", err.Error(), tt.expErr.Error()) |
| 135 | return |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // test if the reverse operation results in the same value |
| 140 | alg := pkix.AlgorithmIdentifier{Algorithm: tt.args.algorithm, Parameters: got} |
| 141 | resultHash, err := getHashFuncForKeyEncryptionAlgorithm(alg) |
| 142 | if err != nil { |
| 143 | t.Errorf("getHashFuncForKeyEncryptionAlgorithm errors = %v", err) |
| 144 | } |
| 145 | |
| 146 | if resultHash != tt.args.hash { |
| 147 | t.Errorf("getHashFuncForKeyEncryptionAlgorithm() = %v, want %v", resultHash, tt.args.hash) |
| 148 | } |
| 149 | }) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | func Test_marshalEncryptedContent(t *testing.T) { |
| 154 | content := []byte{} |
| 155 | got := marshalEncryptedContent(content) |
| 156 | |
| 157 | expected := asn1.RawValue{Class: 2, Tag: 0, IsCompound: false, Bytes: []byte{}, FullBytes: nil} |
| 158 | if !reflect.DeepEqual(expected, got) { |
| 159 | t.Errorf("marshalEncryptedContent() = %v, want %v", got, expected) |
| 160 | } |
| 161 | |
| 162 | content = []byte{34, 165, 121, 103, 15, 109, 119, 147, 39, 236, 212, 103, 143, 164, 172, 22} |
| 163 | got = marshalEncryptedContent(content) |
| 164 | expected = asn1.RawValue{Class: 2, Tag: 0, IsCompound: false, Bytes: []byte{34, 165, 121, 103, 15, 109, 119, 147, 39, 236, 212, 103, 143, 164, 172, 22}, FullBytes: nil} |
| 165 | if !reflect.DeepEqual(expected, got) { |
| 166 | t.Errorf("marshalEncryptedContent() = %v, want %v", got, expected) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | func TestEncryptAndDecryptWithOpenSSL(t *testing.T) { |
| 171 | tf := UnmarshalTestFixture(RSAOAEPSHA256EncryptedTestFixture) // use existing fixture with cert + key |
| 172 | |
| 173 | content := []byte("this is the content") |
| 174 | recipients := []*x509.Certificate{tf.Certificate} |
| 175 | currentAlgorithm := ContentEncryptionAlgorithm |
| 176 | ContentEncryptionAlgorithm = EncryptionAlgorithmAES256CBC |
| 177 | defer func() { |
| 178 | ContentEncryptionAlgorithm = currentAlgorithm |
| 179 | }() |
| 180 | encryptedContent, err := Encrypt(content, recipients) |
| 181 | if err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | |
| 185 | contentFile, err := ioutil.TempFile("", "content") |
| 186 | if err != nil { |
| 187 | t.Fatal(err) |
| 188 | } |
| 189 | defer os.Remove(contentFile.Name()) |
| 190 | |
| 191 | err = pem.Encode(contentFile, &pem.Block{Type: "PKCS7", Bytes: encryptedContent}) |
| 192 | if err != nil { |
| 193 | t.Fatal(err) |
| 194 | } |
| 195 | contentFile.Close() |
| 196 | |
| 197 | recipientFile, err := ioutil.TempFile("", "content") |
| 198 | if err != nil { |
| 199 | t.Fatal(err) |
| 200 | } |
| 201 | defer os.Remove(recipientFile.Name()) |
| 202 | |
| 203 | err = pem.Encode(recipientFile, &pem.Block{Type: "CERTIFICATE", Bytes: tf.Certificate.Raw}) |
| 204 | if err != nil { |
| 205 | t.Fatal(err) |
| 206 | } |
| 207 | |
| 208 | privateKey, err := x509.MarshalPKCS8PrivateKey(tf.PrivateKey) |
| 209 | if err != nil { |
| 210 | t.Fatal(err) |
| 211 | } |
| 212 | |
| 213 | err = pem.Encode(recipientFile, &pem.Block{Type: "PRIVATE KEY", Bytes: privateKey}) |
| 214 | if err != nil { |
| 215 | t.Fatal(err) |
| 216 | } |
| 217 | recipientFile.Close() |
| 218 | |
| 219 | // call openssl to decrypt the content |
| 220 | opensslCMD := exec.Command("openssl", "cms", "-decrypt", |
| 221 | "-inform", "pem", |
| 222 | "-in", contentFile.Name(), |
| 223 | "-recip", recipientFile.Name(), |
| 224 | ) |
| 225 | out, err := opensslCMD.CombinedOutput() |
| 226 | if err != nil { |
| 227 | t.Fatalf("openssl command failed with %s: %s", err, out) |
| 228 | } |
| 229 | |
| 230 | if !bytes.Equal(content, out) { |
| 231 | t.Errorf("EncryptAndDecryptWithOpenSSL() = %v, want %v", out, content) |
| 232 | } |
| 233 | } |