-
Notifications
You must be signed in to change notification settings - Fork 1
/
certificates.go
55 lines (49 loc) · 1.39 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
package digicerttest
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"github.com/marqeta/go-digicert/digicert"
)
func NewTestCertificate(common_name string) (*digicert.Certificate, error) {
dcert := &digicert.Certificate{}
template := &x509.CertificateRequest{}
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, err
}
csr, err := x509.CreateCertificateRequest(rand.Reader, template, key)
if err != nil {
return nil, err
}
csr_text := pem.EncodeToMemory(
&pem.Block{
Type: "CERTIFICATE REQUEST",
Bytes: csr,
},
)
dcert.CSR = fmt.Sprintf("%s", csr_text)
dcert.CommonName = common_name
dcert.ServerPlatform.ID = -1
dcert.DNSNames = []string{common_name}
dcert.SignatureHash = "sha256"
dcert.OrganizationUnits = []string{"Certificate Factory Test Org Inc LLC"}
return dcert, nil
}
func NewChainBundle(chain_length int) (*digicert.PEMBundle, error) {
if chain_length <= 0 {
return nil, errors.New("chain_length must be a positive integer value")
}
bundle := &digicert.PEMBundle{PEMs: make([]*digicert.PEM, chain_length)}
for i, _ := range bundle.PEMs {
bundle.PEMs[i] = newPEM("test")
}
return bundle, nil
}
func newPEM(subject_common_name string) *digicert.PEM {
// NOTE in the future we can make these attributes more useful, e.g. set a real PEM value
return &digicert.PEM{SubjectCommonName: subject_common_name}
}