在openssl中加载之前正确格式化字符串

时间:2016-06-16 04:20:46

标签: php

我有一个像这样动态生成的证书字符串。如果仔细观察,每隔64个字符就会有一个换行符\ r \ n(我知道这是真的,因为我用来创建它们的函数包括它)。仅对于第一行,在5个破折号后发生中断。

示例1 - 失败

$string = '-----BEGIN CERTIFICATE----- MIIFFDCCA/ygAwIBAgIQWh1+7npzhkMdCWe83N5qATANBgkqhkiG9w0BAQsFADB+ MQswCQYDVQQGEwJVUzEdMBsGA1UEChMUU3ltYW50ZWMgQ29ycG9yYXRpb24xHzAd BgNVBAsTFlN5bWFudGVjIFRydXN0IE5ldHdvcmsxLzAtBgNVBAMTJlN5bWFudGVj IENsYXNzIDMgU2VjdXJlIFNlcnZlciBDQSAtIEc0MB4XDTE2MDYwOTAwMDAwMFoX DTE3MDYxMDIzNTk1OVowcjELMAkGA1UEBhMCVVMxETAPBgNVBAgMCE5ldyBZb3Jr MREwDwYDVQQHDAhOZXcgWW9yazEXMBUGA1UECgwOSlBNb3JnYW4gQ2hhc2UxCzAJ BgNVBAsMAklCMRcwFQYDVQQDDA4/LmpwbWNoYXNlLm5ldDCCASIwDQYJKoZIhvcN AQEBBQADggEPADCCAQoCggEBAKqMdBSgHBZmSlQ1XPhjYr3FHsJFbX1XNt5bAbko F74M3SBxwUfQEi+N6JNFGoMpkRZrLd8DgW8iqmAkt4ZSz4hAiJzDFtV+3UzPnI2O LkArSsg2Anyx1H8NAHwq3OH62Q5Tz7Lu2o09l+s+JFL05PUjlKZioYLNQTDkM7xY xrkFeY/FdqTEYs8a5SUm8cijfwnrZN9tBZ3F1rmgF5UfhRBGw0bOvUsVsixhrXYx Oigy+zLCq0feTnm3YjB5tkHIsropJD7wxEHBQ6RWFNRSqN40+YJ7A1cJiLMDcWBO S2GPwt5TA3vKThGMlXpwQx6s0S7jrEz3mFp8DB6S3hkXJTsCAwEAAaOCAZgwggGU MDkGA1UdEQQyMDCCDj8uanBtY2hhc2UubmV0gg4/LmpwbWNoYXNlLm5ldIIOPy5q cG1jaGFzZS5uZXQwCQYDVR0TBAIwADAOBgNVHQ8BAf8EBAMCBaAwHQYDVR0lBBYw FAYIKwYBBQUHAwEGCCsGAQUFBwMCMGEGA1UdIARaMFgwVgYGZ4EMAQICMEwwIwYI KwYBBQUHAgEWF2h0dHBzOi8vZC5zeW1jYi5jb20vY3BzMCUGCCsGAQUFBwICMBkM F2h0dHBzOi8vZC5zeW1jYi5jb20vcnBhMB8GA1UdIwQYMBaAFF9gz2GQVd+EQxSK YCqy9Xr0QxjvMCsGA1UdHwQkMCIwIKAeoByGGmh0dHA6Ly9zcy5zeW1jYi5jb20v c3MuY3JsMFcGCCsGAQUFBwEBBEswSTAfBggrBgEFBQcwAYYTaHR0cDovL3NzLnN5 bWNkLmNvbTAmBggrBgEFBQcwAoYaaHR0cDovL3NzLnN5bWNiLmNvbS9zcy5jcnQw EwYKKwYBBAHWeQIEAwEB/wQCBQAwDQYJKoZIhvcNAQELBQADggEBAKfIv8hA4PE/ q8594w0pNOzb1VP05Ial/z9Nxa2q0rUuOd1mb0T0FzeW+adUyIDHFVITrmiQvEn3 pXDNqw3iM0lCj14paCHlk+cA8X/cEdq/eMmruI15ee1kuDOtPEotnJ2WmUo9VWQz fukRPA6TxP3k3n9cwn8cWUCoW0SymkDcH+SjTbLBbIxDkOGF0lGRMUWtevXYiHVo 04edymdsseqzmHzs9nRlEfb+EZ060eYdfEG79cK/3mCIVkPlavw7+ykp+j5dQ7LU BZf8TLjdUUHSUmUBlb32WhZShSEvHPcnvmZEPkXH0VBIbtkCQZ1nlIpnY4v0Y3Lx 8Uxg/R7Af7Q= -----END CERTIFICATE-----';

出于某种原因,当我尝试将此证书加载到openssl时,它会抱怨以下警告:

$pem='';
openssl_x509_export($string, $pem);
  

警告:openssl_x509_export():无法从参数1获取证书

示例2 - 工作

但是,如果我从文件或下面的字符串加载相同的证书,它可以正常工作。

string ='-----BEGIN CERTIFICATE-----
MIIFFDCCA/ygAwIBAgIQWh1+7npzhkMdCWe83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AQUFBwICMBkM
F2h0dHBzOi8vZC5zeW1jYi5jb20vcnBhMB8GA1UdIwQYMBaAFF9gz2GQVd+EQxSK
YCqy9Xr0QxjvMCsGA1UdHwQkMCIwIKAeoByGGmh0dHA6Ly9zcy5zeW1jYi5jb20v
c3MuY3JsMFcGCCsGAQUFBwEBBEswSTAfBggrBgEFBQcwAYYTaHR0cDovL3NzLnN5
bWNkLmNvbTAmBggrBgEFBQcwAoYaaHR0cDovL3NzLnN5bWNiLmNvbS9zcy5jcnQw
EwYKKwYBBAHWeQIEAwEB/wQCBQAwDQYJKoZIhvcNAQELBQADggEBAKfIv8hA4PE/
q8594w0pNOzb1VP05Ial/z9Nxa2q0rUuOd1mb0T0FzeW+adUyIDHFVITrmiQvEn3
pXDNqw3iM0lCj14paCHlk+cA8X/cEdq/eMmruI15ee1kuDOtPEotnJ2WmUo9VWQz
fukRPA6TxP3k3n9cwn8cWUCoW0SymkDcH+SjTbLBbIxDkOGF0lGRMUWtevXYiHVo
04edymdsseqzmHzs9nRlEfb+EZ060eYdfEG79cK/3mCIVkPlavw7+ykp+j5dQ7LU
BZf8TLjdUUHSUmUBlb32WhZShSEvHPcnvmZEPkXH0VBIbtkCQZ1nlIpnY4v0Y3Lx
8Uxg/R7Af7Q=
-----END CERTIFICATE-----';

有没有办法让我可以使用第一个字符串工作,或者以一种方式重新编写它以使openssl读取它?

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决问题的方法

$header = array("-----BEGIN CERTIFICATE----- "," -----END CERTIFICATE-----"); //headers to strip
$string = str_replace($header, '', $certdata); //string with stripped headers
$string = "-----BEGIN CERTIFICATE-----\r\n".str_replace(' ',"\r\n",$string)."\r\n-----END CERTIFICATE-----"; //rebuld string with headers and new lines

这样可行,但如果有更优雅的解决方案,我仍然会对它感兴趣。