Skip to content

Commit 0b162ca

Browse files
committed
PKCS11/MSH compatible BCCSP SKI gen
This change-set introduces a new way to compute the SKI of bccsp keys that is based on what happen in the PKCS11/HSM world. The SKI of a private key is compute as SHA-256 of the der representation of the corresponding public key. For AES keys, they are prepended by 0x01, for scoping, and then hashed. Change-Id: Id4c5d4a80bdfe07feb39bebd235e0625994bf91c Signed-off-by: Angelo De Caro <[email protected]>
1 parent e0ec9f8 commit 0b162ca

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

core/crypto/bccsp/sw/aeskey.go

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func (k *aesPrivateKey) Bytes() (raw []byte, err error) {
4141
// SKI returns the subject key identifier of this key.
4242
func (k *aesPrivateKey) SKI() (ski []byte) {
4343
hash := sha256.New()
44+
hash.Write([]byte{0x01})
4445
hash.Write(k.privKey)
4546
return hash.Sum(nil)
4647
}

core/crypto/bccsp/sw/ecdsakey.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import (
2424

2525
"errors"
2626

27+
"crypto/elliptic"
28+
2729
"github.com/hyperledger/fabric/core/crypto/bccsp"
28-
"github.com/hyperledger/fabric/core/crypto/bccsp/utils"
2930
)
3031

3132
type ecdsaPrivateKey struct {
@@ -40,9 +41,14 @@ func (k *ecdsaPrivateKey) Bytes() (raw []byte, err error) {
4041

4142
// SKI returns the subject key identifier of this key.
4243
func (k *ecdsaPrivateKey) SKI() (ski []byte) {
43-
raw, _ := utils.PrivateKeyToDER(k.privKey)
44-
// TODO: Error should not be thrown. Anyway, move the marshalling at initialization.
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)
4550

51+
// Hash it
4652
hash := sha256.New()
4753
hash.Write(raw)
4854
return hash.Sum(nil)
@@ -82,9 +88,14 @@ func (k *ecdsaPublicKey) Bytes() (raw []byte, err error) {
8288

8389
// SKI returns the subject key identifier of this key.
8490
func (k *ecdsaPublicKey) SKI() (ski []byte) {
85-
raw, _ := utils.PublicKeyToPEM(k.pubKey, nil)
86-
// TODO: Error should not be thrown. Anyway, move the marshalling at initialization.
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)
8797

98+
// Hash it
8899
hash := sha256.New()
89100
hash.Write(raw)
90101
return hash.Sum(nil)

core/crypto/bccsp/sw/rsakey.go

+26-5
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,18 @@ import (
2424

2525
"errors"
2626

27+
"encoding/asn1"
28+
"math/big"
29+
2730
"github.com/hyperledger/fabric/core/crypto/bccsp"
28-
"github.com/hyperledger/fabric/core/crypto/bccsp/utils"
2931
)
3032

33+
// rsaPublicKey reflects the ASN.1 structure of a PKCS#1 public key.
34+
type rsaPublicKeyASN struct {
35+
N *big.Int
36+
E int
37+
}
38+
3139
type rsaPrivateKey struct {
3240
privKey *rsa.PrivateKey
3341
}
@@ -39,13 +47,18 @@ func (k *rsaPrivateKey) Bytes() (raw []byte, err error) {
3947
}
4048

4149
// SKI returns the subject key identifier of this key.
42-
func (k *rsaPrivateKey) SKI() (gski []byte) {
50+
func (k *rsaPrivateKey) SKI() (ski []byte) {
4351
if k.privKey == nil {
4452
return nil
4553
}
4654

47-
raw := x509.MarshalPKCS1PrivateKey(k.privKey)
55+
// Marshall the public key
56+
raw, _ := asn1.Marshal(rsaPublicKeyASN{
57+
N: k.privKey.N,
58+
E: k.privKey.E,
59+
})
4860

61+
// Hash it
4962
hash := sha256.New()
5063
hash.Write(raw)
5164
return hash.Sum(nil)
@@ -88,9 +101,17 @@ func (k *rsaPublicKey) Bytes() (raw []byte, err error) {
88101

89102
// SKI returns the subject key identifier of this key.
90103
func (k *rsaPublicKey) SKI() (ski []byte) {
91-
raw, _ := utils.PublicKeyToPEM(k.pubKey, nil)
92-
// TODO: Error should not be thrown. Anyway, move the marshalling at initialization.
104+
if k.pubKey == nil {
105+
return nil
106+
}
107+
108+
// Marshall the public key
109+
raw, _ := asn1.Marshal(rsaPublicKeyASN{
110+
N: k.pubKey.N,
111+
E: k.pubKey.E,
112+
})
93113

114+
// Hash it
94115
hash := sha256.New()
95116
hash.Write(raw)
96117
return hash.Sum(nil)

0 commit comments

Comments
 (0)