grafana/k6 · error

failed to validate private key

Error message

failed to validate private key: %w

What it means

Returned by importRSAJWK at the final stage: all JWK fields decoded successfully and an rsa.PrivateKey was assembled, but Go's privKey.Validate() rejected it — the mathematical relationship between primes, exponents and CRT coefficients does not hold (e.g. mismatched p/q from different keys, or corrupted integers). The wrapped error gives the exact consistency check that failed.

Solutions

  1. Re-export the JWK from the original key store — mixed components from different keys are the usual cause
  2. Confirm p, q, dp, dq and qi all belong to the same modulus and exponent
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at internal/js/modules/k6/webcrypto/jwk.go:397 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@8d06114777 (2026-08-18). Data as JSON: /api/errors/69fcb3d9e4cbeb2b. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/webcrypto/jwk.go:398

	}

	privKey := &rsa.PrivateKey{
		PublicKey: pubKey,
		D:         new(big.Int).SetBytes(dBytes),
		Primes: []*big.Int{
			new(big.Int).SetBytes(pBytes),
			new(big.Int).SetBytes(qBytes),
		},
		Precomputed: rsa.PrecomputedValues{
			Dp:   new(big.Int).SetBytes(dpBytes),
			Dq:   new(big.Int).SetBytes(dqBytes),
			Qinv: new(big.Int).SetBytes(qiBytes),
		},
	}

	err = privKey.Validate()
	if err != nil {
		return nil, UnknownCryptoKeyType, 0, fmt.Errorf("failed to validate private key: %w", err)
	}

	return privKey, PrivateCryptoKeyType, pubKey.N.BitLen(), nil
}

func exportRSAJWK(key *CryptoKey) (any, error) {
	exported := &JsonWebKey{}
	exported.Set("kty", "RSA")

	switch rsaKey := key.handle.(type) {
	case *rsa.PrivateKey:
		exported.Set("n", base64URLEncode(rsaKey.N.Bytes()))
		exported.Set("e", base64URLEncode(big.NewInt(int64(rsaKey.E)).Bytes()))
		exported.Set("d", base64URLEncode(rsaKey.D.Bytes()))
		exported.Set("p", base64URLEncode(rsaKey.Primes[0].Bytes()))
		exported.Set("q", base64URLEncode(rsaKey.Primes[1].Bytes()))
		exported.Set("dp", base64URLEncode(rsaKey.Precomputed.Dp.Bytes()))
		exported.Set("dq", base64URLEncode(rsaKey.Precomputed.Dq.Bytes()))

View on GitHub (pinned to 8d06114777)