Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FindAllCertificates method #71

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,61 @@ func (c *Context) FindCertificate(id []byte, label []byte, serial *big.Int) (*x5
return cert, err
}

// FindAllCertificates retrieves all certificates or a nil slice if none can be found.
func (c *Context) FindAllCertificates() ([]*x509.Certificate, error) {

if c.closed.Get() {
return nil, errClosed
}

var certs []*x509.Certificate
err := c.withSession(func(session *pkcs11Session) (err error) {

var template []*pkcs11.Attribute
template = append(template, pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_CERTIFICATE))

if err = session.ctx.FindObjectsInit(session.handle, template); err != nil {
return err
}
defer func() {
finalErr := session.ctx.FindObjectsFinal(session.handle)
if err == nil {
err = finalErr
}
}()

handles, _, err := session.ctx.FindObjects(session.handle, maxHandlePerFind)
if err != nil {
return err
}
if len(handles) == 0 {
return nil
}

for _, handle := range handles {
attributes := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_VALUE, 0),
}

if attributes, err = session.ctx.GetAttributeValue(session.handle, handle, attributes); err != nil {
return err
}

cert, err := x509.ParseCertificate(attributes[0].Value)
if err != nil {
return err
}
certs = append(certs, cert)
}
return nil
})

if err != nil {
return nil, err
}
return certs, err
}

// ImportCertificate imports a certificate onto the token. The id parameter is used to
// set CKA_ID and must be non-nil.
func (c *Context) ImportCertificate(id []byte, certificate *x509.Certificate) error {
Expand Down
64 changes: 64 additions & 0 deletions certificates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"github.com/miekg/pkcs11"
"math/big"
"testing"
"time"
Expand All @@ -35,6 +36,33 @@ import (
"github.com/stretchr/testify/require"
)

func TestFindAllCertificates(t *testing.T) {
skipTest(t, skipTestCert)

ctx, err := ConfigureFromFile("config")
require.NoError(t, err)

defer func() {
require.NoError(t, ctx.Close())
}()

for i := 0; i < 5; i++ {
id := randomBytes()
label := randomBytes()

cert := generateRandomCert(t)

err = ctx.ImportCertificateWithLabel(id, label, cert)
require.NoError(t, err)
}

gotCerts, err := ctx.FindAllCertificates()
require.NoError(t, err)
require.Len(t, gotCerts, 5)

removeAllCertificates(t, ctx)
}

func TestCertificate(t *testing.T) {
skipTest(t, skipTestCert)

Expand Down Expand Up @@ -68,6 +96,8 @@ func TestCertificate(t *testing.T) {
require.NotNil(t, cert2)

assert.Equal(t, cert.Signature, cert2.Signature)

removeAllCertificates(t, ctx)
}

// Test that provided attributes override default values
Expand Down Expand Up @@ -104,6 +134,8 @@ func TestCertificateAttributes(t *testing.T) {
// Find with new serial
c, err = ctx.FindCertificate(nil, nil, ourSerial)
assert.NotNil(t, c)

removeAllCertificates(t, ctx)
}

func TestCertificateRequiredArgs(t *testing.T) {
Expand All @@ -128,6 +160,8 @@ func TestCertificateRequiredArgs(t *testing.T) {

err = ctx.ImportCertificateWithLabel(val, val, nil)
require.Error(t, err)

removeAllCertificates(t, ctx)
}

func generateRandomCert(t *testing.T) *x509.Certificate {
Expand Down Expand Up @@ -158,3 +192,33 @@ func generateRandomCert(t *testing.T) *x509.Certificate {

return cert
}

func removeAllCertificates(t *testing.T, c *Context) {
if c.closed.Get() {
t.Error(errClosed)
}

err := c.withSession(func(session *pkcs11Session) (err error) {

var template []*pkcs11.Attribute
template = append(template, pkcs11.NewAttribute(pkcs11.CKA_CLASS, pkcs11.CKO_CERTIFICATE))

if e := session.ctx.FindObjectsInit(session.handle, template); e != nil {
t.Fatalf("failed to init: %s\n", e)
}
objs, _, e := session.ctx.FindObjects(session.handle, maxHandlePerFind)
if e != nil {
t.Fatalf("failed to find objects")
}
if e := session.ctx.FindObjectsFinal(session.handle); e != nil {
t.Fatalf("failed to finalize: %s\n", e)
}
for _, obj := range objs {
if e := session.ctx.DestroyObject(session.handle, obj); e != nil {
t.Fatalf("DestroyObject failed: %s\n", e)
}
}
return nil
})
require.NoError(t, err)
}