File: certinfo_test.go

package info (click to toggle)
golang-github-cloudflare-cfssl 1.6.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,220 kB
  • sloc: asm: 1,936; javascript: 652; makefile: 94; sql: 89; sh: 64; python: 11
file content (149 lines) | stat: -rw-r--r-- 3,271 bytes parent folder | download
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
package certinfo

import (
	"bytes"
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"crypto/x509/pkix"
	"encoding/pem"
	"math/big"
	"strings"
	"testing"
	"time"

	"github.com/cloudflare/cfssl/certdb"
	"github.com/cloudflare/cfssl/certdb/sql"
	"github.com/cloudflare/cfssl/certdb/testdb"
)

const (
	sqliteDBFile = "../certdb/testdb/certstore_development.db"
	fakeAKI      = "fake_aki"
	testSerial   = 1337
)

func TestParseSerialNumber(t *testing.T) {
	db := testdb.SQLiteDB(sqliteDBFile)
	accessor := sql.NewAccessor(db)

	certificate, err := createCertificate()
	if err != nil {
		t.Logf("could not create certificate: %s", err.Error())
		t.FailNow()
	}

	err = accessor.InsertCertificate(
		certdb.CertificateRecord{
			Serial: big.NewInt(testSerial).String(),
			AKI:    fakeAKI,
			PEM:    certificate,
		},
	)

	if err != nil {
		t.Log(err.Error())
		t.FailNow()
	}

	cases := []struct {
		description        string
		serial             string
		aki                string
		errorShouldContain string
	}{
		{
			description:        "no certificate found - wrong serial",
			serial:             "1",
			aki:                fakeAKI,
			errorShouldContain: "no certificate found",
		},
		{
			description:        "no certificate found - wrong AKI",
			serial:             "123456789",
			aki:                "1",
			errorShouldContain: "no certificate found",
		},
		{
			description: "certificate found",
			serial:      big.NewInt(testSerial).String(),
			aki:         fakeAKI,
		},
	}

	for _, tc := range cases {
		t.Run(tc.description, func(t *testing.T) {
			cert, err := ParseSerialNumber(tc.serial, tc.aki, accessor)

			if tc.errorShouldContain != "" {
				if cert != nil {
					t.Error("no certificate should be returned if error occurs")
				}

				if err == nil {
					t.Error("err expected to not be nil")

					return
				}

				if !strings.Contains(err.Error(), tc.errorShouldContain) {
					t.Errorf("expected error to contain '%s' but was '%s'", tc.errorShouldContain, err.Error())
				}

				return
			}

			if err != nil {
				t.Errorf("expected error to be nil but got '%s'", err.Error())

				return
			}

			if cert.SerialNumber != tc.serial {
				t.Errorf("returned certificate doesn't match the serial queried for")
			}
		})
	}
}

func createCertificate() (string, error) {
	key, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		return "", err
	}

	cert := &x509.Certificate{
		SerialNumber: big.NewInt(testSerial),
		Subject: pkix.Name{
			Country:      []string{"SE"},
			Organization: []string{"CFSSL Unit Testing"},
		},
		NotBefore:             time.Now(),
		NotAfter:              time.Now().AddDate(1, 0, 0),
		ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
		KeyUsage:              x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
		BasicConstraintsValid: true,
	}

	certificate, err := x509.CreateCertificate(rand.Reader, cert, cert, &key.PublicKey, key)
	if err != nil {
		return "", err
	}

	return certificateToPEMBlock(certificate)
}

func certificateToPEMBlock(cert []byte) (string, error) {
	buf := &bytes.Buffer{}

	err := pem.Encode(buf, &pem.Block{
		Type:  "CERTIFICATE",
		Bytes: cert,
	})

	if err != nil {
		return "", err
	}

	return buf.String(), nil
}