@@ -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 ,
6972 }
7073}
7174
75+ impl < C > CryptokiImport for SigningKey < C >
76+ where
77+ C : PrimeCurve + CurveArithmetic ,
78+ Scalar < C > : Invert < Output = CtOption < Scalar < C > > > + SignPrimitive < C > ,
79+ SignatureSize < C > : ArrayLength < u8 > ,
80+
81+ C : AssociatedOid ,
82+ {
83+ fn put_key < S : SessionLike > (
84+ & self ,
85+ session : & S ,
86+ template : impl Into < Vec < Attribute > > ,
87+ ) -> cryptoki:: error:: Result < ObjectHandle > {
88+ let mut template = template. into ( ) ;
89+ template. push ( Attribute :: Class ( ObjectClass :: PRIVATE_KEY ) ) ;
90+ template. push ( Attribute :: KeyType ( KeyType :: EC ) ) ;
91+ template. push ( Attribute :: EcParams ( C :: OID . to_der ( ) . unwrap ( ) ) ) ;
92+ template. push ( Attribute :: Value ( self . to_bytes ( ) . as_slice ( ) . to_vec ( ) ) ) ;
93+
94+ let handle = session. create_object ( & template) ?;
95+
96+ Ok ( handle)
97+ }
98+ }
99+
100+ impl < C > CryptokiImport for VerifyingKey < C >
101+ where
102+ C : PrimeCurve + CurveArithmetic + PointCompression ,
103+ AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
104+ FieldBytesSize < C > : ModulusSize ,
105+ C : AssociatedOid ,
106+ {
107+ fn put_key < S : SessionLike > (
108+ & self ,
109+ session : & S ,
110+ template : impl Into < Vec < Attribute > > ,
111+ ) -> cryptoki:: error:: Result < ObjectHandle > {
112+ let mut template = template. into ( ) ;
113+ template. push ( Attribute :: Class ( ObjectClass :: PUBLIC_KEY ) ) ;
114+ template. push ( Attribute :: KeyType ( KeyType :: EC ) ) ;
115+ template. push ( Attribute :: EcParams ( C :: OID . to_der ( ) . unwrap ( ) ) ) ;
116+ let ec_point = OctetString :: new ( self . to_sec1_bytes ( ) ) . unwrap ( ) ;
117+ template. push ( Attribute :: EcPoint ( ec_point. to_der ( ) . unwrap ( ) ) ) ;
118+
119+ let handle = session. create_object ( & template) ?;
120+
121+ Ok ( handle)
122+ }
123+ }
124+
72125#[ derive( Error , Debug ) ]
73126pub enum Error {
74127 #[ error( "Cryptoki error: {0}" ) ]
@@ -118,8 +171,6 @@ where
118171 pub fn new ( session : S , label : & [ u8 ] ) -> Result < Self , Error > {
119172 // First we'll lookup a private key with that label.
120173 let template = vec ! [
121- Attribute :: Token ( true ) ,
122- Attribute :: Private ( true ) ,
123174 Attribute :: Label ( label. to_vec( ) ) ,
124175 Attribute :: Class ( ObjectClass :: PRIVATE_KEY ) ,
125176 Attribute :: KeyType ( KeyType :: EC ) ,
0 commit comments