@@ -6,18 +6,21 @@ use cryptoki::{
66 object:: { Attribute , AttributeType , KeyType , ObjectClass , ObjectHandle } ,
77} ;
88use der:: {
9- asn1:: { ObjectIdentifier , OctetStringRef } ,
9+ asn1:: { ObjectIdentifier , OctetString , OctetStringRef } ,
1010 oid:: AssociatedOid ,
1111 AnyRef , Decode , Encode ,
1212} ;
1313use ecdsa:: {
1414 elliptic_curve:: {
1515 generic_array:: ArrayLength ,
16+ ops:: Invert ,
17+ point:: PointCompression ,
1618 sec1:: { FromEncodedPoint , ModulusSize , ToEncodedPoint } ,
17- AffinePoint , CurveArithmetic , FieldBytesSize , PublicKey ,
19+ subtle:: CtOption ,
20+ AffinePoint , CurveArithmetic , FieldBytesSize , PublicKey , Scalar ,
1821 } ,
19- hazmat:: DigestPrimitive ,
20- PrimeCurve , Signature , VerifyingKey ,
22+ hazmat:: { DigestPrimitive , SignPrimitive } ,
23+ PrimeCurve , Signature , SignatureSize , SigningKey , VerifyingKey ,
2124} ;
2225use signature:: { digest:: Digest , DigestSigner } ;
2326use spki:: {
@@ -27,7 +30,7 @@ use spki::{
2730use std:: { convert:: TryFrom , ops:: Add } ;
2831use thiserror:: Error ;
2932
30- use crate :: SessionLike ;
33+ use crate :: { CryptokiImport , SessionLike } ;
3134
3235pub fn read_key < S : SessionLike , C : SignAlgorithm > (
3336 session : & S ,
7073 }
7174}
7275
76+ impl < C > CryptokiImport for SigningKey < C >
77+ where
78+ C : PrimeCurve + CurveArithmetic ,
79+ Scalar < C > : Invert < Output = CtOption < Scalar < C > > > + SignPrimitive < C > ,
80+ SignatureSize < C > : ArrayLength < u8 > ,
81+
82+ C : AssociatedOid ,
83+ {
84+ fn put_key < S : SessionLike > (
85+ & self ,
86+ session : & S ,
87+ template : impl Into < Vec < Attribute > > ,
88+ ) -> cryptoki:: error:: Result < ObjectHandle > {
89+ let mut template = template. into ( ) ;
90+ template. push ( Attribute :: Class ( ObjectClass :: PRIVATE_KEY ) ) ;
91+ template. push ( Attribute :: KeyType ( KeyType :: EC ) ) ;
92+ template. push ( Attribute :: EcParams ( C :: OID . to_der ( ) . unwrap ( ) ) ) ;
93+ template. push ( Attribute :: Value ( self . to_bytes ( ) . as_slice ( ) . to_vec ( ) ) ) ;
94+
95+ let handle = session. create_object ( & template) ?;
96+
97+ Ok ( handle)
98+ }
99+ }
100+
101+ impl < C > CryptokiImport for VerifyingKey < C >
102+ where
103+ C : PrimeCurve + CurveArithmetic + PointCompression ,
104+ AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
105+ FieldBytesSize < C > : ModulusSize ,
106+ C : AssociatedOid ,
107+ {
108+ fn put_key < S : SessionLike > (
109+ & self ,
110+ session : & S ,
111+ template : impl Into < Vec < Attribute > > ,
112+ ) -> cryptoki:: error:: Result < ObjectHandle > {
113+ let mut template = template. into ( ) ;
114+ template. push ( Attribute :: Class ( ObjectClass :: PUBLIC_KEY ) ) ;
115+ template. push ( Attribute :: KeyType ( KeyType :: EC ) ) ;
116+ template. push ( Attribute :: EcParams ( C :: OID . to_der ( ) . unwrap ( ) ) ) ;
117+ let ec_point = OctetString :: new ( self . to_sec1_bytes ( ) ) . unwrap ( ) ;
118+ template. push ( Attribute :: EcPoint ( ec_point. to_der ( ) . unwrap ( ) ) ) ;
119+
120+ let handle = session. create_object ( & template) ?;
121+
122+ Ok ( handle)
123+ }
124+ }
125+
73126#[ derive( Error , Debug ) ]
74127pub enum Error {
75128 #[ error( "Cryptoki error: {0}" ) ]
@@ -119,8 +172,6 @@ where
119172 pub fn new ( session : S , label : & [ u8 ] ) -> Result < Self , Error > {
120173 // First we'll lookup a private key with that label.
121174 let template = vec ! [
122- Attribute :: Token ( true ) ,
123- Attribute :: Private ( true ) ,
124175 Attribute :: Label ( label. to_vec( ) ) ,
125176 Attribute :: Class ( ObjectClass :: PRIVATE_KEY ) ,
126177 Attribute :: KeyType ( KeyType :: EC ) ,
0 commit comments