SSL_connect returned=1 errno=0 state=error: certificate verify failed (unable to get local issuer certificate)

后端 未结 1 1382
暗喜
暗喜 2021-01-03 01:27

I am trying to post to a web service from my app and I am getting the following error frequently.

SSL_connect returned=1 errno=0 state=error: certificate ver         


        
相关标签:
1条回答
  • 2021-01-03 01:43

    After lots of testing, I found the correct solution. The problem was with the cert file declaration.

    I tried sending the post request using the bundled cert files (example.com.pem)

    http.ca_file = File.read(File.join(Rails.root, "/crt/example.com.pem"))
    

    So, I changed the above declaration with the each crt and key files

    http.cert = OpenSSL::X509::Certificate.new(File.read(File.join(Rails.root, "/crt/example.com.crt")))
    http.key = OpenSSL::PKey::RSA.new(File.read(File.join(Rails.root, "/crt/example.com.key")))
    req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/xml'}).
    

    It now worked.

    Complete code

    uri = URI("https://test.compassplus.com:8444/Exec")
    xml = "
    <TKKPG>
        <Request>
        <Operation>CreateOrder</Operation> 
        <Language></Language>
        <Order>
            <OrderType>Purchase</OrderType>
            <Merchant>99999</Merchant>
            <Amount>10000</Amount>
            <Currency>524</Currency>
            <Description>Tour Purchase</Description>
            <ApproveURL>/approve.html</ApproveURL>
            <CancelURL>/cancel.html</CancelURL>
            <DeclineURL></DeclineURL>
            <email></email>
            <phone></phone>
            <AddParams>
                <FA-DATA></FA-DATA>
                <SenderPostalCode></SenderPostalCode>
                <AcctType></AcctType> 
                <TranAddendums></TranAddendums> 
                <TranAddendumsVISA></TranAddendumsVISA> 
                <TranAddendumsMC></TranAddendumsMC> 
                <TranAddendumsAMEX></TranAddendumsAMEX> 
                <TranAddendumsJCB></TranAddendumsJCB> 
                <OrderExpirationPeriod></OrderExpirationPeriod> 
                <OrigAmount></OrigAmount> 
                <OrigCurrency></OrigCurrency>
            </AddParams>
            <Fee></Fee> 
        </Order>
        </Request>
    </TKKPG>
    "
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl  = true
    http.ssl_version = :TLSv1_2
    http.cert = OpenSSL::X509::Certificate.new(File.read(File.join(Rails.root, "/crt/example.com.crt")))
    http.key = OpenSSL::PKey::RSA.new(File.read(File.join(Rails.root, "/crt/example.com.key")))
    req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/xml'})
    @res = http.request(req, xml)
    

    Reference.

    HTTP library for Ruby with HTTPS, SSL Client Certificate and Keep-Alive support?

    0 讨论(0)
提交回复
热议问题