@@ -29,106 +29,16 @@ import (
2929 "encoding/pem"
3030 "fmt"
3131 "io/ioutil"
32- "path"
3332 "strings"
3433
3534 "github.com/hyperledger/fabric-sdk-go/api/apicryptosuite"
3635 "github.com/hyperledger/fabric-sdk-go/pkg/errors"
3736
3837 "github.com/cloudflare/cfssl/csr"
39- "github.com/cloudflare/cfssl/helpers"
4038 factory "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/sdkpatch/cryptosuitebridge"
4139 log "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/sdkpatch/logbridge"
4240)
4341
44- // InitBCCSP initializes BCCSP
45- func InitBCCSP (optsPtr * * factory.FactoryOpts , mspDir , homeDir string ) (apicryptosuite.CryptoSuite , error ) {
46- err := ConfigureBCCSP (optsPtr , mspDir , homeDir )
47- if err != nil {
48- return nil , err
49- }
50- csp , err := GetBCCSP (* optsPtr , homeDir )
51- if err != nil {
52- return nil , err
53- }
54- return csp , nil
55- }
56-
57- // ConfigureBCCSP configures BCCSP, using
58- func ConfigureBCCSP (optsPtr * * factory.FactoryOpts , mspDir , homeDir string ) error {
59- var err error
60- if optsPtr == nil {
61- return errors .New ("nil argument not allowed" )
62- }
63- opts := * optsPtr
64- if opts == nil {
65- opts = & factory.FactoryOpts {}
66- }
67- if opts .ProviderName == "" {
68- opts .ProviderName = "SW"
69- }
70- if strings .ToUpper (opts .ProviderName ) == "SW" {
71- if opts .SwOpts == nil {
72- opts .SwOpts = factory .NewSwOpts ()
73- }
74- if opts .SwOpts .HashFamily == "" {
75- opts .SwOpts .HashFamily = "SHA2"
76- }
77- if opts .SwOpts .SecLevel == 0 {
78- opts .SwOpts .SecLevel = 256
79- }
80- if opts .SwOpts .FileKeystore == nil {
81- opts .SwOpts .FileKeystore = factory .NewFileKeystoreOpts ()
82- }
83- // The mspDir overrides the KeyStorePath; otherwise, if not set, set default
84- if mspDir != "" {
85- opts .SwOpts .FileKeystore .KeyStorePath = path .Join (mspDir , "keystore" )
86- } else if opts .SwOpts .FileKeystore .KeyStorePath == "" {
87- opts .SwOpts .FileKeystore .KeyStorePath = path .Join ("msp" , "keystore" )
88- }
89- }
90- err = makeFileNamesAbsolute (opts , homeDir )
91- if err != nil {
92- return errors .WithMessage (err , "Failed to make BCCSP files absolute" )
93- }
94- log .Debugf ("Initializing BCCSP: %+v" , opts )
95- if opts .SwOpts != nil {
96- log .Debugf ("Initializing BCCSP with software options %+v" , opts .SwOpts )
97- }
98- if opts .Pkcs11Opts != nil {
99- log .Debugf ("Initializing BCCSP with PKCS11 options %+v" , opts .Pkcs11Opts )
100- }
101- // Init the BCCSP factories
102- err = factory .InitFactories (opts )
103- if err != nil {
104- return errors .WithMessage (err , "Failed to initialize BCCSP Factories" )
105- }
106- * optsPtr = opts
107- return nil
108- }
109-
110- // GetBCCSP returns BCCSP
111- func GetBCCSP (opts * factory.FactoryOpts , homeDir string ) (apicryptosuite.CryptoSuite , error ) {
112-
113- // Get BCCSP from the opts
114- csp , err := factory .GetBCCSPFromOpts (opts )
115- if err != nil {
116- return nil , errors .WithMessage (err , "Failed to get BCCSP with opts" )
117- }
118- return csp , nil
119- }
120-
121- // makeFileNamesAbsolute makes all relative file names associated with CSP absolute,
122- // relative to 'homeDir'.
123- func makeFileNamesAbsolute (opts * factory.FactoryOpts , homeDir string ) error {
124- var err error
125- if opts != nil && opts .SwOpts != nil && opts .SwOpts .FileKeystore != nil {
126- fks := opts .SwOpts .FileKeystore
127- fks .KeyStorePath , err = MakeFileAbs (fks .KeyStorePath , homeDir )
128- }
129- return err
130- }
131-
13242// getBCCSPKeyOpts generates a key as specified in the request.
13343// This supports ECDSA and RSA.
13444func getBCCSPKeyOpts (kr csr.KeyRequest , ephemeral bool ) (opts apicryptosuite.KeyGenOpts , err error ) {
@@ -183,30 +93,13 @@ func GetSignerFromCert(cert *x509.Certificate, csp apicryptosuite.CryptoSuite) (
18393 return nil , nil , errors .WithMessage (err , "Could not find matching private key for SKI" )
18494 }
18595 // Construct and initialize the signer
186- signer , err := factory .NewCspsigner (csp , privateKey )
96+ signer , err := factory .NewCspSigner (csp , privateKey )
18797 if err != nil {
18898 return nil , nil , errors .WithMessage (err , "Failed to load ski from bccsp" )
18999 }
190100 return privateKey , signer , nil
191101}
192102
193- // GetSignerFromCertFile load skiFile and load private key represented by ski and return bccsp signer that conforms to crypto.Signer
194- func GetSignerFromCertFile (certFile string , csp apicryptosuite.CryptoSuite ) (apicryptosuite.Key , crypto.Signer , * x509.Certificate , error ) {
195- // Load cert file
196- certBytes , err := ioutil .ReadFile (certFile )
197- if err != nil {
198- return nil , nil , nil , errors .Wrapf (err , "Could not read certFile '%s'" , certFile )
199- }
200- // Parse certificate
201- parsedCa , err := helpers .ParseCertificatePEM (certBytes )
202- if err != nil {
203- return nil , nil , nil , err
204- }
205- // Get the signer from the cert
206- key , cspSigner , err := GetSignerFromCert (parsedCa , csp )
207- return key , cspSigner , parsedCa , err
208- }
209-
210103// BCCSPKeyRequestGenerate generates keys through BCCSP
211104// somewhat mirroring to cfssl/req.KeyRequest.Generate()
212105func BCCSPKeyRequestGenerate (req * csr.CertificateRequest , myCSP apicryptosuite.CryptoSuite ) (apicryptosuite.Key , crypto.Signer , error ) {
@@ -220,7 +113,7 @@ func BCCSPKeyRequestGenerate(req *csr.CertificateRequest, myCSP apicryptosuite.C
220113 return nil , nil , err
221114 }
222115
223- cspSigner , err := factory .NewCspsigner (myCSP , key )
116+ cspSigner , err := factory .NewCspSigner (myCSP , key )
224117 if err != nil {
225118 return nil , nil , errors .WithMessage (err , "Failed initializing CryptoSigner" )
226119 }
0 commit comments