@@ -119,7 +119,7 @@ pub fn generate_simple_self_signed(
119
119
) -> Result < CertifiedKey , Error > {
120
120
let key_pair = KeyPair :: generate ( ) ?;
121
121
let cert =
122
- Certificate :: generate_self_signed ( CertificateParams :: new ( subject_alt_names) , & key_pair) ?;
122
+ Certificate :: generate_self_signed ( CertificateParams :: new ( subject_alt_names) ? , & key_pair) ?;
123
123
Ok ( CertifiedKey { cert, key_pair } )
124
124
}
125
125
@@ -152,9 +152,9 @@ const ENCODE_CONFIG: pem::EncodeConfig = {
152
152
/// The type of subject alt name
153
153
pub enum SanType {
154
154
/// Also known as E-Mail address
155
- Rfc822Name ( String ) ,
156
- DnsName ( String ) ,
157
- URI ( String ) ,
155
+ Rfc822Name ( Ia5String ) ,
156
+ DnsName ( Ia5String ) ,
157
+ URI ( Ia5String ) ,
158
158
IpAddress ( IpAddr ) ,
159
159
}
160
160
@@ -174,10 +174,12 @@ impl SanType {
174
174
fn try_from_general ( name : & x509_parser:: extensions:: GeneralName < ' _ > ) -> Result < Self , Error > {
175
175
Ok ( match name {
176
176
x509_parser:: extensions:: GeneralName :: RFC822Name ( name) => {
177
- SanType :: Rfc822Name ( ( * name) . into ( ) )
177
+ SanType :: Rfc822Name ( ( * name) . try_into ( ) ? )
178
178
} ,
179
- x509_parser:: extensions:: GeneralName :: DNSName ( name) => SanType :: DnsName ( ( * name) . into ( ) ) ,
180
- x509_parser:: extensions:: GeneralName :: URI ( name) => SanType :: URI ( ( * name) . into ( ) ) ,
179
+ x509_parser:: extensions:: GeneralName :: DNSName ( name) => {
180
+ SanType :: DnsName ( ( * name) . try_into ( ) ?)
181
+ } ,
182
+ x509_parser:: extensions:: GeneralName :: URI ( name) => SanType :: URI ( ( * name) . try_into ( ) ?) ,
181
183
x509_parser:: extensions:: GeneralName :: IPAddress ( octets) => {
182
184
SanType :: IpAddress ( ip_addr_from_octets ( octets) ?)
183
185
} ,
@@ -582,19 +584,21 @@ impl Default for CertificateParams {
582
584
583
585
impl CertificateParams {
584
586
/// Generate certificate parameters with reasonable defaults
585
- pub fn new ( subject_alt_names : impl Into < Vec < String > > ) -> Self {
587
+ pub fn new ( subject_alt_names : impl Into < Vec < String > > ) -> Result < Self , Error > {
586
588
let subject_alt_names = subject_alt_names
587
589
. into ( )
588
590
. into_iter ( )
589
- . map ( |s| match s. parse ( ) {
590
- Ok ( ip) => SanType :: IpAddress ( ip) ,
591
- Err ( _) => SanType :: DnsName ( s) ,
591
+ . map ( |s| {
592
+ Ok ( match IpAddr :: from_str ( & s) {
593
+ Ok ( ip) => SanType :: IpAddress ( ip) ,
594
+ Err ( _) => SanType :: DnsName ( s. try_into ( ) ?) ,
595
+ } )
592
596
} )
593
- . collect :: < Vec < _ > > ( ) ;
594
- CertificateParams {
597
+ . collect :: < Result < Vec < _ > , _ > > ( ) ? ;
598
+ Ok ( CertificateParams {
595
599
subject_alt_names,
596
600
..Default :: default ( )
597
- }
601
+ } )
598
602
}
599
603
600
604
/// Parses an existing ca certificate from the ASCII PEM format.
@@ -854,7 +858,7 @@ impl CertificateParams {
854
858
|writer| match san {
855
859
SanType :: Rfc822Name ( name)
856
860
| SanType :: DnsName ( name)
857
- | SanType :: URI ( name) => writer. write_ia5_string ( name) ,
861
+ | SanType :: URI ( name) => writer. write_ia5_string ( name. as_str ( ) ) ,
858
862
SanType :: IpAddress ( IpAddr :: V4 ( addr) ) => {
859
863
writer. write_bytes ( & addr. octets ( ) )
860
864
} ,
0 commit comments