Skip to content

Commit 88ee904

Browse files
Only sign if the root certificate is a CA.
1 parent d230b34 commit 88ee904

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

cmd/sign.go

+10
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ func newSignAction(c *cli.Context) {
6666
fmt.Fprintln(os.Stderr, "Get CA certificate error:", err)
6767
os.Exit(1)
6868
}
69+
// Validate that crt is allowed to sign certificates.
70+
raw_crt, err := crt.GetRawCertificate()
71+
if err != nil {
72+
fmt.Fprintln(os.Stderr, "GetRawCertificate failed on CA certificate:", err)
73+
os.Exit(1)
74+
}
75+
if !raw_crt.IsCA {
76+
fmt.Fprintln(os.Stderr, "CA certificate is not allowed to sign certificates.")
77+
os.Exit(1)
78+
}
6979

7080
key, err := depot.GetPrivateKey(d, formattedCAName)
7181
if err != nil {

tests/not_ca_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*-
2+
* Copyright 2015 Square Inc.
3+
* Copyright 2014 CoreOS
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package tests
19+
20+
import (
21+
"os"
22+
"strings"
23+
"testing"
24+
)
25+
26+
// Ensures certificates which aren't a CA can't sign other certificates.
27+
func TestNotCA(t *testing.T) {
28+
os.RemoveAll(depotDir)
29+
defer os.RemoveAll(depotDir)
30+
31+
stdout, stderr, err := run(binPath, "init", "--passphrase", passphrase, "--common-name", "cert1")
32+
if stderr != "" || err != nil {
33+
t.Fatalf("Received unexpected error: %v, %v", stderr, err)
34+
}
35+
if strings.Count(stdout, "Created") != 3 {
36+
t.Fatalf("Received incorrect create: %v", stdout)
37+
}
38+
39+
stdout, stderr, err = run(binPath, "request-cert", "--passphrase", passphrase, "--common-name", "cert2")
40+
if stderr != "" || err != nil {
41+
t.Fatalf("Received unexpected error: %v, %v", stderr, err)
42+
}
43+
if strings.Count(stdout, "Created") != 2 {
44+
t.Fatalf("Received incorrect create: %v", stdout)
45+
}
46+
47+
stdout, stderr, err = run(binPath, "request-cert", "--passphrase", passphrase, "--common-name", "cert3")
48+
if stderr != "" || err != nil {
49+
t.Fatalf("Received unexpected error: %v, %v", stderr, err)
50+
}
51+
if strings.Count(stdout, "Created") != 2 {
52+
t.Fatalf("Received incorrect create: %v", stdout)
53+
}
54+
55+
stdout, stderr, err = run(binPath, "sign", "--passphrase", passphrase, "--CA", "cert1", "cert2")
56+
if stderr != "" || err != nil {
57+
t.Fatalf("Received unexpected error: %v, %v", stderr, err)
58+
}
59+
if strings.Count(stdout, "Created") != 1 {
60+
t.Fatalf("Received incorrect create: %v", stdout)
61+
}
62+
63+
stdout, stderr, err = run(binPath, "sign", "--passphrase", passphrase, "--CA", "cert2", "cert3")
64+
if stderr != "CA certificate is not allowed to sign certificates.\n" {
65+
t.Fatalf("Failed to receive expected error.")
66+
}
67+
}

0 commit comments

Comments
 (0)