# tls: 验证证书失败：x509: 证书已过期或尚未生效

- **ID:** `go/net-http-tls-handshake-error-certificate-validity`
- **领域:** go
- **类别:** auth_error
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

服务器提供的 TLS 证书不在其有效期内，要么已过期，要么尚未激活。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| go1.21 | active | — | — |
| go1.22 | active | — | — |
| go1.23 | active | — | — |

## 解决方案

1. ```
   在服务器端续期证书，或如果使用自定义 CA 则更新客户端的 CA 池：tls.Config{RootCAs: x509.NewCertPool()}
   ```
2. ```
   如果在本地测试，生成新的自签名证书并设置较晚过期时间：go run crypto/tls/generate_cert.go --host localhost
   ```

## 无效尝试

- **Setting InsecureSkipVerify: true in tls.Config** — Bypasses all certificate validation, exposing to MITM attacks; not a real fix, just a dangerous workaround. (90% 失败率)
- **Updating system clock without checking timezone** — If the certificate is genuinely expired, changing clock doesn't fix; also causes other issues. (60% 失败率)
