Advertisement
Guest User

reproduce OpenSSL error

a guest
Apr 9th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. from OpenSSL import crypto
  2.  
  3. # Generate key-pair
  4. key = crypto.PKey()
  5. key.generate_key(crypto.TYPE_RSA, 512)
  6.  
  7. # Generate CSR
  8. req = crypto.X509Req()
  9. req.get_subject().CN = "walrus.example.com"
  10. req.set_pubkey(key)
  11.  
  12. # dump unsigned CSR
  13. unsigned_csr_dump = crypto.dump_certificate_request(crypto.FILETYPE_PEM, req)
  14. with open('unsigned.csr', 'w') as unsigned_csr:
  15.     unsigned_csr.write(unsigned_csr_dump)
  16.  
  17. # sign the CSR
  18. req.sign(key, "sha256")
  19.  
  20. # dump signed CSR
  21. signed_csr_dump = crypto.dump_certificate_request(crypto.FILETYPE_PEM, req)
  22. with open('signed.csr', 'w') as signed_csr:
  23.     signed_csr.write(signed_csr_dump)
  24.  
  25. # If the CSR has the signature, it will successfully load.
  26. csr = crypto.load_certificate_request(crypto.FILETYPE_PEM, signed_csr_dump)
  27. print 'Loaded signed CSR'
  28.  
  29. # If the CSR doesn't have the signature, there will be an error while trying to load it
  30. csr = crypto.load_certificate_request(crypto.FILETYPE_PEM, unsigned_csr_dump)
  31. # The code won't get here
  32. print 'Loaded unsigned CSR'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement