Skip to content
Snippets Groups Projects
Unverified Commit 66e0ab1e authored by dlorenc's avatar dlorenc Committed by GitHub
Browse files

Fix a bug in x509 certificate handling. (#461)


The CryptoPubKey function only returned the key value, but we should
retrieve it from the cert if set. This fixes the rest of #918.

Signed-off-by: default avatarDan Lorenc <lorenc.d@gmail.com>
parent 56531003
No related branches found
No related tags found
No related merge requests found
......@@ -139,6 +139,9 @@ func (k PublicKey) CanonicalValue() (encoded []byte, err error) {
}
func (k PublicKey) CryptoPubKey() crypto.PublicKey {
if k.cert != nil {
return k.cert.c.PublicKey
}
return k.key
}
......
......@@ -27,6 +27,7 @@ import (
"encoding/json"
"encoding/pem"
"fmt"
"math/big"
"reflect"
"testing"
......@@ -90,6 +91,23 @@ func TestV001Entry_Unmarshal(t *testing.T) {
Type: "PUBLIC KEY",
})
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
ca := &x509.Certificate{
SerialNumber: big.NewInt(1),
}
caBytes, err := x509.CreateCertificate(rand.Reader, ca, ca, &priv.PublicKey, priv)
if err != nil {
t.Fatal(err)
}
pemBytes := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: caBytes,
})
invalid, err := json.Marshal(dsse.Envelope{
Payload: "hello",
Signatures: []dsse.Signature{
......@@ -139,6 +157,16 @@ func TestV001Entry_Unmarshal(t *testing.T) {
},
wantErr: false,
},
{
name: "cert",
it: &models.IntotoV001Schema{
PublicKey: p([]byte(pemBytes)),
Content: &models.IntotoV001SchemaContent{
Envelope: envelope(t, priv, validPayload, "text"),
},
},
wantErr: false,
},
{
name: "invalid",
it: &models.IntotoV001Schema{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment