-
Notifications
You must be signed in to change notification settings - Fork 110
/
selfsigned.go
116 lines (93 loc) · 2.75 KB
/
selfsigned.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
/*
Copyright 2021 Flant JSC
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 certificate
import (
"bytes"
"log"
"time"
"github.com/cloudflare/cfssl/cli/genkey"
"github.com/cloudflare/cfssl/config"
"github.com/cloudflare/cfssl/csr"
"github.com/cloudflare/cfssl/helpers"
"github.com/cloudflare/cfssl/signer"
"github.com/cloudflare/cfssl/signer/local"
"github.com/sirupsen/logrus"
)
type Certificate struct {
Key string `json:"key"`
Cert string `json:"cert"`
CA string `json:"ca"`
}
type SigningOption func(signing *config.Signing)
func WithSigningDefaultExpiry(expiry time.Duration) SigningOption {
return func(signing *config.Signing) {
signing.Default.Expiry = expiry
signing.Default.ExpiryString = expiry.String()
}
}
func WithSigningDefaultUsage(usage []string) SigningOption {
return func(signing *config.Signing) {
signing.Default.Usage = usage
}
}
func GenerateSelfSignedCert(logger *logrus.Entry, cn string, ca Authority, options ...interface{}) (Certificate, error) {
logger.Debugf("Generate self-signed cert for %s", cn)
request := &csr.CertificateRequest{
CN: cn,
KeyRequest: &csr.KeyRequest{
A: "ecdsa",
S: 256,
},
}
for _, option := range options {
if f, ok := option.(Option); ok {
f(request)
}
}
// Catch cfssl output and show it only if error is occurred.
var buf bytes.Buffer
logWriter := log.Writer()
log.SetOutput(&buf)
defer log.SetOutput(logWriter)
g := &csr.Generator{Validator: genkey.Validator}
csrBytes, key, err := g.ProcessRequest(request)
if err != nil {
return Certificate{}, err
}
req := signer.SignRequest{Request: string(csrBytes)}
parsedCa, err := helpers.ParseCertificatePEM([]byte(ca.Cert))
if err != nil {
return Certificate{}, err
}
priv, err := helpers.ParsePrivateKeyPEM([]byte(ca.Key))
if err != nil {
return Certificate{}, err
}
signingConfig := &config.Signing{
Default: config.DefaultConfig(),
}
for _, option := range options {
if f, ok := option.(SigningOption); ok {
f(signingConfig)
}
}
s, err := local.NewSigner(priv, parsedCa, signer.DefaultSigAlgo(priv), signingConfig)
if err != nil {
return Certificate{}, err
}
cert, err := s.Sign(req)
if err != nil {
return Certificate{}, err
}
return Certificate{CA: ca.Cert, Key: string(key), Cert: string(cert)}, nil
}