-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a keystore class for improved code flow
FAB-2084 Current code uses the FileKeyValueStore in combination of special key index logic to save and retrieve keys. This makes it error prone. Improved to encapsulate the key persistence strategy in a key store class. Change-Id: I1aa378fbbfe88a33fc54d2fafd32e76f0869ecd2 Signed-off-by: Jim Zhang <[email protected]>
- Loading branch information
1 parent
b9d5f26
commit 4cdabba
Showing
6 changed files
with
287 additions
and
163 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2016-2017 IBM 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. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var jsrsasign = require('jsrsasign'); | ||
var KEYUTIL = jsrsasign.KEYUTIL; | ||
|
||
var FKVS = require('./FileKeyValueStore.js'); | ||
var utils = require('../utils'); | ||
var ECDSAKey = require('./ecdsa/key.js'); | ||
|
||
var logger = utils.getLogger('CryptoKeyStore.js'); | ||
|
||
var CryptoKeyStore = class extends FKVS { | ||
constructor(options) { | ||
return super(options); | ||
} | ||
|
||
getKey(ski) { | ||
var self = this; | ||
|
||
// first try the private key entry, since it encapsulates both | ||
// the private key and public key | ||
return this.getValue(_getKeyIndex(ski, true)) | ||
.then((raw) => { | ||
if (raw !== null) { | ||
var privKey = KEYUTIL.getKeyFromPlainPrivatePKCS8PEM(raw); | ||
// TODO: for now assuming ECDSA keys only, need to add support for RSA keys | ||
return new ECDSAKey(privKey, privKey.ecparams.keylen); | ||
} | ||
|
||
// didn't find the private key entry matching the SKI | ||
// next try the public key entry | ||
return self.getValue(_getKeyIndex(ski, false)); | ||
}).then((key) => { | ||
if (key instanceof ECDSAKey) | ||
return key; | ||
|
||
if (key !== null) { | ||
var pubKey = KEYUTIL.getKey(key); | ||
return new ECDSAKey(pubKey, pubKey.ecparams.keylen); | ||
} | ||
}); | ||
} | ||
|
||
putKey(key) { | ||
var idx = _getKeyIndex(key.getSKI(), key.isPrivate()); | ||
var pem = key.toBytes(); | ||
return this.setValue(idx, pem) | ||
.then(() => { | ||
return key; | ||
}); | ||
} | ||
}; | ||
|
||
function _getKeyIndex(ski, isPrivateKey) { | ||
if (isPrivateKey) | ||
return ski + '-priv'; | ||
else | ||
return ski + '-pub'; | ||
} | ||
|
||
module.exports = CryptoKeyStore; |
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
Oops, something went wrong.