-
Notifications
You must be signed in to change notification settings - Fork 9
/
rsa.go
69 lines (60 loc) · 1.86 KB
/
rsa.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
package virtualwebauthn
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
)
const (
rsaSize = 2048 // https://datatracker.ietf.org/doc/html/rfc8812#section-2
rsaType = 3 // https://datatracker.ietf.org/doc/html/rfc8230#section-4
rsaSHA256Algo = -257 // https://datatracker.ietf.org/doc/html/rfc8812#section-2
)
type rsaSigningKey struct {
privateKey *rsa.PrivateKey
attestationData []byte
}
func newRSASigningKey() (*rsaSigningKey, []byte) {
privateKey, err := rsa.GenerateKey(rand.Reader, rsaSize)
if err != nil {
panic("failed to generate private key")
}
keyData, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
panic("failed to export generated private key")
}
return newRSASigningKeyWithPrivateKey(privateKey), keyData
}
func importRSASigningKey(keyData []byte) *rsaSigningKey {
parsed, err := x509.ParsePKCS8PrivateKey(keyData)
if err != nil {
panic("failed to parse PKCS8 RSA private Key")
}
privateKey, ok := parsed.(*rsa.PrivateKey)
if !ok {
panic("expected RSA key in imported data")
}
return newRSASigningKeyWithPrivateKey(privateKey)
}
func newRSASigningKeyWithPrivateKey(privateKey *rsa.PrivateKey) *rsaSigningKey {
info := rsaKeyInfo{
Type: rsaType,
Algorithm: rsaSHA256Algo,
Modulus: privateKey.N.Bytes(),
Exponent: bigEndianBytes(privateKey.E, 3),
}
attestationData := marshalCbor(info)
return &rsaSigningKey{privateKey: privateKey, attestationData: attestationData}
}
func (k *rsaSigningKey) AttestationData() []byte {
return k.attestationData
}
func (k *rsaSigningKey) Sign(digest []byte) (signature []byte, err error) {
return rsa.SignPKCS1v15(rand.Reader, k.privateKey, crypto.SHA256, digest)
}
type rsaKeyInfo struct {
Type int64 `cbor:"1,keyasint"`
Algorithm int64 `cbor:"3,keyasint"`
Modulus []byte `cbor:"-1,keyasint"`
Exponent []byte `cbor:"-2,keyasint"`
}