grafana/k6 · error

unsupported public key algorithm

Error message

unsupported public key algorithm

What it means

k6's k6/crypto/x509 module exposes parse(), which turns a PEM certificate into a Certificate struct that includes a PublicKey field. makePublicKey only maps three Go key types to an algorithm name: DSA, ECDSA and RSA. Any other key type (most commonly Ed25519) falls into the default branch and the whole parse() call fails with this error, even though the certificate itself parsed successfully.

Solutions

  1. Regenerate the certificate with a supported algorithm: RSA (>=2048 bits) or ECDSA (P-256/P-384), e.g. 'openssl req -x509 -newkey rsa:2048 -nodes -keyout key.pem -out cert.pem'.
  2. If you only need subject/issuer/altNames, call x509.getSubject/getIssuer/getAltNames instead of x509.parse - those do not touch the public key.
  3. If the certificate is fixed, inspect it outside k6 (openssl x509 -text -noout -in cert.pem) and drop the x509.parse call from the script.
  4. Upgrade to the latest k6 in case newer algorithms were added to makePublicKey.

Example fix

// before: cert created with `openssl req -x509 -newkey ed25519 ...`
import x509 from 'k6/crypto/x509';
const cert = x509.parse(open('./cert.pem')); // throws: unsupported public key algorithm

// after: regenerate cert with RSA/ECDSA, or avoid the public-key path
import x509 from 'k6/crypto/x509';
const subject = x509.getSubject(open('./cert.pem')); // works for Ed25519 certs
Defensive patterns

Strategy: validation

Validate before calling

# run before shipping the script; reject keys k6 cannot parse
openssl x509 -in cert.pem -noout -text | grep 'Public Key Algorithm'
# must print rsaEncryption / id-ecPublicKey / DSA; id-Ed25519 will fail in k6

Try / catch

try {
  const cert = x509.parse(pem);
} catch (e) {
  if (String(e.message).includes('unsupported public key algorithm')) {
    console.warn('cert uses an unsupported key algorithm; using getSubject instead');
    return x509.getSubject(pem);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling x509.parse(pemBytes) (import x509 from 'k6/crypto/x509') on a certificate whose SubjectPublicKeyInfo is not DSA/ECDSA/RSA, e.g. a certificate generated with 'openssl req -x509 -newkey ed25519' or issued by a CA that defaults to Ed25519. Only parse() hits this path; getSubject, getIssuer and getAltNames do not build a PublicKey.

Common situations: Modern TLS 1.3 setups and OpenSSL 3.x where Ed25519 keys are cheap and popular; certificates from cloud CAs or internal PKI that switched to Ed25519; reusing service mTLS certificates in k6 scripts to inspect them.

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/f97e405dc8ae886d. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/crypto/x509/x509.go:210

		Country:             first(issuer.Country),
		StateOrProvinceName: first(issuer.Province),
		LocalityName:        first(issuer.Locality),
		OrganizationName:    first(issuer.Organization),
		Names:               makeRdns(issuer.Names),
	}
}

func makePublicKey(parsed any) (PublicKey, error) {
	var algorithm string
	switch parsed.(type) {
	case *dsa.PublicKey:
		algorithm = "DSA"
	case *ecdsa.PublicKey:
		algorithm = "ECDSA"
	case *rsa.PublicKey:
		algorithm = "RSA"
	default:
		err := errors.New("unsupported public key algorithm")
		return PublicKey{}, err
	}
	return PublicKey{
		Algorithm: algorithm,
		Key:       parsed,
	}, nil
}

func first(values []string) string {
	if len(values) > 0 {
		return values[0]
	}
	return ""
}

func iso8601(value time.Time) string {
	return value.Format(time.RFC3339)
}

View on GitHub (pinned to 01ffac6f24)