|
| 1 | +package pam |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/aes" |
| 5 | + "crypto/cipher" |
| 6 | + "crypto/rand" |
| 7 | + "encoding/binary" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net" |
| 11 | + "sync" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +// EncryptedConn wraps a net.Conn with AES-256-GCM encryption. |
| 16 | +// Frame format: [4-byte big-endian total-frame-length][12-byte random nonce][ciphertext + 16-byte GCM auth tag] |
| 17 | +type EncryptedConn struct { |
| 18 | + inner net.Conn |
| 19 | + gcm cipher.AEAD |
| 20 | + |
| 21 | + readMu sync.Mutex |
| 22 | + readBuf []byte |
| 23 | + |
| 24 | + writeMu sync.Mutex |
| 25 | +} |
| 26 | + |
| 27 | +func NewEncryptedConn(inner net.Conn, aesKey []byte) (*EncryptedConn, error) { |
| 28 | + block, err := aes.NewCipher(aesKey) |
| 29 | + if err != nil { |
| 30 | + return nil, fmt.Errorf("failed to create AES cipher: %w", err) |
| 31 | + } |
| 32 | + gcm, err := cipher.NewGCM(block) |
| 33 | + if err != nil { |
| 34 | + return nil, fmt.Errorf("failed to create GCM: %w", err) |
| 35 | + } |
| 36 | + return &EncryptedConn{ |
| 37 | + inner: inner, |
| 38 | + gcm: gcm, |
| 39 | + }, nil |
| 40 | +} |
| 41 | + |
| 42 | +func (ec *EncryptedConn) Read(b []byte) (int, error) { |
| 43 | + ec.readMu.Lock() |
| 44 | + defer ec.readMu.Unlock() |
| 45 | + |
| 46 | + // Serve from buffer if available |
| 47 | + if len(ec.readBuf) > 0 { |
| 48 | + n := copy(b, ec.readBuf) |
| 49 | + ec.readBuf = ec.readBuf[n:] |
| 50 | + return n, nil |
| 51 | + } |
| 52 | + |
| 53 | + // Read frame length |
| 54 | + lengthBuf := make([]byte, 4) |
| 55 | + if _, err := io.ReadFull(ec.inner, lengthBuf); err != nil { |
| 56 | + return 0, err |
| 57 | + } |
| 58 | + frameLen := binary.BigEndian.Uint32(lengthBuf) |
| 59 | + if frameLen > 1<<24 { |
| 60 | + return 0, fmt.Errorf("encrypted frame too large: %d bytes", frameLen) |
| 61 | + } |
| 62 | + |
| 63 | + // Read the full frame (nonce + ciphertext) |
| 64 | + frame := make([]byte, frameLen) |
| 65 | + if _, err := io.ReadFull(ec.inner, frame); err != nil { |
| 66 | + return 0, fmt.Errorf("failed to read encrypted frame: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + nonceSize := ec.gcm.NonceSize() |
| 70 | + if int(frameLen) < nonceSize { |
| 71 | + return 0, fmt.Errorf("frame too short for nonce") |
| 72 | + } |
| 73 | + |
| 74 | + nonce := frame[:nonceSize] |
| 75 | + ciphertext := frame[nonceSize:] |
| 76 | + |
| 77 | + plaintext, err := ec.gcm.Open(nil, nonce, ciphertext, nil) |
| 78 | + if err != nil { |
| 79 | + return 0, fmt.Errorf("GCM decryption failed: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + n := copy(b, plaintext) |
| 83 | + if n < len(plaintext) { |
| 84 | + ec.readBuf = plaintext[n:] |
| 85 | + } |
| 86 | + return n, nil |
| 87 | +} |
| 88 | + |
| 89 | +func (ec *EncryptedConn) Write(b []byte) (int, error) { |
| 90 | + ec.writeMu.Lock() |
| 91 | + defer ec.writeMu.Unlock() |
| 92 | + |
| 93 | + nonce := make([]byte, ec.gcm.NonceSize()) |
| 94 | + if _, err := rand.Read(nonce); err != nil { |
| 95 | + return 0, fmt.Errorf("failed to generate nonce: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + ciphertext := ec.gcm.Seal(nil, nonce, b, nil) |
| 99 | + |
| 100 | + // Frame = nonce + ciphertext |
| 101 | + frameLen := len(nonce) + len(ciphertext) |
| 102 | + lengthBuf := make([]byte, 4) |
| 103 | + binary.BigEndian.PutUint32(lengthBuf, uint32(frameLen)) |
| 104 | + |
| 105 | + if _, err := ec.inner.Write(lengthBuf); err != nil { |
| 106 | + return 0, fmt.Errorf("failed to write frame length: %w", err) |
| 107 | + } |
| 108 | + if _, err := ec.inner.Write(nonce); err != nil { |
| 109 | + return 0, fmt.Errorf("failed to write nonce: %w", err) |
| 110 | + } |
| 111 | + if _, err := ec.inner.Write(ciphertext); err != nil { |
| 112 | + return 0, fmt.Errorf("failed to write ciphertext: %w", err) |
| 113 | + } |
| 114 | + |
| 115 | + return len(b), nil |
| 116 | +} |
| 117 | + |
| 118 | +func (ec *EncryptedConn) Close() error { |
| 119 | + return ec.inner.Close() |
| 120 | +} |
| 121 | + |
| 122 | +func (ec *EncryptedConn) LocalAddr() net.Addr { |
| 123 | + return ec.inner.LocalAddr() |
| 124 | +} |
| 125 | + |
| 126 | +func (ec *EncryptedConn) RemoteAddr() net.Addr { |
| 127 | + return ec.inner.RemoteAddr() |
| 128 | +} |
| 129 | + |
| 130 | +func (ec *EncryptedConn) SetDeadline(t time.Time) error { |
| 131 | + return ec.inner.SetDeadline(t) |
| 132 | +} |
| 133 | + |
| 134 | +func (ec *EncryptedConn) SetReadDeadline(t time.Time) error { |
| 135 | + return ec.inner.SetReadDeadline(t) |
| 136 | +} |
| 137 | + |
| 138 | +func (ec *EncryptedConn) SetWriteDeadline(t time.Time) error { |
| 139 | + return ec.inner.SetWriteDeadline(t) |
| 140 | +} |
0 commit comments