加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

在php中从smtp获取证书

发布时间:2020-12-13 16:58:29 所属栏目:PHP教程 来源:网络整理
导读:我有一个 PHP函数可以从https-connections获取证书,是否可以扩展它以便能够在smtp-starttls上使用? 我可以将其打开为“tcp://”,并在发送“STARTTLS”命令后将其切换为“ssl://”吗? function ssl_fetch_cert($domain,$port = 443){ $url = "ssl://{$dom
我有一个 PHP函数可以从https-connections获取证书,是否可以扩展它以便能够在smtp-starttls上使用?

我可以将其打开为“tcp://”,并在发送“STARTTLS”命令后将其切换为“ssl://”吗?

function ssl_fetch_cert($domain,$port = 443)
{
    $url = "ssl://{$domain}:{$port}";
    $connection_context_option['ssl']['capture_peer_cert'] = TRUE;
    $connection_context = stream_context_create($connection_context_option);
    $connection_client = stream_socket_client($url,$errno,$errstr,30,STREAM_CLIENT_CONNECT,$connection_context);
    $connection_info = stream_context_get_params($connection_client);
    // $sha256 = openssl_x509_fingerprint($connection_info['options']['ssl']['peer_certificate'],'sha256');
    return $connection_info['options']['ssl']['peer_certificate'];
}

解决方法

函数stream_socket_enable_crypto()很有用.

$url = "tcp://{$domain}:{$port}";
$connection_client = stream_socket_client($url,$connection_context);
// timeout fread after 2s
stream_set_timeout($connection_client,2);
// let the server introduce it self before sending command
fread($connection_client,10240);
// send STARTTLS command
fwrite($connection_client,"STARTTLSn");
// wait for server to say its ready,before switching
fread($connection_client,10240);
// Switching to SSL/TLS
stream_socket_enable_crypto($connection_client,TRUE,STREAM_CRYPTO_METHOD_SSLv23_CLIENT);

https://github.com/puggan/tlsa_validation_php/blob/master/functions.php#L111

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读