-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
cert.go
217 lines (180 loc) · 5.74 KB
/
cert.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package http_ns
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"math/big"
"strconv"
"time"
"github.com/caddyserver/certmagic"
"github.com/inoxlang/inox/internal/core"
netaddr "github.com/inoxlang/inox/internal/netaddr"
"github.com/inoxlang/inox/internal/utils"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
HTTP_UPLOAD_RATE_LIMIT_NAME = "http/upload"
HTTP_REQUEST_RATE_LIMIT_NAME = "http/request"
DEFAULT_HTTP_CLIENT_TIMEOUT = 10 * time.Second
MISSING_URL_ARG = "missing core.URL argument"
OPTION_DOES_NOT_EXIST_FMT = "option '%s' does not exist"
CERT_MAGIG_LOG_SRC = "http/certmagic"
)
var DEFAULT_HTTP_REQUEST_OPTIONS = &RequestOptions{
Timeout: DEFAULT_HTTP_CLIENT_TIMEOUT,
InsecureSkipVerify: false,
}
func getPublicKey(privKey interface{}) interface{} {
switch k := privKey.(type) {
case *rsa.PrivateKey:
return &k.PublicKey
case *ecdsa.PrivateKey:
return &k.PublicKey
default:
return nil
}
}
func makePemBlockForKey(privKey interface{}) (*pem.Block, error) {
switch k := privKey.(type) {
case *rsa.PrivateKey:
return &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(k),
}, nil
case *ecdsa.PrivateKey:
b, err := x509.MarshalECPrivateKey(k)
if err != nil {
return nil, fmt.Errorf("unable to marshal ECDSA private key: %v", err)
}
return &pem.Block{
Type: "EC PRIVATE KEY",
Bytes: b,
}, nil
default:
return nil, fmt.Errorf("cannot make PEM block for %#v", k)
}
}
type SelfSignedCertParams struct {
Localhost bool
NonLocalhostIPs bool
ValidityDuration time.Duration //should be >= 100ms, defaults to DEFAULT_SELF_SIGNED_CERT_VALIDITY_DURATION
}
func GenerateSelfSignedCertAndKey(args SelfSignedCertParams) (cert *pem.Block, key *pem.Block, err error) {
if !args.Localhost && !args.NonLocalhostIPs {
return nil, nil, errors.New("invalid arguments: both .Localhost and .NonLocalhostIPs are false")
}
if args.ValidityDuration == 0 {
args.ValidityDuration = DEFAULT_SELF_SIGNED_CERT_VALIDITY_DURATION
} else if args.ValidityDuration < 100*time.Millisecond {
return nil, nil, errors.New("invalid arguments: validity duration is less than 100ms")
}
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, nil, err
}
//Generate a random organization name. Re-using the same (org. name, serial number) pair
//can cause an error (SEC_ERROR_REUSED_ISSUER_AND_SERIAL) in browsers such as Firefox.
orgName := "Acme Co - " + strconv.FormatUint(core.DefaultRandSource.Uint64(), 32)
template := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
Organization: []string{orgName},
},
NotBefore: time.Now(),
NotAfter: time.Now().Add(args.ValidityDuration),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
if args.Localhost {
template.DNSNames = append(template.DNSNames, "localhost", "127.0.0.1")
}
if args.NonLocalhostIPs {
ips, err := netaddr.GetGlobalUnicastIPs()
if err != nil {
return nil, nil, fmt.Errorf("failed to list interfaces IPs: %w", err)
}
for _, ip := range ips {
template.DNSNames = append(template.DNSNames, ip.String())
}
}
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, getPublicKey(priv), priv)
if err != nil {
return nil, nil, fmt.Errorf("failed to create certificate: %w", err)
}
keyBlock, err := makePemBlockForKey(priv)
if err != nil {
return nil, nil, fmt.Errorf("failed to create key: %w", err)
}
return &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}, keyBlock, nil
}
func generateSelfSignedCertAndKeyValues(ctx *core.Context, args SelfSignedCertParams) (core.String, *core.Secret, error) {
cert, key, err := GenerateSelfSignedCertAndKey(args)
if err != nil {
return "", nil, err
}
secret, err := core.SECRET_PEM_STRING_PATTERN.NewSecret(ctx, string(pem.EncodeToMemory(key)))
if err != nil {
return "", nil, err
}
return core.String(pem.EncodeToMemory(cert)), secret, nil
}
func GetTLSConfig(ctx *core.Context, pemEncodedCert string, pemEncodedKey string) (*tls.Config, error) {
var zapLogger *zap.Logger
{
zeroLog := ctx.NewChildLoggerForInternalSource(CERT_MAGIG_LOG_SRC)
core := zapcore.NewCore(
zapcore.NewConsoleEncoder(zap.NewProductionEncoderConfig()),
zapcore.AddSync(utils.FnWriter{
WriteFn: func(p []byte) (n int, err error) {
zeroLog.Debug().Msg(utils.BytesAsString(p))
return len(p), nil
},
}),
zap.DebugLevel,
)
zapLogger = zap.New(core)
}
cache := certmagic.NewCache(certmagic.CacheOptions{
GetConfigForCert: func(cert certmagic.Certificate) (*certmagic.Config, error) {
// Customize the config for the certificate
return &certmagic.Config{
Logger: zapLogger,
//TODO: Storage
}, nil
},
Logger: zapLogger,
// ...
})
magic := certmagic.New(cache, certmagic.Config{
Logger: zapLogger,
// Customizations go here
})
// myACME := certmagic.NewACMEIssuer(magic, certmagic.ACMEIssuer{
// CA: certmagic.LetsEncryptProductionCA,
// Email: "you@yours.com",
// Agreed: true,
// // Other customizations
// })
// magic.Issuers = []certmagic.Issuer{myACME}
// err := magic.ManageSync(context.TODO(), []string{"example.com", "sub.example.com"})
// if err != nil {
// return nil, err
// }
cert, err := tls.X509KeyPair([]byte(pemEncodedCert), []byte(pemEncodedKey))
if err != nil {
return nil, err
}
magic.CacheUnmanagedTLSCertificate(ctx, cert, nil)
tlsConfig := magic.TLSConfig()
tlsConfig.NextProtos = append([]string{"h2", "http/1.1"}, tlsConfig.NextProtos...)
return tlsConfig, nil
}