-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
certificate.go
74 lines (63 loc) · 2.02 KB
/
certificate.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
package security
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"encoding/pem"
"fmt"
"math/big"
"time"
)
type pkcs1PublicKey struct {
N *big.Int
E int
}
type SelfSignedCertificate struct {
}
func NewRsaSelfSignedCertificate(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) *x509.Certificate {
template := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
Organization: []string{"Acme Co"},
},
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour * 24 * 180),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
template2 := x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
Organization: []string{"Acme C1o"},
},
NotBefore: time.Now(),
NotAfter: time.Now().Add(time.Hour * 24 * 260),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey, privateKey)
derBytes1, err := x509.CreateCertificate(rand.Reader, &template2, &template2, publicKey, privateKey)
if err != nil {
logger.Error("There was an error generating the self signed certificate: %v", err.Error())
}
out := &bytes.Buffer{}
pem.Encode(out, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
certTemplate1Fingerprint := sha256.Sum256(derBytes)
certTemplate2Fingerprint := sha256.Sum256(derBytes1)
str1 := base64.StdEncoding.EncodeToString(certTemplate1Fingerprint[:])
str2 := base64.StdEncoding.EncodeToString(certTemplate2Fingerprint[:])
println(str1)
println(str2)
if err != nil {
logger.Error("There was an error generating the self signed certificate: %v", err.Error())
}
fmt.Println(out.String())
cert, err := x509.ParseCertificate(derBytes)
return cert
}