summaryrefslogtreecommitdiff
path: root/certificate_test.go
blob: a2d6e9b95d576033c2a0b1cc9adb2856920d6c68 (plain)
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
package libebics

import (
	"bytes"
	"crypto/rand"
	"crypto/rsa"
	"path/filepath"
	"testing"
)

func TestGenerateRsaKeypair(t *testing.T) {
	privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
	if err != nil {
		t.Error(err.Error())
	}

	err = WriteKeypairToFile(privateKey, filepath.Join("certs", "key.pem"))
	if err != nil {
		t.Error(err.Error())
	}

	loadedPrivateKey, err := ReadKeypairFromFile(filepath.Join("certs", "key.pem"))
	if err != nil {
		t.Error(err.Error())
	}

	if !privateKey.Equal(loadedPrivateKey) {
		t.Error("saved and loaded private keys differ")
	}
}

func TestGenerateSelfSignedCertificate(t *testing.T) {
	privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
	if err != nil {
		t.Error(err.Error())
	}

	certificate, err := GenerateSelfSignedCertificate(X002, *privateKey)
	if err != nil {
		t.Error(err.Error())
	}
	err = WriteCertificateToFile(certificate, filepath.Join("certs", "cert.pem"))
	if err != nil {
		t.Error(err.Error())
	}

	t.Logf("Saved certificate has following fingerprints: %s", GetCertificateFingerprintAsString(certificate))

	loadedCertificate, err := ReadCertificateFromFile(filepath.Join("certs", "cert.pem"), X002)
	if err != nil {
		t.Error(err.Error())
	}

	t.Logf("Loaded certificate has following fingerprints: %s", GetCertificateFingerprintAsString(loadedCertificate))

	if !bytes.Equal(certificate.x509Certificate, loadedCertificate.x509Certificate) {
		t.Error("saved and loaded certificates differ")
	}
}

// also generates keys
func TestGenerateSelfSignedCertificateTriplet(t *testing.T) {
	var filenamePrefix string
	for _, ebicsCertificateType := range []EbicsCertificateType{A005, X002, E002} {
		switch ebicsCertificateType {
		case X002:
			filenamePrefix = "authentication"
		case E002:
			filenamePrefix = "encryption"
		case A005, A006:
			filenamePrefix = "signature"
		}

		privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
		if err != nil {
			t.Error(err.Error())
		}

		err = WriteKeypairToFile(privateKey, filepath.Join("certs", filenamePrefix+"_key.pem"))
		if err != nil {
			t.Error(err.Error())
		}

		certificate, err := GenerateSelfSignedCertificate(ebicsCertificateType, *privateKey)
		if err != nil {
			t.Error(err.Error())
		}

		err = WriteCertificateToFile(certificate, filepath.Join("certs", filenamePrefix+"_cert.pem"))
		if err != nil {
			t.Error(err.Error())
		}
	}
}

func TestPrintCertificateTripletFingerprints(t *testing.T) {
	var filenamePrefix string
	for _, ebicsCertificateType := range []EbicsCertificateType{A005, X002, E002} {
		switch ebicsCertificateType {
		case X002:
			filenamePrefix = "authentication"
		case E002:
			filenamePrefix = "encryption"
		case A005, A006:
			filenamePrefix = "signature"
		}

		loadedCertificate, err := ReadCertificateFromFile(filepath.Join("certs", filenamePrefix+"_cert.pem"), ebicsCertificateType)
		if err != nil {
			t.Error(err.Error())
		}

		t.Logf("Loaded %s certificate has following fingerprints: %s", filenamePrefix, GetCertificateFingerprintAsString(loadedCertificate))

	}
}