in client/clientutil.go [153:182]
func PublicKeyForRSAFingerprint(kek *configpb.KekInfo, keys *configpb.AsymmetricKeys) (*rsa.PublicKey, error) {
for _, path := range keys.GetPublicKeyFiles() {
keyBytes, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to open public key file: %w", err)
}
block, _ := pem.Decode(keyBytes)
if block == nil || block.Type != "PUBLIC KEY" {
return nil, fmt.Errorf("failed to decode PEM block containing public key")
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse public key from PEM: %v", err)
}
key, ok := pub.(*rsa.PublicKey)
if !ok {
return nil, fmt.Errorf("failed to parse RSA public key: %v", err)
}
// Compute SHA-256 digest of the DER-encoded public key.
sha := sha256.Sum256(block.Bytes)
fingerprint := base64.StdEncoding.EncodeToString(sha[:])
if fingerprint == kek.GetRsaFingerprint() {
return key, nil
}
}
return nil, fmt.Errorf("no RSA public key found for fingerprint: %s", kek.GetRsaFingerprint())
}