Hi All,

I'm trying to convert a .p12 file to a .pem using php (because, as I understand it, soap needs to take a .pem format certificate)

Am I going about it the right way?

    $certificate = array();
    $pkcs12 = file_get_contents('certificate.p12');

    if (openssl_pkcs12_read($pkcs12, $certificate, 'pass')) {

        if (isset($certificate['pkey'])) {
            $pem = null;
            openssl_pkey_export($certificate['pkey'], $pem, 'pass');
        }

        if (isset($certificate['cert'])) {
            $cert = null;
            openssl_x509_export($certificate['cert'], $cert);   
        }

        if (isset($certificate['extracerts'][0])) {
            $extracert1 = null;
            openssl_x509_export($certificate['extracerts'][0], $extracert1);    
        }

        if (isset($certificate['extracerts'][1])) {
            $extracert2 = null;
            openssl_x509_export($certificate['extracerts'][1], $extracert2);    
        }

        $pem_file_contents = $cert . $pem . $extracert1 . $extracert2;

        file_put_contents("certificate.pem", $pem_file_contents);

        $options = array(
            'local_cert'=>'/absolute/path/to/file/certificate.pem',
            'passphrase'=>'pass',
            'location'=>"...",
            'uri'=>'...',
        );
Member Avatar for diafol

Am I going about it the right way?

Does it work?

Well, it generates the file without errors. Not sure if the file content is valid for its purposes though as I'm still having issues connecting to the host via soap.

Member Avatar for diafol

OK. Sorry can't help with SOAP.

:) no worries. I always appreciate the interest, regardless.

Thanks for posting this. Really this helped me lot and i resolved my issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.