If openssl_csr_sign() works fine when self-signing, but returns FALSE when using $cacert, and there's no error message, it could have something to do with your openssl.cnf file.
In the section [ v3_ca ] there's a setting
authorityKeyIdentifier=keyid:always,issuer:always
If you change it to
authorityKeyIdentifier=issuer:always
the problem might be solved.
openssl_csr_sign
(PHP 4 >= 4.2.0, PHP 5)
openssl_csr_sign — 他の CERT(あるいは自分自身)で証明書をサインする
説明
openssl_csr_sign() は、 指定した CSR を用いて x509 証明書リソースを作成します。
注意: この関数を正しく動作させるには、正しい形式の openssl.cnf をインストールしておく必要があります。 詳細な情報は、インストールについてのセクション を参照ください。
パラメータ
- csr
-
openssl_csr_new() で作成した CSR。 file://path/to/csr 、あるいは openssl_csr_export() で生成した文字列で指定した場合は PEM エンコードされた CSR も使用可能です。
- cacert
-
作成された証明書は cacert で署名されます。 cacert が NULL の場合は、自己署名の証明書となります。
- priv_key
-
priv_key は cacert に対応する秘密鍵です。
- days
-
days は、作成された証明書の有効期限を日数で指定します。
- configargs
-
configargs で証明書の詳細設定が可能です。 configargs についての詳細な情報は openssl_csr_new() を参照ください。
- serial
-
発行される証明書のシリアル番号を、オプションで指定します。 省略した場合のデフォルトは 0 です。
返り値
成功した場合に x509 証明書リソース、失敗した場合に FALSE を返します。
変更履歴
| バージョン | 説明 |
|---|---|
| 4.3.3 | serial パラメータが追加されました。 |
例
例1 openssl_csr_sign() の例 - CSR に署名する(あなた自身の CA を作成する)
<?php
// このスクリプトでは、前のページのテキストエリアから受け取った
// CSR を利用すると仮定します。
$csrdata = $_POST["CSR"];
// これから、私たち自身 "certificate authority" 証明書を使用して
// 署名を行います。どんな証明書でも署名は可能ですが、署名された
// 証明書がソフトウェアやユーザに信頼されない限り、その手続きは
// 無意味です。
// CA cert および秘密鍵が必要です。
$cacert = "file://path/to/ca.crt";
$privkey = array("file://path/to/ca.key", "your_ca_key_passphrase");
$usercert = openssl_csr_sign($csrdata, $cacert, $privkey, 365);
// 作成された証明書を表示します。これをコピーして、
// ローカル設定(たとえば SSL サーバへの接続設定ファイルなど)
// に貼り付けます。
openssl_x509_export($usercert, $certout);
echo $certout;
// 発生したエラーをすべて表示します。
while (($e = openssl_error_string()) !== false) {
echo $e . "\n";
}
?>
You can use file_get_contents() to directly pass the content instead of giving file paths.
Also, if you get an error "sec_error_reused_issuer_and_serial", put a serial into the last parameter:
<?php
$privkey = array(file_get_contents('ca.key'),"your_ca_key_passphrase");
$usercert = openssl_csr_sign($csrdata, file_get_contents('ca.crt'),$privkey,365,NULL,'06');
openssl_x509_export($usercert,$certout);
file_put_contents('serverCASigned.crt',$certout);
?>
In that above example the serial was "06".
Here is an sample how to create valid X.509 Public and Private Key (cert/key).
When not using self signed the 4.2.1 segault. You need the CVS code at least for openssl.
<?
Header("Content-Type: text/plain");
$CA_CERT = "CA.cert.pem";
$CA_KEY = "CA.key.pem";
$req_key = openssl_pkey_new();
if(openssl_pkey_export ($req_key, $out_key)) {
$dn = array(
"countryName" => "DE",
"stateOrProvinceName" => "Frankfurt",
"organizationName" => "smcc.net",
"organizationalUnitName" => "E-Mail",
"commonName" => "Testcert"
);
$req_csr = openssl_csr_new ($dn, $req_key);
$req_cert = openssl_csr_sign($req_csr, "file://$CA_CERT", "file://$CA_KEY", 365);
if(openssl_x509_export ($req_cert, $out_cert)) {
echo "$out_key\n";
echo "$out_cert\n";
}
else echo "Failed Cert\n";
}
else echo "FailedKey\n";
?>
To generate a self-signed certificate, pass NULL as the signing certificate (2nd parameter). For example:
$req_key = openssl_pkey_new();
$dn = array(
"countryName" => "US",
"stateOrProvinceName" => "Colorado",
"organizationName" => "yPass.net",
"organizationalUnitName" => "yPass.net",
"commonName" => "yPass.net Root Certificate"
);
$req_csr = openssl_csr_new($dn, $req_key);
$req_cert = openssl_csr_sign($req_csr, NULL, $req_key, 365);
