Skip to content

Commit

Permalink
BCCSP Filebased Keystore flexible file names
Browse files Browse the repository at this point in the history
Pem files are very useful to be used directly by users. Lets make
the file names more human-readable.

Change-Id: I14d41d2f2e0c163b5cb1a15ef5ba26f0bd779285
Signed-off-by: Volodymyr Paprotski <[email protected]>
  • Loading branch information
Volodymyr Paprotski committed Feb 28, 2017
1 parent 48d19be commit d953960
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
33 changes: 32 additions & 1 deletion bccsp/pkcs11/fileks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package pkcs11

import (
"bytes"
"crypto/rsa"
"encoding/hex"
"errors"
Expand Down Expand Up @@ -143,7 +144,7 @@ func (ks *FileBasedKeyStore) GetKey(ski []byte) (k bccsp.Key, err error) {
return nil, errors.New("Public key type not recognized")
}
default:
return nil, errors.New("Key type not recognized")
return ks.searchKeystoreForSKI(ski)
}
}

Expand Down Expand Up @@ -189,6 +190,36 @@ func (ks *FileBasedKeyStore) StoreKey(k bccsp.Key) (err error) {
return
}

func (ks *FileBasedKeyStore) searchKeystoreForSKI(ski []byte) (k bccsp.Key, err error) {

files, _ := ioutil.ReadDir(ks.path)
for _, f := range files {
if f.IsDir() {
continue
}
raw, err := ioutil.ReadFile(filepath.Join(ks.path, f.Name()))

key, err := utils.PEMtoPrivateKey(raw, ks.pwd)
if err != nil {
continue
}

switch key.(type) {
case *rsa.PrivateKey:
k = &rsaPrivateKey{key.(*rsa.PrivateKey)}
default:
continue
}

if !bytes.Equal(k.SKI(), ski) {
continue
}

return k, nil
}
return nil, errors.New("Key type not recognized")
}

func (ks *FileBasedKeyStore) getSuffix(alias string) string {
files, _ := ioutil.ReadDir(ks.path)
for _, f := range files {
Expand Down
35 changes: 34 additions & 1 deletion bccsp/sw/fileks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package sw

import (
"bytes"
"io/ioutil"
"os"
"sync"
Expand Down Expand Up @@ -159,7 +160,7 @@ func (ks *fileBasedKeyStore) GetKey(ski []byte) (k bccsp.Key, err error) {
return nil, errors.New("Public key type not recognized")
}
default:
return nil, errors.New("Key type not recognized")
return ks.searchKeystoreForSKI(ski)
}
}

Expand Down Expand Up @@ -221,6 +222,38 @@ func (ks *fileBasedKeyStore) StoreKey(k bccsp.Key) (err error) {
return
}

func (ks *fileBasedKeyStore) searchKeystoreForSKI(ski []byte) (k bccsp.Key, err error) {

files, _ := ioutil.ReadDir(ks.path)
for _, f := range files {
if f.IsDir() {
continue
}
raw, err := ioutil.ReadFile(filepath.Join(ks.path, f.Name()))

key, err := utils.PEMtoPrivateKey(raw, ks.pwd)
if err != nil {
continue
}

switch key.(type) {
case *ecdsa.PrivateKey:
k = &ecdsaPrivateKey{key.(*ecdsa.PrivateKey)}
case *rsa.PrivateKey:
k = &rsaPrivateKey{key.(*rsa.PrivateKey)}
default:
continue
}

if !bytes.Equal(k.SKI(), ski) {
continue
}

return k, nil
}
return nil, errors.New("Key type not recognized")
}

func (ks *fileBasedKeyStore) getSuffix(alias string) string {
files, _ := ioutil.ReadDir(ks.path)
for _, f := range files {
Expand Down
File renamed without changes.

0 comments on commit d953960

Please sign in to comment.