2
$\begingroup$

Given a certificate .p12 I want to extract the public key, the private key and Modulus and Exponent both from the public key and from the private key.

I am using PHP and OpenSSL functions

As I understand it, the Module and Exponent of the public key must be different from the Module and Exponent of the private key.

            // Obtenemos el certificado .p12
            if (!$pfx = file_get_contents(UtilController::getPathDelCertificado())){
                UtilController::registrarLog('ERR - No se puede leer el fichero del certificado o no existe en la ruta especificada: '.UtilController::getPathDelCertificado().' - [Firmador->firmar()]');
                exit;
            }
            
            // Lee el archivo PKCS12.
            //      key:        En esta variable se guardará la clave privada extraida del certificado
            //      password:   Es la constraseña que tiene el certificado
            if (!openssl_pkcs12_read($pfx, $key, UtilController::PASSWORD_CERTIFICADO)){
                UtilController::registrarLog('ERR - No se puede leer el almacén de certificados o la clave no es la correcta - [Firmador->firmar()]');
                exit;
            }

            $publicKey = openssl_x509_read($key["cert"]);
            $aInfoPublicKey = openssl_pkey_get_details(openssl_pkey_get_public($publicKey));

            // Obtenemos informacion detallada sobre la clave privada. Es necesario pasarle como parametro un resource a openssl_pkey_get_details
            $aInfoPrivateKey = openssl_pkey_get_details(openssl_pkey_get_private($key["pkey"]));

            $this->certificate  = $key["cert"]; 
            $this->publicKey    = $aInfoPublicKey["key"];       
            $this->moduloDeLaPublicKey = base64_encode($aInfoPublicKey['rsa']['n']);
            $this->exponenteDeLaPublicKey = base64_encode($aInfoPublicKey['rsa']['e']); 
            $this->privateKey   = $key["pkey"];         // Clave privada del certificado en formato .pem
            $this->moduloDeLaPrivateKey = base64_encode($aInfoPrivateKey['rsa']['n']);
            $this->exponenteDeLaPrivateKey = base64_encode($aInfoPrivateKey['rsa']['e']);

However, when I print the variables: $this->moduloDeLaPrivateKey and $this->moduloDeLaPublicKey and $this->exponentDeLaPublicKey and $this->exponentDeLaPrivateKey.

Why is this happening?

$\endgroup$

2 Answers 2

4
$\begingroup$

the Module and Exponent of the public key must be different from the Module and Exponent of the private key.

Most commonly, public key is $(n,e)$, and private key is either $(n,d)$ or $(n,e,d,p,q,d_p,d_q,q_\text{inv})$. See PKCS#1v2.2 for details in the formatting, and rarely used forms for multiprime RSA.

The public and private keys of the same key pair must share the same public modulus $n$.

And if a private key includes the public exponent $e$ (not all private keys do), then the public and private keys must share the same public exponent $e$. This is what happens in the question.

The public exponent $e$ and the private exponent $d$ can be identical too, but only for pathological and insecure key pairs.

$\endgroup$
1
  • 3
    $\begingroup$ e=d for when you need military-grade ROT13 encryption $\endgroup$
    – cobbal
    Jun 30, 2023 at 18:24
1
$\begingroup$

As I understand it, the Module and Exponent of the public key must be different from the Module and Exponent of the private key.

I don't think your understanding is correct. The modulus is (and must be) the same, in both certificat files. (the modulus is part of the public key). The exponent is the other part of the public key, and you expect to have the same exponent (if included) in both the public and the private components.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.