moodle/moodle · error · WebAuthnException

error on validating root certificate: ' …

Error message

error on validating root certificate: ' . \openssl_error_string()

What it means

The library validates the SafetyNet attestation certificate chain against a set of trusted root CA files using openssl_x509_checkpurpose(). When openssl returns -1 (verification could not be performed), the exception is thrown with the underlying openssl error string and CERTIFICATE_NOT_TRUSTED. This indicates the chain cannot be validated to any configured root, usually a missing or misconfigured CA file list.

Solutions

  1. Provide the correct root CA file path(s) (e.g. the Google attestation root PEM) — openssl_x509_checkpurpose expects file PATHS, not certificate contents
  2. Install/refresh the system CA bundle (apt-get install ca-certificates or point to a bundled cacert.pem) and confirm the files are readable by the PHP process
  3. Check that openssl_error_string()'s message in the exception identifies the specific failure (e.g. unable to get local issuer certificate) and supply that issuer as an intermediate or root
  4. If openssl is fundamentally unable to verify (-1 even with correct CAs), pin and verify the leaf certificate manually against Google's known attestation roots

Example fix

// before: passing directory or empty list
$v = openssl_x509_checkpurpose($cert, -1, []);
// after: pass file paths to PEM CA files
$v = openssl_x509_checkpurpose($cert, -1, ['/etc/ssl/certs/google-attestation-root.pem']);
Defensive patterns

Strategy: validation

Validate before calling

foreach ($rootCaFiles as $file) {
    if (!is_readable($file)) {
        throw new RuntimeException("CA file unreadable: $file");
    }
}
if (!file_exists('/etc/ssl/certs/google-attestation-root.pem')) {
    throw new RuntimeException('Google SafetyNet root CA not installed');
}

Type guard

function caBundleReadable(array $paths): bool {
    return $paths !== [] && array_reduce($paths, fn($ok, $p) => $ok && is_readable($p), true);
}

Try / catch

try {
    $this->validateRootCertificate();
} catch (WebAuthnException $e) {
    if ($e->getCode() === WebAuthnException::CERTIFICATE_NOT_TRUSTED) {
        error_log('SafetyNet chain untrusted: ' . $e->getMessage());
    }
}

Prevention

When it happens

Trigger: Calling the root-certificate validation method (validateRootCertificate via AndroidSafetyNet.getRootCertificates flow) when the root CA file paths array is empty, points to nonexistent/unreadable PEM files, or contains no CA that signed the attestation chain, causing openssl_x509_checkpurpose to return -1.

Common situations: Server missing the Google SafetyNet root CA (GSR_ROOT_R1/R2/R4) in the CA directory; wrong path passed as the $rootCas file list (openssl expects file paths, not file contents); Docker images without ca-certificates; open_basedir/read permission blocking the CA files.

Understand the failure class

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/082d8d5d79d0bc3c. Report an issue: GitHub.

Appendix: source

Thrown at public/lib/webauthn/src/Attestation/Format/AndroidSafetyNet.php:137

        return \openssl_verify($this->_signedValue, $this->_signature, $publicKey, OPENSSL_ALGO_SHA256) === 1;
    }


    /**
     * validates the certificate against root certificates
     * @param array $rootCas
     * @return boolean
     * @throws WebAuthnException
     */
    public function validateRootCertificate($rootCas) {
        $chainC = $this->_createX5cChainFile();
        if ($chainC) {
            $rootCas[] = $chainC;
        }

        $v = \openssl_x509_checkpurpose($this->getCertificatePem(), -1, $rootCas);
        if ($v === -1) {
            throw new WebAuthnException('error on validating root certificate: ' . \openssl_error_string(), WebAuthnException::CERTIFICATE_NOT_TRUSTED);
        }
        return $v;
    }


    /**
     * decode base64 url
     * @param string $data
     * @return string
     */
    private function _base64url_decode($data) {
        return \base64_decode(\strtr($data, '-_', '+/') . \str_repeat('=', 3 - (3 + \strlen($data)) % 4));
    }
}

View on GitHub (pinned to 48ea8c3322)