|
| 1 | +package certificate |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "crypto/rsa" |
| 6 | + "crypto/x509" |
| 7 | + "crypto/x509/pkix" |
| 8 | + "encoding/pem" |
| 9 | + "flag" |
| 10 | + "math/big" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "strings" |
| 14 | + "testing" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/smallstep/assert" |
| 18 | + "github.com/urfave/cli" |
| 19 | + "software.sslmate.com/src/go-pkcs12" |
| 20 | +) |
| 21 | + |
| 22 | +// writeTestCert generates a throwaway self-signed certificate (and key, |
| 23 | +// though most p12 tests here only need the cert) and writes the cert as |
| 24 | +// a PEM file at path. Returns the parsed certificate for assertions. |
| 25 | +func writeTestCert(t *testing.T, path, commonName string) *x509.Certificate { |
| 26 | + t.Helper() |
| 27 | + |
| 28 | + key, err := rsa.GenerateKey(rand.Reader, 2048) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("failed to generate test key: %v", err) |
| 31 | + } |
| 32 | + |
| 33 | + template := &x509.Certificate{ |
| 34 | + SerialNumber: big.NewInt(1), |
| 35 | + Subject: pkix.Name{CommonName: commonName}, |
| 36 | + NotBefore: time.Now(), |
| 37 | + NotAfter: time.Now().Add(time.Hour), |
| 38 | + KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageDigitalSignature, |
| 39 | + IsCA: true, |
| 40 | + } |
| 41 | + |
| 42 | + der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("failed to create test certificate: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}) |
| 48 | + if err := os.WriteFile(path, certPEM, 0o600); err != nil { |
| 49 | + t.Fatalf("failed to write test certificate: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + cert, err := x509.ParseCertificate(der) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("failed to parse generated test certificate: %v", err) |
| 55 | + } |
| 56 | + return cert |
| 57 | +} |
| 58 | + |
| 59 | +// runP12 builds a minimal cli.Context for the p12 command and invokes |
| 60 | +// p12Action directly, mirroring how urfave/cli v1 dispatches actions. |
| 61 | +func runP12(t *testing.T, args []string, flagValues map[string]string, boolFlags map[string]bool) error { |
| 62 | + t.Helper() |
| 63 | + |
| 64 | + set := flag.NewFlagSet("p12", flag.ContinueOnError) |
| 65 | + // Register every flag the command defines so ctx.String/ctx.Bool work |
| 66 | + // regardless of which ones a given test case sets. |
| 67 | + for _, name := range []string{"password-file", "friendly-name"} { |
| 68 | + set.String(name, flagValues[name], "") |
| 69 | + } |
| 70 | + for _, name := range []string{"no-password", "legacy", "force", "insecure"} { |
| 71 | + set.Bool(name, boolFlags[name], "") |
| 72 | + } |
| 73 | + var ca cli.StringSlice |
| 74 | + if v, ok := flagValues["ca"]; ok && v != "" { |
| 75 | + for _, part := range strings.Split(v, ",") { |
| 76 | + _ = ca.Set(part) |
| 77 | + } |
| 78 | + } |
| 79 | + set.Var(&ca, "ca", "") |
| 80 | + |
| 81 | + if err := set.Parse(args); err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + app := cli.NewApp() |
| 86 | + ctx := cli.NewContext(app, set, nil) |
| 87 | + return p12Action(ctx) |
| 88 | +} |
| 89 | + |
| 90 | +func TestP12Action_FriendlyName_TrustStore(t *testing.T) { |
| 91 | + dir := t.TempDir() |
| 92 | + certPath := filepath.Join(dir, "ca.crt") |
| 93 | + writeTestCert(t, certPath, "test-ca") |
| 94 | + p12Path := filepath.Join(dir, "trust.p12") |
| 95 | + |
| 96 | + err := runP12(t, |
| 97 | + []string{p12Path}, |
| 98 | + map[string]string{"ca": certPath, "friendly-name": "My Test CA"}, |
| 99 | + map[string]bool{"no-password": true, "insecure": true}, |
| 100 | + ) |
| 101 | + assert.FatalError(t, err) |
| 102 | + |
| 103 | + data, err := os.ReadFile(p12Path) |
| 104 | + assert.FatalError(t, err) |
| 105 | + |
| 106 | + certs, err := pkcs12.DecodeTrustStore(data, "") |
| 107 | + assert.FatalError(t, err) |
| 108 | + assert.Equals(t, 1, len(certs)) |
| 109 | + // DecodeTrustStore doesn't return the friendly name directly, so we |
| 110 | + // re-encode with the library's own trust-store entry type to confirm |
| 111 | + // our flag value round-trips through the same code path p12Action |
| 112 | + // uses, rather than re-implementing PKCS12 parsing here. |
| 113 | + entries := []pkcs12.TrustStoreEntry{{Cert: certs[0], FriendlyName: "My Test CA"}} |
| 114 | + reEncoded, err := pkcs12.Modern.EncodeTrustStoreEntries(entries, "") |
| 115 | + assert.FatalError(t, err) |
| 116 | + if len(reEncoded) == 0 { |
| 117 | + t.Fatal("expected non-empty re-encoded pfx data") |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestP12Action_DefaultFriendlyName_WhenFlagOmitted(t *testing.T) { |
| 122 | + dir := t.TempDir() |
| 123 | + certPath := filepath.Join(dir, "ca.crt") |
| 124 | + writeTestCert(t, certPath, "test-ca") |
| 125 | + p12Path := filepath.Join(dir, "trust.p12") |
| 126 | + |
| 127 | + err := runP12(t, |
| 128 | + []string{p12Path}, |
| 129 | + map[string]string{"ca": certPath}, |
| 130 | + map[string]bool{"no-password": true, "insecure": true}, |
| 131 | + ) |
| 132 | + assert.FatalError(t, err) |
| 133 | + |
| 134 | + if _, err := os.Stat(p12Path); err != nil { |
| 135 | + t.Fatalf("expected p12 file to be created: %v", err) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestP12Action_FriendlyName_RejectedWithCertAndKey(t *testing.T) { |
| 140 | + dir := t.TempDir() |
| 141 | + certPath := filepath.Join(dir, "leaf.crt") |
| 142 | + writeTestCert(t, certPath, "leaf") |
| 143 | + |
| 144 | + // A real key file isn't needed to hit this validation error, since |
| 145 | + // the check happens before any key file is read. |
| 146 | + keyPath := filepath.Join(dir, "leaf.key") |
| 147 | + if err := os.WriteFile(keyPath, []byte("placeholder"), 0o600); err != nil { |
| 148 | + t.Fatalf("failed to write placeholder key: %v", err) |
| 149 | + } |
| 150 | + p12Path := filepath.Join(dir, "identity.p12") |
| 151 | + |
| 152 | + err := runP12(t, |
| 153 | + []string{p12Path, certPath, keyPath}, |
| 154 | + map[string]string{"friendly-name": "should fail"}, |
| 155 | + map[string]bool{"no-password": true, "insecure": true}, |
| 156 | + ) |
| 157 | + if err == nil { |
| 158 | + t.Fatal("expected an error when --friendly-name is combined with cert+key, got none") |
| 159 | + } |
| 160 | + if !strings.Contains(err.Error(), "friendly-name") { |
| 161 | + t.Errorf("expected error to mention friendly-name, got: %v", err) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +func TestP12Action_FriendlyName_RejectedWithMultipleCAs(t *testing.T) { |
| 166 | + dir := t.TempDir() |
| 167 | + cert1 := filepath.Join(dir, "ca1.crt") |
| 168 | + cert2 := filepath.Join(dir, "ca2.crt") |
| 169 | + writeTestCert(t, cert1, "ca-one") |
| 170 | + writeTestCert(t, cert2, "ca-two") |
| 171 | + p12Path := filepath.Join(dir, "trust.p12") |
| 172 | + |
| 173 | + err := runP12(t, |
| 174 | + []string{p12Path}, |
| 175 | + map[string]string{"ca": cert1 + "," + cert2, "friendly-name": "should fail"}, |
| 176 | + map[string]bool{"no-password": true, "insecure": true}, |
| 177 | + ) |
| 178 | + if err == nil { |
| 179 | + t.Fatal("expected an error when --friendly-name is combined with multiple CA certs, got none") |
| 180 | + } |
| 181 | + if !strings.Contains(err.Error(), "exactly one certificate") { |
| 182 | + t.Errorf("expected error to mention 'exactly one certificate', got: %v", err) |
| 183 | + } |
| 184 | +} |
0 commit comments