mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-08-22 18:07:16 +00:00
T04 (mTLS) had grown a "WebSocketClient" subsection describing wss:// client certificates, and c12/t02 were getting similar WebSocketClient asides for timeouts and CA paths. The Cookbook's own index already separates WebSocket into its own category (W01-W04) from TLS/Security (T01-T05) and Client (C01-C19), so burying WebSocketClient specifics inside those pages fought the site's structure. Move that content into two new recipes under the WebSocket category instead: - W05: wss:// TLS setup (set_ca_cert_path CA directory parity, PemMemory client certificate) - W06: WebSocketClient's three timeouts, including the recently added chrono overloads T04, T02, C12, and W01 now carry a single reference link to the new pages instead of duplicated explanations, matching the site's existing cross-link convention. While rewriting T04's client-side section, noticed it documented SSLClient's file-path constructor but not its PemMemory one, even though the server-side section covered both forms for SSLServer. Added the missing PemMemory example so both sides are symmetric.
56 lines
3.0 KiB
Markdown
56 lines
3.0 KiB
Markdown
---
|
||
title: "T02. SSL証明書の検証を制御する"
|
||
order: 43
|
||
status: "draft"
|
||
---
|
||
|
||
HTTPSクライアントは、デフォルトでサーバー証明書を検証します。OSのルート証明書ストアを使って、証明書チェーンの有効性とホスト名の一致を確認します。この挙動を変えたいときに使うAPIを紹介します。
|
||
|
||
## 独自のCA証明書を指定する
|
||
|
||
社内認証局(CA)で署名された証明書を使うサーバーに接続するときは、`set_ca_cert_path()`でCA証明書を指定します。
|
||
|
||
```cpp
|
||
httplib::Client cli("https://internal.example.com");
|
||
cli.set_ca_cert_path("/etc/ssl/certs/internal-ca.pem");
|
||
|
||
auto res = cli.Get("/");
|
||
```
|
||
|
||
第1引数がCA証明書ファイル、第2引数がCA証明書ディレクトリ(省略可)です。OpenSSLバックエンドなら、`set_ca_cert_store()`で`X509_STORE*`を直接渡すこともできます。
|
||
|
||
## 証明書検証を無効にする(非推奨)
|
||
|
||
開発用のサーバーや自己署名証明書にアクセスしたいときは、検証を無効にできます。
|
||
|
||
```cpp
|
||
httplib::Client cli("https://self-signed.example.com");
|
||
cli.enable_server_certificate_verification(false);
|
||
|
||
auto res = cli.Get("/");
|
||
```
|
||
|
||
これだけで、証明書チェーンの検証がスキップされます。
|
||
|
||
> **Warning:** 証明書検証を無効にすると、中間者攻撃(MITM)を防げなくなります。本番環境では**絶対に使わない**でください。開発やテスト以外で無効化する必要が出たら、「もう一度やり方を間違えていないか確認する」という癖をつけましょう。
|
||
|
||
## ホスト名検証だけを無効にする
|
||
|
||
証明書チェーンは検証したいけれど、ホスト名の一致だけスキップしたい、という中間的な設定もあります。証明書のCN/SANとリクエスト先のホスト名が食い違うサーバーにアクセスするときに使います。
|
||
|
||
```cpp
|
||
cli.enable_server_hostname_verification(false);
|
||
```
|
||
|
||
証明書そのものは有効かどうか検証するので、「検証完全無効」よりは少し安全です。ただ、これも本番ではおすすめしません。
|
||
|
||
## OSの証明書ストアをそのまま使う
|
||
|
||
多くのLinuxディストリビューションでは、`/etc/ssl/certs/ca-certificates.crt`などにルート証明書がまとまっています。cpp-httplibは起動時にOSのデフォルトストアを自動で読みにいくので、普通のサーバーならとくに設定不要です。
|
||
|
||
> mbedTLSやwolfSSLバックエンドでも同じAPIが使えます。バックエンドの選び方は[T01. OpenSSL・mbedTLS・wolfSSLの選択指針](../t01-tls-backends)を参照してください。
|
||
|
||
> 失敗したときの詳細を調べる方法は[C18. SSLエラーをハンドリングする](../c18-ssl-errors)を参照してください。
|
||
|
||
> WebSocketクライアント(`wss://`)のTLS設定は[W05. wss接続でTLSを設定する](../w05-websocket-tls)を参照してください。
|