rubyopenssl

Certificate to pem to certificate not working: nested asn1 error


I want to create a certificate from a string. Why does this not work:

OpenSSL::X509::Certificate.new(OpenSSL::X509::Certificate.new.to_pem)

It returns: OpenSSL::X509::CertificateError: nested asn1 error


Solution

  • So I got the answer myself. The certificate needs at least these information:

    cert = OpenSSL::X509::Certificate.new
    cert.version = 2
    cert.serial = 0
    cert.not_before = Time.now
    cert.not_after = Time.now + 3600
    cert.public_key = key.public_key
    cert.sign key, OpenSSL::Digest::SHA1.new
    

    Then this is possible:

    OpenSSL::X509::Certificate.new(cert.to_pem) => returns