1414
1515use std:: { ffi:: CString , fmt:: Debug , sync:: Arc } ;
1616
17+ use der:: {
18+ asn1:: { ObjectIdentifier , OctetString } ,
19+ Encode ,
20+ } ;
1721use native_pkcs11_traits:: {
1822 backend,
1923 Certificate ,
@@ -22,11 +26,7 @@ use native_pkcs11_traits::{
2226 PrivateKey ,
2327 PublicKey ,
2428} ;
25-
26- use spki:: SubjectPublicKeyInfoRef ;
2729use pkcs1:: { der:: Decode , RsaPublicKey } ;
28- use der:: { asn1:: OctetString , asn1:: ObjectIdentifier , Encode } ;
29-
3030use pkcs11_sys:: {
3131 CKC_X_509 ,
3232 CKK_EC ,
@@ -38,6 +38,7 @@ use pkcs11_sys::{
3838 CK_CERTIFICATE_CATEGORY_UNSPECIFIED ,
3939 CK_PROFILE_ID ,
4040} ;
41+ use spki:: SubjectPublicKeyInfoRef ;
4142use tracing:: debug;
4243
4344use crate :: attribute:: { Attribute , AttributeType , Attributes } ;
@@ -79,8 +80,14 @@ fn extract_ec_params(der_bytes: &[u8]) -> Option<(Vec<u8>, Vec<u8>)> {
7980 // For EC keys, the algorithm parameters contain the curve OID
8081 // For EC keys, the subject public key is the EC point
8182 Some ( (
82- ObjectIdentifier :: from_bytes ( spki. algorithm . parameters . unwrap ( ) . value ( ) ) . unwrap ( ) . to_der ( ) . unwrap ( ) ,
83- OctetString :: new ( spki. subject_public_key . raw_bytes ( ) ) . unwrap ( ) . to_der ( ) . unwrap ( ) ,
83+ ObjectIdentifier :: from_bytes ( spki. algorithm . parameters . unwrap ( ) . value ( ) )
84+ . unwrap ( )
85+ . to_der ( )
86+ . unwrap ( ) ,
87+ OctetString :: new ( spki. subject_public_key . raw_bytes ( ) )
88+ . unwrap ( )
89+ . to_der ( )
90+ . unwrap ( ) ,
8491 ) )
8592}
8693
@@ -136,12 +143,10 @@ impl Object {
136143 . flatten ( )
137144 . and_then ( |public_key| {
138145 let der_bytes = public_key. to_der ( ) ;
139- extract_ec_params ( & der_bytes) . map ( |( params, point) | {
140- match type_ {
141- AttributeType :: EcParams => Attribute :: EcParams ( params) ,
142- AttributeType :: EcPoint => Attribute :: EcPoint ( point) ,
143- _ => unreachable ! ( )
144- }
146+ extract_ec_params ( & der_bytes) . map ( |( params, point) | match type_ {
147+ AttributeType :: EcParams => Attribute :: EcParams ( params) ,
148+ AttributeType :: EcPoint => Attribute :: EcPoint ( point) ,
149+ _ => unreachable ! ( ) ,
145150 } )
146151 } )
147152 }
@@ -153,7 +158,9 @@ impl Object {
153158 } ) ) ,
154159 AttributeType :: Label => Some ( Attribute :: Label ( private_key. label ( ) ) ) ,
155160 AttributeType :: Local => Some ( Attribute :: Local ( false ) ) ,
156- AttributeType :: Modulus | AttributeType :: ModulusBits | AttributeType :: PublicExponent => {
161+ AttributeType :: Modulus
162+ | AttributeType :: ModulusBits
163+ | AttributeType :: PublicExponent => {
157164 if private_key. algorithm ( ) != KeyAlgorithm :: Rsa {
158165 return None ;
159166 }
@@ -167,8 +174,10 @@ impl Object {
167174 match type_ {
168175 AttributeType :: Modulus => Attribute :: Modulus ( modulus) ,
169176 AttributeType :: ModulusBits => Attribute :: ModulusBits ( bits) ,
170- AttributeType :: PublicExponent => Attribute :: PublicExponent ( exponent) ,
171- _ => unreachable ! ( )
177+ AttributeType :: PublicExponent => {
178+ Attribute :: PublicExponent ( exponent)
179+ }
180+ _ => unreachable ! ( ) ,
172181 }
173182 } )
174183 } )
@@ -203,18 +212,18 @@ impl Object {
203212 AttributeType :: Derive => Some ( Attribute :: Derive ( false ) ) ,
204213 AttributeType :: Label => Some ( Attribute :: Label ( pk. label ( ) ) ) ,
205214 AttributeType :: Local => Some ( Attribute :: Local ( false ) ) ,
206- AttributeType :: Modulus | AttributeType :: ModulusBits | AttributeType :: PublicExponent => {
215+ AttributeType :: Modulus
216+ | AttributeType :: ModulusBits
217+ | AttributeType :: PublicExponent => {
207218 if pk. algorithm ( ) != KeyAlgorithm :: Rsa {
208219 return None ;
209220 }
210221 let der_bytes = pk. to_der ( ) ;
211- extract_rsa_params ( & der_bytes) . map ( |( modulus, exponent, bits) | {
212- match type_ {
213- AttributeType :: Modulus => Attribute :: Modulus ( modulus) ,
214- AttributeType :: ModulusBits => Attribute :: ModulusBits ( bits) ,
215- AttributeType :: PublicExponent => Attribute :: PublicExponent ( exponent) ,
216- _ => unreachable ! ( )
217- }
222+ extract_rsa_params ( & der_bytes) . map ( |( modulus, exponent, bits) | match type_ {
223+ AttributeType :: Modulus => Attribute :: Modulus ( modulus) ,
224+ AttributeType :: ModulusBits => Attribute :: ModulusBits ( bits) ,
225+ AttributeType :: PublicExponent => Attribute :: PublicExponent ( exponent) ,
226+ _ => unreachable ! ( ) ,
218227 } )
219228 }
220229 AttributeType :: KeyType => Some ( Attribute :: KeyType ( match pk. algorithm ( ) {
@@ -227,12 +236,10 @@ impl Object {
227236 return None ;
228237 }
229238 let der_bytes = pk. to_der ( ) ;
230- extract_ec_params ( & der_bytes) . map ( |( params, point) | {
231- match type_ {
232- AttributeType :: EcParams => Attribute :: EcParams ( params) ,
233- AttributeType :: EcPoint => Attribute :: EcPoint ( point) ,
234- _ => unreachable ! ( )
235- }
239+ extract_ec_params ( & der_bytes) . map ( |( params, point) | match type_ {
240+ AttributeType :: EcParams => Attribute :: EcParams ( params) ,
241+ AttributeType :: EcPoint => Attribute :: EcPoint ( point) ,
242+ _ => unreachable ! ( ) ,
236243 } )
237244 }
238245 _ => {
0 commit comments