今日は主に、使う側が呼び出す関数と中身の最適化辺りを重点に。
コンストラクタの引数がにデフォルトをつけたり。
つくったもの
インターフェイスの名前と場所を変えた。
Pearのディレクトリで「find . | xargs grep interface」とかやってみたけど、インターフェイスは見つからなかった。
どうやって配置するのが普通なんだろう。
個人的には、"I"+ベースクラス名かなと思ってる。
けど、Pearとかだとディレクトリ構造上変になりそうな。
SSL/Interface.phpはRecordLayerで使われるFragmentのインターフェイスだから、SSL/Handshakeみたいな感じでSSL/Fragmentとかそんなようなディレクトリを作ってもいいのかなと思ってる。
$ tree . . |-- SSL | |-- Certificate.php | |-- CipherSuite.php | |-- CompressionMethod.php | |-- ContentType.php | |-- Handshake | | |-- Certificate.php | | |-- ClientHello.php | | |-- Interface.php | | |-- ServerHello.php | | `-- ServerHelloDone.php | |-- Handshake.php | |-- HandshakeType.php | |-- Interface.php | |-- Plaintext.php | |-- ProtocolVersion.php | |-- Random.php | |-- SessionID.php | `-- Utils.php |-- SSL.php |-- SocketClientTCP.php |-- dump | |-- clienthello | `-- serverhello |-- test.php |-- test2.php `-- test3.php 3 directories, 24 files
SSL.php
ついにここに手をつけた。
今まではrequire_once()の塊だった。
SSL.phpを読み込んだ先でClientHelloとかServerHelloとかを1から生成するのはなかなか骨が折れる。
だから、SSL.phpに処理の塊を生成する関数を作った。
本当はデザインパターンとか解ればもっときれいに書けるんだろうけど、ただのラッパー関数になってしまった。
構造が解りづらいから、とりあえずコメントつけて誤魔化した。
あとdefineが気持ち悪かったから全部cosntに置き換えた。
<?php require_once(dirname(__FILE__).'/SSL/Interface.php'); require_once(dirname(__FILE__).'/SSL/Handshake/Interface.php'); require_once(dirname(__FILE__).'/SSL/Certificate.php'); require_once(dirname(__FILE__).'/SSL/CipherSuite.php'); require_once(dirname(__FILE__).'/SSL/CompressionMethod.php'); require_once(dirname(__FILE__).'/SSL/ContentType.php'); require_once(dirname(__FILE__).'/SSL/Handshake.php'); require_once(dirname(__FILE__).'/SSL/HandshakeType.php'); require_once(dirname(__FILE__).'/SSL/Plaintext.php'); require_once(dirname(__FILE__).'/SSL/ProtocolVersion.php'); require_once(dirname(__FILE__).'/SSL/Random.php'); require_once(dirname(__FILE__).'/SSL/SessionID.php'); require_once(dirname(__FILE__).'/SSL/Utils.php'); require_once(dirname(__FILE__).'/SSL/Handshake/Certificate.php'); require_once(dirname(__FILE__).'/SSL/Handshake/ClientHello.php'); require_once(dirname(__FILE__).'/SSL/Handshake/ServerHello.php'); require_once(dirname(__FILE__).'/SSL/Handshake/ServerHelloDone.php'); class SSL { private $_version = null; public function __construct(SSL_ProtocolVersion $version=null) { if (is_null($version)) { $version = new SSL_ProtocolVersion(0x03, 0x00); } $this->_version = $version; } public function createClientHello() { // Plaintext -> ContentType $content_type = new SSL_ContentType(SSL_ContentType::Handshake); // Plaintext -> Handshake -> HandshakeType $handshake_type = new SSL_HandshakeType(SSL_HandshakeType::ClientHello); // Plaintext -> Handshake -> HandshakeBody -> Random $random = new SSL_Random(); // Plaintext -> Handshake -> HandshakeBody -> SessionID $session = new SSL_SessionID(); // Plaintext -> Handshake -> HandshakeBody -> CipherSuite $cipher = new SSL_CipherSuite(); $cipher->add(SSL_CipherSuite::TLS_RSA_WITH_RC4_128_MD5); $cipher->add(SSL_CipherSuite::TLS_RSA_WITH_RC4_128_SHA); // Plaintext -> Handshake -> HandshakeBody -> CompressionMethod $compression = new SSL_CompressionMethod(); $compression->add(SSL_CompressionMethod::Null); // Plaintext -> Handshake -> HandshakeBody $handshake_body = new SSL_Handshake_ClientHello($this->_version, $random, $session, $cipher, $compression); // Plaintext -> Handshake $fragment = new SSL_Handshake($handshake_type, $handshake_body); return new SSL_Plaintext($content_type, $this->_version, $fragment); } }
使う側
とりあえず、ClientHelloを生成してみる。
<?php require_once('SSL.php'); $ssl = new SSL(); $clienthello = $ssl->createClientHello(); echo $clienthello->build();
結果
ncコマンドを使うとPHPでソケットの処理を書かなくて済んだ。
$ php test3.php | nc google.com 443 | xxd 0000000: 1603 0000 4a02 0000 4603 004b 9196 187f ....J...F..K.... 0000010: 3338 4f1a 96e1 39a8 8ba3 5625 9c28 5279 38O...9...V%.(Ry 0000020: ad85 d296 78dc 56db a949 8520 aebc 979a ....x.V..I. .... 0000030: 725c 7a22 1bc4 e628 40c7 2102 1b58 6e65 r\z"...(@.!..Xne 0000040: 7453 cdcf d970 0189 705e d6db 0005 0016 tS...p..p^...... 0000050: 0300 0659 0b00 0655 0006 5200 0325 3082 ...Y...U..R..%0. 0000060: 0321 3082 028a a003 0201 0202 102f dfbc .!0........../.. 0000070: f6ae 9152 6d0f 9aa3 df40 343e 9a30 0d06 ...Rm....@4>.0.. 0000080: 092a 8648 86f7 0d01 0105 0500 304c 310b .*.H........0L1. 0000090: 3009 0603 5504 0613 025a 4131 2530 2306 0...U....ZA1%0#. 00000a0: 0355 040a 131c 5468 6177 7465 2043 6f6e .U....Thawte Con 00000b0: 7375 6c74 696e 6720 2850 7479 2920 4c74 sulting (Pty) Lt 00000c0: 642e 3116 3014 0603 5504 0313 0d54 6861 d.1.0...U....Tha 00000d0: 7774 6520 5347 4320 4341 301e 170d 3039 wte SGC CA0...09 00000e0: 3132 3138 3030 3030 3030 5a17 0d31 3131 1218000000Z..111 00000f0: 3231 3832 3335 3935 395a 3068 310b 3009 218235959Z0h1.0. 0000100: 0603 5504 0613 0255 5331 1330 1106 0355 ..U....US1.0...U 0000110: 0408 130a 4361 6c69 666f 726e 6961 3116 ....California1. 0000120: 3014 0603 5504 0714 0d4d 6f75 6e74 6169 0...U....Mountai 0000130: 6e20 5669 6577 3113 3011 0603 5504 0a14 n View1.0...U... 0000140: 0a47 6f6f 676c 6520 496e 6331 1730 1506 .Google Inc1.0.. 0000150: 0355 0403 140e 7777 772e 676f 6f67 6c65 .U....www.google 0000160: 2e63 6f6d 3081 9f30 0d06 092a 8648 86f7 .com0..0...*.H.. 0000170: 0d01 0101 0500 0381 8d00 3081 8902 8181 ..........0..... 0000180: 00e8 f986 0f90 fa86 d7df bd72 26b6 d744 ...........r&..D 0000190: 0283 7873 d902 28ef 8845 39fb 10e8 7cae ..xs..(..E9...|. 00001a0: a938 d575 c638 eb0a 1507 9b83 e8cd 82d5 .8.u.8.......... 00001b0: e3f7 1568 45a1 0b19 85bc e2ef 84e7 ddf2 ...hE........... 00001c0: d7b8 98c2 a1bb b5c1 51df d483 02a7 3d06 ........Q.....=. 00001d0: 425b e122 c3de 6b85 5f1c d6da 4e8b d39b B[."..k._...N... 00001e0: eeb9 6722 2a1d 11ef 79a4 b337 8af4 fe18 ..g"*...y..7.... 00001f0: fdbc f946 2350 97f3 acfc 2446 2b5c 3bb7 ...F#P....$F+\;. 0000200: 4502 0301 0001 a381 e730 81e4 300c 0603 E........0..0... 0000210: 551d 1301 01ff 0402 3000 3036 0603 551d U.......0.06..U. 0000220: 1f04 2f30 2d30 2ba0 29a0 2786 2568 7474 ../0-0+.).'.%htt 0000230: 703a 2f2f 6372 6c2e 7468 6177 7465 2e63 p://crl.thawte.c 0000240: 6f6d 2f54 6861 7774 6553 4743 4341 2e63 om/ThawteSGCCA.c 0000250: 726c 3028 0603 551d 2504 2130 1f06 082b rl0(..U.%.!0...+ 0000260: 0601 0505 0703 0106 082b 0601 0505 0703 .........+...... 0000270: 0206 0960 8648 0186 f842 0401 3072 0608 ...`.H...B..0r.. 0000280: 2b06 0105 0507 0101 0466 3064 3022 0608 +........f0d0".. 0000290: 2b06 0105 0507 3001 8616 6874 7470 3a2f +.....0...http:/ 00002a0: 2f6f 6373 702e 7468 6177 7465 2e63 6f6d /ocsp.thawte.com 00002b0: 303e 0608 2b06 0105 0507 3002 8632 6874 0>..+.....0..2ht 00002c0: 7470 3a2f 2f77 7777 2e74 6861 7774 652e tp://www.thawte. 00002d0: 636f 6d2f 7265 706f 7369 746f 7279 2f54 com/repository/T 00002e0: 6861 7774 655f 5347 435f 4341 2e63 7274 hawte_SGC_CA.crt 00002f0: 300d 0609 2a86 4886 f70d 0101 0505 0003 0...*.H......... 0000300: 8181 009f 43cf 5bc4 5029 b1bf e2b0 9aff ....C.[.P)...... 0000310: 6a21 1d2d 12c3 2c4e 5af9 12e2 ceb9 8252 j!.-..,NZ......R 0000320: 2de7 1d7e 1a76 9690 79d1 2452 3879 bb63 -..~.v..y.$R8y.c 0000330: 8d80 977c 2320 0f91 4d16 b9ea eef4 6d89 ...|# ..M.....m. 0000340: cac6 bdcc 2468 d643 5bce 2a58 bf3c 18e0 ....$h.C[.*X.<.. 0000350: e03c 62cf 9602 2d28 4750 34e1 27ba cf99 .<b...-(GP4.'... 0000360: d150 ff29 25c0 3636 1533 5270 be31 8f9f .P.)%.66.3Rp.1.. 0000370: e87f e711 0c8d bf84 a042 1a80 89b0 3158 .........B....1X 0000380: 4107 5f00 0327 3082 0323 3082 028c a003 A._..'0..#0..... 0000390: 0201 0202 0430 0000 0230 0d06 092a 8648 .....0...0...*.H 00003a0: 86f7 0d01 0105 0500 305f 310b 3009 0603 ........0_1.0... 00003b0: 5504 0613 0255 5331 1730 1506 0355 040a U....US1.0...U.. 00003c0: 130e 5665 7269 5369 676e 2c20 496e 632e ..VeriSign, Inc. 00003d0: 3137 3035 0603 5504 0b13 2e43 6c61 7373 1705..U....Class 00003e0: 2033 2050 7562 6c69 6320 5072 696d 6172 3 Public Primar 00003f0: 7920 4365 7274 6966 6963 6174 696f 6e20 y Certification 0000400: 4175 7468 6f72 6974 7930 1e17 0d30 3430 Authority0...040 0000410: 3531 3330 3030 3030 305a 170d 3134 3035 513000000Z..1405 0000420: 3132 3233 3539 3539 5a30 4c31 0b30 0906 12235959Z0L1.0.. 0000430: 0355 0406 1302 5a41 3125 3023 0603 5504 .U....ZA1%0#..U. 0000440: 0a13 1c54 6861 7774 6520 436f 6e73 756c ...Thawte Consul 0000450: 7469 6e67 2028 5074 7929 204c 7464 2e31 ting (Pty) Ltd.1 0000460: 1630 1406 0355 0403 130d 5468 6177 7465 .0...U....Thawte 0000470: 2053 4743 2043 4130 819f 300d 0609 2a86 SGC CA0..0...*. 0000480: 4886 f70d 0101 0105 0003 818d 0030 8189 H............0.. 0000490: 0281 8100 d4d3 67d0 8d15 7fae cd31 fe7d ......g......1.} 00004a0: 1d91 a13f 0b71 3cac ccc8 64fb 63fc 324b ...?.q<...d.c.2K 00004b0: 0794 bd6f 80ba 2fe1 0493 c033 fc09 3323 ...o../....3..3# 00004c0: e90b 742b 71c4 03c6 d2cd e22f f509 63cd ..t+q....../..c. 00004d0: ff48 a500 bfe0 e7f3 88b7 2d32 de98 36e6 .H........-2..6. 00004e0: 0aad 007b c464 4a3b 8475 03f2 7092 7d0e ...{.dJ;.u..p.}. 00004f0: 62f5 21ab 6936 8431 7590 f8bf c76c 881b b.!.i6.1u....l.. 0000500: 0695 7cc9 e5a8 de75 a12c 7a68 dfd5 ca1c ..|....u.,zh.... 0000510: 8758 6019 0203 0100 01a3 81fe 3081 fb30 .X`.........0..0 0000520: 1206 0355 1d13 0101 ff04 0830 0601 01ff ...U.......0.... 0000530: 0201 0030 0b06 0355 1d0f 0404 0302 0106 ...0...U........ 0000540: 3011 0609 6086 4801 86f8 4201 0104 0403 0...`.H...B..... 0000550: 0201 0630 2806 0355 1d11 0421 301f a41d ...0(..U...!0... 0000560: 301b 3119 3017 0603 5504 0313 1050 7269 0.1.0...U....Pri 0000570: 7661 7465 4c61 6265 6c33 2d31 3530 3106 vateLabel3-1501. 0000580: 0355 1d1f 042a 3028 3026 a024 a022 8620 .U...*0(0&.$.". 0000590: 6874 7470 3a2f 2f63 726c 2e76 6572 6973 http://crl.veris 00005a0: 6967 6e2e 636f 6d2f 7063 6133 2e63 726c ign.com/pca3.crl 00005b0: 3032 0608 2b06 0105 0507 0101 0426 3024 02..+........&0$ 00005c0: 3022 0608 2b06 0105 0507 3001 8616 6874 0"..+.....0...ht 00005d0: 7470 3a2f 2f6f 6373 702e 7468 6177 7465 tp://ocsp.thawte 00005e0: 2e63 6f6d 3034 0603 551d 2504 2d30 2b06 .com04..U.%.-0+. 00005f0: 082b 0601 0505 0703 0106 082b 0601 0505 .+.........+.... 0000600: 0703 0206 0960 8648 0186 f842 0401 060a .....`.H...B.... 0000610: 6086 4801 86f8 4501 0801 300d 0609 2a86 `.H...E...0...*. 0000620: 4886 f70d 0101 0505 0003 8181 0055 ac63 H............U.c 0000630: eade a1dd d290 5f9f 0bce 76be 1351 8f93 ......_...v..Q.. 0000640: d905 2bc8 1b77 4bad 6950 a1ee dedc fddb ..+..wK.iP...... 0000650: 07e9 e839 94dc ab72 792f 06bf ab81 70c4 ...9...ry/....p. 0000660: a8ed ea53 34ed ef1e 53d9 06c7 562b d15c ...S4...S...V+.\ 0000670: f4d1 8a8e b42b b137 9048 0842 25c5 3e8a .....+.7.H.B%.>. 0000680: cb7f eb6f 04d1 6dc5 74a2 f7a2 7c7b 603c ...o..m.t...|{`< 0000690: 77cd 0ece 4802 7f01 2fb6 9b37 e02a 2a36 w...H.../..7.**6 00006a0: dcd5 85d6 ace5 3f54 6f96 1e05 af16 0300 ......?To....... 00006b0: 0004 0e00 0000 ......