openssl_csr_new

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

openssl_csr_new - CSR에 생성


설명

openssl_csr_new(
    array $distinguished_names,
    OpenSSLAsymmetricKey &$private_key,
    ?array $options = null,
    ?array $extra_attributes = null
): OpenSSLCertificateSigningRequest|false
                

openssl_csr_new()distinguished_names이 제공하는 정보를 기반으로 새 CSR(인증서 서명 요청)을 생성합니다.

참고: 이 함수가 올바르게 작동하려면 유효한 openssl.cnf가 설치되어 있어야 합니다. 자세한 내용은 설치 섹션 아래의 참고 사항을 참조하십시오.


매개변수

distinguished_names
인증서에 사용할 고유 이름 또는 제목 필드입니다.
private_key
private_key는 이전에 openssl_pkey_new()에 의해 생성된(또는 다른 openssl_pkey 함수 제품군에서 얻은) 개인 키로 설정해야 합니다. 키의 해당 공개 부분은 CSR에 서명하는 데 사용됩니다.
options
기본적으로 시스템 openssl.conf의 정보는 요청을 초기화하는 데 사용됩니다. optionsconfig_section_section 키를 설정하여 구성 파일 섹션을 지정할 수 있습니다. config 키 값을 사용하려는 파일의 경로로 설정하여 대체 openssl 구성 파일을 지정할 수도 있습니다. options에 있는 다음 키는 아래 표에 나열된 대로 openssl.conf에서 해당하는 키처럼 작동합니다.

구성 재정의

options key type openssl.conf equivalent description
digest_alg string default_md 다이제스트 메서드 또는 서명 해시, 일반적으로 openssl_get_md_methods() 중 하나
x509_extensions string x509_extensions x509 인증서를 생성할 때 사용해야 하는 확장을 선택합니다.
req_extensions string req_extensions CSR을 생성할 때 사용해야 하는 확장을 선택합니다.
private_key_bits int default_bits 개인 키를 생성하는 데 사용할 비트 수를 지정합니다.
private_key_type int none 생성할 개인 키의 유형을 지정합니다. OPENSSL_KEYTYPE_DSA, OPENSSL_KEYTYPE_DH, OPENSSL_KEYTYPE_RSA 또는 OPENSSL_KEYTYPE_EC 중 하나일 수 있습니다. 기본값은 OPENSSL_KEYTYPE_RSA입니다.
encrypt_key bool encrypt_key 내보낸 키(암호 포함)를 암호화해야 합니까?
encrypt_key_cipher int none cipher constants 중 하나입니다.
curve_name string none openssl_get_curve_names() 중 하나입니다.
config string N/A 자신의 대체 openssl.conf 파일에 대한 경로입니다.
extra_attributes
extra_attributes는 CSR에 대한 추가 구성 옵션을 지정하는 데 사용됩니다. distinguished_namesextra_attributes는 모두 키가 OID로 변환되고 요청의 관련 부분에 적용되는 연관 배열입니다.

반환 값

CSR을 반환하거나 실패 시 false를 반환합니다.


변경 로그

버전 설명
8.0.0 성공하면 이 함수는 이제 OpenSSLCertificateSigningRequest 인스턴스를 반환합니다. 이전에는 OpenSSL X.509 CSR 유형의 resource가 반환되었습니다.
8.0.0 private_key는 이제 OpenSSLAsymmetricKey 인스턴스를 허용합니다. 이전에는 OpenSSL key 유형의 resource가 허용되었습니다.
7.1.0 options은 이제 curve_name도 지원합니다.

Examples

예제 #1 자체 서명 인증서 만들기

                  
<?php
// for SSL server certificates the commonName is the domain name to be secured
// for S/MIME email certificates the commonName is the owner of the email address
// location and identification fields refer to the owner of domain or email subject to be secured
$dn = array(
    "countryName" => "GB",
    "stateOrProvinceName" => "Somerset",
    "localityName" => "Glastonbury",
    "organizationName" => "The Brain Room Limited",
    "organizationalUnitName" => "PHP Documentation Team",
    "commonName" => "Wez Furlong",
    "emailAddress" => "wez@example.com"
);

// Generate a new private (and public) key pair
$privkey = openssl_pkey_new(array(
    "private_key_bits" => 2048,
    "private_key_type" => OPENSSL_KEYTYPE_RSA,
));

// Generate a certificate signing request
$csr = openssl_csr_new($dn, $privkey, array('digest_alg' => 'sha256'));

// Generate a self-signed cert, valid for 365 days
$x509 = openssl_csr_sign($csr, null, $privkey, $days=365, array('digest_alg' => 'sha256'));

// Save your private key, CSR and self-signed cert for later use
openssl_csr_export($csr, $csrout) and var_dump($csrout);
openssl_x509_export($x509, $certout) and var_dump($certout);
openssl_pkey_export($privkey, $pkeyout, "mypassword") and var_dump($pkeyout);

// Show any errors that occurred here
while (($e = openssl_error_string()) !== false) {
    echo $e . "\n";
}
?>
                  
                

예제 #2 자체 서명된 ECC 인증서 생성(PHP 7.1.0부터)

                  
<?php
$subject = array(
    "commonName" => "docs.php.net",
);

// Generate a new private (and public) key pair
$private_key = openssl_pkey_new(array(
    "private_key_type" => OPENSSL_KEYTYPE_EC,
    "curve_name" => 'prime256v1',
));

// Generate a certificate signing request
$csr = openssl_csr_new($subject, $private_key, array('digest_alg' => 'sha384'));

// Generate self-signed EC cert
$x509 = openssl_csr_sign($csr, null, $private_key, $days=365, array('digest_alg' => 'sha384'));
openssl_x509_export_to_file($x509, 'ecc-cert.pem');
openssl_pkey_export_to_file($private_key, 'ecc-private.key');
?>
                  
                

기타

  • openssl_csr_sign() - 다른 인증서(또는 자체)로 CSR에 서명하고 인증서 생성