forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificates.go
executable file
·185 lines (146 loc) · 5.72 KB
/
certificates.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
package certificates
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"math/big"
"time"
"github.com/kyma-project/kyma/components/connector-service/internal/apperrors"
)
const Certificate_Validity_Days = 90
type CertificateUtility interface {
LoadCert(encodedData []byte) (crt *x509.Certificate, appError apperrors.AppError)
LoadKey(encodedData []byte) (key *rsa.PrivateKey, appError apperrors.AppError)
LoadCSR(encodedData string) (csr *x509.CertificateRequest, appError apperrors.AppError)
CheckCSRValues(csr *x509.CertificateRequest, subject CSRSubject) apperrors.AppError
SignWithCA(caCrt *x509.Certificate, csr *x509.CertificateRequest, key *rsa.PrivateKey) (crtBase64 string, appError apperrors.AppError)
}
type certificateUtility struct {
}
type CSRSubject struct {
CName string
Country string
Organization string
OrganizationalUnit string
Locality string
Province string
}
func NewCertificateUtility() CertificateUtility {
return &certificateUtility{}
}
func (cu *certificateUtility) decodeBytesFromBase64(bytes []byte) (decodedData []byte, appError apperrors.AppError) {
data := make([]byte, base64.StdEncoding.DecodedLen(len(bytes)))
_, err := base64.StdEncoding.Decode(data, bytes)
if err != nil {
return nil, apperrors.Internal("Error while decoding base64 bytes: %s", err)
}
return data, nil
}
func (cu *certificateUtility) decodeStringFromBase64(bytes string) (decodedData []byte, appError apperrors.AppError) {
data, err := base64.StdEncoding.DecodeString(bytes)
if err != nil {
return nil, apperrors.Internal("Error while decoding base64 string: %s", err)
}
return data, nil
}
func (cu *certificateUtility) encodeStringBase64(bytes []byte) (data string) {
return base64.StdEncoding.EncodeToString(bytes)
}
func (cu *certificateUtility) LoadCert(encodedData []byte) (crt *x509.Certificate, appError apperrors.AppError) {
pemBlock, _ := pem.Decode(encodedData)
if pemBlock == nil {
return nil, apperrors.Internal("Error while decoding pem block")
}
caCRT, err := x509.ParseCertificate(pemBlock.Bytes)
if err != nil {
return nil, apperrors.Internal("Error while parsing certificate: %s", err)
}
return caCRT, nil
}
func (cu *certificateUtility) LoadKey(encodedData []byte) (key *rsa.PrivateKey, appError apperrors.AppError) {
pemBlock, _ := pem.Decode(encodedData)
if pemBlock == nil {
return nil, apperrors.Internal("Error while decoding pem block.")
}
caPrivateKey, err := x509.ParsePKCS8PrivateKey(pemBlock.Bytes)
if err != nil {
return nil, apperrors.Internal("Error while parsing private key: %s", err)
}
return caPrivateKey.(*rsa.PrivateKey), nil
}
func (cu *certificateUtility) LoadCSR(encodedData string) (csr *x509.CertificateRequest, appError apperrors.AppError) {
decodedData, appErr := cu.decodeStringFromBase64(encodedData)
if appErr != nil {
return nil, appErr
}
pemBlock, _ := pem.Decode(decodedData)
if pemBlock == nil {
return nil, apperrors.Internal("Error while decoding pem block.")
}
clientCSR, err := x509.ParseCertificateRequest(pemBlock.Bytes)
if err != nil {
return nil, apperrors.Internal("Error while parsing CSR: %s", err)
}
err = clientCSR.CheckSignature()
if err != nil {
return nil, apperrors.Internal("CSR signature invalid: %s", err)
}
return clientCSR, nil
}
func (cu *certificateUtility) CheckCSRValues(csr *x509.CertificateRequest, subject CSRSubject) apperrors.AppError {
if csr.Subject.CommonName != subject.CName {
return apperrors.Forbidden("CSR: Invalid CName provided.")
}
if csr.Subject.Country == nil {
return apperrors.Forbidden("CSR: No country provided.")
} else if csr.Subject.Country[0] != subject.Country {
return apperrors.Forbidden("CSR: Invalid country provided.")
}
if csr.Subject.Organization == nil {
return apperrors.Forbidden("CSR: No organization provided.")
} else if csr.Subject.Organization[0] != subject.Organization {
return apperrors.Forbidden("CSR: Invalid organization provided.")
}
if csr.Subject.OrganizationalUnit == nil {
return apperrors.Forbidden("CSR: No organizational unit provided.")
} else if csr.Subject.OrganizationalUnit[0] != subject.OrganizationalUnit {
return apperrors.Forbidden("CSR: Invalid organizational unit provided.")
}
if csr.Subject.Locality == nil {
return apperrors.Forbidden("CSR: No locality provided.")
} else if csr.Subject.Locality[0] != subject.Locality {
return apperrors.Forbidden("CSR: Invalid locality provided.")
}
if csr.Subject.Province == nil {
return apperrors.Forbidden("CSR: No province provided.")
} else if csr.Subject.Province[0] != subject.Province {
return apperrors.Forbidden("CSR: Invalid province provided.")
}
return nil
}
func (cu *certificateUtility) SignWithCA(caCrt *x509.Certificate, csr *x509.CertificateRequest, key *rsa.PrivateKey) (
crtBase64 string, appError apperrors.AppError) {
clientCRTTemplate := x509.Certificate{
Signature: csr.Signature,
SignatureAlgorithm: csr.SignatureAlgorithm,
PublicKeyAlgorithm: csr.PublicKeyAlgorithm,
PublicKey: csr.PublicKey,
SerialNumber: big.NewInt(2),
Issuer: caCrt.Subject,
Subject: csr.Subject,
NotBefore: time.Now(),
NotAfter: time.Now().Add(Certificate_Validity_Days * 24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
}
clientCrtRaw, err := x509.CreateCertificate(rand.Reader, &clientCRTTemplate, caCrt, csr.PublicKey, key)
if err != nil {
return "", apperrors.Internal("Error while creating certificate: %s", err)
}
clientCrt := &bytes.Buffer{}
pem.Encode(clientCrt, &pem.Block{Type: "CERTIFICATE", Bytes: clientCrtRaw})
return cu.encodeStringBase64(clientCrt.Bytes()), nil
}