@@ -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 ,
69
72
}
70
73
}
71
74
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
+
72
125
#[ derive( Error , Debug ) ]
73
126
pub enum Error {
74
127
#[ error( "Cryptoki error: {0}" ) ]
@@ -118,8 +171,6 @@ where
118
171
pub fn new ( session : S , label : & [ u8 ] ) -> Result < Self , Error > {
119
172
// First we'll lookup a private key with that label.
120
173
let template = vec ! [
121
- Attribute :: Token ( true ) ,
122
- Attribute :: Private ( true ) ,
123
174
Attribute :: Label ( label. to_vec( ) ) ,
124
175
Attribute :: Class ( ObjectClass :: PRIVATE_KEY ) ,
125
176
Attribute :: KeyType ( KeyType :: EC ) ,
0 commit comments