OpenSSL creates asynchronous key pairs, however I wanted to have the private key something that was human-memorizable. With the standard keys generated, this is not possible. How I achieved it was to use two types of encryption.
After generating a key pair with OpenSSL, the public key can be stored in plain text format. I then encrypted the private key itself using regular mcrypt with the human-memorizable key of my choice and converted it to ACSII using base64_encode. Then to get the private key back, I just decrypted it with mcrypt. This way I could store the encrypted private key on the server without worrying about having things stored unencrypted.
Of course, this will only be as good as your human-memorizable key is and can potentially reduce the security of your script if you choose something simple or don't use salts.
OpenSSL
- 導入
- インストール/設定
- 定義済み定数
- キー/証明書パラメータ
- 証明書の認証
- OpenSSL 関数
- openssl_csr_export_to_file — CSR をファイルにエクスポートする
- openssl_csr_export — CSR を文字列としてエクスポートする
- openssl_csr_get_public_key — CERT の公開鍵を返す
- openssl_csr_get_subject — CERT の subject を返す
- openssl_csr_new — CSR を作成する
- openssl_csr_sign — 他の CERT(あるいは自分自身)で証明書をサインする
- openssl_decrypt — Decrypts data
- openssl_dh_compute_key — Computes shared secret for public value of remote DH key and local DH key
- openssl_digest — Computes a digest
- openssl_encrypt — Encrypts data
- openssl_error_string — OpenSSL エラーメッセージを返す
- openssl_free_key — キーリソースを開放する
- openssl_get_cipher_methods — Gets available cipher methods
- openssl_get_md_methods — Gets available digest methods
- openssl_get_privatekey — openssl_pkey_get_private のエイリアス
- openssl_get_publickey — openssl_pkey_get_public のエイリアス
- openssl_open — シール(暗号化)されたデータをオープン(復号)する
- openssl_pkcs12_export_to_file — PKCS#12 互換の証明書保存ファイルをエクスポートする
- openssl_pkcs12_export — PKCS#12 互換の証明書保存ファイルを変数にエクスポートする
- openssl_pkcs12_read — PKCS#12 認証ストアをパースして配列形式にする
- openssl_pkcs7_decrypt — S/MIME 暗号化されたメッセージを復号する
- openssl_pkcs7_encrypt — S/MIME メッセージを暗号化する
- openssl_pkcs7_sign — S/MIME メッセージにサインする
- openssl_pkcs7_verify — S/MIME でサインされたメッセージの署名を検証する
- openssl_pkey_export_to_file — エクスポート可能な形式で、キーをファイルに取得する
- openssl_pkey_export — エクスポート可能な形式で、キーを文字列に取得する
- openssl_pkey_free — 秘密鍵を開放する
- openssl_pkey_get_details — キーの詳細の配列を返す
- openssl_pkey_get_private — 秘密鍵を取得する
- openssl_pkey_get_public — 証明書から公開鍵を抽出し、使用できるようにする
- openssl_pkey_new — 新規に秘密鍵を生成する
- openssl_private_decrypt — 秘密鍵でデータを復号する
- openssl_private_encrypt — 秘密鍵でデータを暗号化する
- openssl_public_decrypt — 公開鍵でデータを復号する
- openssl_public_encrypt — 公開鍵でデータを暗号化する
- openssl_random_pseudo_bytes — Generate a pseudo-random string
- openssl_seal — データをシール(暗号化)する
- openssl_sign — 署名を生成する
- openssl_verify — 署名を検証する
- openssl_x509_check_private_key — 秘密鍵が証明書に対応するかを確認する
- openssl_x509_checkpurpose — 証明書が特定の目的に使用可能かどうか確認する
- openssl_x509_export_to_file — 証明書をファイルにエクスポートする
- openssl_x509_export — 証明書を文字列としてエクスポートする
- openssl_x509_free — 証明書リソースを開放する
- openssl_x509_parse — X509 証明書をパースし、配列として情報を返す
- openssl_x509_read — X.509 証明書をパースし、リソース ID を返す
OpenSSL
Anonymous
08-Oct-2009 12:28
08-Oct-2009 12:28
bdh dot hall at gmail dot com
29-May-2009 10:18
29-May-2009 10:18
I was having a heck of a time finding help on making asynchronous encryption/decryption using private key/public key systems working, and I had to have it for creating a credit card module that uses recurring billing.
You'd be a fool to use normal, 'synchronous' or two-way encryption for this, so the whole mcrypt library won't help.
But, it turns out OpenSSL is extremely easy to use...yet it is so sparsely documented that it seems it would be incredibly hard.
So I share my day of hacking with you - I hope you find it helpful!
<?php
if (isset($_SERVER['HTTPS']) )
{
echo "SECURE: This page is being accessed through a secure connection.<br><br>";
}
else
{
echo "UNSECURE: This page is being access through an unsecure connection.<br><br>";
}
// Create the keypair
$res=openssl_pkey_new();
// Get private key
openssl_pkey_export($res, $privatekey);
// Get public key
$publickey=openssl_pkey_get_details($res);
$publickey=$publickey["key"];
echo "Private Key:<BR>$privatekey<br><br>Public Key:<BR>$publickey<BR><BR>";
$cleartext = '1234 5678 9012 3456';
echo "Clear text:<br>$cleartext<BR><BR>";
openssl_public_encrypt($cleartext, $crypttext, $publickey);
echo "Crypt text:<br>$crypttext<BR><BR>";
openssl_private_decrypt($crypttext, $decrypted, $privatekey);
echo "Decrypted text:<BR>$decrypted<br><br>";
?>
Many thanks to other contributors in the docs for making this less painful.
Note that you will want to use these sorts of functions to generate a key ONCE - save your privatekey offline for decryption, and put your public key in your scripts/configuration file. If your data is compromised you don't care about the encrypted stuff or the public key, it's only the private key and cleartext that really matter.
Good luck!
koen dot thomeer at pubmed dot be
31-Aug-2008 05:27
31-Aug-2008 05:27
For checking the status of a client certificate using OCSP, you can use this script:
<?php
// User variables:
$dir = '/path/to/temp/'; // Directory where apache has access to (chmod 777).
$RootCA = '/path/to/Root.cer'; // Points to the Root CA in PEM format.
$OCSPUrl = 'http://ocsp.url'; //Points to the OCSP URL
// Script:
$a = rand(1000,99999); // Needed if you expect more page clicks in one second!
file_put_contents($dir.$a.'cert_i.pem', $_SERVER['SSL_CLIENT_CERT_CHAIN_0']); // Issuer certificate.
file_put_contents($dir.$a.'cert_c.pem', $_SERVER['SSL_CLIENT_CERT']); // Client (authentication) certificate.
$output = shell_exec('openssl ocsp -CAfile '.$RootCA.' -issuer '.$dir.$a.'cert_i.pem -cert '.$dir.$a.'cert_c.pem -url '.$OCSPUrl);
$output2 = preg_split('/[\r\n]/', $output);
$output3 = preg_split('/: /', $output2[0]);
$ocsp = $output3[1];
echo "OCSP status: ".$ocsp; // will be "good", "revoked", or "unknown"
unlink($dir.$a.'cert_i.pem');
unlink($dir.$a.'cert_c.pem');
?>
It can be ameliorated, but it's just a beginning!
Normally, you can extract the ocsp url from the client certificate. Also, an OCSP request contains only the hash of the issuer name, the hash of the issuer's key, and the serial number of the client certificate. All three can be extracted directly from the client certificate.
