-
Notifications
You must be signed in to change notification settings - Fork 15
Add web for Superday #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
PrestigePvP
wants to merge
1
commit into
Infisical:main
Choose a base branch
from
PrestigePvP:tre-super
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add web for Superday #124
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| package pam | ||
|
|
||
| import ( | ||
| "crypto/aes" | ||
| "crypto/cipher" | ||
| "crypto/rand" | ||
| "encoding/binary" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| // EncryptedConn wraps a net.Conn with AES-256-GCM encryption. | ||
| // Frame format: [4-byte big-endian total-frame-length][12-byte random nonce][ciphertext + 16-byte GCM auth tag] | ||
| type EncryptedConn struct { | ||
| inner net.Conn | ||
| gcm cipher.AEAD | ||
|
|
||
| readMu sync.Mutex | ||
| readBuf []byte | ||
|
|
||
| writeMu sync.Mutex | ||
| } | ||
|
|
||
| func NewEncryptedConn(inner net.Conn, aesKey []byte) (*EncryptedConn, error) { | ||
| block, err := aes.NewCipher(aesKey) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create AES cipher: %w", err) | ||
| } | ||
| gcm, err := cipher.NewGCM(block) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create GCM: %w", err) | ||
| } | ||
| return &EncryptedConn{ | ||
| inner: inner, | ||
| gcm: gcm, | ||
| }, nil | ||
| } | ||
|
|
||
| func (ec *EncryptedConn) Read(b []byte) (int, error) { | ||
| ec.readMu.Lock() | ||
| defer ec.readMu.Unlock() | ||
|
|
||
| // Serve from buffer if available | ||
| if len(ec.readBuf) > 0 { | ||
| n := copy(b, ec.readBuf) | ||
| ec.readBuf = ec.readBuf[n:] | ||
| return n, nil | ||
| } | ||
|
|
||
| // Read frame length | ||
| lengthBuf := make([]byte, 4) | ||
| if _, err := io.ReadFull(ec.inner, lengthBuf); err != nil { | ||
| return 0, err | ||
| } | ||
| frameLen := binary.BigEndian.Uint32(lengthBuf) | ||
| if frameLen > 1<<24 { | ||
| return 0, fmt.Errorf("encrypted frame too large: %d bytes", frameLen) | ||
| } | ||
|
|
||
| // Read the full frame (nonce + ciphertext) | ||
| frame := make([]byte, frameLen) | ||
| if _, err := io.ReadFull(ec.inner, frame); err != nil { | ||
| return 0, fmt.Errorf("failed to read encrypted frame: %w", err) | ||
| } | ||
|
|
||
| nonceSize := ec.gcm.NonceSize() | ||
| if int(frameLen) < nonceSize { | ||
| return 0, fmt.Errorf("frame too short for nonce") | ||
| } | ||
|
|
||
| nonce := frame[:nonceSize] | ||
| ciphertext := frame[nonceSize:] | ||
|
|
||
| plaintext, err := ec.gcm.Open(nil, nonce, ciphertext, nil) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("GCM decryption failed: %w", err) | ||
| } | ||
|
|
||
| n := copy(b, plaintext) | ||
| if n < len(plaintext) { | ||
| ec.readBuf = plaintext[n:] | ||
| } | ||
| return n, nil | ||
| } | ||
|
|
||
| func (ec *EncryptedConn) Write(b []byte) (int, error) { | ||
| ec.writeMu.Lock() | ||
| defer ec.writeMu.Unlock() | ||
|
|
||
| nonce := make([]byte, ec.gcm.NonceSize()) | ||
| if _, err := rand.Read(nonce); err != nil { | ||
| return 0, fmt.Errorf("failed to generate nonce: %w", err) | ||
| } | ||
|
|
||
| ciphertext := ec.gcm.Seal(nil, nonce, b, nil) | ||
|
|
||
| // Frame = nonce + ciphertext | ||
| frameLen := len(nonce) + len(ciphertext) | ||
| lengthBuf := make([]byte, 4) | ||
| binary.BigEndian.PutUint32(lengthBuf, uint32(frameLen)) | ||
|
|
||
| if _, err := ec.inner.Write(lengthBuf); err != nil { | ||
| return 0, fmt.Errorf("failed to write frame length: %w", err) | ||
| } | ||
| if _, err := ec.inner.Write(nonce); err != nil { | ||
| return 0, fmt.Errorf("failed to write nonce: %w", err) | ||
| } | ||
| if _, err := ec.inner.Write(ciphertext); err != nil { | ||
| return 0, fmt.Errorf("failed to write ciphertext: %w", err) | ||
| } | ||
|
|
||
| return len(b), nil | ||
| } | ||
|
|
||
| func (ec *EncryptedConn) Close() error { | ||
| return ec.inner.Close() | ||
| } | ||
|
|
||
| func (ec *EncryptedConn) LocalAddr() net.Addr { | ||
| return ec.inner.LocalAddr() | ||
| } | ||
|
|
||
| func (ec *EncryptedConn) RemoteAddr() net.Addr { | ||
| return ec.inner.RemoteAddr() | ||
| } | ||
|
|
||
| func (ec *EncryptedConn) SetDeadline(t time.Time) error { | ||
| return ec.inner.SetDeadline(t) | ||
| } | ||
|
|
||
| func (ec *EncryptedConn) SetReadDeadline(t time.Time) error { | ||
| return ec.inner.SetReadDeadline(t) | ||
| } | ||
|
|
||
| func (ec *EncryptedConn) SetWriteDeadline(t time.Time) error { | ||
| return ec.inner.SetWriteDeadline(t) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Creating
prefixedVirtualConnectionwithout passing thefirstBytethat was read. This means the magic byte (0x00) won't be available to the PAM web proxy handler, potentially causing protocol issues.