Skip to content

Commit 4f52c55

Browse files
authored
set x5t#S256 header (#65)
1 parent 6a2ded5 commit 4f52c55

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

credential_issuer/issuer.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/rsa"
66
"crypto/sha1"
7+
"crypto/sha256"
78
"crypto/x509"
89
"encoding/base64"
910
"errors"
@@ -110,12 +111,20 @@ func Issue(chain []*x509.Certificate, caFingerprintCert *x509.Certificate, key *
110111
}
111112

112113
// x5t
114+
// Note: although x5t is less safe than x5t#s256 (since is uses SHA-1, which has been broken), it's in here for backwards compatibility.
113115
hashSha1 := sha1.Sum(signingCert.Raw)
114116
err = hdrs.Set("x5t", base64.RawURLEncoding.EncodeToString(hashSha1[:]))
115117
if err != nil {
116118
return "", err
117119
}
118120

121+
// x5t#S256
122+
hashSha256 := sha256.Sum256(signingCert.Raw)
123+
err = hdrs.Set("x5t#S256", base64.RawURLEncoding.EncodeToString(hashSha256[:]))
124+
if err != nil {
125+
return "", err
126+
}
127+
119128
sign, err := jwt.Sign(token, jwt.WithKey(jwa.PS256, *key, jws.WithProtectedHeaders(hdrs)))
120129
return string(sign), err
121130
})

credential_issuer/issuer_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/rsa"
55
"crypto/x509"
66
"crypto/x509/pkix"
7+
"github.com/lestrrat-go/jwx/v2/jws"
78
"github.com/nuts-foundation/go-didx509-toolkit/internal"
89
"testing"
910

@@ -106,6 +107,10 @@ func TestIssue(t *testing.T) {
106107

107108
assert.Equal(t, expectedCredentialSubject, vc.CredentialSubject)
108109
assert.Equal(t, validChain[0].NotAfter, *vc.ExpirationDate, "expiration date of VC must match signing certificate")
110+
parsedJWT, err := jws.Parse([]byte(vc.Raw()))
111+
require.NoError(t, err)
112+
assert.Equal(t, "v4nyg4rKy6MBIxnutabaUwXCxYY", parsedJWT.Signatures()[0].ProtectedHeaders().X509CertThumbprint())
113+
assert.Equal(t, "XC-vUEDhKsMrtpwtYEQty5PgSj4ZphDLNDG_Rg9hQDk", parsedJWT.Signatures()[0].ProtectedHeaders().X509CertThumbprintS256())
109114
})
110115

111116
t.Run("ok - correct escaping of special characters", func(t *testing.T) {

0 commit comments

Comments
 (0)