This commit is contained in:
Brad Davidson 2025-06-04 17:45:13 +02:00 committed by GitHub
commit 33aa7f7d2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 2 deletions

View File

@ -354,6 +354,9 @@ func newAccessController(options map[string]interface{}) (auth.AccessController,
if key := GetJWKThumbprint(rootCert.PublicKey); key != "" {
trustedKeys[key] = rootCert.PublicKey
}
if key := GetLibtrustKeyID(rootCert.PublicKey); key != "" {
trustedKeys[key] = rootCert.PublicKey
}
}
if jwks != nil {

View File

@ -143,8 +143,8 @@ func TestRootCertIncludedInTrustedKeys(t *testing.T) {
// newAccessController return type is an interface built from
// accessController struct. The type check can be safely ignored.
ac2, _ := ac.(*accessController)
if got := len(ac2.trustedKeys); got != 1 {
t.Fatalf("Unexpected number of trusted keys, expected 1 got: %d", got)
if got := len(ac2.trustedKeys); got != 2 {
t.Fatalf("Unexpected number of trusted keys, expected 2 got: %d", got)
}
}

View File

@ -1,14 +1,18 @@
package token
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base32"
"encoding/base64"
"fmt"
"math/big"
"strings"
)
// actionSet is a special type of stringSet.
@ -101,3 +105,26 @@ func getJWKThumbprint(publickey crypto.PublicKey, skipED25519 bool) string {
return ""
}
}
// Returns a libtrust-compatible Key ID, for backwards compatibility
// with JWT headers expected by distribution/v2
func GetLibtrustKeyID(publickey crypto.PublicKey) string {
keyBytes, err := x509.MarshalPKIXPublicKey(publickey)
if err != nil {
return ""
}
sum := sha256.Sum256(keyBytes)
b64 := strings.TrimRight(base32.StdEncoding.EncodeToString(sum[:30]), "=")
var buf bytes.Buffer
var i int
for i = 0; i < len(b64)/4-1; i++ {
start := i * 4
end := start + 4
buf.WriteString(b64[start:end] + ":")
}
buf.WriteString(b64[i*4:])
return buf.String()
}