summaryrefslogtreecommitdiff
path: root/certificate.go
blob: 52bbb5c7ea8e9aa59227fd51ec0d89e2b7663665 (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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package libebics

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/sha256"
	"crypto/x509"
	"crypto/x509/pkix"
	"encoding/pem"
	"fmt"
	"os"
	"time"
)

type EbicsCertificateType int

const (
	A005 EbicsCertificateType = iota
	A006
	X002
	E002
)

func (t EbicsCertificateType) String() string {
	switch t {
	case A005:
		return "A005"
	case A006:
		return "A006"
	case X002:
		return "X002"
	case E002:
		return "E002"
	default:
		panic("invalid EbicsCertificateType")
	}
}

type EbicsCertificate struct {
	x509Certificate      []byte
	ebicsCertificateType EbicsCertificateType
}

func GenerateSelfSignedCertificate(ebicsCertificateType EbicsCertificateType, privateKey rsa.PrivateKey) (*EbicsCertificate, error) {
	var keyUsage x509.KeyUsage

	switch ebicsCertificateType {
	case X002:
		keyUsage = x509.KeyUsageDigitalSignature
	case E002:
		keyUsage = x509.KeyUsageKeyEncipherment
	case A005, A006:
		keyUsage = x509.KeyUsageContentCommitment
	default:
		return nil, fmt.Errorf("certificate type not implemented")
	}

	template := x509.Certificate{
		Subject: pkix.Name{
			CommonName: "SCHOLZ01",
		},
		NotBefore: time.Now(),
		NotAfter:  time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC),
		KeyUsage:  keyUsage,
	}

	x509Certificate, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, &privateKey)
	if err != nil {
		return nil, err
	}

	return &EbicsCertificate{
		x509Certificate:      x509Certificate,
		ebicsCertificateType: ebicsCertificateType,
	}, nil
}

func GetCertificateFingerprintAsString(ebicsCertificate *EbicsCertificate) string {
	return fmt.Sprintf("% x", sha256.Sum256(ebicsCertificate.x509Certificate))
}

func WriteCertificateToFile(ebicsCertificate *EbicsCertificate, path string) error {
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()

	return pem.Encode(f, &pem.Block{
		Type:  "CERTIFICATE",
		Bytes: ebicsCertificate.x509Certificate,
	})
}

func ReadCertificateFromFile(path string, ebicsCertificateType EbicsCertificateType) (*EbicsCertificate, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		return nil, err
	}

	block, _ := pem.Decode(data)
	if block == nil {
		return nil, fmt.Errorf("no PEM data found")
	}
	if block.Type != "CERTIFICATE" {
		return nil, fmt.Errorf("unexpected PEM type: %s", block.Type)
	}

	cert, err := x509.ParseCertificate(block.Bytes)
	if err != nil {
		return nil, err
	}

	var keyUsage x509.KeyUsage
	switch ebicsCertificateType {
	case X002:
		keyUsage = x509.KeyUsageDigitalSignature
	case E002:
		keyUsage = x509.KeyUsageKeyEncipherment
	case A005, A006:
		keyUsage = x509.KeyUsageContentCommitment
	}

	if cert.KeyUsage != keyUsage {
		return nil, fmt.Errorf("expected KeyUsage %s does not match KeyUsage in loaded Certificate (%s)", keyUsage, cert.KeyUsage)
	}
	return &EbicsCertificate{
		x509Certificate:      cert.Raw,
		ebicsCertificateType: ebicsCertificateType,
	}, nil
}

func WriteKeypairToFile(privateKey *rsa.PrivateKey, path string) error {
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()

	der, err := x509.MarshalPKCS8PrivateKey(privateKey)

	return pem.Encode(f, &pem.Block{
		Type:  "PRIVATE KEY",
		Bytes: der,
	})
}

func ReadKeypairFromFile(path string) (*rsa.PrivateKey, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		return nil, err
	}

	block, _ := pem.Decode(data)
	if block == nil {
		return nil, fmt.Errorf("no PEM data found")
	}
	if block.Type != "PRIVATE KEY" {
		return nil, fmt.Errorf("unexpected PEM type: %s", block.Type)
	}

	privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
	switch privateKey := privateKey.(type) {
	case *rsa.PrivateKey:
		return privateKey, nil
	default:
		return nil, fmt.Errorf("private key type not supported")
	}
}