dongnachuang6635 2012-12-16 16:38
浏览 55
已采纳

PHP使用现有私钥创建CSR

<?php 
$c = $_POST['c'];
$city = $_POST['city'];
$cn = $_POST['cn'];
$mail = $_POST['email'];
$p = $_POST['pass'];
echo "$c<BR>$city<br>$cn<br>$mail";
$key = $_POST['key'];
$dn = array(
"countryName" => "$c",
"stateOrProvinceName" => ".",
"localityName" => "$city",
"organizationName" => ".",
"organizationalUnitName" => ".",
"commonName" => "$cn",
"emailAddress" => "$key"
//"passphase" => "$p"
);
// Generate a certificate signing request
$csr = openssl_csr_new($dn, $key);
var_dump($csr);
while ($msg = openssl_error_string())
echo $msg . "<br />
";
?>

It is showing me an error:

Warning: openssl_csr_new(): dn: add_entry_by_NID 48 -&gt; -----BEGIN RSA PRIVATE KEY----- M[...]= -----END RSA PRIVATE KEY----- (failed; check error queue and value of string_mask OpenSSL option if illegal characters are reported) in D:\wamp\www\csr.php on line 21

and

boolean false
error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0E06D06C:configuration file routines:NCONF_get_string:no value
error:0D07A097:asn1 encoding routines:ASN1_mbstring_ncopy:string too long

This is what I found on php.net

mixed openssl_csr_new ( array $dn , resource &$privkey [, array $configargs [, array $extraattribs ]] )  

$pivkey should be a resource type, what if I already have got a private key as string?

  • 写回答

1条回答 默认 最新

  • dph58509 2012-12-17 02:36
    关注

    Personally, I'd use phpseclib, a pure PHP CSR implementation. eg.

    <?php
    include('File/X509.php');
    include('Crypt/RSA.php');
    
    $key = new Crypt_RSA();
    $key->loadKey($_POST['key']);
    
    $x509 = new File_X509();
    $x509->setPrivateKey($key);
    $x509->setDN(array(
        'countryName' => $_POST['c'],
        'stateOrProvinceName' => '.',
        'localityName' => $_POST['city'],
        'organizationName' => '.',
        'organizationalUnitName' => '.',
        'commonName' => $_POST['cn'],
        'emailAddress' => $_POST['email']
    ));
    
    $csr = $x509->signCSR();
    
    echo $x509->saveCSR($csr);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改