Skip to content

Commit f1e3eac

Browse files
authored
scion-pki: allow creating voting certificates without ISD-AS (scionproto#4843)
Only for cp-root, cp-ca, and cp-as certificates the ISD-AS is mandatory.
1 parent 8b89e76 commit f1e3eac

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

scion-pki/certs/create.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func newCreateCmd(pather command.Pather) *cobra.Command {
142142
Default: "depends on profile",
143143
}
144144

145-
var cmd = &cobra.Command{
145+
cmd := &cobra.Command{
146146
Use: "create [flags] <subject-template> <cert-file> <key-file>",
147147
Short: "Create a certificate or certificate signing request",
148148
Example: fmt.Sprintf(` %[1]s create --profile cp-root subject.tmpl cp-root.crt cp-root.key
@@ -202,7 +202,8 @@ A valid example for a JSON formatted template::
202202
if err != nil {
203203
return serrors.Wrap("parsing profile", err)
204204
}
205-
subject, err := createSubject(args[0], flags.commonName)
205+
requireIA := ct != cppki.Sensitive && ct != cppki.Regular
206+
subject, err := createSubject(args[0], flags.commonName, requireIA)
206207
if err != nil {
207208
return serrors.Wrap("creating subject", err)
208209
}
@@ -277,7 +278,7 @@ A valid example for a JSON formatted template::
277278
panic("failed to encode CSR")
278279
}
279280
csrFile := args[1]
280-
err = file.WriteFile(csrFile, encodedCSR, 0644, file.WithForce(flags.force))
281+
err = file.WriteFile(csrFile, encodedCSR, 0o644, file.WithForce(flags.force))
281282
if err != nil {
282283
return serrors.Wrap("writing CSR", err)
283284
}
@@ -313,7 +314,7 @@ A valid example for a JSON formatted template::
313314
encodedCert = append(encodedCert, caCertRaw...)
314315
}
315316
certFile := args[1]
316-
err = file.WriteFile(certFile, encodedCert, 0644, file.WithForce(flags.force))
317+
err = file.WriteFile(certFile, encodedCert, 0o644, file.WithForce(flags.force))
317318
if err != nil {
318319
return serrors.Wrap("writing certificate", err)
319320
}
@@ -325,7 +326,7 @@ A valid example for a JSON formatted template::
325326
if err := file.CheckDirExists(filepath.Dir(keyFile)); err != nil {
326327
return serrors.Wrap("checking that directory of private key exists", err)
327328
}
328-
err := file.WriteFile(keyFile, encodedKey, 0600, file.WithForce(flags.force))
329+
err := file.WriteFile(keyFile, encodedKey, 0o600, file.WithForce(flags.force))
329330
if err != nil {
330331
return serrors.Wrap("writing private key", err)
331332
}
@@ -414,8 +415,8 @@ func parseCertType(input string) (cppki.CertType, error) {
414415
}
415416
}
416417

417-
func createSubject(tmpl, commonName string) (pkix.Name, error) {
418-
subject, err := loadSubject(tmpl)
418+
func createSubject(tmpl, commonName string, requireIA bool) (pkix.Name, error) {
419+
subject, err := loadSubject(tmpl, requireIA)
419420
if err != nil {
420421
return pkix.Name{}, err
421422
}
@@ -425,7 +426,7 @@ func createSubject(tmpl, commonName string) (pkix.Name, error) {
425426
return subject, nil
426427
}
427428

428-
func loadSubject(tmpl string) (pkix.Name, error) {
429+
func loadSubject(tmpl string, requireIA bool) (pkix.Name, error) {
429430
raw, err := os.ReadFile(tmpl)
430431
if err != nil {
431432
return pkix.Name{}, err
@@ -447,7 +448,7 @@ func loadSubject(tmpl string) (pkix.Name, error) {
447448
if err := json.Unmarshal(raw, &vars); err != nil {
448449
return pkix.Name{}, err
449450
}
450-
return subjectFromVars(vars)
451+
return subjectFromVars(vars, requireIA)
451452
}
452453

453454
func parseCertificate(raw []byte) (*x509.Certificate, error) {

scion-pki/certs/renew.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ The template is expressed in JSON. A valid example::
358358
if flags.subject != "" {
359359
template = flags.subject
360360
}
361-
subject, err := createSubject(template, flags.commonName)
361+
subject, err := createSubject(template, flags.commonName, true)
362362
if err != nil {
363363
return err
364364
}
@@ -925,8 +925,8 @@ func extractChainLegacy(rep *cppb.ChainRenewalResponse) ([]*x509.Certificate, er
925925
return chain, nil
926926
}
927927

928-
func subjectFromVars(vars SubjectVars) (pkix.Name, error) {
929-
if vars.IA.IsZero() {
928+
func subjectFromVars(vars SubjectVars, requireIA bool) (pkix.Name, error) {
929+
if requireIA && vars.IA.IsZero() {
930930
return pkix.Name{}, serrors.New("isd_as required in template")
931931
}
932932
s := pkix.Name{

scion-pki/certs/renew_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,40 +59,52 @@ func TestCSRTemplate(t *testing.T) {
5959
testCases := map[string]struct {
6060
File string
6161
CommonName string
62+
RequireIA bool
6263
Expected pkix.RDNSequence
6364
ErrAssertion assert.ErrorAssertionFunc
6465
}{
6566
"valid": {
6667
File: "testdata/renew/ISD1-ASff00_0_111.csr.json",
68+
RequireIA: true,
69+
Expected: wantSubject.ToRDNSequence(),
70+
ErrAssertion: assert.NoError,
71+
},
72+
"valid - no ISD-AS": {
73+
File: "testdata/renew/ISD1-ASff00_0_111.csr.json",
74+
RequireIA: false,
6775
Expected: wantSubject.ToRDNSequence(),
6876
ErrAssertion: assert.NoError,
6977
},
7078
"from chain": {
7179
File: "testdata/renew/ISD1-ASff00_0_111.pem",
80+
RequireIA: true,
7281
Expected: wantSubject.ToRDNSequence(),
7382
ErrAssertion: assert.NoError,
7483
},
7584
"custom common name": {
7685
File: "testdata/renew/ISD1-ASff00_0_111.csr.json",
7786
CommonName: "custom",
87+
RequireIA: true,
7888
Expected: customSubject.ToRDNSequence(),
7989
ErrAssertion: assert.NoError,
8090
},
8191
"custom common name from chain": {
8292
File: "testdata/renew/ISD1-ASff00_0_111.pem",
8393
CommonName: "custom",
94+
RequireIA: true,
8495
Expected: customSubject.ToRDNSequence(),
8596
ErrAssertion: assert.NoError,
8697
},
8798
"no ISD-AS": {
8899
File: "testdata/renew/no_isd_as.json",
100+
RequireIA: true,
89101
ErrAssertion: assert.Error,
90102
},
91103
}
92104
for name, tc := range testCases {
93105
t.Run(name, func(t *testing.T) {
94106
t.Parallel()
95-
subject, err := createSubject(tc.File, tc.CommonName)
107+
subject, err := createSubject(tc.File, tc.CommonName, tc.RequireIA)
96108
tc.ErrAssertion(t, err)
97109
if err != nil {
98110
return

0 commit comments

Comments
 (0)