99-generate-self-signed.php 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. // A very simple helper script used to generate self-signed certificates.
  3. // Accepts the CN and an optional passphrase to encrypt the private key.
  4. //
  5. // $ php examples/99-generate-self-signed.php localhost my-secret-passphrase > secret.pem
  6. // certificate details (Distinguished Name)
  7. // (OpenSSL applies defaults to missing fields)
  8. $dn = array(
  9. "commonName" => isset($argv[1]) ? $argv[1] : "localhost",
  10. // "countryName" => "AU",
  11. // "stateOrProvinceName" => "Some-State",
  12. // "localityName" => "London",
  13. // "organizationName" => "Internet Widgits Pty Ltd",
  14. // "organizationalUnitName" => "R&D",
  15. // "emailAddress" => "admin@example.com"
  16. );
  17. // create certificate which is valid for ~10 years
  18. $privkey = openssl_pkey_new();
  19. $cert = openssl_csr_new($dn, $privkey);
  20. $cert = openssl_csr_sign($cert, null, $privkey, 3650);
  21. // export public and (optionally encrypted) private key in PEM format
  22. openssl_x509_export($cert, $out);
  23. echo $out;
  24. $passphrase = isset($argv[2]) ? $argv[2] : null;
  25. openssl_pkey_export($privkey, $out, $passphrase);
  26. echo $out;