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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
//
// Copyright (c) 2015-2024 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package estream
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha512"
"crypto/x509"
"encoding/hex"
"errors"
"fmt"
"hash"
"io"
"github.com/cespare/xxhash/v2"
"github.com/secure-io/sio-go"
"github.com/tinylib/msgp/msgp"
)
// ReplaceFn provides key replacement.
//
// When a key is found on stream, the function is called with the public key.
// The function must then return a private key to decrypt matching the key sent.
// The public key must then be specified that should be used to re-encrypt the stream.
//
// If no private key is sent and the public key matches the one sent to the function
// the key will be kept as is. Other returned values will cause an error.
//
// For encrypting unencrypted keys on stream a nil key will be sent.
// If a public key is returned the key will be encrypted with the public key.
// No private key should be returned for this.
type ReplaceFn func(key *rsa.PublicKey) (*rsa.PrivateKey, *rsa.PublicKey)
// ReplaceKeysOptions allows passing additional options to ReplaceKeys.
type ReplaceKeysOptions struct {
// If EncryptAll set all unencrypted keys will be encrypted.
EncryptAll bool
// PassErrors will pass through error an error packet,
// and not return an error.
PassErrors bool
}
// ReplaceKeys will replace the keys in a stream.
//
// A replace function must be provided. See ReplaceFn for functionality.
// If encryptAll is set.
func ReplaceKeys(w io.Writer, r io.Reader, replace ReplaceFn, o ReplaceKeysOptions) error {
var ver [2]byte
if _, err := io.ReadFull(r, ver[:]); err != nil {
return err
}
switch ver[0] {
case 2:
default:
return fmt.Errorf("unknown stream version: 0x%x", ver[0])
}
if _, err := w.Write(ver[:]); err != nil {
return err
}
// Input
mr := msgp.NewReader(r)
mw := msgp.NewWriter(w)
// Temporary block storage.
block := make([]byte, 1024)
// Write a block.
writeBlock := func(id blockID, sz uint32, content []byte) error {
if err := mw.WriteInt8(int8(id)); err != nil {
return err
}
if err := mw.WriteUint32(sz); err != nil {
return err
}
_, err := mw.Write(content)
return err
}
for {
// Read block ID.
n, err := mr.ReadInt8()
if err != nil {
return err
}
id := blockID(n)
// Read size
sz, err := mr.ReadUint32()
if err != nil {
return err
}
if cap(block) < int(sz) {
block = make([]byte, sz)
}
block = block[:sz]
_, err = io.ReadFull(mr, block)
if err != nil {
return err
}
switch id {
case blockEncryptedKey:
ogBlock := block
// Read public key
publicKey, block, err := msgp.ReadBytesZC(block)
if err != nil {
return err
}
pk, err := x509.ParsePKCS1PublicKey(publicKey)
if err != nil {
return err
}
private, public := replace(pk)
if private == nil && public == pk {
if err := writeBlock(id, sz, ogBlock); err != nil {
return err
}
}
if private == nil {
return errors.New("no private key provided, unable to re-encrypt")
}
// Read cipher key
cipherKey, _, err := msgp.ReadBytesZC(block)
if err != nil {
return err
}
// Decrypt stream key
key, err := rsa.DecryptOAEP(sha512.New(), rand.Reader, private, cipherKey, nil)
if err != nil {
return err
}
if len(key) != 32 {
return fmt.Errorf("unexpected key length: %d", len(key))
}
cipherKey, err = rsa.EncryptOAEP(sha512.New(), rand.Reader, public, key[:], nil)
if err != nil {
return err
}
// Write Public key
tmp := msgp.AppendBytes(nil, x509.MarshalPKCS1PublicKey(public))
// Write encrypted cipher key
tmp = msgp.AppendBytes(tmp, cipherKey)
if err := writeBlock(blockEncryptedKey, uint32(len(tmp)), tmp); err != nil {
return err
}
case blockPlainKey:
if !o.EncryptAll {
if err := writeBlock(id, sz, block); err != nil {
return err
}
continue
}
_, public := replace(nil)
if public == nil {
if err := writeBlock(id, sz, block); err != nil {
return err
}
continue
}
key, _, err := msgp.ReadBytesZC(block)
if err != nil {
return err
}
if len(key) != 32 {
return fmt.Errorf("unexpected key length: %d", len(key))
}
cipherKey, err := rsa.EncryptOAEP(sha512.New(), rand.Reader, public, key[:], nil)
if err != nil {
return err
}
// Write Public key
tmp := msgp.AppendBytes(nil, x509.MarshalPKCS1PublicKey(public))
// Write encrypted cipher key
tmp = msgp.AppendBytes(tmp, cipherKey)
if err := writeBlock(blockEncryptedKey, uint32(len(tmp)), tmp); err != nil {
return err
}
case blockEOF:
if err := writeBlock(id, sz, block); err != nil {
return err
}
return mw.Flush()
case blockError:
if o.PassErrors {
if err := writeBlock(id, sz, block); err != nil {
return err
}
return mw.Flush()
}
// Return error
msg, _, err := msgp.ReadStringBytes(block)
if err != nil {
return err
}
return errors.New(msg)
default:
if err := writeBlock(id, sz, block); err != nil {
return err
}
}
}
}
// DebugStream will print stream block information to w.
func (r *Reader) DebugStream(w io.Writer) error {
if r.err != nil {
return r.err
}
if r.inStream {
return errors.New("previous stream not read until EOF")
}
fmt.Fprintf(w, "stream major: %v, minor: %v\n", r.majorV, r.minorV)
// Temp storage for blocks.
block := make([]byte, 1024)
hashers := []hash.Hash{nil, xxhash.New()}
for {
// Read block ID.
n, err := r.mr.ReadInt8()
if err != nil {
return r.setErr(fmt.Errorf("reading block id: %w", err))
}
id := blockID(n)
// Read block size
sz, err := r.mr.ReadUint32()
if err != nil {
return r.setErr(fmt.Errorf("reading block size: %w", err))
}
fmt.Fprintf(w, "block type: %v, size: %d bytes, in stream: %v\n", id, sz, r.inStream)
// Read block data
if cap(block) < int(sz) {
block = make([]byte, sz)
}
block = block[:sz]
_, err = io.ReadFull(r.mr, block)
if err != nil {
return r.setErr(fmt.Errorf("reading block data: %w", err))
}
// Parse block
switch id {
case blockPlainKey:
// Read plaintext key.
key, _, err := msgp.ReadBytesBytes(block, make([]byte, 0, 32))
if err != nil {
return r.setErr(fmt.Errorf("reading key: %w", err))
}
if len(key) != 32 {
return r.setErr(fmt.Errorf("unexpected key length: %d", len(key)))
}
// Set key for following streams.
r.key = (*[32]byte)(key)
fmt.Fprintf(w, "plain key read\n")
case blockEncryptedKey:
// Read public key
publicKey, block, err := msgp.ReadBytesZC(block)
if err != nil {
return r.setErr(fmt.Errorf("reading public key: %w", err))
}
// Request private key if we have a custom function.
if r.privateFn != nil {
fmt.Fprintf(w, "requesting private key from privateFn\n")
pk, err := x509.ParsePKCS1PublicKey(publicKey)
if err != nil {
return r.setErr(fmt.Errorf("parse public key: %w", err))
}
r.private = r.privateFn(pk)
if r.private == nil {
fmt.Fprintf(w, "privateFn did not provide private key\n")
if r.skipEncrypted || r.returnNonDec {
fmt.Fprintf(w, "continuing. skipEncrypted: %v, returnNonDec: %v\n", r.skipEncrypted, r.returnNonDec)
r.key = nil
continue
}
return r.setErr(errors.New("nil private key returned"))
}
}
// Read cipher key
cipherKey, _, err := msgp.ReadBytesZC(block)
if err != nil {
return r.setErr(fmt.Errorf("reading cipherkey: %w", err))
}
if r.private == nil {
if r.skipEncrypted || r.returnNonDec {
fmt.Fprintf(w, "no private key, continuing due to skipEncrypted: %v, returnNonDec: %v\n", r.skipEncrypted, r.returnNonDec)
r.key = nil
continue
}
return r.setErr(errors.New("private key has not been set"))
}
// Decrypt stream key
key, err := rsa.DecryptOAEP(sha512.New(), rand.Reader, r.private, cipherKey, nil)
if err != nil {
if r.returnNonDec {
fmt.Fprintf(w, "no private key, continuing due to returnNonDec: %v\n", r.returnNonDec)
r.key = nil
continue
}
return fmt.Errorf("decrypting key: %w", err)
}
if len(key) != 32 {
return r.setErr(fmt.Errorf("unexpected key length: %d", len(key)))
}
r.key = (*[32]byte)(key)
fmt.Fprintf(w, "stream key decoded\n")
case blockPlainStream, blockEncStream:
// Read metadata
name, block, err := msgp.ReadStringBytes(block)
if err != nil {
return r.setErr(fmt.Errorf("reading name: %w", err))
}
extra, block, err := msgp.ReadBytesBytes(block, nil)
if err != nil {
return r.setErr(fmt.Errorf("reading extra: %w", err))
}
c, block, err := msgp.ReadUint8Bytes(block)
if err != nil {
return r.setErr(fmt.Errorf("reading checksum: %w", err))
}
checksum := checksumType(c)
if !checksum.valid() {
return r.setErr(fmt.Errorf("unknown checksum type %d", checksum))
}
fmt.Fprintf(w, "new stream. name: %v, extra size: %v, checksum type: %v\n", name, len(extra), checksum)
for _, h := range hashers {
if h != nil {
h.Reset()
}
}
// Return plaintext stream
if id == blockPlainStream {
r.inStream = true
continue
}
// Handle encrypted streams.
if r.key == nil {
if r.skipEncrypted {
fmt.Fprintf(w, "nil key, skipEncrypted: %v\n", r.skipEncrypted)
r.inStream = true
continue
}
return ErrNoKey
}
// Read stream nonce
nonce, _, err := msgp.ReadBytesZC(block)
if err != nil {
return r.setErr(fmt.Errorf("reading nonce: %w", err))
}
stream, err := sio.AES_256_GCM.Stream(r.key[:])
if err != nil {
return r.setErr(fmt.Errorf("initializing sio: %w", err))
}
// Check if nonce is expected length.
if len(nonce) != stream.NonceSize() {
return r.setErr(fmt.Errorf("unexpected nonce length: %d", len(nonce)))
}
fmt.Fprintf(w, "nonce: %v\n", nonce)
r.inStream = true
case blockEOS:
if !r.inStream {
return errors.New("end-of-stream without being in stream")
}
h, _, err := msgp.ReadBytesZC(block)
if err != nil {
return r.setErr(fmt.Errorf("reading block data: %w", err))
}
fmt.Fprintf(w, "end-of-stream. stream hash: %s. data hashes: ", hex.EncodeToString(h))
for i, h := range hashers {
if h != nil {
fmt.Fprintf(w, "%s:%s. ", checksumType(i), hex.EncodeToString(h.Sum(nil)))
}
}
fmt.Fprint(w, "\n")
r.inStream = false
case blockEOF:
if r.inStream {
return errors.New("end-of-file without finishing stream")
}
fmt.Fprintf(w, "end-of-file\n")
return nil
case blockError:
msg, _, err := msgp.ReadStringBytes(block)
if err != nil {
return r.setErr(fmt.Errorf("reading error string: %w", err))
}
fmt.Fprintf(w, "error recorded on stream: %v\n", msg)
return nil
case blockDatablock:
buf, _, err := msgp.ReadBytesZC(block)
if err != nil {
return r.setErr(fmt.Errorf("reading block data: %w", err))
}
for _, h := range hashers {
if h != nil {
h.Write(buf)
}
}
fmt.Fprintf(w, "data block, length: %v\n", len(buf))
default:
fmt.Fprintf(w, "skipping block\n")
if id >= 0 {
return fmt.Errorf("unknown block type: %d", id)
}
}
}
}
|