| 1 |
package domain |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/rand" |
| 5 |
"crypto/rsa" |
| 6 |
"math/big" |
| 7 |
) |
| 8 |
|
| 9 |
// Key provides an interface to an encryption/signing key |
| 10 |
type Key interface { |
| 11 |
// KeyName returns the KeyName |
| 12 |
KeyName() KeyName |
| 13 |
// SetKeyNumber sets the key number in the KeyName |
| 14 |
SetKeyNumber(number int) |
| 15 |
// SetKeyVersion sets the key version in the KeyName |
| 16 |
SetKeyVersion(version int) |
| 17 |
// Sign signs message |
| 18 |
Sign(message []byte) (signature []byte, err error) |
| 19 |
// Encrypt encrypts message |
| 20 |
Encrypt(message []byte) (encrypted []byte, err error) |
| 21 |
// CanSign returns true if the key can be used for signing |
| 22 |
CanSign() bool |
| 23 |
// CanEncrypt returns true if the key can be used for encryption |
| 24 |
CanEncrypt() bool |
| 25 |
} |
| 26 |
|
| 27 |
const ( |
| 28 |
initialKeyNumber = 999 |
| 29 |
initialKeyVersion = 999 |
| 30 |
KeyTypeSigning = KeyType("S") |
| 31 |
KeyTypeEncryption = KeyType("V") |
| 32 |
) |
| 33 |
|
| 34 |
type KeyType string |
| 35 |
|
| 36 |
func (k KeyType) String() string { return string(k) } |
| 37 |
|
| 38 |
// NewPinTanKeyName returns a new KeyName for the pin/tan flow |
| 39 |
func NewPinTanKeyName(bankID BankID, userID string, keyType KeyType) *KeyName { |
| 40 |
return &KeyName{ |
| 41 |
BankID: bankID, |
| 42 |
UserID: userID, |
| 43 |
KeyType: keyType, |
| 44 |
KeyNumber: 0, |
| 45 |
KeyVersion: 0, |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// NewInitialKeyName represents a KeyName ready to use for initial communication |
| 50 |
func NewInitialKeyName(countryCode int, bankID, userID string, keyType KeyType) *KeyName { |
| 51 |
return &KeyName{ |
| 52 |
BankID: BankID{CountryCode: countryCode, ID: bankID}, |
| 53 |
UserID: userID, |
| 54 |
KeyType: keyType, |
| 55 |
KeyNumber: initialKeyNumber, |
| 56 |
KeyVersion: initialKeyVersion, |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// KeyName provides data about a given key |
| 61 |
type KeyName struct { |
| 62 |
BankID BankID |
| 63 |
UserID string |
| 64 |
KeyType KeyType |
| 65 |
KeyNumber int |
| 66 |
KeyVersion int |
| 67 |
} |
| 68 |
|
| 69 |
// IsInitial returns true if the KeyName represents an initial KeyName, false otherwise |
| 70 |
func (k *KeyName) IsInitial() bool { |
| 71 |
return k.KeyNumber == initialKeyNumber && k.KeyVersion == initialKeyVersion |
| 72 |
} |
| 73 |
|
| 74 |
// SetInitial resets the KeyName to reflect an initial KeyName |
| 75 |
func (k *KeyName) SetInitial() { |
| 76 |
k.KeyNumber = initialKeyNumber |
| 77 |
k.KeyVersion = initialKeyVersion |
| 78 |
} |
| 79 |
|
| 80 |
// NewPinKey returns a new PinKey |
| 81 |
func NewPinKey(pin string, keyName *KeyName) *PinKey { |
| 82 |
return &PinKey{pin: pin, keyName: keyName} |
| 83 |
} |
| 84 |
|
| 85 |
// PinKey represents a Key used for pin/tan flow |
| 86 |
type PinKey struct { |
| 87 |
pin string |
| 88 |
keyName *KeyName |
| 89 |
} |
| 90 |
|
| 91 |
// KeyName returns the KeyName |
| 92 |
func (p *PinKey) KeyName() KeyName { |
| 93 |
return *p.keyName |
| 94 |
} |
| 95 |
|
| 96 |
// SetKeyNumber sets the key number in the KeyName |
| 97 |
func (p *PinKey) SetKeyNumber(number int) { |
| 98 |
p.keyName.KeyNumber = number |
| 99 |
} |
| 100 |
|
| 101 |
// SetKeyVersion sets the key version in the KeyName |
| 102 |
func (p *PinKey) SetKeyVersion(version int) { |
| 103 |
p.keyName.KeyVersion = version |
| 104 |
} |
| 105 |
|
| 106 |
// CanSign returns true if the key can be used for signing |
| 107 |
func (p *PinKey) CanSign() bool { |
| 108 |
return true |
| 109 |
} |
| 110 |
|
| 111 |
// CanEncrypt returns true if the key can be used for encryption |
| 112 |
func (p *PinKey) CanEncrypt() bool { |
| 113 |
return true |
| 114 |
} |
| 115 |
|
| 116 |
// Pin returns the pin within this key |
| 117 |
func (p *PinKey) Pin() string { |
| 118 |
return p.pin |
| 119 |
} |
| 120 |
|
| 121 |
// Sign signs message |
| 122 |
func (p *PinKey) Sign(message []byte) ([]byte, error) { |
| 123 |
return []byte(p.pin), nil |
| 124 |
} |
| 125 |
|
| 126 |
// Encrypt encryptes the message |
| 127 |
func (p *PinKey) Encrypt(message []byte) ([]byte, error) { |
| 128 |
encMessage := make([]byte, len(message)) |
| 129 |
// Make a deep copy, just in case |
| 130 |
copy(encMessage, message) |
| 131 |
return encMessage, nil |
| 132 |
} |
| 133 |
|
| 134 |
// Decrypt decrypts the encryptedMessage |
| 135 |
func (p *PinKey) Decrypt(encryptedMessage []byte) ([]byte, error) { |
| 136 |
decMessage := make([]byte, len(encryptedMessage)) |
| 137 |
// Make a deep copy, just in case |
| 138 |
copy(decMessage, encryptedMessage) |
| 139 |
return decMessage, nil |
| 140 |
} |
| 141 |
|
| 142 |
// GenerateSigningKey generates a new signing key |
| 143 |
func GenerateSigningKey() (*PublicKey, error) { |
| 144 |
rsaKey, err := rsa.GenerateKey(rand.Reader, 768) |
| 145 |
if err != nil { |
| 146 |
return nil, err |
| 147 |
} |
| 148 |
p := PublicKey{ |
| 149 |
Type: "S", |
| 150 |
Modulus: rsaKey.N.Bytes(), |
| 151 |
Exponent: big.NewInt(int64(rsaKey.E)).Bytes(), |
| 152 |
rsaPrivateKey: rsaKey, |
| 153 |
} |
| 154 |
return &p, nil |
| 155 |
} |
| 156 |
|
| 157 |
// NewRSAKey returns a new RSA key |
| 158 |
func NewRSAKey(pubKey *PublicKey, keyName *KeyName) *RSAKey { |
| 159 |
return &RSAKey{PublicKey: pubKey, keyName: keyName} |
| 160 |
} |
| 161 |
|
| 162 |
// RSAKey represents a public RSA key which implements the Key interface |
| 163 |
type RSAKey struct { |
| 164 |
*PublicKey |
| 165 |
keyName *KeyName |
| 166 |
} |
| 167 |
|
| 168 |
// KeyName returns the KeyName |
| 169 |
func (r *RSAKey) KeyName() KeyName { |
| 170 |
return *r.keyName |
| 171 |
} |
| 172 |
|
| 173 |
// SetKeyNumber sets the key number in the KeyName |
| 174 |
func (r *RSAKey) SetKeyNumber(number int) { |
| 175 |
r.keyName.KeyNumber = number |
| 176 |
} |
| 177 |
|
| 178 |
// SetKeyVersion sets the key version in the KeyName |
| 179 |
func (r *RSAKey) SetKeyVersion(version int) { |
| 180 |
r.keyName.KeyVersion = version |
| 181 |
} |
| 182 |
|
| 183 |
// CanSign returns true if the key can be used for signing |
| 184 |
func (r *RSAKey) CanSign() bool { |
| 185 |
return r.PublicKey.rsaPrivateKey != nil |
| 186 |
} |
| 187 |
|
| 188 |
// CanEncrypt returns true if the key can be used for encryption |
| 189 |
func (r *RSAKey) CanEncrypt() bool { |
| 190 |
return r.PublicKey.rsaPublicKey != nil |
| 191 |
} |
| 192 |
|
| 193 |
// NewEncryptionKey creates a new RSA encryption key |
| 194 |
func NewEncryptionKey(modulus, exponent []byte) *PublicKey { |
| 195 |
p := &PublicKey{ |
| 196 |
Type: "V", |
| 197 |
} |
| 198 |
copy(p.Modulus, modulus) |
| 199 |
copy(p.Exponent, exponent) |
| 200 |
mod := new(big.Int).SetBytes(modulus) |
| 201 |
exp := new(big.Int).SetBytes(exponent) |
| 202 |
pubKey := rsa.PublicKey{ |
| 203 |
N: mod, |
| 204 |
E: int(exp.Int64()), |
| 205 |
} |
| 206 |
p.rsaPublicKey = &pubKey |
| 207 |
return p |
| 208 |
} |
| 209 |
|
| 210 |
// PublicKey represents a key which can either embed a private or public RSA key |
| 211 |
type PublicKey struct { |
| 212 |
Type string |
| 213 |
Modulus []byte |
| 214 |
Exponent []byte |
| 215 |
rsaPrivateKey *rsa.PrivateKey |
| 216 |
rsaPublicKey *rsa.PublicKey |
| 217 |
} |
| 218 |
|
| 219 |
// SigningKey returns the RSA private key to sign with, or nil when not set |
| 220 |
func (p *PublicKey) SigningKey() *rsa.PrivateKey { |
| 221 |
return p.rsaPrivateKey |
| 222 |
} |
| 223 |
|
| 224 |
// Sign signs message with the private key |
| 225 |
func (p *PublicKey) Sign(message []byte) ([]byte, error) { |
| 226 |
return rsa.SignPKCS1v15(rand.Reader, p.rsaPrivateKey, 0, message) |
| 227 |
} |
| 228 |
|
| 229 |
// Encrypt encryptes the message with the public key |
| 230 |
func (p *PublicKey) Encrypt(message []byte) ([]byte, error) { |
| 231 |
return rsa.EncryptPKCS1v15(rand.Reader, p.rsaPublicKey, message) |
| 232 |
} |