在php中从smtp获取证书

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了在php中从smtp获取证书脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 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,"STARTTLS\n");
// 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

脚本宝典总结

以上是脚本宝典为你收集整理的在php中从smtp获取证书全部内容,希望文章能够帮你解决在php中从smtp获取证书所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。