PHP Create CSR and Private Key



<?php
$subject = array(
    "commonName" => "aws.amazon.com",
    "organizationName" => "Amazon",
    "organizationalUnitName" => "IT",
    "localityName" => "Seattle",
    "stateOrProvinceName" => "Washington",
    "countryName" => "US",
    "emailAddress" => "example@amazon.com"
);
 
// Generate a new private (and public) key pair
$private_key = openssl_pkey_new( array('private_key_type'=>OPENSSL_KEYTYPE_RSA,'private_key_bits'=>2048) );
$csr_resource = openssl_csr_new($subject, $private_key, array('digest_alg'=>'sha256') );
 
openssl_csr_export($csr_resource, $csr_string);
openssl_pkey_export($private_key, $private_key_string, "mypassword");
echo $csr_string."\n";
echo $private_key_string."\n";
?>

output:
-----BEGIN CERTIFICATE REQUEST-----
MIIC1DCCAbwCAQAwgY4xFzAVBgNVBAMMDmF3cy5hbWF6b24uY29tMQ8wDQYDVQQK
DAZBbWF6b24xCzAJBgNVBAsMAklUMRAwDgYDVQQHDAdTZWF0dGxlMRMwEQYDVQQI
DApXYXNoaW5ndG9uMQswCQYDVQQGEwJVUzEhMB8GCSqGSIb3DQEJARYSZXhhbXBs
ZUBhbWF6b24uY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArk/k
U2FpWyE1IglvJgB+xTo1ooE9wi2J0VEL/EKVqn5jk5ZakFwm1AvG9kOtjgXNJR+s
cQZzXIag6lI4AoQsX8vX3a/zbiAnFKf62IytKJqiQk8Be+Asmkl5hMx9N12WZ0aM
W+ZDLD+6pQ1eSRBB75s1DAYEHal1bWLutxZ89iWN2nNUzBgNA48LMPmJud4rWOIJ
WouBy+D97uM1o2qyoRbfr+VO8SxkGBWpc0UsLOWchK/FcERaqsjhxayOYddfzObY
IkfWxQ9ga3SJROEO6XqOKsmkz9A3ZcQRLM92PV+gKHOQVv1ji3K6kSu3EFjyZE8j
Fd/XHL+4QLnKSygKUwIDAQABoAAwDQYJKoZIhvcNAQELBQADggEBAHwnocexJTSI
DfHCz4mmJLiwEX+iPLCcIcwUmi518DzsglmW4+fFYVtKKhJa6T5yUxzziGQzvc2L
tI2l5Xttrfj64HULbJtfj4/oR7LMUvCbrjCZE7H933Kz5ZKiNfcACyho8o08SzOw
WgT+j4hwxWaPC/u387RTBRmIsQOA0GWQ0KqgvYcITcMiyuIVAO2FCJV4pGs4U0MW
Wj6A97gXql+Z02iELpiD4wXMa0v8MTk4E+HQnqAsuuopzaIr/ZUgJsjNOfd5OK3w
OX4wWkqgWrR+uhg4kYy54eNHrbF1DvuCz7OqD0wzZ9VdUR5CK1qRffug8zJM4sbM
Hz6jLw4GxDk=
-----END CERTIFICATE REQUEST-----
 
 
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQI13PFl7v7js4CAggA
MBQGCCqGSIb3DQMHBAixLobjfWgZUQSCBMieflVwQC2sUrhakI/QxA+M5eKDYoyf
BXQ7xTkNA6U3VM8GhBPwaNzs7rhB2mYcGtxvm1FmsuCwgxaacJlOFCtIIq5F+jH9
nt7eXC4UvyPZcoCBAPtqKaV6c6L7t0thneguqiwAjM/abNkUzZGuxZ5fuViJnpg8
NAEZsiJsCO6pcxzEKCcSYZ9dKKzb+SwU5hklgwWNps6g8fH+5rC/1yPPOclpLqtM
[... abbreviated ...]
4Lan/O3GSfDKNSR/CdM8XlwMBHl/kmfUKdBMcrKuLc/krlw4eNt7qa4e/yKTKJBt
zgH7IMVlno62cNV13VWHNam0UmkIa81yjs4aSMZtx/dWPVkRolWUNBLPHbhIkQC/
OfSvlMwm8ORdWm3Vp0lxH9brhkuOBRfDoTLSCk378B+9gYIPYDXPkFOywn3hTJda
kcJddlOfP3cy3tq9mFsqj4IWCsfvRBCxYNh/zu6A5FfF8PNLQWTZjN+bL2NupVhA
gpfFvxpxjXqulW/MesdZNz+TGFp3wkX757FgOs5j8zWoMQkfYLpWaWQL5RccmCiZ
Qwo=
-----END ENCRYPTED PRIVATE KEY-----
code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)