为了防止中间人攻击(假装是其他人的服务器),我想验证我通过SSL连接的SMTP服务器是否有一个有效的SSL证书,证明它是我认为的.
例如,在端口25上连接到SMTP服务器后,我可以切换到安全连接,如下所示:
<?php
$smtp = fsockopen( "tcp://mail.example.com", 25, $errno, $errstr );
fread( $smtp, 512 );
fwrite($smtp,"HELO mail.example.me\r\n"); // .me is client, .com is server
fread($smtp, 512);
fwrite($smtp,"STARTTLS\r\n");
fread($smtp, 512);
stream_socket_enable_crypto( $smtp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT );
fwrite($smtp,"HELO mail.example.me\r\n");
Run Code Online (Sandbox Code Playgroud)
但是,没有提到PHP检查SSL证书的位置.PHP有一个内置的根CA列表吗?它只是接受任何东西吗?
什么是验证证书有效的正确方法,以及SMTP服务器真的是我认为的那个?
根据PHP.net上的这条评论,似乎我可以使用一些流选项进行SSL检查.最好的部分是stream_context_set_option接受上下文或流资源.因此,在TCP连接的某个时刻,您可以使用CA证书捆绑包切换到SSL .
$resource = fsockopen( "tcp://mail.example.com", 25, $errno, $errstr );
...
stream_set_blocking($resource, true);
stream_context_set_option($resource, 'ssl', 'verify_host', true);
stream_context_set_option($resource, 'ssl', 'verify_peer', true);
stream_context_set_option($resource, 'ssl', 'allow_self_signed', false);
stream_context_set_option($resource, 'ssl', 'cafile', __DIR__ . '/cacert.pem'); …Run Code Online (Sandbox Code Playgroud)