siyuan-note/siyuan · error
failed to decode CA key PEM
Error message
failed to decode CA key PEM
What it means
loadCA loads the local TLS CA certificate and key from PEM files. This error means pem.Decode could not parse the CA private key file into a PEM block — the key file is empty, corrupt, or not PEM-encoded, so no EC private key can be extracted.
Solutions
- Delete the corrupted CA key (and cert) files so the kernel regenerates the CA on next start
- Regenerate the TLS certificate pair via the app's TLS/certificate refresh path
- Restore the key file from backup, ensuring valid PEM: -----BEGIN EC PRIVATE KEY----- ... -----END EC PRIVATE KEY-----
- Check disk space and file permissions to prevent truncated writes
Example fix
// before: key file contains garbage ca.key: <random bytes> // after: valid PEM or delete to regenerate -----BEGIN EC PRIVATE KEY----- MHQCAQEEI... -----END EC PRIVATE KEY-----
Defensive patterns
Strategy: fallback
Validate before calling
const pem = fs.readFileSync(keyPath, 'utf8');
if (!pem.includes('-----BEGIN EC PRIVATE KEY-----')) regenerateCA(); Type guard
func isValidKeyPEM(b []byte) bool {
blk, _ := pem.Decode(b)
return blk != nil && blk.Type == "EC PRIVATE KEY"
} Try / catch
caCert, caKey, err := loadCA()
if err != nil && strings.Contains(err.Error(), "decode CA key PEM") {
caCert, caKey, err = generateCA() // regenerate and retry
} Prevention
- Never hand-edit generated key files
- Ensure writes complete (check disk space, avoid interrupting first-run generation)
- Back up the CA pair as a unit
- Validate PEM on restore from backup
When it happens
Trigger: GetOrCreateTLSCert / refreshCertificate loading an existing but corrupted/truncated CA key PEM; the key file was overwritten with non-PEM content or created with zero bytes; disk corruption or interrupted write during CA generation.
Common situations: Manual edits to the workspace CA key file; backup/restore copying partial files; antivirus or disk-full truncating the key during first-run generation.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- failed to decode CA certificate PEM
- failed to decode certificate PEM
- failed to parse CA certificate
- failed to decode CA private key PEM
- failed to generate TLS server certificate
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/a4f66a66d2dcd4f1.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/util/cert.go:274
block, _ := pem.Decode(certPEM)
if block == nil {
return nil, nil, fmt.Errorf("failed to decode CA certificate PEM")
}
caCert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, nil, err
}
keyPEM, err := os.ReadFile(keyPath)
if err != nil {
return nil, nil, err
}
block, _ = pem.Decode(keyPEM)
if block == nil {
return nil, nil, fmt.Errorf("failed to decode CA key PEM")
}
caKey, err := x509.ParseECPrivateKey(block.Bytes)
if err != nil {
return nil, nil, err
}
return caCert, caKey, nil
}
func writeCertAndKey(certPath, keyPath string, certDER []byte, privateKey *ecdsa.PrivateKey) error {
certFile, err := os.Create(certPath)
if err != nil {
return err
}
defer certFile.Close()
if err = pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: certDER}); err != nil {View on GitHub (pinned to 9f775e8a12)