{"record":{"id":"082d8d5d79d0bc3c","repo":"moodle/moodle","slug":"error-on-validating-root-certificate-openssl-error-string","errorCode":null,"errorMessage":"error on validating root certificate: ' . \\openssl_error_string()","messagePattern":"error on validating root certificate: ' \\. \\\\openssl_error_string\\(\\)","errorType":"exception","errorClass":"WebAuthnException","httpStatus":null,"severity":"error","filePath":"public/lib/webauthn/src/Attestation/Format/AndroidSafetyNet.php","lineNumber":137,"sourceCode":"        return \\openssl_verify($this->_signedValue, $this->_signature, $publicKey, OPENSSL_ALGO_SHA256) === 1;\n    }\n\n\n    /**\n     * validates the certificate against root certificates\n     * @param array $rootCas\n     * @return boolean\n     * @throws WebAuthnException\n     */\n    public function validateRootCertificate($rootCas) {\n        $chainC = $this->_createX5cChainFile();\n        if ($chainC) {\n            $rootCas[] = $chainC;\n        }\n\n        $v = \\openssl_x509_checkpurpose($this->getCertificatePem(), -1, $rootCas);\n        if ($v === -1) {\n            throw new WebAuthnException('error on validating root certificate: ' . \\openssl_error_string(), WebAuthnException::CERTIFICATE_NOT_TRUSTED);\n        }\n        return $v;\n    }\n\n\n    /**\n     * decode base64 url\n     * @param string $data\n     * @return string\n     */\n    private function _base64url_decode($data) {\n        return \\base64_decode(\\strtr($data, '-_', '+/') . \\str_repeat('=', 3 - (3 + \\strlen($data)) % 4));\n    }\n}\n\n","sourceCodeStart":119,"sourceCodeEnd":153,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/webauthn/src/Attestation/Format/AndroidSafetyNet.php#L119-L153","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Provide the correct root CA file path(s) (e.g. the Google attestation root PEM) — openssl_x509_checkpurpose expects file PATHS, not certificate contents","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","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","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"],"exampleFix":"// before: passing directory or empty list\n$v = openssl_x509_checkpurpose($cert, -1, []);\n// after: pass file paths to PEM CA files\n$v = openssl_x509_checkpurpose($cert, -1, ['/etc/ssl/certs/google-attestation-root.pem']);","handlingStrategy":"validation","validationCode":"foreach ($rootCaFiles as $file) {\n    if (!is_readable($file)) {\n        throw new RuntimeException(\"CA file unreadable: $file\");\n    }\n}\nif (!file_exists('/etc/ssl/certs/google-attestation-root.pem')) {\n    throw new RuntimeException('Google SafetyNet root CA not installed');\n}","typeGuard":"function caBundleReadable(array $paths): bool {\n    return $paths !== [] && array_reduce($paths, fn($ok, $p) => $ok && is_readable($p), true);\n}","tryCatchPattern":"try {\n    $this->validateRootCertificate();\n} catch (WebAuthnException $e) {\n    if ($e->getCode() === WebAuthnException::CERTIFICATE_NOT_TRUSTED) {\n        error_log('SafetyNet chain untrusted: ' . $e->getMessage());\n    }\n}","preventionTips":["Install ca-certificates in container images and mount the Google attestation root PEM","Remember openssl_x509_checkpurpose takes CA file PATHS, never file contents","Log openssl_error_string() alongside the exception for diagnosis","Refresh the CA bundle on a schedule"],"tags":["webauthn","android-safetynet","openssl","certificate-chain"],"backgroundTag":"certificate-not-trusted","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}