2012-11-28 4 views
2

PHP를 사용하여 개인/공개 키 쌍을 생성하려고합니다.openssl 함수를 사용할 때 Apache 서버 (xampp)가 충돌합니다.

서버 : 아파치/2.4.3 (Win32에서)에는 OpenSSL/1.0.1c의 PHP/5.4.7

OS가 설치된 모든 Windows 업데이트로 윈도우 XP SP3입니다.

나는 다음과 같은 스크립트를 실행하려고 해요 :이 스크립트 아파치 충돌 및 다시 시작을 실행하려고하면

####################################################################### 
# File name: openssl.cnf 
# Created By: The Uniform Server Development Team 
######################################################################## 

# 
# OpenSSL configuration file. 
# 

# Establish working directory. 
dir   = . 

[ req ] 
default_bits   = 1024 
default_md    = sha1 
default_keyfile   = privkey.pem 
distinguished_name  = req_distinguished_name 
x509_extensions   = v3_ca 
string_mask    = nombstr 

[ req_distinguished_name ] 
countryName    = Country Name (2 letter code) 
countryName_min   = 2 
countryName_max   = 2 
stateOrProvinceName  = State or Province Name (full name) 
localityName   = Locality Name (eg, city) 
0.organizationName  = Organization Name (eg, company) 
organizationalUnitName = Organizational Unit Name (eg, section) 
commonName    = Common Name (eg, YOUR fqdn) 
commonName_max   = 64 
emailAddress   = Email Address 
emailAddress_max  = 64 

[ ssl_server ] 
basicConstraints  = CA:FALSE 
nsCertType    = server 
keyUsage    = digitalSignature, keyEncipherment 
extendedKeyUsage  = serverAuth, nsSGC, msSGC 
nsComment    = "OpenSSL Certificate for SSL Web Server" 

[ v3_req ] 
basicConstraints = CA:FALSE 
keyUsage   = nonRepudiation, digitalSignature, keyEncipherment 

[ v3_ca ] 
basicConstraints  = critical, CA:true, pathlen:0 
nsCertType    = sslCA 
keyUsage    = cRLSign, keyCertSign 
extendedKeyUsage  = serverAuth, clientAuth 
nsComment    = "OpenSSL CA Certificate" 

:

<?php 

$ssl_path = getcwd(); 
$ssl_path = preg_replace('/\\\/','/', $ssl_path); // Replace \ with/

$config = array(
    'config'   => "$ssl_path/openssl.cnf", 
    'private_key_bits' => 1024, 
    'private_key_type' => OPENSSL_KEYTYPE_RSA 
); 

$dn = array(
    "countryName"   => "AT", 
    "stateOrProvinceName" => "Vienna", 
    "localityName"   => "Cambs", 
    "organizationName"  => "UniServer", 
    "organizationalUnitName" => "Demo", 
    "commonName"    => "localhost", 
    "emailAddress"   => "[email protected]" 
); 

$privateKey = openssl_pkey_new($config); 
$csr = openssl_csr_new($dn, $privateKey, $config); 
$sscert = openssl_csr_sign($csr, NULL, $privateKey, 365, $config); 
openssl_pkey_export_to_file($privateKey, "C:/server.key", NULL, $config); 
openssl_x509_export_to_file($sscert, "C:/server.crt", FALSE); 
openssl_csr_export_to_file($csr, "C:/server.csr"); 
$keyDetails = openssl_pkey_get_details($privateKey); 
file_put_contents('C:/public.key', $keyDetails['key']); 

?> 

이 내 openssl.cnf입니다. 이 문제의 원인은 무엇입니까?

BTW : phpseclib0.3.1 lib를 사용하려고하면 같은 오류가 발생합니다.

미리 감사드립니다.

+0

예 :; apache를 충돌시키는 함수는 openssl_pkey_get_details()입니다. – Omegavirus

답변

2

내 경험 OpenSSL_pkey_get_details()는 개인 키가 아닌 공개 키를 얻기 위해 X.509 인증서가 필요합니다 (설명서의 내용에도 불구하고).

phpseclib, a pure PHP X.509 implementation을 사용하면이 모든 작업을 실제로 더 쉽게 수행 할 수 있습니다. 내가 추가하는 것을 잊었다

http://phpseclib.sourceforge.net/x509/examples.html#selfsigned

<?php 
include('File/X509.php'); 
include('Crypt/RSA.php'); 

// create private key/x.509 cert for stunnel/website 
$privKey = new Crypt_RSA(); 
extract($privKey->createKey()); 
$privKey->loadKey($privatekey); 

$pubKey = new Crypt_RSA(); 
$pubKey->loadKey($publickey); 
$pubKey->setPublicKey(); 

$subject = new File_X509(); 
$subject->setDN(array(
    "countryName"   => "AT", 
    "stateOrProvinceName" => "Vienna", 
    "localityName"   => "Cambs", 
    "organizationName"  => "UniServer", 
    "organizationalUnitName" => "Demo", 
    "commonName"    => "localhost", 
    "emailAddress"   => "[email protected]" 
)); 
$subject->setPublicKey($pubKey); 

$issuer = new File_X509(); 
$issuer->setPrivateKey($privKey); 
$issuer->setDN($subject->getDN()); 

$x509 = new File_X509(); 

$result = $x509->sign($issuer, $subject); 

$csr = $issuer->signCSR(); 
$csr = $x509->saveCSR($csr); 

file_put_contents("C:/server.key", $privKey->getPrivateKey()); 
file_put_contents("C:/server.crt", $x509->saveX509($result)); 
file_put_contents('C:/public.key', $privKey->getPublicKey()); 
file_put_contents("C:/server.csr", $csr); 
?> 
관련 문제