-
Notifications
You must be signed in to change notification settings - Fork 709
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More tcert library APIs prior to COP integration
This contains more tcert library APIs to enable code reuse and also to minimize the change set size. In particular, there are some more utility functions and one new object in keytree.go. This will be used to generate a tree of derived keys for affiliation groups. The next change set which will shortly follow will integrate the tcert library into the COP server and client. The changes to cli/cop_test.go were necessary to fix transient test failures. There is no need to start a server when testing cop.go to get test coverage, so I simplified test cases. The server and client commands are tested under server and client packages respectively, where an actual server is started. See https://jira.hyperledger.org/browse/FAB-876 Change-Id: I025722ef8acf6d524fde2a5ea4d471c9ab532fd3 Signed-off-by: Keith Smith <[email protected]>
- Loading branch information
Keith Smith
committed
Jan 3, 2017
1 parent
107fdff
commit c11e7f4
Showing
7 changed files
with
278 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
Copyright IBM Corp. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package tcert | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hyperledger/fabric/core/crypto/bccsp" | ||
) | ||
|
||
/* | ||
* A key tree is a hierarchy of derived keys with a single root key. | ||
* Each node in the tree has a key and a name, where the key is secret | ||
* and the name may be public. If the secret associated with a node | ||
* is known, then the secret of each node in it's sub-tree can be derived | ||
* if the name of the nodes are known; however, it is not possible to | ||
* derive the keys associated with other nodes in the tree which are not | ||
* part of this sub-tree. | ||
* | ||
* This data structure is useful to support releasing a secret associated | ||
* with any node to an auditor without giving the auditor access to all | ||
* nodes in the tree. | ||
*/ | ||
|
||
const ( | ||
keyPathSep = "/" | ||
) | ||
|
||
// NewKeyTree is the constructor for a key tree | ||
func NewKeyTree(bccspMgr bccsp.BCCSP, rootKey bccsp.Key) *KeyTree { | ||
tree := new(KeyTree) | ||
tree.bccspMgr = bccspMgr | ||
tree.rootKey = rootKey | ||
tree.keys = make(map[string]bccsp.Key) | ||
return tree | ||
} | ||
|
||
// KeyTree is a tree of derived keys | ||
type KeyTree struct { | ||
bccspMgr bccsp.BCCSP | ||
rootKey bccsp.Key | ||
keys map[string]bccsp.Key | ||
} | ||
|
||
// GetKey returns a key associated with a specific path in the tree. | ||
func (m *KeyTree) GetKey(path []string) (bccsp.Key, error) { | ||
if path == nil || len(path) == 0 { | ||
return m.rootKey, nil | ||
} | ||
pathStr := strings.Join(path, keyPathSep) | ||
key := m.keys[pathStr] | ||
if key != nil { | ||
return key, nil | ||
} | ||
parentKey, err := m.GetKey(path[0 : len(path)-1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
childName := path[len(path)-1] | ||
key, err = m.deriveChildKey(parentKey, childName, pathStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
m.keys[pathStr] = key | ||
return key, nil | ||
} | ||
|
||
// Given a parentKey and a childName, derive the child's key | ||
func (m *KeyTree) deriveChildKey(parentKey bccsp.Key, childName, path string) (bccsp.Key, error) { | ||
opts := &bccsp.HMACDeriveKeyOpts{ | ||
Temporary: true, | ||
Arg: []byte(childName), | ||
} | ||
key, err := m.bccspMgr.KeyDeriv(parentKey, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to derive key %s: %s", path, err) | ||
} | ||
return key, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright IBM Corp. 2016 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package tcert | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/cloudflare/cfssl/log" | ||
"github.com/hyperledger/fabric/core/crypto/bccsp" | ||
"github.com/hyperledger/fabric/core/crypto/bccsp/factory" | ||
) | ||
|
||
func TestKeyTree(t *testing.T) { | ||
|
||
log.Level = log.LevelDebug | ||
|
||
path := []string{"A", "B", "C"} | ||
|
||
csp, err := factory.GetDefault() | ||
if err != nil { | ||
t.Fatalf("Failed to get BCCSP factory: %s", err) | ||
} | ||
opts := &bccsp.AES256KeyGenOpts{Temporary: true} | ||
rootKey, err := csp.KeyGen(opts) | ||
if err != nil { | ||
t.Fatalf("Failed to create root key: %s", err) | ||
} | ||
|
||
tree1 := NewKeyTree(csp, rootKey) | ||
key1, err := tree1.GetKey(path) | ||
if err != nil { | ||
t.Fatalf("Failed to get key1: %s", err) | ||
} | ||
|
||
tree2 := NewKeyTree(csp, rootKey) | ||
key2, err := tree2.GetKey(path) | ||
if err != nil { | ||
t.Fatalf("Failed to get key2: %s", err) | ||
} | ||
|
||
ski1 := key1.SKI() | ||
ski2 := key2.SKI() | ||
if !bytes.Equal(ski1, ski2) { | ||
t.Errorf("keys are not equal %s != %s", ski1, ski2) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.