| 1 |
package cert |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/aes" |
| 5 |
"crypto/cipher" |
| 6 |
"crypto/ed25519" |
| 7 |
"crypto/rand" |
| 8 |
"encoding/pem" |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"math" |
| 12 |
|
| 13 |
"golang.org/x/crypto/argon2" |
| 14 |
"google.golang.org/protobuf/proto" |
| 15 |
) |
| 16 |
|
| 17 |
type NebulaEncryptedData struct { |
| 18 |
EncryptionMetadata NebulaEncryptionMetadata |
| 19 |
Ciphertext []byte |
| 20 |
} |
| 21 |
|
| 22 |
type NebulaEncryptionMetadata struct { |
| 23 |
EncryptionAlgorithm string |
| 24 |
Argon2Parameters Argon2Parameters |
| 25 |
} |
| 26 |
|
| 27 |
// Argon2Parameters KDF factors |
| 28 |
type Argon2Parameters struct { |
| 29 |
version rune |
| 30 |
Memory uint32 // KiB |
| 31 |
Parallelism uint8 |
| 32 |
Iterations uint32 |
| 33 |
salt []byte |
| 34 |
} |
| 35 |
|
| 36 |
// NewArgon2Parameters Returns a new Argon2Parameters object with current version set |
| 37 |
func NewArgon2Parameters(memory uint32, parallelism uint8, iterations uint32) *Argon2Parameters { |
| 38 |
return &Argon2Parameters{ |
| 39 |
version: argon2.Version, |
| 40 |
Memory: memory, // KiB |
| 41 |
Parallelism: parallelism, |
| 42 |
Iterations: iterations, |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// Encrypts data using AES-256-GCM and the Argon2id key derivation function |
| 47 |
func aes256Encrypt(passphrase []byte, kdfParams *Argon2Parameters, data []byte) ([]byte, error) { |
| 48 |
key, err := aes256DeriveKey(passphrase, kdfParams) |
| 49 |
if err != nil { |
| 50 |
return nil, err |
| 51 |
} |
| 52 |
|
| 53 |
// this should never happen, but since this dictates how our calls into the |
| 54 |
// aes package behave and could be catastraphic, let's sanity check this |
| 55 |
if len(key) != 32 { |
| 56 |
return nil, fmt.Errorf("invalid AES-256 key length (%d) - cowardly refusing to encrypt", len(key)) |
| 57 |
} |
| 58 |
|
| 59 |
block, err := aes.NewCipher(key) |
| 60 |
if err != nil { |
| 61 |
return nil, err |
| 62 |
} |
| 63 |
|
| 64 |
gcm, err := cipher.NewGCM(block) |
| 65 |
if err != nil { |
| 66 |
return nil, err |
| 67 |
} |
| 68 |
|
| 69 |
nonce := make([]byte, gcm.NonceSize()) |
| 70 |
if _, err := io.ReadFull(rand.Reader, nonce); err != nil { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
|
| 74 |
ciphertext := gcm.Seal(nil, nonce, data, nil) |
| 75 |
blob := joinNonceCiphertext(nonce, ciphertext) |
| 76 |
|
| 77 |
return blob, nil |
| 78 |
} |
| 79 |
|
| 80 |
// Decrypts data using AES-256-GCM and the Argon2id key derivation function |
| 81 |
// Expects the data to include an Argon2id parameter string before the encrypted data |
| 82 |
func aes256Decrypt(passphrase []byte, kdfParams *Argon2Parameters, data []byte) ([]byte, error) { |
| 83 |
key, err := aes256DeriveKey(passphrase, kdfParams) |
| 84 |
if err != nil { |
| 85 |
return nil, err |
| 86 |
} |
| 87 |
|
| 88 |
block, err := aes.NewCipher(key) |
| 89 |
if err != nil { |
| 90 |
return nil, err |
| 91 |
} |
| 92 |
|
| 93 |
gcm, err := cipher.NewGCM(block) |
| 94 |
if err != nil { |
| 95 |
return nil, err |
| 96 |
} |
| 97 |
|
| 98 |
nonce, ciphertext, err := splitNonceCiphertext(data, gcm.NonceSize()) |
| 99 |
if err != nil { |
| 100 |
return nil, err |
| 101 |
} |
| 102 |
|
| 103 |
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil) |
| 104 |
if err != nil { |
| 105 |
return nil, fmt.Errorf("invalid passphrase or corrupt private key") |
| 106 |
} |
| 107 |
|
| 108 |
return plaintext, nil |
| 109 |
} |
| 110 |
|
| 111 |
func aes256DeriveKey(passphrase []byte, params *Argon2Parameters) ([]byte, error) { |
| 112 |
if params.salt == nil { |
| 113 |
params.salt = make([]byte, 32) |
| 114 |
if _, err := rand.Read(params.salt); err != nil { |
| 115 |
return nil, err |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// keySize of 32 bytes will result in AES-256 encryption |
| 120 |
key, err := deriveKey(passphrase, 32, params) |
| 121 |
if err != nil { |
| 122 |
return nil, err |
| 123 |
} |
| 124 |
|
| 125 |
return key, nil |
| 126 |
} |
| 127 |
|
| 128 |
// Derives a key from a passphrase using Argon2id |
| 129 |
func deriveKey(passphrase []byte, keySize uint32, params *Argon2Parameters) ([]byte, error) { |
| 130 |
if params.version != argon2.Version { |
| 131 |
return nil, fmt.Errorf("incompatible Argon2 version: %d", params.version) |
| 132 |
} |
| 133 |
|
| 134 |
if params.salt == nil { |
| 135 |
return nil, fmt.Errorf("salt must be set in argon2Parameters") |
| 136 |
} else if len(params.salt) < 16 { |
| 137 |
return nil, fmt.Errorf("salt must be at least 128 bits") |
| 138 |
} |
| 139 |
|
| 140 |
key := argon2.IDKey(passphrase, params.salt, params.Iterations, params.Memory, params.Parallelism, keySize) |
| 141 |
|
| 142 |
return key, nil |
| 143 |
} |
| 144 |
|
| 145 |
// Prepends nonce to ciphertext |
| 146 |
func joinNonceCiphertext(nonce []byte, ciphertext []byte) []byte { |
| 147 |
return append(nonce, ciphertext...) |
| 148 |
} |
| 149 |
|
| 150 |
// Splits nonce from ciphertext |
| 151 |
func splitNonceCiphertext(blob []byte, nonceSize int) ([]byte, []byte, error) { |
| 152 |
if len(blob) <= nonceSize { |
| 153 |
return nil, nil, fmt.Errorf("invalid ciphertext blob - blob shorter than nonce length") |
| 154 |
} |
| 155 |
|
| 156 |
return blob[:nonceSize], blob[nonceSize:], nil |
| 157 |
} |
| 158 |
|
| 159 |
// EncryptAndMarshalSigningPrivateKey is a simple helper to encrypt and PEM encode a private key |
| 160 |
func EncryptAndMarshalSigningPrivateKey(curve Curve, b []byte, passphrase []byte, kdfParams *Argon2Parameters) ([]byte, error) { |
| 161 |
ciphertext, err := aes256Encrypt(passphrase, kdfParams, b) |
| 162 |
if err != nil { |
| 163 |
return nil, err |
| 164 |
} |
| 165 |
|
| 166 |
b, err = proto.Marshal(&RawNebulaEncryptedData{ |
| 167 |
EncryptionMetadata: &RawNebulaEncryptionMetadata{ |
| 168 |
EncryptionAlgorithm: "AES-256-GCM", |
| 169 |
Argon2Parameters: &RawNebulaArgon2Parameters{ |
| 170 |
Version: kdfParams.version, |
| 171 |
Memory: kdfParams.Memory, |
| 172 |
Parallelism: uint32(kdfParams.Parallelism), |
| 173 |
Iterations: kdfParams.Iterations, |
| 174 |
Salt: kdfParams.salt, |
| 175 |
}, |
| 176 |
}, |
| 177 |
Ciphertext: ciphertext, |
| 178 |
}) |
| 179 |
if err != nil { |
| 180 |
return nil, err |
| 181 |
} |
| 182 |
|
| 183 |
switch curve { |
| 184 |
case Curve_CURVE25519: |
| 185 |
return pem.EncodeToMemory(&pem.Block{Type: EncryptedEd25519PrivateKeyBanner, Bytes: b}), nil |
| 186 |
case Curve_P256: |
| 187 |
return pem.EncodeToMemory(&pem.Block{Type: EncryptedECDSAP256PrivateKeyBanner, Bytes: b}), nil |
| 188 |
default: |
| 189 |
return nil, fmt.Errorf("invalid curve: %v", curve) |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
// UnmarshalNebulaEncryptedData will unmarshal a protobuf byte representation of a nebula cert into its |
| 194 |
// protobuf-generated struct. |
| 195 |
func UnmarshalNebulaEncryptedData(b []byte) (*NebulaEncryptedData, error) { |
| 196 |
if len(b) == 0 { |
| 197 |
return nil, fmt.Errorf("nil byte array") |
| 198 |
} |
| 199 |
var rned RawNebulaEncryptedData |
| 200 |
err := proto.Unmarshal(b, &rned) |
| 201 |
if err != nil { |
| 202 |
return nil, err |
| 203 |
} |
| 204 |
|
| 205 |
if rned.EncryptionMetadata == nil { |
| 206 |
return nil, fmt.Errorf("encoded EncryptionMetadata was nil") |
| 207 |
} |
| 208 |
|
| 209 |
if rned.EncryptionMetadata.Argon2Parameters == nil { |
| 210 |
return nil, fmt.Errorf("encoded Argon2Parameters was nil") |
| 211 |
} |
| 212 |
|
| 213 |
params, err := unmarshalArgon2Parameters(rned.EncryptionMetadata.Argon2Parameters) |
| 214 |
if err != nil { |
| 215 |
return nil, err |
| 216 |
} |
| 217 |
|
| 218 |
ned := NebulaEncryptedData{ |
| 219 |
EncryptionMetadata: NebulaEncryptionMetadata{ |
| 220 |
EncryptionAlgorithm: rned.EncryptionMetadata.EncryptionAlgorithm, |
| 221 |
Argon2Parameters: *params, |
| 222 |
}, |
| 223 |
Ciphertext: rned.Ciphertext, |
| 224 |
} |
| 225 |
|
| 226 |
return &ned, nil |
| 227 |
} |
| 228 |
|
| 229 |
func unmarshalArgon2Parameters(params *RawNebulaArgon2Parameters) (*Argon2Parameters, error) { |
| 230 |
if params.Version < math.MinInt32 || params.Version > math.MaxInt32 { |
| 231 |
return nil, fmt.Errorf("Argon2Parameters Version must be at least %d and no more than %d", math.MinInt32, math.MaxInt32) |
| 232 |
} |
| 233 |
if params.Memory <= 0 || params.Memory > math.MaxUint32 { |
| 234 |
return nil, fmt.Errorf("Argon2Parameters Memory must be be greater than 0 and no more than %d KiB", uint32(math.MaxUint32)) |
| 235 |
} |
| 236 |
if params.Parallelism <= 0 || params.Parallelism > math.MaxUint8 { |
| 237 |
return nil, fmt.Errorf("Argon2Parameters Parallelism must be be greater than 0 and no more than %d", math.MaxUint8) |
| 238 |
} |
| 239 |
if params.Iterations <= 0 || params.Iterations > math.MaxUint32 { |
| 240 |
return nil, fmt.Errorf("-argon-iterations must be be greater than 0 and no more than %d", uint32(math.MaxUint32)) |
| 241 |
} |
| 242 |
|
| 243 |
return &Argon2Parameters{ |
| 244 |
version: params.Version, |
| 245 |
Memory: params.Memory, |
| 246 |
Parallelism: uint8(params.Parallelism), |
| 247 |
Iterations: params.Iterations, |
| 248 |
salt: params.Salt, |
| 249 |
}, nil |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
// DecryptAndUnmarshalSigningPrivateKey will try to pem decode and decrypt an Ed25519/ECDSA private key with |
| 254 |
// the given passphrase, returning any other bytes b or an error on failure |
| 255 |
func DecryptAndUnmarshalSigningPrivateKey(passphrase, b []byte) (Curve, []byte, []byte, error) { |
| 256 |
var curve Curve |
| 257 |
|
| 258 |
k, r := pem.Decode(b) |
| 259 |
if k == nil { |
| 260 |
return curve, nil, r, fmt.Errorf("input did not contain a valid PEM encoded block") |
| 261 |
} |
| 262 |
|
| 263 |
switch k.Type { |
| 264 |
case EncryptedEd25519PrivateKeyBanner: |
| 265 |
curve = Curve_CURVE25519 |
| 266 |
case EncryptedECDSAP256PrivateKeyBanner: |
| 267 |
curve = Curve_P256 |
| 268 |
default: |
| 269 |
return curve, nil, r, fmt.Errorf("bytes did not contain a proper nebula encrypted Ed25519/ECDSA private key banner") |
| 270 |
} |
| 271 |
|
| 272 |
ned, err := UnmarshalNebulaEncryptedData(k.Bytes) |
| 273 |
if err != nil { |
| 274 |
return curve, nil, r, err |
| 275 |
} |
| 276 |
|
| 277 |
var bytes []byte |
| 278 |
switch ned.EncryptionMetadata.EncryptionAlgorithm { |
| 279 |
case "AES-256-GCM": |
| 280 |
bytes, err = aes256Decrypt(passphrase, &ned.EncryptionMetadata.Argon2Parameters, ned.Ciphertext) |
| 281 |
if err != nil { |
| 282 |
return curve, nil, r, err |
| 283 |
} |
| 284 |
default: |
| 285 |
return curve, nil, r, fmt.Errorf("unsupported encryption algorithm: %s", ned.EncryptionMetadata.EncryptionAlgorithm) |
| 286 |
} |
| 287 |
|
| 288 |
switch curve { |
| 289 |
case Curve_CURVE25519: |
| 290 |
if len(bytes) != ed25519.PrivateKeySize { |
| 291 |
return curve, nil, r, fmt.Errorf("key was not %d bytes, is invalid ed25519 private key", ed25519.PrivateKeySize) |
| 292 |
} |
| 293 |
case Curve_P256: |
| 294 |
if len(bytes) != 32 { |
| 295 |
return curve, nil, r, fmt.Errorf("key was not 32 bytes, is invalid ECDSA P256 private key") |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
return curve, bytes, r, nil |
| 300 |
} |