0

Я пытаюсь создать соединение на стороне сервера для уведомлений push push. Во-первых, я прошу пользователя (который, вероятно, будет ios dev) предоставить файлы .cer и .p12, которые Apple предоставляет, чтобы сделать его файлом .pem.Сертификат APNS .pem

Ниже приведено описание сертификата .pem.

$dir = $this->directory.'/certificates'; 
$password = 'a_user_password'; 
$certificate = $dir.'/certificate.cer'; 
$key_password = $dir.'/key.p12'; 

exec('openssl x509 -inform der -in '.$certificate.' -out '.$dir.'/certificate.pem'); 
exec('openssl pkcs12 -nocerts -out '.$dir.'/key.pem -in '.$key_password.' -passout pass:'.$password.' -passin pass:'.$password); 

$filename = $key_password; 
$results = array(); 
$worked = openssl_pkcs12_read(file_get_contents($filename), $results, $obj->password); 
if($worked) { 
    $current = file_get_contents($dir.'/key.pem'); 
    $current .= $results['pkey']; 
    file_put_contents($dir.'/key.pem', $current); 
} else { 
    echo openssl_error_string(); 
} 
exec('cat '.$dir.'/certificate.pem '.$dir.'/key.pem > '.$dir.'/apns_certificate.pem'); 

Пока что так хорошо. Я проверил, что выше генерироваться apns_certificate.pem успешно с яблоком через командную строку с помощью:

s_client -connect gateway.sandbox.push.apple.com:2195 -cert certificate.pem -key key.pem 

Однако Когда я пытаюсь соединиться с APNS через PHP не могу. Следует последний PHP код, который я попробовал, и я видел, что для других работал:

$this->certificate = ROOT.'/certificates/apns_certificate.pem'; 
$this->socket = 'ssl://gateway.push.apple.com:2195'; 
if (!file_exists($this->certificate)) { 
     $this->error = 'Certificate file not found'; 
     return false; 
    } 

    $this->stream_context = stream_context_create(); 
    $this->stream_options = array(
     'ssl' => array(
      'local_cert' => $this->certificate, 
      'passphrase' => 'a_user_password', //same with the one used in my previous code 
     ) 
    ); 
    $success = stream_context_set_option($this->stream_context, $this->stream_options); 
    if ($success == false) { 
     $this->error = 'Secure connection failed'; 
     return false; 
    } 

    $this->socket_client = stream_socket_client($this->socket, $con_error, $con_error_string, $this->timeout, STREAM_CLIENT_CONNECT, $this->stream_context); 

    if ($this->socket_client === false) { 
     $this->error = $con_error_string; 
     return false; 
    } else { 
     return true; 
    } 

Приведенный выше код возвращает мне ошибку: Предупреждение: stream_socket_client(): операция SSL не удалось с кодом 1. OpenSSL Ошибка сообщения: ошибка: 14094416: SSL процедуры: SSL3_READ_BYTES: SSLv3 сертификат предупреждение неизвестно Предупреждение: stream_socket_client(): не удалось подключиться к SSL: //gateway.push.apple.com: 2195

заранее спасибо за помощь !

ответ

0

Приведенный выше код верен. Произошла ошибка с сертификацией .p12. Также я изменил файл exec на файл конверсии .p12 на:

exec('openssl pkcs12 -out '.$dir.'/key.pem -in '.$key_password.' -passout pass:'.$password.' -passin pass:'.$password.' -nodes');