Not sure whether the "bug" (undocumented behavior) I encountered is common to other people, but this comment might save hours of painful debug:
If you can't generate a new private key using openssl_pkey_new() or openssl_csr_new(), your script hangs during the call of these functions and in case you specified a "private_key_bits" parameter, ensure that you cast the variable to an int. Took me ages to notice that.
<?php
$SSLcnf = array('config' => '/usr/local/nessy2/share/ssl/openssl.cnf',
'encrypt_key' => true,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
'digest_alg' => 'sha1',
'x509_extensions' => 'v3_ca',
'private_key_bits' => $someVariable // ---> bad
'private_key_bits' => (int)$someVariable // ---> good
'private_key_bits' => 512 // ---> obviously good
);
?>
openssl_csr_new
(PHP 4 >= 4.2.0, PHP 5)
openssl_csr_new — Bir sertifika imzalama isteği (CSR) üretir
Açıklama
$dn
, resource &$gizanh
[, array $ayarlar
[, array $ek_seçenekler
]] )
dn ile belirtilen, sertifika için kullanılacak
ayırt edilebilir isim ile sağlanan bilgiye dayanarak yeni bir sertifika
imzalama isteği (CSR) üretir.
Bilginize: Bu işlevin gerektiği gibi çalışması için geçerli bir openssl.cnf kurulu olmalıdır. Daha ayrıntılı bilgi için kurulum bölümüne bakınız.
Değiştirgeler
-
dn -
Sertifika için kullanılacak ayırt edilebilir isim.
-
gizanh -
openssl_pkey_new() (veya başka bir openssl_pkey işlevi) ile üretilmiş bir gizli anahtar. Anahtar çiftinin genel anahtar parçası CSR'yi imzalamakta kullanılır.
-
ayarlar -
İsteği ilklendirmek için öntanımlı olarak sisteminizdeki openssl.conf kullanılır.
ayarlardizisinin config_section_section anahtarında başka bir yapılandırma dosyası bölümü belirtebileceğiniz gibi config anahtarında başka bir openssl yapılandırma dosyasının yolunu da belirtebilirsiniz.ayarlardizisi aşağıdaki anahtarları içerebilir. Bu anahtarlar openssl.conf dosyasındaki eşdeğerleri gibi davranırlar.Yapılandırma Geçersizleştiricileri ayarlaranahtarıTürü openssl.conf eşdeğeri Açıklama digest_alg string default_md Kullanılacak özet yöntemini belirler. x509_extensions string x509_extensions Bir x509 sertifikası üretilirken kullanılacak eklentileri belirler. req_extensions string req_extensions Bir CSR üretilirken kullanılacak eklentileri belirler. private_key_bits integer default_bits Bir gizli anahtarın kaç bitlik olacağını belirler. private_key_type integer none Üretilecek gizli anahtarın türünü belirler. Şu sabitlerden biri olabilir: OPENSSL_KEYTYPE_DSA,OPENSSL_KEYTYPE_DH,OPENSSL_KEYTYPE_RSA. Öntanımlı değerOPENSSL_KEYTYPE_RSAolup şimdilik desteklenen tek anahtar türüdür.encrypt_key boolean encrypt_key İhraç edilecek anahtar (bir parola ile) şifrelenececek mi? -
ek_seçenekler -
CSR için kullanılacak ek seçenekleri belirtmek için kullanılır.
dnveek_seçeneklerbirer ilişkisel dizi olup anahtarları nesne kimliklerine (OID) dönüştürülerek isteğin ilgili parçasına uygulanır.
Dönen Değerler
Üretilen CSR'yi döndürür.
Örnekler
Örnek 1 - Öz-imzalı sertifika üretimi
<?php
// Sertifikada ayırt edilebilir isim için kullanılacak veriyi oluşturalım.
// Bu anahtar değerlerinde, isminiz, şirketiniz ve hatta sertifikanın adına
// üretildiği şirketin veya şahsın isminin bulunmasını sağlayın.
// SSL sertifikaları için commonName genellikle sertifikayı kullanacak
// alan ismidir. Fakat S/MIME sertifikalar için commonName, sertifikayı
// kullanacak şahsın ismidir.
$dn = array(
"countryName" => "UK",
"stateOrProvinceName" => "Somerset",
"localityName" => "Glastonbury",
"organizationName" => "The Brain Room Limited",
"organizationalUnitName" => "PHP Documentation Team",
"commonName" => "Wez Furlong",
"emailAddress" => "wez@example.com"
);
// Yeni bir gizli/genel anahtar çifti üretelim
$privkey = openssl_pkey_new();
// Bir sertifika imzalama isteği üretelim
$csr = openssl_csr_new($dn, $privkey);
// CA'nız isteği yerine getirene kadar bir öz-imzalı sertifika üretelim.
// Bu sertifika 365 günlük olsun.
$sscert = openssl_csr_sign($csr, null, $privkey, 365);
// (Sertifikanın kullanılacağı yere bağlı olarak) posta istemcisine,
// posta sunucusuna veya HTTP sunucusuna kurulacak öz-imzali sertifikayı,
// CSR'yi ve gizli anahtarı alalım. Örnekte bunlar değişkenlere atanmıştır.
// Fakat siz bunları doğrudan dosyalarına kaydedebilirsiniz.
// CSR genellikle "gerçek" sertifikayı üretecek CA'ya gönderilir.
openssl_csr_export($csr, $csrout); var_dump($csrout);
openssl_x509_export($sscert, $certout); var_dump($certout);
openssl_pkey_export($privkey, $pkeyout, "mypassword"); var_dump($pkeyout);
// Oluşan hatalar varsa gösterelim
while (($e = openssl_error_string()) !== false) {
echo $e . "\n";
}
?>
*hint* openssl_csr_new() automatically generates private key / public key pair:
<?php
$configargs = array(
'config' => '/etc/ssl/openssl.cnf',
'digest_alg' => 'md5',
'x509_extensions' => 'v3_ca',
'req_extensions' => 'v3_req',
'private_key_bits' => 666,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
'encrypt_key' => false,
);
$dn = array(
"countryName" => "DE",
"stateOrProvinceName" => "wubbla wubbla",
"localityName" => "wubbla",
"organizationName" => "Internet Widgits Pty Ltd",
"organizationalUnitName" => "Organizational Unit Name",
"commonName" => "example.com",
"emailAddress" => "Email Address"
);
$privkey = null; // <--- !!
$csr = openssl_csr_new($dn, $privkey, $configargs);
var_dump(openssl_pkey_get_details($privkey));
/*
array(3) {
["bits"]=>
int(666)
["key"]=>
string(207) "-----BEGIN PUBLIC KEY-----
MG8wDQYJKoZIhvcN (...) gMBAAE=
-----END PUBLIC KEY-----
"
["type"]=>
int(0)
}
*/
?>
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.
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.
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.
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
);
?>
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, [...])
