Skip to content

Commit fd0c5c4

Browse files
author
Volodymyr Paprotski
committed
[FAB-1648] PKCS11 BCCSP now calls PKCS11 functions
For all ECDSA operations, replace current PKCS11 CSP software calls with PKCS11 operations Note: KeyImport and KeyDerivation should not be allowed by a properly configured crypto card, but it does work with SoftHSM and is legal PKCS11 Change-Id: I0087c86cda048bf5f8df965580bef8896984e897 Signed-off-by: Volodymyr Paprotski <[email protected]>
1 parent cafeaf1 commit fd0c5c4

File tree

7 files changed

+218
-181
lines changed

7 files changed

+218
-181
lines changed

bccsp/pkcs11/conf.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ limitations under the License.
1616
package pkcs11
1717

1818
import (
19-
"crypto/elliptic"
2019
"crypto/sha256"
2120
"crypto/sha512"
21+
"encoding/asn1"
2222
"fmt"
2323
"hash"
2424

@@ -30,7 +30,7 @@ type config struct {
3030
securityLevel int
3131
hashFamily string
3232

33-
ellipticCurve elliptic.Curve
33+
ellipticCurve asn1.ObjectIdentifier
3434
hashFunction func() hash.Hash
3535
aesBitLength int
3636
rsaBitLength int
@@ -51,12 +51,12 @@ func (conf *config) setSecurityLevel(securityLevel int, hashFamily string) (err
5151
func (conf *config) setSecurityLevelSHA2(level int) (err error) {
5252
switch level {
5353
case 256:
54-
conf.ellipticCurve = elliptic.P256()
54+
conf.ellipticCurve = oidNamedCurveP256
5555
conf.hashFunction = sha256.New
5656
conf.rsaBitLength = 2048
5757
conf.aesBitLength = 32
5858
case 384:
59-
conf.ellipticCurve = elliptic.P384()
59+
conf.ellipticCurve = oidNamedCurveP384
6060
conf.hashFunction = sha512.New384
6161
conf.rsaBitLength = 3072
6262
conf.aesBitLength = 32
@@ -69,12 +69,12 @@ func (conf *config) setSecurityLevelSHA2(level int) (err error) {
6969
func (conf *config) setSecurityLevelSHA3(level int) (err error) {
7070
switch level {
7171
case 256:
72-
conf.ellipticCurve = elliptic.P256()
72+
conf.ellipticCurve = oidNamedCurveP256
7373
conf.hashFunction = sha3.New256
7474
conf.rsaBitLength = 2048
7575
conf.aesBitLength = 32
7676
case 384:
77-
conf.ellipticCurve = elliptic.P384()
77+
conf.ellipticCurve = oidNamedCurveP384
7878
conf.hashFunction = sha3.New384
7979
conf.rsaBitLength = 3072
8080
conf.aesBitLength = 32

bccsp/pkcs11/ecdsa.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ limitations under the License.
1616
package pkcs11
1717

1818
import (
19-
"crypto/ecdsa"
2019
"crypto/elliptic"
21-
"crypto/rand"
2220
"encoding/asn1"
2321
"errors"
2422
"fmt"
@@ -74,44 +72,44 @@ func unmarshalECDSASignature(raw []byte) (*big.Int, *big.Int, error) {
7472
return sig.R, sig.S, nil
7573
}
7674

77-
func (csp *impl) signECDSA(k *ecdsa.PrivateKey, digest []byte, opts bccsp.SignerOpts) (signature []byte, err error) {
78-
r, s, err := ecdsa.Sign(rand.Reader, k, digest)
75+
func (csp *impl) signECDSA(k ecdsaPrivateKey, digest []byte, opts bccsp.SignerOpts) (signature []byte, err error) {
76+
r, s, err := signECDSA(k.ski, digest)
7977
if err != nil {
8078
return nil, err
8179
}
8280

8381
// check for low-S
84-
halfOrder, ok := curveHalfOrders[k.Curve]
82+
halfOrder, ok := curveHalfOrders[k.pub.pub.Curve]
8583
if !ok {
86-
return nil, fmt.Errorf("Curve not recognized [%s]", k.Curve)
84+
return nil, fmt.Errorf("Curve not recognized [%s]", k.pub.pub.Curve)
8785
}
8886

8987
// is s > halfOrder Then
9088
if s.Cmp(halfOrder) == 1 {
9189
// Set s to N - s that will be then in the lower part of signature space
9290
// less or equal to half order
93-
s.Sub(k.Params().N, s)
91+
s.Sub(k.pub.pub.Params().N, s)
9492
}
9593

9694
return marshalECDSASignature(r, s)
9795
}
9896

99-
func (csp *impl) verifyECDSA(k *ecdsa.PublicKey, signature, digest []byte, opts bccsp.SignerOpts) (valid bool, err error) {
97+
func (csp *impl) verifyECDSA(k ecdsaPublicKey, signature, digest []byte, opts bccsp.SignerOpts) (valid bool, err error) {
10098
r, s, err := unmarshalECDSASignature(signature)
10199
if err != nil {
102100
return false, fmt.Errorf("Failed unmashalling signature [%s]", err)
103101
}
104102

105103
// check for low-S
106-
halfOrder, ok := curveHalfOrders[k.Curve]
104+
halfOrder, ok := curveHalfOrders[k.pub.Curve]
107105
if !ok {
108-
return false, fmt.Errorf("Curve not recognized [%s]", k.Curve)
106+
return false, fmt.Errorf("Curve not recognized [%s]", k.pub.Curve)
109107
}
110108

111109
// If s > halfOrder Then
112110
if s.Cmp(halfOrder) == 1 {
113111
return false, fmt.Errorf("Invalid S. Must be smaller than half the order [%s][%s].", s, halfOrder)
114112
}
115113

116-
return ecdsa.Verify(k, digest, r, s), nil
114+
return verifyECDSA(k.ski, digest, r, s)
117115
}

bccsp/pkcs11/ecdsakey.go

+9-32
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,15 @@ package pkcs11
1818
import (
1919
"crypto/ecdsa"
2020
"crypto/x509"
21-
"fmt"
22-
23-
"crypto/sha256"
24-
2521
"errors"
26-
27-
"crypto/elliptic"
22+
"fmt"
2823

2924
"github.com/hyperledger/fabric/bccsp"
3025
)
3126

3227
type ecdsaPrivateKey struct {
33-
privKey *ecdsa.PrivateKey
28+
ski []byte
29+
pub ecdsaPublicKey
3430
}
3531

3632
// Bytes converts this key to its byte representation,
@@ -41,17 +37,7 @@ func (k *ecdsaPrivateKey) Bytes() (raw []byte, err error) {
4137

4238
// SKI returns the subject key identifier of this key.
4339
func (k *ecdsaPrivateKey) SKI() (ski []byte) {
44-
if k.privKey == nil {
45-
return nil
46-
}
47-
48-
// Marshall the public key
49-
raw := elliptic.Marshal(k.privKey.Curve, k.privKey.PublicKey.X, k.privKey.PublicKey.Y)
50-
51-
// Hash it
52-
hash := sha256.New()
53-
hash.Write(raw)
54-
return hash.Sum(nil)
40+
return k.ski
5541
}
5642

5743
// Symmetric returns true if this key is a symmetric key,
@@ -69,17 +55,18 @@ func (k *ecdsaPrivateKey) Private() bool {
6955
// PublicKey returns the corresponding public key part of an asymmetric public/private key pair.
7056
// This method returns an error in symmetric key schemes.
7157
func (k *ecdsaPrivateKey) PublicKey() (bccsp.Key, error) {
72-
return &ecdsaPublicKey{&k.privKey.PublicKey}, nil
58+
return &k.pub, nil
7359
}
7460

7561
type ecdsaPublicKey struct {
76-
pubKey *ecdsa.PublicKey
62+
ski []byte
63+
pub *ecdsa.PublicKey
7764
}
7865

7966
// Bytes converts this key to its byte representation,
8067
// if this operation is allowed.
8168
func (k *ecdsaPublicKey) Bytes() (raw []byte, err error) {
82-
raw, err = x509.MarshalPKIXPublicKey(k.pubKey)
69+
raw, err = x509.MarshalPKIXPublicKey(k.pub)
8370
if err != nil {
8471
return nil, fmt.Errorf("Failed marshalling key [%s]", err)
8572
}
@@ -88,17 +75,7 @@ func (k *ecdsaPublicKey) Bytes() (raw []byte, err error) {
8875

8976
// SKI returns the subject key identifier of this key.
9077
func (k *ecdsaPublicKey) SKI() (ski []byte) {
91-
if k.pubKey == nil {
92-
return nil
93-
}
94-
95-
// Marshall the public key
96-
raw := elliptic.Marshal(k.pubKey.Curve, k.pubKey.X, k.pubKey.Y)
97-
98-
// Hash it
99-
hash := sha256.New()
100-
hash.Write(raw)
101-
return hash.Sum(nil)
78+
return k.ski
10279
}
10380

10481
// Symmetric returns true if this key is a symmetric key,

bccsp/pkcs11/fileks.go

+5-28
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@ limitations under the License.
1616
package pkcs11
1717

1818
import (
19-
"io/ioutil"
20-
"os"
21-
"sync"
22-
23-
"errors"
24-
"strings"
25-
26-
"crypto/ecdsa"
2719
"crypto/rsa"
2820
"encoding/hex"
21+
"errors"
2922
"fmt"
23+
"io/ioutil"
24+
"os"
3025
"path/filepath"
26+
"strings"
27+
"sync"
3128

3229
"github.com/hyperledger/fabric/bccsp"
3330
"github.com/hyperledger/fabric/bccsp/utils"
@@ -127,8 +124,6 @@ func (ks *FileBasedKeyStore) GetKey(ski []byte) (k bccsp.Key, err error) {
127124
}
128125

129126
switch key.(type) {
130-
case *ecdsa.PrivateKey:
131-
return &ecdsaPrivateKey{key.(*ecdsa.PrivateKey)}, nil
132127
case *rsa.PrivateKey:
133128
return &rsaPrivateKey{key.(*rsa.PrivateKey)}, nil
134129
default:
@@ -142,8 +137,6 @@ func (ks *FileBasedKeyStore) GetKey(ski []byte) (k bccsp.Key, err error) {
142137
}
143138

144139
switch key.(type) {
145-
case *ecdsa.PublicKey:
146-
return &ecdsaPublicKey{key.(*ecdsa.PublicKey)}, nil
147140
case *rsa.PublicKey:
148141
return &rsaPublicKey{key.(*rsa.PublicKey)}, nil
149142
default:
@@ -165,22 +158,6 @@ func (ks *FileBasedKeyStore) StoreKey(k bccsp.Key) (err error) {
165158
return errors.New("Invalid key. It must be different from nil.")
166159
}
167160
switch k.(type) {
168-
case *ecdsaPrivateKey:
169-
kk := k.(*ecdsaPrivateKey)
170-
171-
err = ks.storePrivateKey(hex.EncodeToString(k.SKI()), kk.privKey)
172-
if err != nil {
173-
return fmt.Errorf("Failed storing ECDSA private key [%s]", err)
174-
}
175-
176-
case *ecdsaPublicKey:
177-
kk := k.(*ecdsaPublicKey)
178-
179-
err = ks.storePublicKey(hex.EncodeToString(k.SKI()), kk.pubKey)
180-
if err != nil {
181-
return fmt.Errorf("Failed storing ECDSA public key [%s]", err)
182-
}
183-
184161
case *rsaPrivateKey:
185162
kk := k.(*rsaPrivateKey)
186163

bccsp/pkcs11/fileks_test.go

-10
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,6 @@ func TestInvalidStoreKey(t *testing.T) {
3434
t.Fatal("Error should be different from nil in this case")
3535
}
3636

37-
err = ks.StoreKey(&ecdsaPrivateKey{nil})
38-
if err == nil {
39-
t.Fatal("Error should be different from nil in this case")
40-
}
41-
42-
err = ks.StoreKey(&ecdsaPublicKey{nil})
43-
if err == nil {
44-
t.Fatal("Error should be different from nil in this case")
45-
}
46-
4737
err = ks.StoreKey(&rsaPublicKey{nil})
4838
if err == nil {
4939
t.Fatal("Error should be different from nil in this case")

0 commit comments

Comments
 (0)