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 rsassa-pss-sha256 verifier #126

Closed
Closed
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
12 changes: 7 additions & 5 deletions data/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
)

const (
KeyIDLength = sha256.Size * 2
KeyTypeEd25519 = "ed25519"
KeyTypeECDSA_SHA2_P256 = "ecdsa-sha2-nistp256"
KeySchemeEd25519 = "ed25519"
KeySchemeECDSA_SHA2_P256 = "ecdsa-sha2-nistp256"
KeyIDLength = sha256.Size * 2
KeyTypeRSASSA_PSS_SHA256 = "rsassa-pss-sha256"
KeyTypeEd25519 = "ed25519"
KeyTypeECDSA_SHA2_P256 = "ecdsa-sha2-nistp256"
KeySchemeRSASSA_PSS_SHA256 = "rsassa-pss-sha256"
KeySchemeEd25519 = "ed25519"
KeySchemeECDSA_SHA2_P256 = "ecdsa-sha2-nistp256"
)

var (
Expand Down
57 changes: 55 additions & 2 deletions verify/verifiers.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package verify

import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/asn1"
"math/big"

Expand All @@ -25,8 +28,9 @@ type Verifier interface {

// Verifiers is used to map key types to Verifier instances.
var Verifiers = map[string]Verifier{
data.KeySchemeEd25519: ed25519Verifier{},
data.KeySchemeECDSA_SHA2_P256: p256Verifier{},
data.KeySchemeEd25519: ed25519Verifier{},
data.KeySchemeECDSA_SHA2_P256: p256Verifier{},
data.KeySchemeRSASSA_PSS_SHA256: rsaVerifier{},
}

type ed25519Verifier struct{}
Expand Down Expand Up @@ -73,3 +77,52 @@ func (p256Verifier) ValidKey(k []byte) bool {
x, _ := elliptic.Unmarshal(elliptic.P256(), k)
return x != nil
}

type rsaVerifier struct{}

func (v rsaVerifier) Verify(key, msg, sig []byte) error {
digest := sha256.Sum256(msg)
pub, err := parseKey(key)
if err != nil {
return ErrInvalid
}

rsaPub, ok := pub.(*rsa.PublicKey)
if !ok {
return ErrInvalid
}

if err = rsa.VerifyPKCS1v15(rsaPub, crypto.SHA256, digest[:], sig); err != nil {
return ErrInvalid
}
return nil
}

func (v rsaVerifier) ValidKey(k []byte) bool {
pub, err := parseKey(k)
if err != nil {
return false
}

if _, ok := pub.(*rsa.PublicKey); !ok {
return false
}
return true
}

// parseKey tries to parse a PEM []byte slice by attempting PKCS8, PKCS1, and PKIX in order.
func parseKey(data []byte) (interface{}, error) {
key, err := x509.ParsePKCS8PrivateKey(data)
if err == nil {
return key, nil
}
key, err = x509.ParsePKCS1PrivateKey(data)
if err == nil {
return key, nil
}
key, err = x509.ParsePKIXPublicKey(data)
if err == nil {
return key, nil
}
return nil, ErrInvalid
}
60 changes: 60 additions & 0 deletions verify/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"io"
"testing"
"time"
Expand Down Expand Up @@ -59,6 +61,41 @@ func (ecdsaSigner) Scheme() string {
return data.KeySchemeECDSA_SHA2_P256
}

type rsaSigner struct {
*rsa.PrivateKey
}

func (s rsaSigner) PublicData() *data.Key {
pub, _ := x509.MarshalPKIXPublicKey(s.Public().(*rsa.PublicKey))
return &data.Key{
Type: data.KeyTypeRSASSA_PSS_SHA256,
Scheme: data.KeySchemeRSASSA_PSS_SHA256,
Algorithms: data.KeyAlgorithms,
Value: data.KeyValue{Public: pub},
}
}

func (s rsaSigner) Sign(rand io.Reader, msg []byte, opts crypto.SignerOpts) ([]byte, error) {
hash := sha256.Sum256(msg)
return s.PrivateKey.Sign(rand, hash[:], crypto.SHA256)
}

func (s rsaSigner) IDs() []string {
return s.PublicData().IDs()
}

func (s rsaSigner) ContainsID(id string) bool {
return s.PublicData().ContainsID(id)
}

func (rsaSigner) Type() string {
return data.KeyTypeRSASSA_PSS_SHA256
}

func (rsaSigner) Scheme() string {
return data.KeySchemeRSASSA_PSS_SHA256
}

func (VerifySuite) Test(c *C) {
type test struct {
name string
Expand Down Expand Up @@ -198,6 +235,29 @@ func (VerifySuite) Test(c *C) {
},
err: ErrInvalid,
},
{
name: "valid rsa signature",
mut: func(t *test) {
k, _ := rsa.GenerateKey(rand.Reader, 2048)
s := rsaSigner{k}
sign.Sign(t.s, s)
t.s.Signatures = t.s.Signatures[1:]
t.keys = []*data.Key{s.PublicData()}
t.roles["root"].KeyIDs = s.PublicData().IDs()
},
},
{
name: "invalid rsa signature",
mut: func(t *test) {
k, _ := rsa.GenerateKey(rand.Reader, 2048)
s := rsaSigner{k}
sign.Sign(t.s, s)
t.s.Signatures[1].Signature[0]++
t.keys = append(t.keys, s.PublicData())
t.roles["root"].KeyIDs = append(t.roles["root"].KeyIDs, s.PublicData().IDs()...)
},
err: ErrInvalid,
},
}
for _, t := range tests {
if t.role == "" {
Expand Down