Skip to content

Commit

Permalink
Fix a bug in x509 certificate handling. (#461)
Browse files Browse the repository at this point in the history
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: Dan Lorenc <lorenc.d@gmail.com>
  • Loading branch information
dlorenc authored Oct 19, 2021
1 parent 5653100 commit 66e0ab1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/pki/x509/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
28 changes: 28 additions & 0 deletions pkg/types/intoto/v0.0.1/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"encoding/json"
"encoding/pem"
"fmt"
"math/big"
"reflect"
"testing"

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 66e0ab1

Please sign in to comment.