-
Notifications
You must be signed in to change notification settings - Fork 9
/
key.go
57 lines (48 loc) · 1.11 KB
/
key.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
package virtualwebauthn
type Key struct {
Type KeyType `json:"type"`
Data []byte `json:"data"`
signingKey
}
func (k *Key) AttestationData() []byte {
k.ensureSigningKey()
return k.signingKey.AttestationData()
}
func (k *Key) Sign(digest []byte) (signature []byte, err error) {
k.ensureSigningKey()
return k.signingKey.Sign(digest)
}
func (k *Key) ensureSigningKey() {
switch k.Type {
case KeyTypeEC2:
k.signingKey = importEC2SigningKey(k.Data)
case KeyTypeRSA:
k.signingKey = importRSASigningKey(k.Data)
default:
panic("invalid key type")
}
}
type KeyType string
const (
KeyTypeEC2 KeyType = "ec2"
KeyTypeRSA KeyType = "rsa"
)
func (keyType KeyType) newKey() *Key {
key := &Key{Type: keyType}
switch keyType {
case KeyTypeEC2:
key.signingKey, key.Data = newEC2SigningKey()
case KeyTypeRSA:
key.signingKey, key.Data = newRSASigningKey()
default:
panic("invalid key type")
}
return key
}
func (keyType KeyType) importKey(keyData []byte) *Key {
return &Key{Type: keyType, Data: keyData}
}
type signingKey interface {
AttestationData() []byte
Sign(digest []byte) (signature []byte, err error)
}