@@ -11,6 +11,8 @@ use crate::{
11
11
} ;
12
12
#[ cfg( feature = "x509-parser" ) ]
13
13
use crate :: { DistinguishedName , SanType } ;
14
+ #[ cfg( feature = "x509-parser" ) ]
15
+ use x509_parser:: asn1_rs:: Oid ;
14
16
15
17
/// A public key, extracted from a CSR
16
18
#[ derive( Debug , PartialEq , Eq , Hash ) ]
@@ -84,6 +86,23 @@ impl CertificateSigningRequestParams {
84
86
Self :: from_der ( & csr. contents ( ) . into ( ) )
85
87
}
86
88
89
+ /// Parse a certificate signing request from the ASCII PEM format
90
+ /// using the provided validator function to handle unknown extension
91
+ /// types.
92
+ ///
93
+ /// The validator function must return an error if the attribute OID or value
94
+ /// is incorrect.
95
+ ///
96
+ /// See [`from_der`](Self::from_der) for more details.
97
+ #[ cfg( all( feature = "pem" , feature = "x509-parser" ) ) ]
98
+ pub fn from_pem_validated < F > ( pem_str : & str , valid_fn : F ) -> Result < Self , Error >
99
+ where
100
+ F : FnMut ( & Oid , & [ u8 ] ) -> Result < ( ) , Error > ,
101
+ {
102
+ let csr = pem:: parse ( pem_str) . or ( Err ( Error :: CouldNotParseCertificationRequest ) ) ?;
103
+ Self :: from_der_validated ( & csr. contents ( ) . into ( ) , valid_fn)
104
+ }
105
+
87
106
/// Parse a certificate signing request from DER-encoded bytes
88
107
///
89
108
/// Currently, this only supports the `Subject Alternative Name` extension.
@@ -96,6 +115,24 @@ impl CertificateSigningRequestParams {
96
115
/// [`rustls_pemfile::csr()`]: https://docs.rs/rustls-pemfile/latest/rustls_pemfile/fn.csr.html
97
116
#[ cfg( feature = "x509-parser" ) ]
98
117
pub fn from_der ( csr : & CertificateSigningRequestDer < ' _ > ) -> Result < Self , Error > {
118
+ Self :: from_der_validated ( csr, |_, _| Ok ( ( ) ) )
119
+ }
120
+
121
+ /// Parse a certificate signing request from DER-encoded bytes using the provided
122
+ /// validator function to handle unknown extension types.
123
+ ///
124
+ /// The validator function must return an error if the attribute OID or value
125
+ /// is incorrect.
126
+ ///
127
+ /// See [`from_der`](Self::from_der) for more details.
128
+ #[ cfg( feature = "x509-parser" ) ]
129
+ pub fn from_der_validated < F > (
130
+ csr : & CertificateSigningRequestDer < ' _ > ,
131
+ mut valid_fn : F ,
132
+ ) -> Result < Self , Error >
133
+ where
134
+ F : FnMut ( & Oid , & [ u8 ] ) -> Result < ( ) , Error > ,
135
+ {
99
136
use crate :: KeyUsagePurpose ;
100
137
use x509_parser:: prelude:: FromDer ;
101
138
@@ -171,7 +208,13 @@ impl CertificateSigningRequestParams {
171
208
return Err ( Error :: UnsupportedExtension ) ;
172
209
}
173
210
} ,
174
- _ => return Err ( Error :: UnsupportedExtension ) ,
211
+ x509_parser:: extensions:: ParsedExtension :: UnsupportedExtension { oid, value } => {
212
+ valid_fn ( oid, value) ?;
213
+ } ,
214
+ other => {
215
+ dbg ! ( & other) ;
216
+ return Err ( Error :: UnsupportedExtension ) ;
217
+ }
175
218
}
176
219
}
177
220
}
0 commit comments