1+ use asn1:: SimpleAsn1Readable ;
12use pyo3:: types:: IntoPyDict ;
23use pyo3:: types:: { PyAnyMethods , PyListMethods } ;
34
@@ -12,23 +13,27 @@ fn parse_name_attribute<'p>(
1213 )
1314 } ) ?;
1415 let py_tag = crate :: util:: ASN1_TYPE_TO_ENUM . get ( py) ?. get_item ( tag_val) ?;
15- let py_data = match attribute. value . tag ( ) . as_u8 ( ) {
16- // BitString tag value
17- Some ( 3 ) => pyo3:: types:: PyBytes :: new ( py, attribute. value . data ( ) ) . into_any ( ) ,
18- // BMPString tag value
19- Some ( 30 ) => {
20- let py_bytes = pyo3:: types:: PyBytes :: new ( py, attribute. value . data ( ) ) ;
21- py_bytes. call_method1 ( pyo3:: intern!( py, "decode" ) , ( "utf_16_be" , ) ) ?
16+ let py_data = match & attribute. value {
17+ cryptography_x509:: common:: AttributeValue :: AnyString ( s) => {
18+ if s. tag ( ) == asn1:: BitString :: TAG {
19+ pyo3:: types:: PyBytes :: new ( py, s. data ( ) ) . into_any ( )
20+ } else {
21+ let parsed = std:: str:: from_utf8 ( s. data ( ) ) . map_err ( |_| {
22+ pyo3:: exceptions:: PyValueError :: new_err ( "Parsing error in ASN1" )
23+ } ) ?;
24+ pyo3:: types:: PyString :: new ( py, parsed) . into_any ( )
25+ }
26+ }
27+ cryptography_x509:: common:: AttributeValue :: PrintableString ( printable_string) => {
28+ pyo3:: types:: PyString :: new ( py, printable_string. as_str ( ) ) . into_any ( )
2229 }
23- // UniversalString
24- Some ( 28 ) => {
25- let py_bytes = pyo3:: types:: PyBytes :: new ( py, attribute. value . data ( ) ) ;
30+ cryptography_x509:: common:: AttributeValue :: UniversalString ( universal_string) => {
31+ let py_bytes = pyo3:: types:: PyBytes :: new ( py, universal_string. as_utf32_be_bytes ( ) ) ;
2632 py_bytes. call_method1 ( pyo3:: intern!( py, "decode" ) , ( "utf_32_be" , ) ) ?
2733 }
28- _ => {
29- let parsed = std:: str:: from_utf8 ( attribute. value . data ( ) )
30- . map_err ( |_| pyo3:: exceptions:: PyValueError :: new_err ( "Parsing error in ASN1" ) ) ?;
31- pyo3:: types:: PyString :: new ( py, parsed) . into_any ( )
34+ cryptography_x509:: common:: AttributeValue :: BmpString ( bmp_string) => {
35+ let py_bytes = pyo3:: types:: PyBytes :: new ( py, bmp_string. as_utf16_be_bytes ( ) ) ;
36+ py_bytes. call_method1 ( pyo3:: intern!( py, "decode" ) , ( "utf_16_be" , ) ) ?
3237 }
3338 } ;
3439 let kwargs = [ ( pyo3:: intern!( py, "_validate" ) , false ) ] . into_py_dict ( py) ?;
0 commit comments