To set the "basicConstraints" to "critical,CA:TRUE", you have to define configargs, but in the openssl_csr_sign() function !
That's my example of code to sign a "child" certificate :
$CAcrt = "file://ca.crt";
$CAkey = array("file://ca.key", "myPassWord");
$clientKeys = openssl_pkey_new();
$dn = array(
"countryName" => "FR",
"stateOrProvinceName" => "Finistere",
"localityName" => "Plouzane",
"organizationName" => "Ecole Nationale d'Ingenieurs de Brest",
"organizationalUnitName" => "Enib Students",
"commonName" => "www.enib.fr",
"emailAddress" => "ilovessl@php.net"
);
$csr = openssl_csr_new($dn, $clientPrivKey);
$configArgs = array("x509_extensions" => "v3_req");
$cert = openssl_csr_sign($csr, $CAcrt, $CAkey, 100, $configArgs);
openssl_x509_export_to_file($cert, "childCert.crt");
Then if you want to add some more options, you can edit the "/etc/ssl/openssl.cnf" ssl config' file (debian path), and add these after the [ v3_req ] tag.
openssl_csr_new
(PHP 4 >= 4.2.0, PHP 5)
openssl_csr_new — CSR を作成する
説明
openssl_csr_new() は、新しい CSR (Certificate Signing Request) を dn の情報に基づいて作成します。dn は、証明書で使用される識別名です。
注意: この関数を正しく動作させるには、正しい形式の openssl.cnf をインストールしておく必要があります。 詳細な情報は、インストールについてのセクション を参照ください。
パラメータ
- dn
-
証明書で使用される識別名。
- privkey
-
privkey には、事前に openssl_pkey_new() (あるいはその他の openssl_pkey 系の関数)で作成した秘密鍵を設定します。 これに対応する公開鍵が、CSR への署名に使用されます。
- configargs
-
デフォルトでは、システムの openssl.conf の設定に したがってリクエストが初期化されます。configargs のキー config_section_section を設定することで、この デフォルト項目を変更することが可能です。また、キー config に別の openssl 設定ファイルを指定することで別の設定を使用することも可能です。 もし configargs に以下の表のキーが存在すれば、それらは openssl.conf の対応する項目と同じ働きをします。
設定の上書き configargs のキー 型 openssl.conf で同等の意味を持つ項目 説明 digest_alg string default_md 使用するダイジェストメソッドを選択します x509_extensions string x509_extensions x509 証明書を作成する際に使用する拡張モジュールを選択します req_extensions string req_extensions CSR を作成する際に使用する拡張モジュールを選択します private_key_bits integer default_bits 秘密鍵を作成する際に使用するビット数を指定します private_key_type integer none 作成する秘密鍵の型を指定します。以下の定数 OPENSSL_KEYTYPE_DSA、 OPENSSL_KEYTYPE_DH あるいは OPENSSL_KEYTYPE_RSA からひとつ選択します。 デフォルト値は OPENSSL_KEYTYPE_RSA で、 現時点でサポートしているのはこの型のみです。 encrypt_key boolean encrypt_key (パスフレーズとともに)エクスポートされるキーを暗号化するか? - extraattribs
-
extraattribs は、CSR に関する追加の設定情報を 設定するために使用します。dn および extraattribs はどちらも連想配列で、それらの キーが OID に変換されたうえでリクエストの関連する部分に適用されます。
返り値
CSR を返します。
例
例1 自己署名証明書の作成
<?php
// 証明書で使用する識別名を指定します。
// ご自分の環境に応じて、名前や会社名を変更する必要があります。
// より厳密に言うなら、あなたが証明書を作成しようとしている個人あるいは
// 組織の名前と会社名です。
// SSL 証明書では、通常は証明書を使用するドメイン名を commonName に
// 指定します。しかし S/MIME 証明書では、証明書を使用する個人の名前を
// commonName に指定します。
$dn = array(
"countryName" => "UK",
"stateOrProvinceName" => "Somerset",
"localityName" => "Glastonbury",
"organizationName" => "The Brain Room Limited",
"organizationalUnitName" => "PHP Documentation Team",
"commonName" => "Wez Furlong",
"emailAddress" => "wez@example.com"
);
// 新しい秘密鍵(および公開鍵)のペアを作成します
$privkey = openssl_pkey_new();
// 証明書への署名要求を作成します
$csr = openssl_csr_new($dn, $privkey);
// 認証局があなたの申請を受け付けてくれるまでは、自己署名の証明書を
// 作成したくなるでしょう。
// これは、365 日間有効な自己署名の証明書を作成します。
$sscert = openssl_csr_sign($csr, null, $privkey, 365);
// ここで、Web サーバやメールサーバ、メールクライアント(証明書の使用方法に
// 依存します)にインストールするために、
// 作成した秘密鍵・CSR および自己署名の証明書を保存したくなることでしょう。
// この例では、これらを変数に格納する方法を示します。とはいえ、もちろん
// それを直接ファイルに保存することも可能です。
// 一般的には、CSR を認証局に送り、「正式な」証明書として証明してもらう
// ことになります。
openssl_csr_export($csr, $csrout) and var_dump($csrout);
openssl_x509_export($sscert, $certout) and var_dump($certout);
openssl_pkey_export($privkey, $pkeyout, "mypassword") and var_dump($pkeyout);
// 発生したエラーを表示します。
while (($e = openssl_error_string()) !== false) {
echo $e . "\n";
}
?>
openssl_csr_new
02-Sep-2008 08:09
04-Jul-2007 05:45
Is there some way to change the distinguished name using this function? I have tried adding overrides to the dn to configargs and extraattribs but this did not have an impact on the certificate.
Example: A CSR is submitted and I want to change only the commonName (CN) before signing the certificate.
14-Feb-2006 10:57
How in openssl_csr_new usign [, array configargs [, array extraattribs]]
because I am have add this extension to certificate
/********************
basicConstraints = critical,CA:TRUE,pathlen:0
nsCertType = sslCA,emailCA,objCA
**********************************/
Rafal
If you get the error:
error:0D11A086:asn1 encoding routines:ASN1_mbstring_copy:string too short
then look at your key:value pairs in the $dn (distinguished name) array.
If you have one value (like "organizationalUnitName" = "") set to an empty string, it will throw the above error.
Fix the error by either eliminating that array element from $dn completely, or using a space " " instead of an empty string.
12-May-2005 08:12
I am using PHP-4.3.11.
The type of configargs--private_key_bits is a INTEGER, not a string.
An example of configration:
<?php
$config = array(
"digest_alg" => "sha1",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_DSA,
"encrypt_key" => false
);
?>
09-Feb-2005 06:31
As you probably guessed from the example, the documentation is misinforming. openssl_csr_new returns a CSR resource or FALSE on failure.
mixed openssl_csr_new (assoc_array dn, resource_privkey, [...])
