@@ -13,6 +13,11 @@ pub use std::io::{Read, Write};
13
13
pub use std:: net:: { SocketAddr , TcpListener , TcpStream } ;
14
14
pub use std:: thread:: { self , JoinHandle } ;
15
15
16
+ const RSA : & [ u8 ; 8 ] = b"RSA-PSS\0 " ;
17
+ use openssl:: pkey:: Private ;
18
+ use parsec_openssl_provider:: parsec_openssl2:: ossl_param;
19
+ use parsec_openssl_provider:: PARSEC_PROVIDER_KEY_NAME ;
20
+
16
21
// Loads a provider into the given library context
17
22
pub fn load_provider ( lib_ctx : & LibCtx , provider_name : & str , provider_path : String ) -> Provider {
18
23
assert ! ( Provider :: set_default_search_path( Some ( lib_ctx) , & provider_path) . is_ok( ) ) ;
@@ -131,33 +136,43 @@ impl Client {
131
136
}
132
137
}
133
138
134
- // ToDo: This needs to modified in the future to use the PKey object from parsec provider
135
- // Uses all the Client configurations to build a SslContext object
136
- pub fn build ( & mut self ) -> SslContext {
137
- let mut ctx_builder = SslContext :: builder ( self . ssl_method ) . unwrap ( ) ;
138
- if let Some ( certificate) = & self . certificate {
139
- ctx_builder
140
- . set_certificate_file ( certificate, SslFiletype :: PEM )
141
- . unwrap ( ) ;
142
- }
143
- if let Some ( key) = & self . private_key {
144
- ctx_builder
145
- . set_private_key_file ( key, SslFiletype :: PEM )
146
- . unwrap ( ) ;
147
- }
148
- if let Some ( ca_cert) = & self . ca_certificate {
149
- ctx_builder. set_ca_file ( ca_cert) . unwrap ( ) ;
150
- }
151
- ctx_builder. set_verify ( self . ssl_mode ) ;
152
- ctx_builder. build ( )
153
- }
154
-
155
139
// Creates a TCP stream and initiates a TLS handshake to the server
156
- pub fn connect ( mut self , addr : SocketAddr ) {
157
- let socket = TcpStream :: connect ( addr) . unwrap ( ) ;
158
- let client_ctx = self . build ( ) ;
159
- let ssl = Ssl :: new ( & client_ctx) . unwrap ( ) ;
160
- let mut channel = ssl. connect ( socket) . unwrap ( ) ;
161
- channel. read_exact ( & mut [ 0 ] ) . unwrap ( ) ;
140
+ pub fn connect ( self , addr : SocketAddr ) {
141
+ unsafe {
142
+ let provider_path = String :: from ( "../../target/debug/" ) ;
143
+ let provider_name = String :: from ( "libparsec_openssl_provider_shared" ) ;
144
+ let lib_ctx = LibCtx :: new ( ) . unwrap ( ) ;
145
+ let _provider: Provider = load_provider ( & lib_ctx, & provider_name, provider_path) ;
146
+
147
+ let mut parsec_pkey: * mut EVP_PKEY = std:: ptr:: null_mut ( ) ;
148
+
149
+ let mut ctx_builder = SslContextBuilder :: new ( self . ssl_method ) . unwrap ( ) ;
150
+
151
+ if let Some ( key) = & self . private_key_name {
152
+ let mut param = ossl_param ! ( PARSEC_PROVIDER_KEY_NAME , OSSL_PARAM_UTF8_PTR , key) ;
153
+ load_key ( & lib_ctx, & mut param, & mut parsec_pkey, RSA ) ;
154
+
155
+ let key: openssl:: pkey:: PKey < Private > =
156
+ openssl:: pkey:: PKey :: from_ptr ( parsec_pkey as _ ) ;
157
+ ctx_builder. set_private_key ( & key) . unwrap ( ) ;
158
+ }
159
+
160
+ if let Some ( certificate) = & self . certificate {
161
+ ctx_builder
162
+ . set_certificate_file ( certificate, SslFiletype :: PEM )
163
+ . unwrap ( ) ;
164
+ }
165
+ if let Some ( ca_cert) = & self . ca_certificate {
166
+ ctx_builder. set_ca_file ( ca_cert) . unwrap ( ) ;
167
+ }
168
+
169
+ ctx_builder. set_verify ( self . ssl_mode ) ;
170
+
171
+ let client_ctx = ctx_builder. build ( ) ;
172
+ let socket = TcpStream :: connect ( addr) . unwrap ( ) ;
173
+ let ssl = Ssl :: new ( & client_ctx) . unwrap ( ) ;
174
+ let mut s = ssl. connect ( socket) . unwrap ( ) ;
175
+ s. read_exact ( & mut [ 0 ] ) . unwrap ( ) ;
176
+ }
162
177
}
163
178
}
0 commit comments