This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
98 lines (84 loc) · 2.5 KB
/
utils.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
/*
* Copyright 2022 The flomesh.io Authors.
*
* 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 utils
import (
"bytes"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"k8s.io/klog/v2"
)
func ConvertPEMCertToX509(pemCrt []byte) (*x509.Certificate, error) {
block, _ := pem.Decode(pemCrt)
if block == nil {
klog.Error("No valid certificate in PEM")
return nil, fmt.Errorf("no valid certificate in PEM")
}
x509Cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
klog.Errorf("failed to convert PEM certificate to x509, %s ", err.Error())
return nil, err
}
return x509Cert, nil
}
func ConvertPEMPrivateKeyToX509(pemKey []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(pemKey)
if block == nil {
klog.Error("No valid private key in PEM")
return nil, fmt.Errorf("no valid private key in PEM")
}
x509Key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
klog.Errorf("failed to convert PEM private key to x509, %s ", err.Error())
return nil, err
}
return x509Key.(*rsa.PrivateKey), nil
}
func CertToPEM(caBytes []byte) ([]byte, error) {
caPEM := new(bytes.Buffer)
if err := pem.Encode(caPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: caBytes,
}); err != nil {
return nil, fmt.Errorf("encode cert: %s", err.Error())
}
return caPEM.Bytes(), nil
}
func RSAKeyToPEM(privateKey *rsa.PrivateKey) ([]byte, error) {
privateBytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return nil, fmt.Errorf("marshal private key: %s", err.Error())
}
keyPEM := new(bytes.Buffer)
if err := pem.Encode(keyPEM, &pem.Block{
Type: "PRIVATE KEY",
Bytes: privateBytes,
}); err != nil {
return nil, fmt.Errorf("encode key: %s", err.Error())
}
return keyPEM.Bytes(), nil
}
func CsrToPEM(csrBytes []byte) ([]byte, error) {
csrPEM := new(bytes.Buffer)
if err := pem.Encode(csrPEM, &(pem.Block{
Type: "CERTIFICATE REQUEST",
Bytes: csrBytes,
})); err != nil {
return nil, fmt.Errorf("encode CSR: %s", err.Error())
}
return csrPEM.Bytes(), nil
}