| 1 | package pkcs12scip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64scip-go gomod github.com/golang/go/src go1.19 `encoding/base64`/" |
| 5 | "osscip-go gomod github.com/golang/go/src go1.19 os/" |
| 6 | "path/filepathscip-go gomod github.com/golang/go/src go1.19 `path/filepath`/" |
| 7 | "testingscip-go gomod github.com/golang/go/src go1.19 testing/" |
| 8 | ) |
| 9 | |
| 10 | func loadTestDatascip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/loadTestData().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#, filename string) []byte { |
| 11 | base64data, err := osscip-go gomod github.com/golang/go/src go1.19 os/.ReadFilescip-go gomod github.com/golang/go/src go1.19 os/ReadFile().(filepathscip-go gomod github.com/golang/go/src go1.19 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.19 `path/filepath`/Join().("testdata", filename)) |
| 12 | if err != nil { |
| 13 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("failed to load test data: %v", err) |
| 14 | } |
| 15 | rawData, err := base64scip-go gomod github.com/golang/go/src go1.19 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.19 `encoding/base64`/StdEncoding..DecodeStringscip-go gomod github.com/golang/go/src go1.19 `encoding/base64`/Encoding#DecodeString().(string(base64data)) |
| 16 | if err != nil { |
| 17 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("failed to decode test data %q: %v", filename, err) |
| 18 | } |
| 19 | return rawData |
| 20 | } |
| 21 | |
| 22 | // RFC 9579 Appendix A.1 |
| 23 | func TestDecodePKCS12DataOkscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/TestDecodePKCS12DataOk().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 24 | pfxData := loadTestDatascip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/loadTestData().(t, "rfc9579-a1.txt") |
| 25 | password := "1234" |
| 26 | |
| 27 | encodedPassword, err := bmpStringZeroTerminatedscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/bmpStringZeroTerminated().(password) |
| 28 | if err != nil { |
| 29 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("Failed to encode password to BMP string: %v", err) |
| 30 | } |
| 31 | |
| 32 | safeContents, updatedPassword, err := getSafeContentsscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/getSafeContents().(pfxData, encodedPassword, 1, 10) |
| 33 | if err != nil { |
| 34 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("Failed to load PKCS#12 data with getSafeContents: %v", err) |
| 35 | } |
| 36 | |
| 37 | if len(safeContents) == 0 { |
| 38 | t.Errorscip-go gomod github.com/golang/go/src go1.19 testing/common#Error().("Expected non-empty safe contents") |
| 39 | } |
| 40 | |
| 41 | t.Logfscip-go gomod github.com/golang/go/src go1.19 testing/common#Logf().("Successfully loaded PKCS#12 data with %d safe bag(s)", len(safeContents)) |
| 42 | t.Logfscip-go gomod github.com/golang/go/src go1.19 testing/common#Logf().("Updated password length: %d", len(updatedPassword)) |
| 43 | |
| 44 | for i, bag := range safeContents { |
| 45 | t.Logfscip-go gomod github.com/golang/go/src go1.19 testing/common#Logf().(" Bag %d: ID=%s", i, bag.Idscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/safeBag#Id..Stringscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/ObjectIdentifier#String().()) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // RFC 9579 Appendix A.2 |
| 50 | func TestDecodePKCS12DataSha256Sha512scip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/TestDecodePKCS12DataSha256Sha512().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 51 | pfxData := loadTestDatascip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/loadTestData().(t, "rfc9579-a2.txt") |
| 52 | password := "1234" |
| 53 | |
| 54 | encodedPassword, err := bmpStringZeroTerminatedscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/bmpStringZeroTerminated().(password) |
| 55 | if err != nil { |
| 56 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("Failed to encode password to BMP string: %v", err) |
| 57 | } |
| 58 | |
| 59 | safeContents, updatedPassword, err := getSafeContentsscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/getSafeContents().(pfxData, encodedPassword, 1, 10) |
| 60 | if err != nil { |
| 61 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("Failed to load PKCS#12 data with getSafeContents: %v", err) |
| 62 | } |
| 63 | |
| 64 | if len(safeContents) == 0 { |
| 65 | t.Errorscip-go gomod github.com/golang/go/src go1.19 testing/common#Error().("Expected non-empty safe contents") |
| 66 | } |
| 67 | |
| 68 | t.Logfscip-go gomod github.com/golang/go/src go1.19 testing/common#Logf().("Successfully loaded PKCS#12 SHA256/SHA512 data with %d safe bag(s)", len(safeContents)) |
| 69 | t.Logfscip-go gomod github.com/golang/go/src go1.19 testing/common#Logf().("Updated password length: %d", len(updatedPassword)) |
| 70 | |
| 71 | for i, bag := range safeContents { |
| 72 | t.Logfscip-go gomod github.com/golang/go/src go1.19 testing/common#Logf().(" Bag %d: ID=%s", i, bag.Idscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/safeBag#Id..Stringscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/ObjectIdentifier#String().()) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // RFC 9579 Appendix A.3 |
| 77 | func TestDecodePKCS12DataSha512Sha512scip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/TestDecodePKCS12DataSha512Sha512().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 78 | pfxData := loadTestDatascip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/loadTestData().(t, "rfc9579-a3.txt") |
| 79 | password := "1234" |
| 80 | |
| 81 | encodedPassword, err := bmpStringZeroTerminatedscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/bmpStringZeroTerminated().(password) |
| 82 | if err != nil { |
| 83 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("Failed to encode password to BMP string: %v", err) |
| 84 | } |
| 85 | |
| 86 | safeContents, updatedPassword, err := getSafeContentsscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/getSafeContents().(pfxData, encodedPassword, 1, 10) |
| 87 | if err != nil { |
| 88 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("Failed to load PKCS#12 data with getSafeContents: %v", err) |
| 89 | } |
| 90 | |
| 91 | if len(safeContents) == 0 { |
| 92 | t.Errorscip-go gomod github.com/golang/go/src go1.19 testing/common#Error().("Expected non-empty safe contents") |
| 93 | } |
| 94 | |
| 95 | t.Logfscip-go gomod github.com/golang/go/src go1.19 testing/common#Logf().("Successfully loaded PKCS#12 SHA512/SHA512 data with %d safe bag(s)", len(safeContents)) |
| 96 | t.Logfscip-go gomod github.com/golang/go/src go1.19 testing/common#Logf().("Updated password length: %d", len(updatedPassword)) |
| 97 | |
| 98 | for i, bag := range safeContents { |
| 99 | t.Logfscip-go gomod github.com/golang/go/src go1.19 testing/common#Logf().(" Bag %d: ID=%s", i, bag.Idscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/safeBag#Id..Stringscip-go gomod github.com/golang/go/src go1.19 `encoding/asn1`/ObjectIdentifier#String().()) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // RFC 9579 Appendix A.4 |
| 104 | // Test with bad iteration count |
| 105 | func TestDecodePKCS12DataBadIterationCountscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/TestDecodePKCS12DataBadIterationCount().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 106 | pfxData := loadTestDatascip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/loadTestData().(t, "rfc9579-a4.txt") |
| 107 | password := "1234" |
| 108 | |
| 109 | encodedPassword, err := bmpStringZeroTerminatedscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/bmpStringZeroTerminated().(password) |
| 110 | if err != nil { |
| 111 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("Failed to encode password to BMP string: %v", err) |
| 112 | } |
| 113 | |
| 114 | _, _, err = getSafeContentsscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/getSafeContents().(pfxData, encodedPassword, 1, 10) |
| 115 | if err == nil { |
| 116 | t.Fatalscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatal().("Expected getSafeContents to fail with bad iteration count, but it succeeded") |
| 117 | } |
| 118 | |
| 119 | if err != ErrIncorrectPasswordscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/ErrIncorrectPassword. { |
| 120 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("Got error %v but expected %v", err, ErrIncorrectPasswordscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/ErrIncorrectPassword.) |
| 121 | } |
| 122 | |
| 123 | t.Logfscip-go gomod github.com/golang/go/src go1.19 testing/common#Logf().("Successfully detected bad iteration count: %v", err) |
| 124 | } |
| 125 | |
| 126 | // RFC 9579 Appendix A.5 |
| 127 | // Test with incorrect salt |
| 128 | func TestDecodePKCS12DataIncorrectSaltscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/TestDecodePKCS12DataIncorrectSalt().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 129 | pfxData := loadTestDatascip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/loadTestData().(t, "rfc9579-a5.txt") |
| 130 | password := "1234" |
| 131 | |
| 132 | encodedPassword, err := bmpStringZeroTerminatedscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/bmpStringZeroTerminated().(password) |
| 133 | if err != nil { |
| 134 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("Failed to encode password to BMP string: %v", err) |
| 135 | } |
| 136 | |
| 137 | _, _, err = getSafeContentsscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/getSafeContents().(pfxData, encodedPassword, 1, 10) |
| 138 | if err == nil { |
| 139 | t.Fatalscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatal().("Expected getSafeContents to fail with incorrect salt, but it succeeded") |
| 140 | } |
| 141 | |
| 142 | if err != ErrIncorrectPasswordscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/ErrIncorrectPassword. { |
| 143 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("Got error %v but expected %v", err, ErrIncorrectPasswordscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/ErrIncorrectPassword.) |
| 144 | } |
| 145 | |
| 146 | t.Logfscip-go gomod github.com/golang/go/src go1.19 testing/common#Logf().("Successfully detected incorrect salt: %v", err) |
| 147 | } |
| 148 | |
| 149 | // RFC 9579 Appendix A.6 |
| 150 | // Test with missing key length |
| 151 | func TestDecodePKCS12DataMissingKeyLengthscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/TestDecodePKCS12DataMissingKeyLength().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 152 | pfxData := loadTestDatascip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/loadTestData().(t, "rfc9579-a6.txt") |
| 153 | password := "1234" |
| 154 | |
| 155 | encodedPassword, err := bmpStringZeroTerminatedscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/bmpStringZeroTerminated().(password) |
| 156 | if err != nil { |
| 157 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("Failed to encode password to BMP string: %v", err) |
| 158 | } |
| 159 | |
| 160 | _, _, err = getSafeContentsscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/getSafeContents().(pfxData, encodedPassword, 1, 10) |
| 161 | if err == nil { |
| 162 | t.Fatalscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatal().("Expected getSafeContents to fail with missing key length, but it succeeded") |
| 163 | } |
| 164 | |
| 165 | if expected := "pkcs12: PBMAC1 requires explicit KeyLength parameter in PBKDF2 parameters"; err.Error() != expected { |
| 166 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("Got error %v but expected %v", err.Error(), expected) |
| 167 | } |
| 168 | |
| 169 | t.Logfscip-go gomod github.com/golang/go/src go1.19 testing/common#Logf().("Successfully detected missing key length: %v", err) |
| 170 | } |
| 171 | |
| 172 | func TestModern2026TrustStoreEntriesscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/TestModern2026TrustStoreEntries().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) { |
| 173 | for commonName, base64P12 := range testdatascip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/testdata. { |
| 174 | p12, err := base64scip-go gomod github.com/golang/go/src go1.19 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.19 `encoding/base64`/StdEncoding..DecodeStringscip-go gomod github.com/golang/go/src go1.19 `encoding/base64`/Encoding#DecodeString().(base64P12) |
| 175 | if err != nil { |
| 176 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("failed to decode test PKCS#12 data: %v", err) |
| 177 | } |
| 178 | |
| 179 | _, cert, err := Decodescip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/Decode().(p12, "") |
| 180 | if err != nil { |
| 181 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("failed to decode test certificate: %v", err) |
| 182 | } |
| 183 | |
| 184 | pfxData, err := Modern2026scip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/Modern2026..EncodeTrustStoreEntriesscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/Encoder#EncodeTrustStoreEntries().([]TrustStoreEntryscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/TrustStoreEntry#{{ |
| 185 | Certscip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/TrustStoreEntry#Cert.: cert, |
| 186 | FriendlyNamescip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/TrustStoreEntry#FriendlyName.: "trust-anchor", |
| 187 | }}, "password") |
| 188 | if err != nil { |
| 189 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("failed to encode Modern2026 trust store: %v", err) |
| 190 | } |
| 191 | |
| 192 | decodedCerts, err := DecodeTrustStorescip-go gomod software.sslmate.com/src/go-pkcs12 . `software.sslmate.com/src/go-pkcs12`/DecodeTrustStore().(pfxData, "password") |
| 193 | if err != nil { |
| 194 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("failed to decode Modern2026 trust store: %v", err) |
| 195 | } |
| 196 | |
| 197 | if len(decodedCerts) != 1 { |
| 198 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("got %d decoded certs, want 1", len(decodedCerts)) |
| 199 | } |
| 200 | |
| 201 | if decodedCerts[0].Subjectscip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/Certificate#Subject..CommonNamescip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/Name#CommonName. != commonName { |
| 202 | t.Fatalfscip-go gomod github.com/golang/go/src go1.19 testing/common#Fatalf().("decoded common name = %q, want %q", decodedCerts[0].Subjectscip-go gomod github.com/golang/go/src go1.19 `crypto/x509`/Certificate#Subject..CommonNamescip-go gomod github.com/golang/go/src go1.19 `crypto/x509/pkix`/Name#CommonName., commonName) |
| 203 | } |
| 204 | } |
| 205 | } |