Skip to content

Commit 9ed9ce4

Browse files
committed
core/crypto/primitives cleanup: second step
This change-set continues the cleanup of the core/crypto/primitives package. Refactoring has been applied to move methods and files under the packages which need them. Change-Id: Icfe6adf938b9d96df9dfde3dfebf95f3004fcae7 Signed-off-by: Angelo De Caro <[email protected]>
1 parent 4c63856 commit 9ed9ce4

File tree

19 files changed

+117
-97
lines changed

19 files changed

+117
-97
lines changed

accesscontrol/api.go

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
116
package accesscontrol
217

318
// Attribute defines a name, value pair to be verified.

core/crypto/attributes/attributes.go accesscontrol/attributes/attributes.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ import (
2525
"strconv"
2626
"strings"
2727

28-
pb "github.com/hyperledger/fabric/core/crypto/attributes/proto"
28+
pb "github.com/hyperledger/fabric/accesscontrol/attributes/proto"
2929
"github.com/hyperledger/fabric/core/crypto/primitives"
3030

3131
"github.com/golang/protobuf/proto"
32+
"github.com/hyperledger/fabric/accesscontrol/crypto/utils"
3233
)
3334

3435
var (
@@ -80,7 +81,7 @@ func ReadAttributeHeader(tcert *x509.Certificate, headerKey []byte) (map[string]
8081
var err error
8182
var headerRaw []byte
8283
encrypted := false
83-
if headerRaw, err = primitives.GetCriticalExtension(tcert, TCertAttributesHeaders); err != nil {
84+
if headerRaw, err = utils.GetCriticalExtension(tcert, TCertAttributesHeaders); err != nil {
8485
return nil, encrypted, err
8586
}
8687
headerStr := string(headerRaw)
@@ -112,7 +113,7 @@ func ReadTCertAttributeByPosition(tcert *x509.Certificate, position int) ([]byte
112113
}
113114

114115
oid := asn1.ObjectIdentifier{1, 2, 3, 4, 5, 6, 9 + position}
115-
value, err := primitives.GetCriticalExtension(tcert, oid)
116+
value, err := utils.GetCriticalExtension(tcert, oid)
116117
if err != nil {
117118
return nil, err
118119
}
@@ -139,7 +140,7 @@ func ReadTCertAttribute(tcert *x509.Certificate, attributeName string, headerKey
139140
//EncryptAttributeValue encrypts "attributeValue" using "attributeKey"
140141
func EncryptAttributeValue(attributeKey []byte, attributeValue []byte) ([]byte, error) {
141142
value := append(attributeValue, padding...)
142-
return primitives.CBCPKCS7Encrypt(attributeKey, value)
143+
return utils.CBCPKCS7Encrypt(attributeKey, value)
143144
}
144145

145146
//getAttributeKey returns the attributeKey derived from the preK0 to the attributeName.
@@ -155,7 +156,7 @@ func EncryptAttributeValuePK0(preK0 []byte, attributeName string, attributeValue
155156

156157
//DecryptAttributeValue decrypts "encryptedValue" using "attributeKey" and return the decrypted value.
157158
func DecryptAttributeValue(attributeKey []byte, encryptedValue []byte) ([]byte, error) {
158-
value, err := primitives.CBCPKCS7Decrypt(attributeKey, encryptedValue)
159+
value, err := utils.CBCPKCS7Decrypt(attributeKey, encryptedValue)
159160
if err != nil {
160161
return nil, err
161162
}
@@ -238,7 +239,7 @@ func CreateAttributesMetadataFromCert(cert *x509.Certificate, metadata []byte, p
238239

239240
//CreateAttributesMetadata create the AttributesMetadata from the original metadata
240241
func CreateAttributesMetadata(raw []byte, metadata []byte, preK0 []byte, attributeKeys []string) ([]byte, error) {
241-
cert, err := primitives.DERToX509Certificate(raw)
242+
cert, err := utils.DERToX509Certificate(raw)
242243
if err != nil {
243244
return nil, err
244245
}

core/crypto/attributes/attributes_test.go accesscontrol/attributes/attributes_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"testing"
2929

3030
"github.com/golang/protobuf/proto"
31-
pb "github.com/hyperledger/fabric/core/crypto/attributes/proto"
31+
pb "github.com/hyperledger/fabric/accesscontrol/attributes/proto"
3232
"github.com/hyperledger/fabric/core/crypto/primitives"
3333
)
3434

accesscontrol/crypto/attr/attr_support.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"errors"
2323

2424
"github.com/hyperledger/fabric/accesscontrol"
25-
"github.com/hyperledger/fabric/core/crypto/attributes"
26-
"github.com/hyperledger/fabric/core/crypto/primitives"
25+
"github.com/hyperledger/fabric/accesscontrol/attributes"
26+
"github.com/hyperledger/fabric/accesscontrol/crypto/utils"
2727
)
2828

2929
// chaincodeHolder is the struct that hold the certificate and the metadata. An implementation is ChaincodeStub
@@ -107,7 +107,7 @@ func NewAttributesHandlerImpl(holder chaincodeHolder) (*AttributesHandlerImpl, e
107107
return nil, errors.New("The certificate can't be nil.")
108108
}
109109
var tcert *x509.Certificate
110-
tcert, err = primitives.DERToX509Certificate(certRaw)
110+
tcert, err = utils.DERToX509Certificate(certRaw)
111111
if err != nil {
112112
return nil, err
113113
}

accesscontrol/crypto/ecdsa/ecdsa_test.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,21 @@ package ecdsa
1919
import (
2020
"testing"
2121

22+
"github.com/hyperledger/fabric/accesscontrol/crypto/utils"
2223
"github.com/hyperledger/fabric/core/crypto/primitives"
2324
)
2425

2526
func TestSignatureVerifier(t *testing.T) {
2627
// Create a signature
2728
primitives.SetSecurityLevel("SHA3", 256)
2829

29-
cert, key, err := primitives.NewSelfSignedCert()
30+
cert, key, err := utils.NewSelfSignedCert()
3031
if err != nil {
3132
t.Fatal(err)
3233
}
3334

3435
message := []byte("Hello World!")
35-
signature, err := primitives.ECDSASign(key, message)
36+
signature, err := utils.ECDSASign(key, message)
3637
if err != nil {
3738
t.Fatal(err)
3839
}
@@ -54,13 +55,13 @@ func TestSignatureVerifierSHA2(t *testing.T) {
5455
// Create a signature
5556
primitives.SetSecurityLevel("SHA2", 256)
5657

57-
cert, key, err := primitives.NewSelfSignedCert()
58+
cert, key, err := utils.NewSelfSignedCert()
5859
if err != nil {
5960
t.Fatal(err)
6061
}
6162

6263
message := []byte("Hello World!")
63-
signature, err := primitives.ECDSASign(key, message)
64+
signature, err := utils.ECDSASign(key, message)
6465
if err != nil {
6566
t.Fatal(err)
6667
}
@@ -82,13 +83,13 @@ func TestSignatureVerifierSHA2_384(t *testing.T) {
8283
// Create a signature
8384
primitives.SetSecurityLevel("SHA2", 384)
8485

85-
cert, key, err := primitives.NewSelfSignedCert()
86+
cert, key, err := utils.NewSelfSignedCert()
8687
if err != nil {
8788
t.Fatal(err)
8889
}
8990

9091
message := []byte("Hello World!")
91-
signature, err := primitives.ECDSASign(key, message)
92+
signature, err := utils.ECDSASign(key, message)
9293
if err != nil {
9394
t.Fatal(err)
9495
}
@@ -110,13 +111,13 @@ func TestSignatureVerifierSHA3_384(t *testing.T) {
110111
// Create a signature
111112
primitives.SetSecurityLevel("SHA3", 384)
112113

113-
cert, key, err := primitives.NewSelfSignedCert()
114+
cert, key, err := utils.NewSelfSignedCert()
114115
if err != nil {
115116
t.Fatal(err)
116117
}
117118

118119
message := []byte("Hello World!")
119-
signature, err := primitives.ECDSASign(key, message)
120+
signature, err := utils.ECDSASign(key, message)
120121
if err != nil {
121122
t.Fatal(err)
122123
}
@@ -138,13 +139,13 @@ func TestSignatureVerifierSHA2_512(t *testing.T) {
138139
// Create a signature
139140
primitives.SetSecurityLevel("SHA2", 512)
140141

141-
cert, key, err := primitives.NewSelfSignedCert()
142+
cert, key, err := utils.NewSelfSignedCert()
142143
if err != nil {
143144
t.Fatal(err)
144145
}
145146

146147
message := []byte("Hello World!")
147-
signature, err := primitives.ECDSASign(key, message)
148+
signature, err := utils.ECDSASign(key, message)
148149
if err != nil {
149150
t.Fatal(err)
150151
}
@@ -166,13 +167,13 @@ func TestSignatureVerifierSHA3_512(t *testing.T) {
166167
// Create a signature
167168
primitives.SetSecurityLevel("SHA3", 512)
168169

169-
cert, key, err := primitives.NewSelfSignedCert()
170+
cert, key, err := utils.NewSelfSignedCert()
170171
if err != nil {
171172
t.Fatal(err)
172173
}
173174

174175
message := []byte("Hello World!")
175-
signature, err := primitives.ECDSASign(key, message)
176+
signature, err := utils.ECDSASign(key, message)
176177
if err != nil {
177178
t.Fatal(err)
178179
}

core/crypto/primitives/aes.go accesscontrol/crypto/utils/aes.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package primitives
17+
package utils
1818

1919
import (
2020
"bytes"
@@ -24,6 +24,8 @@ import (
2424
"errors"
2525
"fmt"
2626
"io"
27+
28+
"github.com/hyperledger/fabric/core/crypto/primitives"
2729
)
2830

2931
const (
@@ -36,7 +38,7 @@ const (
3638

3739
// GenAESKey returns a random AES key of length AESKeyLength
3840
func GenAESKey() ([]byte, error) {
39-
return GetRandomBytes(AESKeyLength)
41+
return primitives.GetRandomBytes(AESKeyLength)
4042
}
4143

4244
// PKCS7Padding pads as prescribed by the PKCS7 standard

0 commit comments

Comments
 (0)