Skip to content

Commit aee43bb

Browse files
committed
[FAB-7368]Make cryptogen tool support extend
This patchset add a new command 'extend' in cryptogen tool. It make cryptogen tool can generate new artifacts on existing crypto artifacts. It is useful for adding new peer, orderer, user and org. For example, 1.cryptogen generate --config config.yaml --output xxx 2.modify config.yaml for adding peer/org/orderer/user 3.cryptogen extend --config config.yaml --input xxx Change-Id: I1f501e3bef56c445796579b11d03091326fa050c Signed-off-by: grapebaba <281165273@qq.com>
1 parent 8becdf0 commit aee43bb

File tree

5 files changed

+291
-21
lines changed

5 files changed

+291
-21
lines changed

common/tools/cryptogen/ca/ca_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ import (
3131
const (
3232
testCAName = "root0"
3333
testCA2Name = "root1"
34+
testCA3Name = "root2"
3435
testName = "cert0"
3536
testName2 = "cert1"
37+
testName3 = "cert2"
3638
testIP = "172.16.10.31"
3739
testCountry = "US"
3840
testProvince = "California"
@@ -44,6 +46,38 @@ const (
4446

4547
var testDir = filepath.Join(os.TempDir(), "ca-test")
4648

49+
func TestLoadCertificateECDSA(t *testing.T) {
50+
caDir := filepath.Join(testDir, "ca")
51+
certDir := filepath.Join(testDir, "certs")
52+
// generate private key
53+
priv, _, err := csp.GeneratePrivateKey(certDir)
54+
assert.NoError(t, err, "Failed to generate signed certificate")
55+
56+
// get EC public key
57+
ecPubKey, err := csp.GetECPublicKey(priv)
58+
assert.NoError(t, err, "Failed to generate signed certificate")
59+
assert.NotNil(t, ecPubKey, "Failed to generate signed certificate")
60+
61+
// create our CA
62+
rootCA, err := ca.NewCA(caDir, testCA3Name, testCA3Name, testCountry, testProvince, testLocality, testOrganizationalUnit, testStreetAddress, testPostalCode)
63+
assert.NoError(t, err, "Error generating CA")
64+
65+
cert, err := rootCA.SignCertificate(certDir, testName3, nil, ecPubKey,
66+
x509.KeyUsageDigitalSignature|x509.KeyUsageKeyEncipherment,
67+
[]x509.ExtKeyUsage{x509.ExtKeyUsageAny})
68+
assert.NoError(t, err, "Failed to generate signed certificate")
69+
// KeyUsage should be x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment
70+
assert.Equal(t, x509.KeyUsageDigitalSignature|x509.KeyUsageKeyEncipherment,
71+
cert.KeyUsage)
72+
assert.Contains(t, cert.ExtKeyUsage, x509.ExtKeyUsageAny)
73+
74+
loadedCert, err := ca.LoadCertificateECDSA(certDir)
75+
assert.NotNil(t, loadedCert, "Should load cert")
76+
assert.Equal(t, cert.SerialNumber, loadedCert.SerialNumber, "Should have same serial number")
77+
assert.Equal(t, cert.Subject.CommonName, loadedCert.Subject.CommonName, "Should have same CN")
78+
cleanup(testDir)
79+
}
80+
4781
func TestNewCA(t *testing.T) {
4882

4983
caDir := filepath.Join(testDir, "ca")

common/tools/cryptogen/ca/generator.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ import (
2222
"crypto/x509"
2323
"crypto/x509/pkix"
2424
"encoding/pem"
25+
"io/ioutil"
2526
"math/big"
2627
"net"
2728
"os"
28-
"time"
29-
3029
"path/filepath"
30+
"strings"
31+
"time"
3132

33+
"github.com/hyperledger/fabric/bccsp/utils"
3234
"github.com/hyperledger/fabric/common/tools/cryptogen/csp"
3335
)
3436

@@ -219,3 +221,28 @@ func genCertificateECDSA(baseDir, name string, template, parent *x509.Certificat
219221
}
220222
return x509Cert, nil
221223
}
224+
225+
// LoadCertificateECDSA load a ecdsa cert from a file in cert path
226+
func LoadCertificateECDSA(certPath string) (*x509.Certificate, error) {
227+
var cert *x509.Certificate
228+
var err error
229+
230+
walkFunc := func(path string, info os.FileInfo, err error) error {
231+
if strings.HasSuffix(path, ".pem") {
232+
rawCert, err := ioutil.ReadFile(path)
233+
if err != nil {
234+
return err
235+
}
236+
block, _ := pem.Decode(rawCert)
237+
cert, err = utils.DERToX509Certificate(block.Bytes)
238+
}
239+
return nil
240+
}
241+
242+
err = filepath.Walk(certPath, walkFunc)
243+
if err != nil {
244+
return nil, err
245+
}
246+
247+
return cert, err
248+
}

common/tools/cryptogen/csp/csp.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,71 @@ import (
1919
"crypto"
2020
"crypto/ecdsa"
2121
"crypto/x509"
22+
"encoding/pem"
23+
"io/ioutil"
24+
"os"
25+
"path/filepath"
26+
"strings"
2227

2328
"github.com/hyperledger/fabric/bccsp"
2429
"github.com/hyperledger/fabric/bccsp/factory"
2530
"github.com/hyperledger/fabric/bccsp/signer"
2631
)
2732

33+
// LoadPrivateKey loads a private key from file in keystorePath
34+
func LoadPrivateKey(keystorePath string) (bccsp.Key, crypto.Signer, error) {
35+
var err error
36+
var priv bccsp.Key
37+
var s crypto.Signer
38+
39+
opts := &factory.FactoryOpts{
40+
ProviderName: "SW",
41+
SwOpts: &factory.SwOpts{
42+
HashFamily: "SHA2",
43+
SecLevel: 256,
44+
45+
FileKeystore: &factory.FileKeystoreOpts{
46+
KeyStorePath: keystorePath,
47+
},
48+
},
49+
}
50+
51+
csp, err := factory.GetBCCSPFromOpts(opts)
52+
if err != nil {
53+
return nil, nil, err
54+
}
55+
56+
walkFunc := func(path string, info os.FileInfo, err error) error {
57+
if strings.HasSuffix(path, "_sk") {
58+
rawKey, err := ioutil.ReadFile(path)
59+
if err != nil {
60+
return err
61+
}
62+
63+
block, _ := pem.Decode(rawKey)
64+
priv, err = csp.KeyImport(block.Bytes, &bccsp.ECDSAPrivateKeyImportOpts{Temporary: true})
65+
if err != nil {
66+
return err
67+
}
68+
69+
s, err = signer.New(csp, priv)
70+
if err != nil {
71+
return err
72+
}
73+
74+
return nil
75+
}
76+
return nil
77+
}
78+
79+
err = filepath.Walk(keystorePath, walkFunc)
80+
if err != nil {
81+
return nil, nil, err
82+
}
83+
84+
return priv, s, err
85+
}
86+
2887
// GeneratePrivateKey creates a private key and stores it in keystorePath
2988
func GeneratePrivateKey(keystorePath string) (bccsp.Key,
3089
crypto.Signer, error) {

common/tools/cryptogen/csp/csp_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ func (mk *mockKey) Private() bool { return false }
5757

5858
var testDir = filepath.Join(os.TempDir(), "csp-test")
5959

60+
func TestLoadPrivateKey(t *testing.T) {
61+
priv, _, _ := csp.GeneratePrivateKey(testDir)
62+
pkFile := filepath.Join(testDir, hex.EncodeToString(priv.SKI())+"_sk")
63+
assert.Equal(t, true, checkForFile(pkFile),
64+
"Expected to find private key file")
65+
loadedPriv, _, _ := csp.LoadPrivateKey(testDir)
66+
assert.NotNil(t, loadedPriv, "Should have returned a bccsp.Key")
67+
assert.Equal(t, priv.SKI(), loadedPriv.SKI(), "Should have same subject identifier")
68+
cleanup(testDir)
69+
}
70+
6071
func TestGeneratePrivateKey(t *testing.T) {
6172

6273
priv, signer, err := csp.GeneratePrivateKey(testDir)

0 commit comments

Comments
 (0)