-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_rsa.go
102 lines (80 loc) · 2.31 KB
/
key_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
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
// SPDX-FileCopyrightText: 2023-2024 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package openpgp
import (
"crypto"
"crypto/rsa"
"crypto/sha1" //nolint:gosec
"encoding/binary"
"fmt"
"io"
"math/big"
"time"
"cunicu.li/go-iso7816/encoding/tlv"
)
var (
_ crypto.Signer = (*PrivateKeyRSA)(nil)
_ crypto.Decrypter = (*PrivateKeyRSA)(nil)
)
//nolint:unused
type rsaPublicKey struct {
*rsa.PublicKey
private *PrivateKeyRSA
}
type PrivateKeyRSA struct {
card *Card
lenModulus int
key KeyRef
public *rsa.PublicKey
}
func (k *PrivateKeyRSA) Public() crypto.PublicKey {
return k.public
}
func (k *PrivateKeyRSA) Bits() int {
return k.lenModulus
}
// See: OpenPGP Smart Card Application - Section 7.2.10 PSO: COMPUTE DIGITAL SIGNATURE
func (k *PrivateKeyRSA) Sign(_ io.Reader, _ /*digest*/ []byte, _ /*opts*/ crypto.SignerOpts) (signature []byte, err error) {
return nil, ErrUnsupported
}
// See: OpenPGP Smart Card Application - Section 7.2.11 PSO: DECIPHER
func (k *PrivateKeyRSA) Decrypt(_ io.Reader, _ /*msg*/ []byte, _ /*opts*/ crypto.DecrypterOpts) (plaintext []byte, err error) {
return nil, ErrUnsupported
}
func (k PrivateKeyRSA) fingerprint(creationTime time.Time) []byte {
buf := []byte{
0x99, // Prefix
0, 0, // Packet length
0x04, // Version
0, 0, 0, 0, // Creation timestamp
byte(AlgPubkeyRSA),
}
buf = appendMPI(buf, k.public.N)
buf = appendMPI(buf, big.NewInt(int64(k.public.E)))
binary.BigEndian.PutUint16(buf[1:], uint16(len(buf)-3)) // Fill in packet length
binary.BigEndian.PutUint32(buf[4:], uint32(creationTime.Unix())) // Fill in creation timestamp
digest := sha1.New() // nolint:gosec
digest.Write(buf)
return digest.Sum(nil)
}
func decodePublicRSA(tvs tlv.TagValues) (*rsa.PublicKey, error) {
_, tvs, ok := tvs.Get(tagPublicKey)
if !ok {
return nil, fmt.Errorf("%w: public key", errMissingTag)
}
mod, _, ok := tvs.Get(tagModulus)
if !ok {
return nil, fmt.Errorf("%w modulus", errUnmarshal)
}
exp, _, ok := tvs.Get(tagExponent)
if !ok {
return nil, fmt.Errorf("%w exponent", errUnmarshal)
}
var n, e big.Int
n.SetBytes(mod)
e.SetBytes(exp)
if !e.IsInt64() {
return nil, fmt.Errorf("%w: returned exponent too large: %s", ErrInvalidLength, e.String())
}
return &rsa.PublicKey{N: &n, E: int(e.Int64())}, nil
}