@@ -4,85 +4,77 @@ use rustls_pki_types::{pem::PemObject, CertificateDer, PrivateKeyDer};
44use serde:: Deserialize ;
55use tracing:: debug;
66
7- use crate :: log:: CONFIG ;
7+ use crate :: { error :: TlsConfigError , log:: CONFIG } ;
88
99///
1010/// Server TLS Configuration
1111/// This is listener/inbound connection config
1212///
1313#[ derive( Clone , Debug , Deserialize ) ]
14- #[ serde( tag = "type" ) ]
14+ #[ serde( untagged ) ]
1515pub enum TlsConfig {
1616 Pem {
17- certificate : String ,
18- private_key : String ,
17+ certificate_pem : String ,
18+ private_key_pem : String ,
1919 } ,
2020 Path {
21- certificate : String ,
22- private_key : String ,
21+ certificate_path : String ,
22+ private_key_path : String ,
2323 } ,
2424}
2525
26- #[ derive( Clone , Debug , Deserialize ) ]
27- pub struct KeyCertPair {
28- pub certificate : String ,
29- pub private_key : String ,
30- }
31-
3226impl TlsConfig {
33- pub fn cert_exists ( & self ) -> bool {
27+ pub fn check_cert ( & self ) -> Result < ( ) , TlsConfigError > {
3428 match self {
35- TlsConfig :: Pem { certificate, .. } => {
36- debug ! ( target: CONFIG , msg = "TLS certificate is a pem string (content omitted)" ) ;
29+ TlsConfig :: Pem {
30+ certificate_pem : certificate,
31+ ..
32+ } => {
33+ debug ! ( target: CONFIG , msg = "TLS certificate from PEM string" ) ;
3734 let certs = CertificateDer :: pem_slice_iter ( certificate. as_bytes ( ) )
3835 . collect :: < Result < Vec < _ > , _ > > ( )
3936 . unwrap_or ( Vec :: new ( ) ) ;
40- !certs. is_empty ( )
37+ if certs. is_empty ( ) {
38+ return Err ( TlsConfigError :: InvalidCertificate ) ;
39+ }
4140 }
42- TlsConfig :: Path { certificate, .. } => {
43- debug ! ( target: CONFIG , msg = "TLS certificate is a path: {}" , certificate) ;
44- PathBuf :: from ( certificate) . exists ( )
41+ TlsConfig :: Path {
42+ certificate_path, ..
43+ } => {
44+ debug ! ( target: CONFIG , msg = "TLS certificate from path" , certificate_path) ;
45+ if !PathBuf :: from ( certificate_path) . exists ( ) {
46+ return Err ( TlsConfigError :: MissingCertificate {
47+ path : certificate_path. to_owned ( ) ,
48+ } ) ;
49+ }
4550 }
4651 }
52+ Ok ( ( ) )
4753 }
4854
49- pub fn private_key_exists ( & self ) -> bool {
55+ pub fn check_private_key ( & self ) -> Result < ( ) , TlsConfigError > {
5056 match self {
51- TlsConfig :: Pem { private_key, .. } => {
52- debug ! ( target: CONFIG , msg = "TLS private_key is a pem string (content omitted)" ) ;
53- PrivateKeyDer :: from_pem_slice ( private_key. as_bytes ( ) ) . is_ok ( )
57+ TlsConfig :: Pem {
58+ private_key_pem : private_key,
59+ ..
60+ } => {
61+ debug ! ( target: CONFIG , msg = "TLS private key from PEM string" ) ;
62+ if PrivateKeyDer :: from_pem_slice ( private_key. as_bytes ( ) ) . is_err ( ) {
63+ return Err ( TlsConfigError :: InvalidPrivateKey ) ;
64+ }
5465 }
55- TlsConfig :: Path { private_key, .. } => {
56- debug ! ( target: CONFIG , msg = "TLS private_key is a path: {}" , private_key) ;
57- PathBuf :: from ( private_key) . exists ( )
66+ TlsConfig :: Path {
67+ private_key_path, ..
68+ } => {
69+ debug ! ( target: CONFIG , msg = "TLS private key from path" , private_key_path) ;
70+ if !PathBuf :: from ( private_key_path) . exists ( ) {
71+ return Err ( TlsConfigError :: MissingPrivateKey {
72+ path : private_key_path. to_owned ( ) ,
73+ } ) ;
74+ }
5875 }
5976 }
60- }
61-
62- pub fn certificate ( & self ) -> & str {
63- match self {
64- Self :: Pem { certificate, .. } | Self :: Path { certificate, .. } => certificate,
65- }
66- }
67-
68- pub fn private_key ( & self ) -> & str {
69- match self {
70- Self :: Pem { private_key, .. } | Self :: Path { private_key, .. } => private_key,
71- }
72- }
73-
74- pub fn certificate_err_msg ( & self ) -> & str {
75- match self {
76- Self :: Pem { .. } => "Transport Layer Security (TLS) Certificate is invalid" ,
77- Self :: Path { .. } => "Transport Layer Security (TLS) Certificate not found" ,
78- }
79- }
80-
81- pub fn private_key_err_msg ( & self ) -> & str {
82- match self {
83- Self :: Pem { .. } => "Transport Layer Security (TLS) Private key is invalid" ,
84- Self :: Path { .. } => "Transport Layer Security (TLS) Private key not found" ,
85- }
77+ Ok ( ( ) )
8678 }
8779}
8880
@@ -92,21 +84,21 @@ mod tests {
9284
9385 fn test_config_with_path ( ) -> TlsConfig {
9486 TlsConfig :: Path {
95- certificate : "../../tests/tls/server.cert" . to_string ( ) ,
96- private_key : "../../tests/tls/server.key" . to_string ( ) ,
87+ certificate_path : "../../tests/tls/server.cert" . to_string ( ) ,
88+ private_key_path : "../../tests/tls/server.key" . to_string ( ) ,
9789 }
9890 }
9991
10092 fn test_config_with_invalid_path ( ) -> TlsConfig {
10193 TlsConfig :: Path {
102- certificate : "/path/to/non-existent/file" . to_string ( ) ,
103- private_key : "/path/to/non-existent/file" . to_string ( ) ,
94+ certificate_path : "/path/to/non-existent/file" . to_string ( ) ,
95+ private_key_path : "/path/to/non-existent/file" . to_string ( ) ,
10496 }
10597 }
10698
10799 fn test_config_with_pem ( ) -> TlsConfig {
108100 TlsConfig :: Pem {
109- certificate : "\
101+ certificate_pem : "\
110102 -----BEGIN CERTIFICATE-----
111103MIIDKzCCAhOgAwIBAgIUMXfu7Mj22j+e9Gt2gjV73TBg20wwDQYJKoZIhvcNAQEL
112104BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTI1MDEyNjAxNDkzMVoXDTI2MDEy
@@ -128,7 +120,7 @@ TU/T2RF2sDsSHrUIVMeifhYc0jfNlRwnUG5liN9BiGo1QxNZ9jGY/3ts5eu8+XM=
128120-----END CERTIFICATE-----
129121"
130122 . to_string ( ) ,
131- private_key : "\
123+ private_key_pem : "\
132124 -----BEGIN PRIVATE KEY-----
133125MIIEugIBADANBgkqhkiG9w0BAQEFAASCBKQwggSgAgEAAoIBAQCm6o6q/Q/wg97t
134126OZAY7Yd47QOM+shXJhgK0lcTJSE9K6rbR4+Nvo/IJ4CUbzvd8lbj59IXpg/Sexvs
@@ -164,32 +156,32 @@ B+qwsnNEiDoJhgYj+cQ=
164156
165157 fn test_config_with_invalid_pem ( ) -> TlsConfig {
166158 TlsConfig :: Pem {
167- certificate : "-----INVALID PEM-----" . to_string ( ) ,
168- private_key : "-----INVALID PEM-----" . to_string ( ) ,
159+ certificate_pem : "-----INVALID PEM-----" . to_string ( ) ,
160+ private_key_pem : "-----INVALID PEM-----" . to_string ( ) ,
169161 }
170162 }
171163
172164 #[ test]
173165 fn test_tls_cert_exists_with_path ( ) {
174- assert ! ( test_config_with_path( ) . cert_exists ( ) ) ;
175- assert ! ( ! test_config_with_invalid_path( ) . cert_exists ( ) ) ;
166+ assert ! ( test_config_with_path( ) . check_cert ( ) . is_ok ( ) ) ;
167+ assert ! ( test_config_with_invalid_path( ) . check_cert ( ) . is_err ( ) ) ;
176168 }
177169
178170 #[ test]
179171 fn test_tls_cert_exists_with_pem ( ) {
180- assert ! ( test_config_with_pem( ) . cert_exists ( ) ) ;
181- assert ! ( ! test_config_with_invalid_pem( ) . cert_exists ( ) ) ;
172+ assert ! ( test_config_with_pem( ) . check_cert ( ) . is_ok ( ) ) ;
173+ assert ! ( test_config_with_invalid_pem( ) . check_cert ( ) . is_err ( ) ) ;
182174 }
183175
184176 #[ test]
185177 fn test_tls_private_key_exists_with_path ( ) {
186- assert ! ( test_config_with_path( ) . private_key_exists ( ) ) ;
187- assert ! ( ! test_config_with_invalid_path( ) . private_key_exists ( ) ) ;
178+ assert ! ( test_config_with_path( ) . check_private_key ( ) . is_ok ( ) ) ;
179+ assert ! ( test_config_with_invalid_path( ) . check_private_key ( ) . is_err ( ) ) ;
188180 }
189181
190182 #[ test]
191183 fn test_tls_private_key_exists_with_pem ( ) {
192- assert ! ( test_config_with_pem( ) . private_key_exists ( ) ) ;
193- assert ! ( ! test_config_with_invalid_pem( ) . private_key_exists ( ) ) ;
184+ assert ! ( test_config_with_pem( ) . check_private_key ( ) . is_ok ( ) ) ;
185+ assert ! ( test_config_with_invalid_pem( ) . check_private_key ( ) . is_err ( ) ) ;
194186 }
195187}
0 commit comments