-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcertificate.go
182 lines (155 loc) · 4.39 KB
/
certificate.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
package quark
import (
"bytes"
"errors"
"io"
"github.com/karalef/quark/crypto"
"github.com/karalef/quark/crypto/sign"
"github.com/karalef/quark/pack"
)
// CertID represents a certificate ID.
type CertID = crypto.Fingerprint
// Copier interface.
type Copier[T any] interface {
// Copy returns a copy of the object.
// The returned value must be the same type.
Copy() T
}
// RawData represents a msgpack raw message and implements the Copier interface.
type RawData []byte
func (r RawData) Copy() RawData { return crypto.Copy(r) }
func (r RawData) EncodeMsgpack(enc *pack.Encoder) error {
_, err := enc.Writer().Write(r)
return err
}
func (r *RawData) DecodeMsgpack(dec *pack.Decoder) error {
d, err := dec.DecodeRaw()
if err != nil {
return err
}
*r = RawData(d)
return nil
}
// CertData interface.
type CertData[T Copier[T]] interface {
// CertType returns the certificate type.
CertType() string
Copier[T]
}
// Certifyable represents an object that can be certified.
type Certifyable[T CertData[T]] interface {
CertData[T]
}
// NewCertificate creates a new unsigned certificate.
func NewCertificate[T Certifyable[T]](data T) Certificate[T] {
c := Certificate[T]{
Type: data.CertType(),
Data: data.Copy(),
}
c.ID = c.CalcID()
return c
}
// Certificate contains data with signature.
type Certificate[Type Certifyable[Type]] struct {
ID CertID `msgpack:"id"`
Type string `msgpack:"type"`
Data Type `msgpack:"data"`
Signature Signature `msgpack:"sig"`
}
// RawCertifyable represents a certifyable that holds raw data.
type RawCertifyable struct {
Type string
RawData
}
func (r RawCertifyable) CertType() string { return r.Type }
func (r RawCertifyable) Copy() RawCertifyable {
r.RawData = r.RawData.Copy()
return r
}
// RawCertificate represents a certificate with raw data.
type RawCertificate = Certificate[RawCertifyable]
// Raw returns the certificate with raw data.
func (c Certificate[Type]) Raw() RawCertificate {
b := bytes.NewBuffer(nil)
err := pack.EncodeBinary(b, c.Data)
if err != nil {
panic("unexpected error: " + err.Error())
}
return RawCertificate{
ID: c.ID,
Type: c.Type,
Data: RawCertifyable{Type: c.Type, RawData: b.Bytes()},
Signature: c.Signature.Copy(),
}
}
// Copy returns a copy of the certificate.
func (c Certificate[Type]) Copy() Certificate[Type] {
c.Data = c.Data.Copy()
c.Signature = c.Signature.Copy()
return c
}
// CheckIntegrity validates the certificate integrity without signature verification.
func (c Certificate[Data]) CheckIntegrity() bool {
return c.ID == c.CalcID()
}
// CalcID calculates the certificate ID.
func (c Certificate[Data]) CalcID() CertID {
return crypto.FingerprintFunc(func(w io.Writer) {
w.Write([]byte(c.Type))
if err := pack.EncodeBinary(w, c.Data); err != nil {
panic("unexpected error: " + err.Error())
}
})
}
// SignEncode implements Signable.
func (c Certificate[Data]) SignEncode(w io.Writer) error {
w.Write(c.ID[:])
w.Write([]byte(c.Type))
err := pack.EncodeBinary(w, c.Data)
if err != nil {
panic("unexpected error: " + err.Error())
}
return nil
}
// Sign signs the certificate.
func (c *Certificate[Data]) Sign(sk sign.PrivateKey, v Validity) error {
sig, err := SignObject(sk, v, c)
if err != nil {
return err
}
c.Signature = sig
return nil
}
// Verify verifies the certificate signature.
func (c Certificate[Data]) Verify(pk sign.PublicKey) (bool, error) {
return c.Signature.VerifyObject(pk, c)
}
// Validity returns the validity of the certificate.
func (c Certificate[Data]) Validity() Validity { return c.Signature.Validity }
// Validate validates the certificate.
func (c Certificate[Data]) Validate() error {
if c.ID.IsEmpty() || c.Type == "" || !c.CheckIntegrity() {
return ErrCertMalformed
}
if t := c.Data.CertType(); t != "" && c.Type != t {
return ErrWrongCertType
}
return nil
}
// CertificateAs converts a raw certificate to a typed certificate.
func CertificateAs[T Certifyable[T]](cert RawCertificate) (Certificate[T], error) {
constrained := Certificate[T]{
ID: cert.ID,
Type: cert.Type,
Signature: cert.Signature.Copy(),
}
err := pack.DecodeBinary(bytes.NewReader(cert.Data.RawData), &constrained.Data)
if err != nil {
return Certificate[T]{}, err
}
return constrained, constrained.Validate()
}
var (
ErrWrongCertType = errors.New("wrong certificate type")
ErrCertMalformed = errors.New("malformed certificate")
)