@@ -6,18 +6,21 @@ use cryptoki::{
6
6
object:: { Attribute , AttributeType , KeyType , ObjectClass , ObjectHandle } ,
7
7
} ;
8
8
use der:: {
9
- asn1:: { ObjectIdentifier , OctetStringRef } ,
9
+ asn1:: { ObjectIdentifier , OctetString , OctetStringRef } ,
10
10
oid:: AssociatedOid ,
11
11
AnyRef , Decode , Encode ,
12
12
} ;
13
13
use ecdsa:: {
14
14
elliptic_curve:: {
15
15
generic_array:: ArrayLength ,
16
+ ops:: Invert ,
17
+ point:: PointCompression ,
16
18
sec1:: { FromEncodedPoint , ModulusSize , ToEncodedPoint } ,
17
- AffinePoint , CurveArithmetic , FieldBytesSize , PublicKey ,
19
+ subtle:: CtOption ,
20
+ AffinePoint , CurveArithmetic , FieldBytesSize , PublicKey , Scalar ,
18
21
} ,
19
- hazmat:: DigestPrimitive ,
20
- PrimeCurve , Signature , VerifyingKey ,
22
+ hazmat:: { DigestPrimitive , SignPrimitive } ,
23
+ PrimeCurve , Signature , SignatureSize , SigningKey , VerifyingKey ,
21
24
} ;
22
25
use signature:: { digest:: Digest , DigestSigner } ;
23
26
use spki:: {
@@ -27,7 +30,7 @@ use spki::{
27
30
use std:: { convert:: TryFrom , ops:: Add } ;
28
31
use thiserror:: Error ;
29
32
30
- use crate :: SessionLike ;
33
+ use crate :: { CryptokiImport , SessionLike } ;
31
34
32
35
pub fn read_key < S : SessionLike , C : SignAlgorithm > (
33
36
session : & S ,
70
73
}
71
74
}
72
75
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
+
73
126
#[ derive( Error , Debug ) ]
74
127
pub enum Error {
75
128
#[ error( "Cryptoki error: {0}" ) ]
@@ -119,8 +172,6 @@ where
119
172
pub fn new ( session : S , label : & [ u8 ] ) -> Result < Self , Error > {
120
173
// First we'll lookup a private key with that label.
121
174
let template = vec ! [
122
- Attribute :: Token ( true ) ,
123
- Attribute :: Private ( true ) ,
124
175
Attribute :: Label ( label. to_vec( ) ) ,
125
176
Attribute :: Class ( ObjectClass :: PRIVATE_KEY ) ,
126
177
Attribute :: KeyType ( KeyType :: EC ) ,
0 commit comments