-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring in PKCS11 Golang bindings. Test the vendored files by loading softhsm if can be found Softhsm is being installed by https://gerrit.hyperledger.org/r/#/c/4359/ Change-Id: I848c66b778e131ff91819b11f8b53e03934374a1 Signed-off-by: Volodymyr Paprotski <vpaprots@ca.ibm.com>
- Loading branch information
Volodymyr Paprotski
committed
Jan 24, 2017
1 parent
a0898e6
commit 4916ac4
Showing
14 changed files
with
6,007 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Copyright IBM Corp. 2017 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 pkcs11 | ||
|
||
import ( | ||
"fmt" | ||
"github.com/miekg/pkcs11" | ||
) | ||
|
||
var ( | ||
ctx *pkcs11.Ctx | ||
sessions = make(chan pkcs11.SessionHandle, 2000) | ||
slot uint | ||
) | ||
|
||
func initPKCS11(lib, pin, label string) error { | ||
return loadLib(lib, pin, label) | ||
} | ||
|
||
func loadLib(lib, pin, label string) error { | ||
logger.Debugf("Loading pkcs11 library [%s]\n", lib) | ||
if lib == "" { | ||
return fmt.Errorf("No PKCS11 library default") | ||
} | ||
|
||
ctx = pkcs11.New(lib) | ||
if ctx == nil { | ||
return fmt.Errorf("Instantiate failed [%s]", lib) | ||
} | ||
|
||
ctx.Initialize() | ||
slots, err := ctx.GetSlotList(true) | ||
if err != nil { | ||
return err | ||
} | ||
found := false | ||
for _, s := range slots { | ||
info, err := ctx.GetTokenInfo(s) | ||
if err != nil { | ||
continue | ||
} | ||
if label == info.Label { | ||
found = true | ||
slot = s | ||
break | ||
} | ||
} | ||
if !found { | ||
return fmt.Errorf("Could not find token with label %s", label) | ||
} | ||
session := getSession() | ||
defer returnSession(session) | ||
|
||
if pin == "" { | ||
return fmt.Errorf("No PIN set\n") | ||
} | ||
err = ctx.Login(session, pkcs11.CKU_USER, pin) | ||
if err != nil { | ||
return fmt.Errorf("Login failed [%s]\n", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getSession() (session pkcs11.SessionHandle) { | ||
select { | ||
case session = <-sessions: | ||
logger.Debugf("Reusing existing pkcs11 session %x on slot %d\n", session, slot) | ||
|
||
default: | ||
// create one | ||
var s pkcs11.SessionHandle | ||
var err error = nil | ||
for i := 0; i < 10; i++ { | ||
s, err = ctx.OpenSession(slot, pkcs11.CKF_SERIAL_SESSION|pkcs11.CKF_RW_SESSION) | ||
if err != nil { | ||
logger.Warningf("OpenSession failed, retrying [%s]\n", err) | ||
} else { | ||
break | ||
} | ||
} | ||
if err != nil { | ||
logger.Fatalf("OpenSession [%s]\n", err) | ||
} | ||
logger.Debugf("Created new pkcs11 session %x on slot %d\n", session, slot) | ||
session = s | ||
} | ||
return session | ||
} | ||
|
||
func returnSession(session pkcs11.SessionHandle) { | ||
sessions <- session | ||
} |
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,29 @@ | ||
/* | ||
Copyright IBM Corp. 2017 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 pkcs11 | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestPKCS11GetSession(t *testing.T) { | ||
if !enablePKCS11tests { | ||
t.SkipNow() | ||
} | ||
|
||
session := getSession() | ||
defer returnSession(session) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.