openssl.cheat 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. % openssl, certificate, encryption
  2. ## General OpenSSL Commands
  3. # Create a new signing request and key
  4. openssl req -new -newkey rsa:<RSA_LENGTH> -nodes -out <OUTPUT_CSR> -keyout <OUTPUT_KEY>
  5. # Create a new self-signed certificate
  6. openssl req -x509 -sha256 -nodes -days <VALIDITY> -newkey rsa:<RSA_LENGTH> -out <OUTPUT_CRT> -keyout <OUTPUT_KEY>
  7. # Create a signing request from existing key
  8. openssl req -out <OUTPUT_CSR> -key <INPUT_KEY> -new
  9. # Create a signing request from existing certificate and key
  10. openssl x509 -x509toreq -out <OUTPUT_CSR> -in <INPUT_CRT> -signkey <INPUT_KEY>
  11. # Remove a passphrase from a private key
  12. openssl rsa -in <INPUT_KEY> -out <OUTPUT_PLAINTEXT_KEY>
  13. ## Converting between the different encoding
  14. # Convert a DER encoded file to a PEM encoded file
  15. openssl x509 -inform der -in <INPUT_CRT> -out <OUTPUT_PEM>
  16. # Convert a PEM encoded file to a DER encoded file
  17. openssl x509 -outform der -in <INPUT_PEM> -out <OUTPUT_CRT>
  18. # Convert a PKCS12 encoded file containing a private key and certificates to PEM
  19. openssl pkcs12 -in <INPUT_PKCS12> -out <OUTPUT_PEM> -nodes
  20. # Extract the private key from a PKCS12 encoded file
  21. openssl pkcs12 -in <INPUT_PKCS12> -out <OUTPUT_PEM> -nodes -nocerts
  22. # Extract the certificate from a PKCS12 encoded file
  23. openssl pkcs12 -in <INPUT_PKCS12> -out <OUTPUT_PEM> -nodes -nokeys
  24. # Convert a PEM certificate file and a private key to PKCS12 encoded file
  25. openssl pkcs12 -export -out <OUTPUT_PKCS12> -inkey <INPUT_KEY> -in <INPUT_CRT> -certfile <INPUT_CRT>
  26. ## Validating certificates and keys using OpenSSL
  27. # Validate a certificate signing request
  28. openssl req -text -noout -verify -in <OUTPUT_CSR>
  29. # Validate a private key
  30. openssl rsa -in <INPUT_KEY> -check
  31. # Validate a certificate
  32. openssl x509 -in <INPUT_CRT> -text -noout
  33. # Validate a PKCS12 file (.pfx or .p12)
  34. openssl pkcs12 -info -in <INPUT_PKCS12>
  35. ## Debugging using OpenSSL
  36. # Compare the MD5 hash of a certificate
  37. openssl x509 -noout -modulus -in <INPUT_CRT> | openssl md5
  38. # Compare the MD5 hash of a private key
  39. openssl rsa -noout -modulus -in <INPUT_KEY> | openssl md5
  40. # Compare the MD5 hash of a certificate signing request
  41. openssl req -noout -modulus -in <INPUT_CSR> | openssl md5
  42. # Display the server certificate chain
  43. openssl s_client -connect <URL>:<PORT>
  44. # Sensible/common default alternatives
  45. $ VALIDITY: printf "DAYS\tCOMMENT\n1\ta day\n30\ta month\n365\ta year\n730\ttwo years" --- --column 1 --headers 1
  46. $ RSA_LENGTH: printf "KEY LENGTH\tCOMMENT\n2048\t\tDefault\n4096\t\tBetter\n8192\t\tSlow?" --- --column 1 --headers 1
  47. # Attempt to find files with the appropriate endings, default to everything.
  48. $ INPUT_PKCS12: ls -a | grep -e "\(.pfx\|.p12\)" || ls -a
  49. $ INPUT_CSR: ls -a | grep -e "\(.csr\)" || ls -a
  50. $ INPUT_KEY: ls -a | grep -e "\(.key\|.pem\)" || ls -a
  51. $ INPUT_CRT: ls -a | grep -e "\(.crt\|.cer\|.der\)" || ls -a
  52. $ INPUT_PEM: ls -a | grep -e "\(.pem\)" || ls -a