fszlin / certes

A client implementation for the Automated Certificate Management Environment (ACME) protocol
MIT License
552 stars 122 forks source link

It encounters a full break when using in Visual Basic. #327

Closed MilanHoesl closed 3 months ago

MilanHoesl commented 3 months ago

I've created a simple script in C# following the documentation, but when I try to replicate it in VB, it fails to create an account and instead breaks without even throwing an error.

.Net 8.0 Console Application

`

Imports System.IO Imports Certes Imports Certes.Acme Imports Certes.Pkcs

Module Program Sub Main() CertificateManager("", "", "") End Sub

Async Sub CertificateManager(Mail As String, Domain As String, Password As String)
    Try
        Console.WriteLine("- Started -")
        Dim Acme As New AcmeContext(WellKnownServers.LetsEncryptStagingV2)
        Dim Account As IAccountContext = Await AcmeAccount(Acme, Mail)
        Dim Order As IOrderContext = Await AcmeOrder(Acme, Domain)
        AcmeAuthorization(Order)
        Dim Certificate As CertificateChain = Await AcmeCertificate(Order, Password)
        Console.WriteLine("- Finished -")
    Catch ex As AcmeException
        Console.WriteLine(ex.Message)
    End Try

    Console.ReadLine()
End Sub

Async Function AcmeAccount(Acme As AcmeContext, Mail As String) As Task(Of IAccountContext)
    Dim Path As String = "account.pem"
    Dim Account As IAccountContext

    If File.Exists(Path) Then
        Console.WriteLine("Load Account...")
        Dim Pem As String = File.ReadAllText(Path)
        Dim Key As IKey = KeyFactory.FromPem(Pem)
        Acme = New AcmeContext(WellKnownServers.LetsEncryptStagingV2, Key)
        Account = Await Acme.Account()
    Else
        Console.WriteLine("Create Account...")
        Account = Await Acme.NewAccount(Mail, True)
        Dim Pem As String = Acme.AccountKey.ToPem()
        File.WriteAllText(Path, Pem)
    End If

    Return Nothing
End Function

Async Function AcmeOrder(Acme As AcmeContext, Domain As String) As Task(Of IOrderContext)
    Dim Order As IOrderContext = Await Acme.NewOrder(New List(Of String) From {Domain})
    Console.WriteLine("Order Created...")
    Return Order
End Function

Async Sub AcmeAuthorization(Order As IOrderContext)
    Dim Authz As IAuthorizationContext = (Await Order.Authorizations()).First()
    Dim HttpChallange As IChallengeContext = Await Authz.Http()
    Dim KeyAuthz As String = HttpChallange.KeyAuthz

    Console.WriteLine("Validate Order...")
    Await HttpChallange.Validate()
End Sub

Async Function AcmeCertificate(Order As IOrderContext, Password As String) As Task(Of CertificateChain)
    Dim Key As IKey = KeyFactory.NewKey(KeyAlgorithm.ES256)
    Dim Csr As New CsrInfo() With
    {
        .CountryName = "DE",
        .State = "",
        .Locality = "",
        .Organization = "",
        .OrganizationUnit = "Dev"
    }

    Console.WriteLine("Create Certificate...")
    Dim Cert As CertificateChain = Await Order.Generate(Csr, Key)

    Dim Pem As String = Cert.ToPem()

    Dim PfxBuild As PfxBuilder = Cert.ToPfx(Key)
    Dim Pfx As Byte() = PfxBuild.Build("my-cert", Password)

    Return Cert
End Function

End Module

`