@@ -13,18 +13,12 @@ use std::path::PathBuf;
1313use crate :: commands:: { BasicResult , Dispatch } ;
1414use crate :: error:: HsmError ;
1515use crate :: module:: Module ;
16- use crate :: util:: attribute:: { AttributeMap , AttributeType , KeyType , ObjectClass } ;
16+ use crate :: util:: attribute:: { AttributeMap , KeyType , ObjectClass } ;
1717use crate :: util:: helper;
18+ use crate :: util:: key:: mldsa;
1819use crate :: util:: key:: KeyEncoding ;
1920use crate :: util:: wrap:: { Wrap , WrapPrivateKey } ;
2021
21- use der:: { Encode , EncodePem } ;
22- use ml_dsa:: {
23- EncodedSigningKey , EncodedVerifyingKey , MlDsa44 , MlDsa65 , MlDsa87 , SigningKey , VerifyingKey ,
24- } ;
25- use pkcs8:: { LineEnding , PrivateKeyInfo } ;
26- use spki:: { AssociatedAlgorithmIdentifier , EncodePublicKey } ;
27-
2822#[ derive( clap:: Args , Debug , Serialize , Deserialize ) ]
2923pub struct Export {
3024 #[ arg( long) ]
@@ -48,89 +42,13 @@ pub struct Export {
4842impl Export {
4943 fn export ( & self , session : & Session , object : ObjectHandle ) -> Result < ( ) > {
5044 let map = AttributeMap :: from_object ( session, object) ?;
51- let val = map
52- . get ( & AttributeType :: Value )
53- . ok_or ( anyhow ! ( "Key does not contain a value" ) ) ?;
54- let key_value: Vec < u8 > = val. try_into ( ) ?;
55-
56- let encoded_bytes = if self . private {
57- if let Ok ( arr) = EncodedSigningKey :: < MlDsa44 > :: try_from ( key_value. as_slice ( ) ) {
58- let _ = SigningKey :: < MlDsa44 > :: decode ( & arr) ; // Validate
59- let pk_info = PrivateKeyInfo :: new ( MlDsa44 :: ALGORITHM_IDENTIFIER , & key_value) ;
60- match self . format {
61- KeyEncoding :: Der | KeyEncoding :: Pkcs8Der => pk_info. to_der ( ) ?,
62- KeyEncoding :: Pem | KeyEncoding :: Pkcs8Pem => {
63- pk_info. to_pem ( LineEnding :: LF ) ?. as_bytes ( ) . to_vec ( )
64- }
65- _ => return Err ( anyhow ! ( "Unsupported format for MLDSA export" ) ) ,
66- }
67- } else if let Ok ( arr) = EncodedSigningKey :: < MlDsa65 > :: try_from ( key_value. as_slice ( ) ) {
68- let _ = SigningKey :: < MlDsa65 > :: decode ( & arr) ; // Validate
69- let pk_info = PrivateKeyInfo :: new ( MlDsa65 :: ALGORITHM_IDENTIFIER , & key_value) ;
70- match self . format {
71- KeyEncoding :: Der | KeyEncoding :: Pkcs8Der => pk_info. to_der ( ) ?,
72- KeyEncoding :: Pem | KeyEncoding :: Pkcs8Pem => {
73- pk_info. to_pem ( LineEnding :: LF ) ?. as_bytes ( ) . to_vec ( )
74- }
75- _ => return Err ( anyhow ! ( "Unsupported format for MLDSA export" ) ) ,
76- }
77- } else if let Ok ( arr) = EncodedSigningKey :: < MlDsa87 > :: try_from ( key_value. as_slice ( ) ) {
78- let _ = SigningKey :: < MlDsa87 > :: decode ( & arr) ; // Validate
79- let pk_info = PrivateKeyInfo :: new ( MlDsa87 :: ALGORITHM_IDENTIFIER , & key_value) ;
80- match self . format {
81- KeyEncoding :: Der | KeyEncoding :: Pkcs8Der => pk_info. to_der ( ) ?,
82- KeyEncoding :: Pem | KeyEncoding :: Pkcs8Pem => {
83- pk_info. to_pem ( LineEnding :: LF ) ?. as_bytes ( ) . to_vec ( )
84- }
85- _ => return Err ( anyhow ! ( "Unsupported format for MLDSA export" ) ) ,
86- }
87- } else {
88- return Err ( anyhow ! (
89- "Could not decode MLDSA private key (length: {})" ,
90- key_value. len( )
91- ) ) ;
92- }
93- } else if let Ok ( arr) = EncodedVerifyingKey :: < MlDsa44 > :: try_from ( key_value. as_slice ( ) ) {
94- let key = VerifyingKey :: < MlDsa44 > :: decode ( & arr) ;
95- match self . format {
96- KeyEncoding :: Der | KeyEncoding :: Pkcs8Der => {
97- key. to_public_key_der ( ) ?. as_bytes ( ) . to_vec ( )
98- }
99- KeyEncoding :: Pem | KeyEncoding :: Pkcs8Pem => {
100- key. to_public_key_pem ( LineEnding :: LF ) ?. as_bytes ( ) . to_vec ( )
101- }
102- _ => return Err ( anyhow ! ( "Unsupported format for MLDSA export" ) ) ,
103- }
104- } else if let Ok ( arr) = EncodedVerifyingKey :: < MlDsa65 > :: try_from ( key_value. as_slice ( ) ) {
105- let key = VerifyingKey :: < MlDsa65 > :: decode ( & arr) ;
106- match self . format {
107- KeyEncoding :: Der | KeyEncoding :: Pkcs8Der => {
108- key. to_public_key_der ( ) ?. as_bytes ( ) . to_vec ( )
109- }
110- KeyEncoding :: Pem | KeyEncoding :: Pkcs8Pem => {
111- key. to_public_key_pem ( LineEnding :: LF ) ?. as_bytes ( ) . to_vec ( )
112- }
113- _ => return Err ( anyhow ! ( "Unsupported format for MLDSA export" ) ) ,
114- }
115- } else if let Ok ( arr) = EncodedVerifyingKey :: < MlDsa87 > :: try_from ( key_value. as_slice ( ) ) {
116- let key = VerifyingKey :: < MlDsa87 > :: decode ( & arr) ;
117- match self . format {
118- KeyEncoding :: Der | KeyEncoding :: Pkcs8Der => {
119- key. to_public_key_der ( ) ?. as_bytes ( ) . to_vec ( )
120- }
121- KeyEncoding :: Pem | KeyEncoding :: Pkcs8Pem => {
122- key. to_public_key_pem ( LineEnding :: LF ) ?. as_bytes ( ) . to_vec ( )
123- }
124- _ => return Err ( anyhow ! ( "Unsupported format for MLDSA export" ) ) ,
125- }
45+ if self . private {
46+ let key = mldsa:: MldsaSigningKey :: try_from ( & map) ?;
47+ mldsa:: save_private_key ( & self . filename , & key, self . format ) ?;
12648 } else {
127- return Err ( anyhow ! (
128- "Could not decode MLDSA public key (length: {})" ,
129- key_value. len( )
130- ) ) ;
131- } ;
132-
133- fs:: write ( & self . filename , & encoded_bytes) ?;
49+ let key = mldsa:: MldsaVerifyingKey :: try_from ( & map) ?;
50+ mldsa:: save_public_key ( & self . filename , & key, self . format ) ?;
51+ }
13452 Ok ( ( ) )
13553 }
13654
0 commit comments