| 1 | <?php |
| 2 | /************************************************************* |
| 3 | # 076 License |
| 4 | |
| 5 | Copyright (c) テクニカル諏訪子 |
| 6 | |
| 7 | Permission is hereby granted to any person obtaining a copy of the software |
| 8 | Little Beast (the "Software") to use, modify, merge, copy, publish, distribute, |
| 9 | sublicense, and/or sell copies of the Software, subject to the following conditions: |
| 10 | |
| 11 | 1. **Origin Attribution**: |
| 12 | - You must not misrepresent the origin of the Software; you must not claim |
| 13 | you created the original Software. |
| 14 | - If the Software is used in a product, you must either: |
| 15 | a. Provide clear attribution in the product's documentation, user interface, |
| 16 | or other visible areas, **OR** |
| 17 | b. Pay the original developers a fee they specify in writing. |
| 18 | 2. **Usage Restriction**: |
| 19 | - The Software, or any derivative works, dependencies, or libraries |
| 20 | incorporating it, must not be used for censorship or to suppress freedom of |
| 21 | speech, expression, or creativity. Prohibited uses include, but are not |
| 22 | limited to: |
| 23 | - Censorship of so-called "hate speech", visuals, non-mainstream opinions, |
| 24 | ideas, or objective reality. |
| 25 | - Tools or systems designed to restrict access to information or |
| 26 | artistic works. |
| 27 | 3. **Notice Preservation**: |
| 28 | - This license and the above copyright notice must remain intact in all copies |
| 29 | of the source code. |
| 30 | |
| 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 32 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 33 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 34 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 35 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 36 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 37 | */ |
| 38 | // テスト用のTLS 1.3のサーバー(MockServerが起動する、Std\Lib\Tlsのサーバー側を使う) |
| 39 | // 使い方:php tls-server.php ポート 証明書のPEMファイル 秘密鍵のPEMファイル [ALPN(カンマ区切り)] [グループ(カンマ区切り)] [悪意] [最大のバージョン] |
| 40 | // 悪意:bad-finished(間違ったFinishedを送る)、bad-cv(間違ったCertificateVerify・ServerKeyExchangeの署名を送る)、 |
| 41 | // downgrade(クライアントのTLS 1.3を無視して1.2にする=攻撃者がClientHelloを書き換えた場合) |
| 42 | // 受け取った行をそのまま返す。特別な行: |
| 43 | // INFO 接続の情報(JSON) |
| 44 | // KEYUPDATE 鍵を更新して(相手にも求める)「updated」 |
| 45 | // BIG n nバイトを返す |
| 46 | // CLOSE close_notifyを送って閉じる |
| 47 | // DROP close_notifyを送らずに切る |
| 48 | // GARBAGE 壊れた(復号出来ない)レコードを送る |
| 49 | // HELLOREQ TLS 1.2の再ネゴシエーションを求める(HelloRequest)、その後「asked」 |
| 50 | |
| 51 | require_once __DIR__.'/../../../../autoload.php'; |
| 52 | |
| 53 | use Std\Lib\Crypto\Ec; |
| 54 | use Std\Lib\Crypto\Rsa; |
| 55 | use Std\Lib\Tls; |
| 56 | use Std\Lib\Tls\Connection; |
| 57 | use Std\Lib\Tls\Stream; |
| 58 | |
| 59 | /** クライアントの確認をテストする為の、間違ったメッセージを送るサーバー */ |
| 60 | class EvilConnection extends Connection { |
| 61 | private int $finishedCalls = 0; |
| 62 | |
| 63 | public function __construct($sock, array $opt, private string $mode) { |
| 64 | parent::__construct($sock, $opt, true); |
| 65 | } |
| 66 | |
| 67 | protected function finished(string $base): string { |
| 68 | $f = parent::finished($base); |
| 69 | // サーバーは最初に自分のFinishedを作る |
| 70 | return $this->mode === 'bad-finished' && $this->finishedCalls++ === 0 ? ~$f : $f; |
| 71 | } |
| 72 | |
| 73 | protected function finished12(string $master, string $label): string { |
| 74 | $f = parent::finished12($master, $label); |
| 75 | return $this->mode === 'bad-finished' && $label === 'server finished' ? ~$f : $f; |
| 76 | } |
| 77 | |
| 78 | protected function signTranscript(Rsa|Ec $key, int $scheme, string $context): string { |
| 79 | $sig = parent::signTranscript($key, $scheme, $context); |
| 80 | return $this->mode === 'bad-cv' ? $key->sign('別のメッセージ', 'sha256') : $sig; |
| 81 | } |
| 82 | |
| 83 | protected function signKeyExchange(Rsa|Ec $key, int $scheme, string $data): string { |
| 84 | $sig = parent::signKeyExchange($key, $scheme, $data); |
| 85 | return $this->mode === 'bad-cv' ? $key->sign('別のメッセージ', 'sha256') : $sig; |
| 86 | } |
| 87 | |
| 88 | protected function parseClientHello(string $b): array { |
| 89 | $ch = parent::parseClientHello($b); |
| 90 | if ($this->mode === 'downgrade') $ch['versions'] = array_values(array_diff($ch['versions'], [0x0304])); |
| 91 | return $ch; |
| 92 | } |
| 93 | |
| 94 | /** TLS 1.2の再ネゴシエーションを求める */ |
| 95 | public function helloRequest(): void { |
| 96 | $this->writeRecord(22, "\x00\x00\x00\x00"); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | $port = (int)($argv[1] ?? 0); |
| 101 | $opt = ['cert' => $argv[2] ?? '', 'key' => $argv[3] ?? '']; |
| 102 | if (!empty($argv[4])) $opt['alpn'] = explode(',', $argv[4]); |
| 103 | if (!empty($argv[5])) $opt['groups'] = explode(',', $argv[5]); |
| 104 | $evil = $argv[6] ?? ''; |
| 105 | if (!empty($argv[7])) $opt['max_version'] = $argv[7]; |
| 106 | |
| 107 | $server = @stream_socket_server("tcp://127.0.0.1:{$port}", $errno, $errstr); |
| 108 | if (!$server) { |
| 109 | fwrite(STDOUT, "error {$errstr}\n"); |
| 110 | exit(1); |
| 111 | } |
| 112 | fwrite(STDOUT, "ready\n"); |
| 113 | fflush(STDOUT); |
| 114 | |
| 115 | while (true) { |
| 116 | $c = @stream_socket_accept($server, 120); |
| 117 | if (!$c) break; |
| 118 | stream_set_timeout($c, 10); |
| 119 | // MockServer::stop()の終了のリクエスト(暗号化されていない) |
| 120 | $peek = stream_socket_recvfrom($c, 4, STREAM_PEEK); |
| 121 | if ($peek === 'QUIT') break; |
| 122 | |
| 123 | try { |
| 124 | $conn = new EvilConnection($c, $opt, $evil); |
| 125 | $conn->handshake(); |
| 126 | $s = Stream::wrap($conn); |
| 127 | } catch (\Throwable $e) { |
| 128 | fwrite(STDERR, $e->getMessage()."\n"); |
| 129 | @fclose($c); |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | while (($line = fgets($s)) !== false) { |
| 134 | $cmd = rtrim($line, "\r\n"); |
| 135 | if ($cmd === 'INFO') { |
| 136 | $i = Tls::info($s); |
| 137 | unset($i['peer_certificates']); |
| 138 | fwrite($s, json_encode($i)."\n"); |
| 139 | } else if ($cmd === 'KEYUPDATE') { |
| 140 | Stream::connection($s)->keyUpdate(true); |
| 141 | fwrite($s, "updated\n"); |
| 142 | } else if (str_starts_with($cmd, 'BIG ')) { |
| 143 | fwrite($s, str_repeat('x', (int)substr($cmd, 4))); |
| 144 | } else if ($cmd === 'CLOSE') { |
| 145 | break; |
| 146 | } else if ($cmd === 'HELLOREQ') { |
| 147 | $conn->helloRequest(); |
| 148 | fwrite($s, "asked\n"); |
| 149 | } else if ($cmd === 'GARBAGE') { |
| 150 | fwrite(Stream::connection($s)->socket(), "\x17\x03\x03\x00\x20".random_bytes(32)); |
| 151 | } else if ($cmd === 'DROP') { |
| 152 | fclose(Stream::connection($s)->socket()); |
| 153 | continue 2; |
| 154 | } else { |
| 155 | fwrite($s, $line); |
| 156 | } |
| 157 | } |
| 158 | fclose($s); |
| 159 | } |