1
- use crate :: Error ;
1
+ use crate :: { Error , InvalidAsn1String } ;
2
2
use std:: str:: FromStr ;
3
3
4
4
/// ASN.1 `PrintableString` type.
@@ -93,7 +93,11 @@ impl TryFrom<String> for PrintableString {
93
93
| b':'
94
94
| b'='
95
95
| b'?' => ( ) ,
96
- _ => return Err ( Error :: InvalidPrintableString ) ,
96
+ _ => {
97
+ return Err ( Error :: InvalidAsn1String (
98
+ InvalidAsn1String :: PrintableString ( value) ,
99
+ ) )
100
+ } ,
97
101
}
98
102
}
99
103
Ok ( Self ( value) )
@@ -168,7 +172,9 @@ impl TryFrom<String> for Ia5String {
168
172
/// See [`Ia5String`] documentation for more information.
169
173
fn try_from ( input : String ) -> Result < Self , Error > {
170
174
if !input. is_ascii ( ) {
171
- return Err ( Error :: InvalidIa5String ) ;
175
+ return Err ( Error :: InvalidAsn1String ( InvalidAsn1String :: Ia5String (
176
+ input,
177
+ ) ) ) ;
172
178
}
173
179
Ok ( Self ( input) )
174
180
}
@@ -251,7 +257,9 @@ impl TryFrom<String> for TeletexString {
251
257
fn try_from ( input : String ) -> Result < Self , Error > {
252
258
// Check all bytes are visible
253
259
if !input. as_bytes ( ) . iter ( ) . all ( |b| ( 0x20 ..=0x7f ) . contains ( b) ) {
254
- return Err ( Error :: InvalidTeletexString ) ;
260
+ return Err ( Error :: InvalidAsn1String ( InvalidAsn1String :: TeletexString (
261
+ input,
262
+ ) ) ) ;
255
263
}
256
264
Ok ( Self ( input) )
257
265
}
@@ -318,7 +326,9 @@ impl BmpString {
318
326
/// Decode a UTF-16BE–encoded vector `vec` into a `BmpString`, returning [Err](`std::result::Result::Err`) if `vec` contains any invalid data.
319
327
pub fn from_utf16be ( vec : Vec < u8 > ) -> Result < Self , Error > {
320
328
if vec. len ( ) % 2 != 0 {
321
- return Err ( Error :: InvalidBmpString ) ;
329
+ return Err ( Error :: InvalidAsn1String ( InvalidAsn1String :: BmpString (
330
+ "Invalid UTF-16 encoding" . to_string ( ) ,
331
+ ) ) ) ;
322
332
}
323
333
324
334
// FIXME: Update this when `array_chunks` is stabilized.
@@ -331,7 +341,11 @@ impl BmpString {
331
341
// Character is in the Basic Multilingual Plane
332
342
Ok ( c) if ( c as u64 ) < u64:: from ( u16:: MAX ) => ( ) ,
333
343
// Characters outside Basic Multilingual Plane or unpaired surrogates
334
- _ => return Err ( Error :: InvalidBmpString ) ,
344
+ _ => {
345
+ return Err ( Error :: InvalidAsn1String ( InvalidAsn1String :: BmpString (
346
+ "Invalid UTF-16 encoding" . to_string ( ) ,
347
+ ) ) ) ;
348
+ } ,
335
349
}
336
350
}
337
351
Ok ( Self ( vec. to_vec ( ) ) )
@@ -348,10 +362,9 @@ impl TryFrom<&str> for BmpString {
348
362
///
349
363
/// The result is allocated on the heap.
350
364
fn try_from ( value : & str ) -> Result < Self , Self :: Error > {
351
- let capacity = value
352
- . len ( )
353
- . checked_mul ( 2 )
354
- . ok_or_else ( || Error :: InvalidBmpString ) ?;
365
+ let capacity = value. len ( ) . checked_mul ( 2 ) . ok_or_else ( || {
366
+ Error :: InvalidAsn1String ( InvalidAsn1String :: BmpString ( value. to_string ( ) ) )
367
+ } ) ?;
355
368
356
369
let mut bytes = Vec :: with_capacity ( capacity) ;
357
370
@@ -432,7 +445,9 @@ impl UniversalString {
432
445
/// Decode a UTF-32BE–encoded vector `vec` into a `UniversalString`, returning [Err](`std::result::Result::Err`) if `vec` contains any invalid data.
433
446
pub fn from_utf32be ( vec : Vec < u8 > ) -> Result < UniversalString , Error > {
434
447
if vec. len ( ) % 4 != 0 {
435
- return Err ( Error :: InvalidUniversalString ) ;
448
+ return Err ( Error :: InvalidAsn1String (
449
+ InvalidAsn1String :: UniversalString ( "Invalid UTF-32 encoding" . to_string ( ) ) ,
450
+ ) ) ;
436
451
}
437
452
438
453
// FIXME: Update this when `array_chunks` is stabilized.
@@ -441,7 +456,9 @@ impl UniversalString {
441
456
. map ( |chunk| u32:: from_be_bytes ( [ chunk[ 0 ] , chunk[ 1 ] , chunk[ 2 ] , chunk[ 3 ] ] ) )
442
457
{
443
458
if core:: char:: from_u32 ( maybe_char) . is_none ( ) {
444
- return Err ( Error :: InvalidUniversalString ) ;
459
+ return Err ( Error :: InvalidAsn1String (
460
+ InvalidAsn1String :: UniversalString ( "Invalid UTF-32 encoding" . to_string ( ) ) ,
461
+ ) ) ;
445
462
}
446
463
}
447
464
@@ -459,10 +476,9 @@ impl TryFrom<&str> for UniversalString {
459
476
///
460
477
/// The result is allocated on the heap.
461
478
fn try_from ( value : & str ) -> Result < Self , Self :: Error > {
462
- let capacity = value
463
- . len ( )
464
- . checked_mul ( 4 )
465
- . ok_or_else ( || Error :: InvalidUniversalString ) ?;
479
+ let capacity = value. len ( ) . checked_mul ( 4 ) . ok_or_else ( || {
480
+ Error :: InvalidAsn1String ( InvalidAsn1String :: UniversalString ( value. to_string ( ) ) )
481
+ } ) ?;
466
482
467
483
let mut bytes = Vec :: with_capacity ( capacity) ;
468
484
0 commit comments