1
1
import NIO
2
+ import CryptoKit
2
3
import Logging
3
4
import NIOSSH
4
5
6
+ extension SSHAlgorithms . Modification < NIOSSHTransportProtection . Type > {
7
+ func apply( to configuration: inout [ any NIOSSHTransportProtection . Type ] ) {
8
+ switch self {
9
+ case . add( let algorithms) :
10
+ configuration. append ( contentsOf: algorithms)
11
+
12
+ for algorithm : any NIOSSHTransportProtection . Type in algorithms {
13
+ NIOSSHAlgorithms . register ( transportProtectionScheme: algorithm)
14
+ }
15
+ case . replace( with: let algorithms) :
16
+ configuration = algorithms
17
+
18
+ for algorithm in algorithms {
19
+ NIOSSHAlgorithms . register ( transportProtectionScheme: algorithm)
20
+ }
21
+ }
22
+ }
23
+ }
24
+
25
+ extension SSHAlgorithms . Modification < NIOSSHKeyExchangeAlgorithmProtocol . Type > {
26
+ func apply( to configuration: inout [ any NIOSSHKeyExchangeAlgorithmProtocol . Type ] ) {
27
+ switch self {
28
+ case . add( let algorithms) :
29
+ configuration. append ( contentsOf: algorithms)
30
+
31
+ for algorithm in algorithms {
32
+ NIOSSHAlgorithms . register ( keyExchangeAlgorithm: algorithm)
33
+ }
34
+ case . replace( with: let algorithms) :
35
+ configuration = algorithms
36
+
37
+ for algorithm in algorithms {
38
+ NIOSSHAlgorithms . register ( keyExchangeAlgorithm: algorithm)
39
+ }
40
+ }
41
+ }
42
+ }
43
+
44
+ extension SSHAlgorithms . Modification < ( NIOSSHPublicKeyProtocol . Type , NIOSSHSignatureProtocol . Type ) > {
45
+ func register( ) {
46
+ switch self {
47
+ case . add( let algorithms) :
48
+ for (publicKey, signature) in algorithms {
49
+ NIOSSHAlgorithms . register ( publicKey: publicKey, signature: signature)
50
+ }
51
+ case . replace( with: let algorithms) :
52
+ for (publicKey, signature) in algorithms {
53
+ NIOSSHAlgorithms . register ( publicKey: publicKey, signature: signature)
54
+ }
55
+ }
56
+ }
57
+ }
58
+
5
59
public struct SSHAlgorithms {
6
60
/// Represents a modification to a list of items.
7
61
///
@@ -18,47 +72,40 @@ public struct SSHAlgorithms {
18
72
/// The enabled KeyExchangeAlgorithms
19
73
public var keyExchangeAlgorithms : Modification < NIOSSHKeyExchangeAlgorithmProtocol . Type > ?
20
74
75
+ public var publicKeyAlgorihtms : Modification < ( NIOSSHPublicKeyProtocol . Type , NIOSSHSignatureProtocol . Type ) > ?
76
+
21
77
func apply( to clientConfiguration: inout SSHClientConfiguration ) {
22
- switch transportProtectionSchemes {
23
- case . add( let algorithms) :
24
- clientConfiguration. transportProtectionSchemes. append ( contentsOf: algorithms)
25
- case . replace( with: let algorithms) :
26
- clientConfiguration. transportProtectionSchemes = algorithms
27
- case . none:
28
- ( )
29
- }
30
-
31
- switch keyExchangeAlgorithms {
32
- case . add( let algorithms) :
33
- clientConfiguration. keyExchangeAlgorithms. append ( contentsOf: algorithms)
34
- case . replace( with: let algorithms) :
35
- clientConfiguration. keyExchangeAlgorithms = algorithms
36
- case . none:
37
- ( )
38
- }
78
+ transportProtectionSchemes? . apply ( to: & clientConfiguration. transportProtectionSchemes)
79
+ keyExchangeAlgorithms? . apply ( to: & clientConfiguration. keyExchangeAlgorithms)
80
+ publicKeyAlgorihtms? . register ( )
39
81
}
40
82
41
83
func apply( to serverConfiguration: inout SSHServerConfiguration ) {
42
- switch transportProtectionSchemes {
43
- case . add( let algorithms) :
44
- serverConfiguration. transportProtectionSchemes. append ( contentsOf: algorithms)
45
- case . replace( with: let algorithms) :
46
- serverConfiguration. transportProtectionSchemes = algorithms
47
- case . none:
48
- ( )
49
- }
50
-
51
- switch keyExchangeAlgorithms {
52
- case . add( let algorithms) :
53
- serverConfiguration. keyExchangeAlgorithms. append ( contentsOf: algorithms)
54
- case . replace( with: let algorithms) :
55
- serverConfiguration. keyExchangeAlgorithms = algorithms
56
- case . none:
57
- ( )
58
- }
84
+ transportProtectionSchemes? . apply ( to: & serverConfiguration. transportProtectionSchemes)
85
+ keyExchangeAlgorithms? . apply ( to: & serverConfiguration. keyExchangeAlgorithms)
86
+ publicKeyAlgorihtms? . register ( )
59
87
}
60
88
61
89
public init ( ) { }
90
+
91
+ public static let all : SSHAlgorithms = {
92
+ var algorithms = SSHAlgorithms ( )
93
+
94
+ algorithms. transportProtectionSchemes = . add( [
95
+ AES128CTR . self
96
+ ] )
97
+
98
+ algorithms. keyExchangeAlgorithms = . add( [
99
+ DiffieHellmanGroup14Sha1 . self,
100
+ DiffieHellmanGroup14Sha256 . self
101
+ ] )
102
+
103
+ algorithms. publicKeyAlgorihtms = . add( [
104
+ ( Insecure . RSA. PublicKey. self, Insecure . RSA. Signature. self) ,
105
+ ] )
106
+
107
+ return algorithms
108
+ } ( )
62
109
}
63
110
64
111
/// Represents an SSH connection.
0 commit comments