-
Notifications
You must be signed in to change notification settings - Fork 13
/
asntest.go
93 lines (84 loc) · 2.31 KB
/
asntest.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
// Package for helper function in the test suite
package asntest
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"log"
"math/big"
"os"
"os/exec"
"path"
"testing"
)
// Init a basic set of certs
func InitCert() (*x509.Certificate, *rsa.PrivateKey) {
serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
if err != nil {
log.Fatalf("Failed to generate serial number: %v", err)
}
c := x509.Certificate{
SerialNumber: serialNumber,
PublicKeyAlgorithm: x509.RSA,
SignatureAlgorithm: x509.SHA256WithRSA,
Subject: pkix.Name{
Country: []string{"TEST STRING"},
},
}
priv, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
log.Fatal(err)
}
derBytes, _ := x509.CreateCertificate(rand.Reader, &c, &c, &priv.PublicKey, priv)
cert, _ := x509.ParseCertificate(derBytes)
return cert, priv
}
func CertToBytes(cert *x509.Certificate) []byte {
certOut := new(bytes.Buffer)
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}); err != nil {
log.Fatalf("Failed to write data to cert.pem: %v", err)
}
return certOut.Bytes()
}
func RSAToBytes(r *rsa.PrivateKey) []byte {
privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(r)
if err != nil {
log.Fatalf("Unable to marshal private key: %v", err)
}
keyOut := new(bytes.Buffer)
if err := pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: privateKeyBytes}); err != nil {
log.Fatalf("Failed to write data to key.pem: %v", err)
}
return keyOut.Bytes()
}
func Asn1Parse(t *testing.T, buf []byte) []byte {
f := path.Join(t.TempDir(), "asn1.der")
os.WriteFile(f, buf, 0644)
out, err := exec.Command("openssl", "asn1parse", "-in", f, "-inform", "der", "-dump", "-i").Output()
if err != nil {
fmt.Println(string(out))
log.Fatal(err)
}
return out
}
func DiffText(t *testing.T, a, b []byte) {
aFile := path.Join(t.TempDir(), "a.der")
bFile := path.Join(t.TempDir(), "b.der")
os.WriteFile(aFile, a, 0644)
os.WriteFile(bFile, b, 0644)
out, _ := exec.Command("git", "diff", "--color=always", "--no-index", "--", aFile, bFile).Output()
if len(out) > 0 {
fmt.Println(string(a))
fmt.Println(string(b))
fmt.Println(string(out))
}
}
func Asn1Compare(t *testing.T, a, b []byte) {
a = Asn1Parse(t, a)
b = Asn1Parse(t, b)
DiffText(t, a, b)
}