-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10_CompositionVsInheritance.ps1
More file actions
204 lines (187 loc) · 7.12 KB
/
Copy path10_CompositionVsInheritance.ps1
File metadata and controls
204 lines (187 loc) · 7.12 KB
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
<#
.SYNOPSIS
OOP Reference: Composition vs Inheritance
.DESCRIPTION
Topic: Prefer composition (has-a) over inheritance (is-a)
Category: Structural
Agent Task: Add a LoggingSecureChannel that wraps SecureChannel and logs
Send/Receive calls. Use composition, not inheritance.
Add Pester tests that swap the cipher implementation and verify
SecureChannel still works without modification.
Done Conditions:
- SecureChannel has no direct reference to FipsAesCipher
- Swapping cipher at construction produces correct behavior
- Pester tests pass: Invoke-Pester -Output Detailed
Non-Scope:
- Do not add network transport
#>
# Interfaces (reuse from 06_AbstractFactory if dot-sourced, or redefine here for standalone use)
class ICipher2 {
[byte[]] Seal([byte[]]$pt) { throw [System.NotImplementedException]'Seal' }
[byte[]] Open([byte[]]$ct) { throw [System.NotImplementedException]'Open' }
}
class ISigner2 {
[byte[]] Sign([byte[]]$data) { throw [System.NotImplementedException]'Sign' }
[bool] Verify([byte[]]$data, [byte[]]$sig) { throw [System.NotImplementedException]'Verify' }
}
# Composition-based SecureChannel — injected dependencies, not inherited
class ComposedSecureChannel {
hidden [ICipher2]$_cipher
hidden [ISigner2]$_signer
[string]$PeerId
ComposedSecureChannel([ICipher2]$cipher, [ISigner2]$signer, [string]$peerId) {
$this._cipher = $cipher
$this._signer = $signer
$this.PeerId = $peerId
}
[hashtable] Send([byte[]]$message) {
$sealed = $this._cipher.Seal($message)
$sig = $this._signer.Sign($sealed)
return @{ Sealed=$sealed; Signature=$sig; Peer=$this.PeerId }
}
[byte[]] Receive([hashtable]$packet) {
if (-not $this._signer.Verify($packet.Sealed, $packet.Signature)) {
throw [System.Security.SecurityException]'Signature verification failed'
}
return $this._cipher.Open($packet.Sealed)
}
}
# ======== Agent Task Implementation ========
# Concrete cipher implementations (no FIPS reference in SecureChannel)
class AesGcmCipher : ICipher2 {
hidden [byte[]]$_k
AesGcmCipher() {
$this._k = [byte[]]::new(32)
[System.Security.Cryptography.RandomNumberGenerator]::Fill($this._k)
}
[byte[]] Seal([byte[]]$pt) {
$gcm = [System.Security.Cryptography.AesGcm]::new($this._k)
$nonce = [byte[]]::new(12); $ct = [byte[]]::new($pt.Length); $tag = [byte[]]::new(16)
[System.Security.Cryptography.RandomNumberGenerator]::Fill($nonce)
$gcm.Encrypt($nonce, $pt, $ct, $tag); $gcm.Dispose()
return $nonce + $tag + $ct
}
[byte[]] Open([byte[]]$ct) {
$nonce = $ct[0..11]; $tag = $ct[12..27]
if ($ct.Length -gt 28) {
$body = $ct[28..($ct.Length-1)]
} else {
$body = [byte[]]@()
}
$pt = [byte[]]::new($body.Length)
$gcm = [System.Security.Cryptography.AesGcm]::new($this._k)
$gcm.Decrypt($nonce, $body, $tag, $pt); $gcm.Dispose()
return $pt
}
}
# Alternative cipher implementation (AES-CBC)
class AesCbcCipher : ICipher2 {
hidden [byte[]]$_k
AesCbcCipher() {
$this._k = [byte[]]::new(32)
[System.Security.Cryptography.RandomNumberGenerator]::Fill($this._k)
}
[byte[]] Seal([byte[]]$pt) {
$aes = [System.Security.Cryptography.Aes]::Create()
try {
$aes.KeySize = 256
$aes.Key = $this._k
$aes.Mode = [System.Security.Cryptography.CipherMode]::CBC
$aes.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7
$aes.GenerateIV()
$iv = $aes.IV
$encryptor = $aes.CreateEncryptor()
$ct = $encryptor.TransformFinalBlock($pt, 0, $pt.Length)
$encryptor.Dispose()
return $iv + $ct
} finally {
$aes.Dispose()
}
}
[byte[]] Open([byte[]]$ct) {
$iv = $ct[0..15]
if ($ct.Length -gt 16) {
$body = $ct[16..($ct.Length-1)]
} else {
$body = [byte[]]@()
}
$aes = [System.Security.Cryptography.Aes]::Create()
try {
$aes.KeySize = 256
$aes.Key = $this._k
$aes.Mode = [System.Security.Cryptography.CipherMode]::CBC
$aes.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7
$aes.IV = $iv
$decryptor = $aes.CreateDecryptor()
$pt = $decryptor.TransformFinalBlock($body, 0, $body.Length)
$decryptor.Dispose()
return $pt
} finally {
$aes.Dispose()
}
}
}
# RSA-based signer
class RsaSigner : ISigner2 {
hidden [System.Security.Cryptography.RSA]$_rsa
RsaSigner() { $this._rsa = [System.Security.Cryptography.RSA]::Create(2048) }
[byte[]] Sign([byte[]]$data) {
return $this._rsa.SignData($data,
[System.Security.Cryptography.HashAlgorithmName]::SHA256,
[System.Security.Cryptography.RSASignaturePadding]::Pss)
}
[bool] Verify([byte[]]$data, [byte[]]$sig) {
return $this._rsa.VerifyData($data, $sig,
[System.Security.Cryptography.HashAlgorithmName]::SHA256,
[System.Security.Cryptography.RSASignaturePadding]::Pss)
}
}
# HMAC-based signer
class HmacSigner : ISigner2 {
hidden [byte[]]$_k
HmacSigner() {
$this._k = [byte[]]::new(32)
[System.Security.Cryptography.RandomNumberGenerator]::Fill($this._k)
}
[byte[]] Sign([byte[]]$data) {
$hmac = [System.Security.Cryptography.HMACSHA256]::new($this._k)
try { return $hmac.ComputeHash($data) } finally { $hmac.Dispose() }
}
[bool] Verify([byte[]]$data, [byte[]]$sig) {
$hmac = [System.Security.Cryptography.HMACSHA256]::new($this._k)
try {
$computed = $hmac.ComputeHash($data)
if ($computed.Length -ne $sig.Length) { return $false }
$result = 0
for ($i = 0; $i -lt $computed.Length; $i++) {
$result = $result -bor ($computed[$i] -bxor $sig[$i])
}
return $result -eq 0
} finally {
$hmac.Dispose()
}
}
}
# LoggingSecureChannel — uses composition to wrap SecureChannel
class LoggingSecureChannel {
hidden [ComposedSecureChannel]$_channel
hidden [System.Collections.Generic.List[string]]$_log
LoggingSecureChannel([ICipher2]$cipher, [ISigner2]$signer, [string]$peerId) {
$this._channel = [ComposedSecureChannel]::new($cipher, $signer, $peerId)
$this._log = [System.Collections.Generic.List[string]]::new()
}
[hashtable] Send([byte[]]$message) {
$this._log.Add("Send: $($message.Length) bytes to $($this._channel.PeerId)")
return $this._channel.Send($message)
}
[byte[]] Receive([hashtable]$packet) {
$this._log.Add("Receive: from $($packet.Peer)")
return $this._channel.Receive($packet)
}
[string[]] GetLog() {
return $this._log.ToArray()
}
[void] ClearLog() {
$this._log.Clear()
}
}