首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何保存证书链中的每个证书

如何保存证书链中的每个证书
EN

Stack Overflow用户
提问于 2009-04-22 18:12:17
回答 1查看 566关注 0票数 0

我正在寻找一种方法来检索目标服务器的证书链。我已经有了代码,可以让我获取连接时提供的服务器证书,但我还希望选择将链中的每个子证书一直拉到根证书。

下面是我用来获取目标服务器公钥的代码。不幸的是,当我只知道VB.NET的时候,我写了这篇文章,但我更喜欢C#样本……

代码语言:javascript
复制
Private Function openSSLStream(ByRef server As ServerEntry) As SslStream
    Dim sslStream As SslStream = Nothing
    Dim newClient As New System.Net.Sockets.TcpClient

    Try
        newClient = New TcpClient
        newClient.Connect(server.Name, server.Port)
        sslStream = New SslStream(newClient.GetStream(), False, New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate), Nothing)
        sslStream.AuthenticateAsClient(server.Name)
        Return sslStream
    Catch ex As Exception
        Debug.WriteLine(ex.Message)
        Return Nothing
    Finally
        If newClient.Connected Then newClient.Close()
    End Try
End Function

Private Sub GetDetails(ByRef Server As ServerEntry)
    Dim expcerdate As New Date
    Dim newSSLstream As SslStream = openSSLStream(Server)
    If Not newSSLstream Is Nothing Then
        Dim newCertificate As New X509Certificate
        Try
            newCertificate = newSSLstream.RemoteCertificate()
            expcerdate = CDate(newCertificate.GetExpirationDateString())
            Server.Subject = newCertificate.Subject
            newCertificate.GetPublicKeyString()
            Server.ValidFrom = newCertificate.GetEffectiveDateString()
            Server.ValidTo = newCertificate.GetExpirationDateString()
        Catch ex As Exception
            Server.Subject = ex.Message
        Finally
            newSSLstream = Nothing
            newCertificate = Nothing
            expcerdate = Nothing
        End Try
    End If
End Sub

这样做的最终结果是,我拥有了公钥的本地副本,可以将其保存到本地驱动器。我希望为链中的每个证书执行类似的任务,以便获得给定目标服务器的公共证书的完整列表。

要查看目前的完整应用程序,您可以使用download it from my site

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-04-23 20:54:25

我做了一些挖掘,在X509Chain和X509Certificate2调用中找到了我需要的答案。我不打算发布完整的代码,但这里是最相关的部分:

代码语言:javascript
复制
Dim cert2 As X509Certificate2
Dim ch As New X509Chain()
ch.Build(cert2)

Dim element As X509ChainElement

For Each element In ch.ChainElements
    blob = element.Certificate.RawData()
Next Element
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/778493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档