【问题标题】:Getting certificate from smtp in php在php中从smtp获取证书
【发布时间】:2015-11-14 21:36:30
【问题描述】:

我有一个可以从 https-connections 获取证书的 php 函数,是否可以将其扩展为也可以在 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'];
}

【问题讨论】:

    标签: php ssl starttls


    【解决方案1】:

    函数 stream_socket_enable_crypto() 很有用。

    $url = "tcp://{$domain}:{$port}";
    $connection_client = stream_socket_client($url, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $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

    【讨论】:

      猜你喜欢
      • 2018-01-09
      • 2013-01-05
      • 2011-07-03
      • 2013-01-07
      • 2017-06-29
      • 1970-01-01
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多