forked from square/certstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.go
136 lines (121 loc) · 4.34 KB
/
sign.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*-
* Copyright 2015 Square Inc.
* Copyright 2014 CoreOS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd
import (
"fmt"
"os"
"strings"
"github.com/square/certstrap/Godeps/_workspace/src/github.com/codegangsta/cli"
"github.com/square/certstrap/depot"
"github.com/square/certstrap/pkix"
)
// NewSignCommand sets up a "sign" command to sign a CSR with a given CA for a new certificate
func NewSignCommand() cli.Command {
return cli.Command{
Name: "sign",
Usage: "Sign certificate request",
Description: "Sign certificate request with CA, and generate certificate for the host.",
Flags: []cli.Flag{
cli.StringFlag{"passphrase", "", "Passphrase to decrypt private-key PEM block of CA", ""},
cli.IntFlag{"years", 0, "DEPRECATED; Use --expires instead", ""},
cli.StringFlag{"expires", "2 years", "How long until the certificate expires. Example: 1 year 2 days 3 months 4 hours", ""},
cli.StringFlag{"CA", "", "CA to sign cert", ""},
cli.BoolFlag{"stdout", "Print certificate to stdout in addition to saving file", ""},
cli.BoolFlag{"intermediate", "Generated certificate should be a intermediate", ""},
},
Action: newSignAction,
}
}
func newSignAction(c *cli.Context) {
if len(c.Args()) != 1 {
fmt.Fprintln(os.Stderr, "One host name must be provided.")
os.Exit(1)
}
formattedReqName := strings.Replace(c.Args()[0], " ", "_", -1)
formattedCAName := strings.Replace(c.String("CA"), " ", "_", -1)
if depot.CheckCertificate(d, formattedReqName) {
fmt.Fprintln(os.Stderr, "Certificate has existed!")
os.Exit(1)
}
expires := c.String("expires")
if years := c.Int("years"); years != 0 {
expires = fmt.Sprintf("%s %s years", expires, years)
}
// Expiry parsing is a naive regex implementation
// Token based parsing would provide better feedback but
expiresTime, err := parseExpiry(expires)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid expiry: %s\n", err)
os.Exit(1)
}
csr, err := depot.GetCertificateSigningRequest(d, formattedReqName)
if err != nil {
fmt.Fprintln(os.Stderr, "Get certificate request error:", err)
os.Exit(1)
}
crt, err := depot.GetCertificate(d, formattedCAName)
if err != nil {
fmt.Fprintln(os.Stderr, "Get CA certificate error:", err)
os.Exit(1)
}
// Validate that crt is allowed to sign certificates.
raw_crt, err := crt.GetRawCertificate()
if err != nil {
fmt.Fprintln(os.Stderr, "GetRawCertificate failed on CA certificate:", err)
os.Exit(1)
}
// We punt on checking BasicConstraintsValid and checking MaxPathLen. The goal
// is to prevent accidentally creating invalid certificates, not protecting
// against malicious input.
if !raw_crt.IsCA {
fmt.Fprintln(os.Stderr, "Selected CA certificate is not allowed to sign certificates.")
os.Exit(1)
}
key, err := depot.GetPrivateKey(d, formattedCAName)
if err != nil {
key, err = depot.GetEncryptedPrivateKey(d, formattedCAName, getPassPhrase(c, "CA key"))
if err != nil {
fmt.Fprintln(os.Stderr, "Get CA key error:", err)
os.Exit(1)
}
}
var crtOut *pkix.Certificate
if c.Bool("intermediate") {
fmt.Fprintf(os.Stderr, "Building intermediate")
crtOut, err = pkix.CreateIntermediateCertificateAuthority(crt, key, csr, expiresTime)
} else {
crtOut, err = pkix.CreateCertificateHost(crt, key, csr, expiresTime)
}
if err != nil {
fmt.Fprintln(os.Stderr, "Create certificate error:", err)
os.Exit(1)
} else {
fmt.Printf("Created %s/%s.crt from %s/%s.csr signed by %s/%s.key\n", depotDir, formattedReqName, depotDir, formattedReqName, depotDir, formattedCAName)
}
if c.Bool("stdout") {
crtBytes, err := crtOut.Export()
if err != nil {
fmt.Fprintln(os.Stderr, "Print certificate error:", err)
os.Exit(1)
} else {
fmt.Printf(string(crtBytes[:]))
}
}
if err = depot.PutCertificate(d, formattedReqName, crtOut); err != nil {
fmt.Fprintln(os.Stderr, "Save certificate error:", err)
}
}