vedranvinko

self-signed_cert

Mar 28th, 2016
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.81 KB | None | 0 0
  1. # https://gist.github.com/nickyp/886884
  2.  
  3. require 'openssl'
  4.  
  5. key = OpenSSL::PKey::RSA.new(1024)
  6. public_key = key.public_key
  7.  
  8. subject = "/C=BE/O=Test/OU=Test/CN=Test"
  9.  
  10. cert = OpenSSL::X509::Certificate.new
  11. cert.subject = cert.issuer = OpenSSL::X509::Name.parse(subject)
  12. cert.not_before = Time.now
  13. cert.not_after = Time.now + 365 * 24 * 60 * 60
  14. cert.public_key = public_key
  15. cert.serial = 0x0
  16. cert.version = 2
  17.  
  18. ef = OpenSSL::X509::ExtensionFactory.new
  19. ef.subject_certificate = cert
  20. ef.issuer_certificate = cert
  21. cert.extensions = [
  22.   ef.create_extension("basicConstraints","CA:TRUE", true),
  23.   ef.create_extension("subjectKeyIdentifier", "hash"),
  24. ]
  25. cert.add_extension ef.create_extension("authorityKeyIdentifier", "keyid:always,issuer:always")
  26.  
  27. cert.sign key, OpenSSL::Digest::SHA1.new
  28.  
  29. puts cert.to_pem
Advertisement
Add Comment
Please, Sign In to add comment