Openssl_verify usage !? Openssl_verify
What is the use of openssl_verify?
I want to know the values passed by the three parameters!
Reply to discussion (solution)
$ Pub_key_id is a public key! What do the other two parameters $ data and $ signature mean?
$ Signature: data generated by public key encryption, $ original data
$ Fp = fopen ("pem public key", "r ");
$ Cert = fread ($ fp, 8192 );
Fclose ($ fp );
$ Pub_key_id = openssl_get_publickey ($ cert );
$ Signature: data generated by public key encryption, $ original data
Do you mean $ signature is generated by $ pub_key_id encryption? How to encrypt it?
This generally does not matter.
Solved!
I found a class online. yes.
/**
* Is one pem encoded certificate the signer of another?
*
* The PHP openssl functionality is severely limited by the lack of a stable
* Api and documentation that might as well have been encrypted itself.
* In particle the specified ention on openssl_verify () never explains where
* To get the actual signature to verify. The isCertSigner () function below
* Will accept two PEM encoded certs as arguments and will return true if
* One certificate was used to sign the other. It only relies on
* Openssl_pkey_get_public () and openssl_public_decrypt () openssl functions,
* Which shocould stay fairly stable. The ASN parsing code snippets were mostly
* Borrowed from the horde project's smime. php.
*
* @ Author Mike Green
* @ Copyright Copyright (c) 2010, Mike Green
* @ License http://opensource.org/licenses/gpl-2.0.php GPLv2
*/
/**
* If viewSource is in the request string, show the source, luke.
If (isset ($ _ REQUEST ['viewsource']) {
Die (highlight_file (_ FILE __));
}
*/
Class OpensslVerifyAPI {
/**
* Extract signature from der encoded cert.
* Expects x509 der encoded certificate consisting of a section container
* Containing 2 sections and a bitstream. The bitstream contains
* Original encrypted signature, encrypted by the public key of the issuing
* Signer.
* @ Param string $ der
* @ Return string on success
* @ Return bool false on failures
*/
Function extractSignature ($ der = false ){
If (strlen ($ der) <5) {return false ;}
// Skip container sequence
$ Der = substr ($ der, 4 );
// Now burn through two sequences and the return the final bitstream
While (strlen ($ der)> 1 ){
$ Class = ord ($ der [0]);
$ ClassHex = dechex ($ class );
Switch ($ class ){
// BITSTREAM
Case 0x03:
$ Len = ord ($ der [1]);
$ Bytes = 0;
If ($ len & 0x80 ){
$ Bytes = $ len & 0x0f;
$ Len = 0;
For ($ I = 0; $ I <$ bytes; $ I ++ ){
$ Len = ($ len <8) | ord ($ der [$ I + 2]);
}
}
Return substr ($ der, 3 + $ bytes, $ len );
Break;
// SEQUENCE
Case 0x30:
$ Len = ord ($ der [1]);
$ Bytes = 0;
If ($ len & 0x80 ){
$ Bytes = $ len & 0x0f;
$ Len = 0;
For ($ I = 0; $ I <$ bytes; $ I ++ ){
$ Len = ($ len <8) | ord ($ der [$ I + 2]);
}
}
$ Contents = substr ($ der, 2 + $ bytes, $ len );
$ Der = substr ($ der, 2 + $ bytes + $ len );
Break;
Default:
Return false;
Break;
}
}
Return false;
}
/**
* Get signature algorithm oid from der encoded signature data.
* Expects decrypted signature data from a certificate in der format.
* This ASN1 data shoshould contain the following structure:
* SEQUENCE
* SEQUENCE
* OID (signature algorithm)
* NULL
* Octet string (signature hash)
* @ Return bool false on failures
* @ Return string oid
*/
Function getSignatureAlgorithmOid ($ der = null ){
// Validate this is the der we need...
If (! Is_string ($ der) or strlen ($ der) <5) {return false ;}
$ Bit_seq1 = 0;
$ Bit_seq2 = 2;
$ Bit_oid = 4;
If (ord ($ der [$ bit_seq1])! = 0x30 ){
Die ('invalid DER passed to getSignatureAlgorithmOid ()');
}
If (ord ($ der [$ bit_seq2])! = 0x30 ){
Die ('invalid DER passed to getSignatureAlgorithmOid ()');
}
If (ord ($ der [$ bit_oid])! = 0x06 ){
Die ('invalidder passed to getsignaturealgorithmoid ');
}
// Strip out what we don't need and get the oid
$ Der = substr ($ der, $ bit_oid );
// Get the oid
$ Len = ord ($ der [1]);
$ Bytes = 0;
If ($ len & 0x80 ){
$ Bytes = $ len & 0x0f;
$ Len = 0;
For ($ I = 0; $ I <$ bytes; $ I ++ ){
$ Len = ($ len <8) | ord ($ der [$ I + 2]);
}
}
$ Oid_data = substr ($ der, 2 + $ bytes, $ len );
// Unpack the OID
$ Oid = floor (ord ($ oid_data [0])/40 );
$ Oid. = '.'. ord ($ oid_data [0]) % 40;
$ Value = 0;
$ I = 1;
While ($ I <strlen ($ oid_data )){
$ Value = $ value <7;
$ Value = $ value | (ord ($ oid_data [$ I]) & 0x7f );
If (! (Ord ($ oid_data [$ I]) & 0x80 )){
$ Oid. = '.'. $ value;
$ Value = 0;
}
$ I ++;
}
Return $ oid;
}
/**
* Get signature hash from der encoded signature data.
* Expects decrypted signature data from a certificate in der format.
* This ASN1 data shoshould contain the following structure:
* SEQUENCE
* SEQUENCE
* OID (signature algorithm)
* NULL
* Octet string (signature hash)
* @ Return bool false on failures
* @ Return string hash
*/
Function getSignatureHash ($ der = null ){
// Validate this is the der we need...
If (! Is_string ($ der) or strlen ($ der) <5) {return false ;}
If (ord ($ der [0])! = 0x30 ){
Die ('invalid DER passed to getSignatureHash ()');
}
// Strip out the container sequence
$ Der = substr ($ der, 2 );
If (ord ($ der [0])! = 0x30 ){
Die ('invalid DER passed to getSignatureHash ()');
}
// Get the length of the first sequence so we can strip it out.
$ Len = ord ($ der [1]);
$ Bytes = 0;
If ($ len & 0x80 ){
$ Bytes = $ len & 0x0f;
$ Len = 0;
For ($ I = 0; $ I <$ bytes; $ I ++ ){
$ Len = ($ len <8) | ord ($ der [$ I + 2]);
}
}
$ Der = substr ($ der, 2 + $ bytes + $ len );
// Now we shoshould have an octet string
If (ord ($ der [0])! = 0x04 ){
Die ('invalid DER passed to getSignatureHash ()');
}
$ Len = ord ($ der [1]);
$ Bytes = 0;
If ($ len & 0x80 ){
$ Bytes = $ len & 0x0f;
$ Len = 0;
For ($ I = 0; $ I <$ bytes; $ I ++ ){
$ Len = ($ len <8) | ord ($ der [$ I + 2]);
}
}
Return bin2hex (substr ($ der, 2 + $ bytes, $ len ));
}
/**
* Determine if one cert was used to sign another
* Note that more than one CA cert can give a positive result, some certs
* Re-issue signing certs after having only changed the expiration dates.
* @ Param string $ cert-PEM encoded cert
* @ Param string $ caCert-PEM encoded cert that possibly signed $ cert
* @ Return bool
*/
Function isCertSigner ($ certPem = null, $ caCertPem = null ){
If (! Function_exists ('openssl _ pkey_get_public ')){
Die ('need the openssl_pkey_get_public () function .');
}
If (! Function_exists ('openssl _ public_decrypt ')){
Die ('need the openssl_public_decrypt () function .');
}
If (! Function_exists ('hash ')){
Die ('need the php hash () function .');
}
If (empty ($ certPem) or empty ($ caCertPem) {return false ;}
// Convert the cert to der for feeding to extractSignature.
$ CertDer = $ this-> pemToDer ($ certPem );
If (! Is_string ($ certDer) {die ('invalid certpem ');}
// Grab the encrypted signature from the der encoded cert.
$ EncryptedSig = $ this-> extractSignature ($ certDer );
If (! Is_string ($ encryptedSig )){
Die ('failed' to extract encrypted signature from certPem .');
}
// Extract the public key from the ca cert, which is what has
// Been used to encrypt the signature in the cert.
$ PubKey = openssl_pkey_get_public ($ caCertPem );
If ($ pubKey = false ){
Die ('failed' to extract the public key from the ca cert .');
}
// Attempt to decrypt the encrypted signature using the CA's public
// Key, returning the decrypted signature in $ decryptedSig. If
// It can't be decrypted, this ca was not used to sign it for sure...
$ Rc = openssl_public_decrypt ($ encryptedSig, $ decryptedSig, $ pubKey );
If ($ rc = false) {return false ;}
// We now have the decrypted signature, which is der encoded
// Asn1 data containing the signature algorithm and signature hash.
// Now we need what was originally hashed by the issuer, which is
// The original DER encoded certificate without the issuer and
// Signature information.
$ OrigCert = $ this-> stripSignerAsn ($ certDer );
If ($ origCert = false ){
Die ('failed' to extract unsigned cert .');
}
// Get the oid of the signature hash algorithm, which is required
// To generate our own hash of the original cert. This hash is
// What will be compared to the issuers hash.
$ Oid = $ this-> getSignatureAlgorithmOid ($ decryptedSig );
If ($ oid === false ){
Die ('failed' to determine the signature algorithm .');
}
Switch ($ oid ){
Case '1. 2.840.113549.2.2 ': $ algo = 'md2'; break;
Case '1. 2.840.113549.2.4 ': $ algo = 'md4'; break;
Case '1. 2.840.113549.2.5 ': $ algo = 'md5'; break;
Case '1. 3.14.3.2.18 ': $ algo = 'Sha'; break;
Case '1. 3.14.3.2.26 ': $ algo = 'sha1'; break;
Case '2. 16.840.1.101.3.4.2.1 ': $ algo = 'sha256'; break;
Case '2. 16.840.1.101.3.4.2.2 ': $ algo = 'sha384'; break;
Case '2. 16.840.1.101.3.4.2.3 ': $ algo = 'sha512'; break;
Default:
Die ('unknown signature hash algorithm oid: '. $ oid );
Break;
}
// Get the issuer generated hash from the decrypted signature.
$ DecryptedHash = $ this-> getSignatureHash ($ decryptedSig );
// OK, hash the original unsigned cert with the same algorithm
// And if it matches $ decryptedHash we have a winner.
$ CertHash = hash ($ algo, $ origCert );
Return ($ decryptedHash ===$ certHash );
}
/**
* Convert pem encoded certificate to DER encoding
* @ Return string $ derEncoded on success
* @ Return bool false on failures
*/
Function pemToDer ($ pem = null ){
If (! Is_string ($ pem) {return false ;}
$ Cert_split = preg_split ('/(----- (BEGIN) | (END) CERTIFICATE -----)/', $ pem );
If (! Isset ($ cert_split [1]) {return false ;}
Return base64_decode ($ cert_split [1]);
}
/**
* Obtain der cert with issuer and signature sections stripped.
* @ Param string $ der-der encoded certificate
* @ Return string $ der on success
* @ Return bool false on failures.
*/
Function stripSignerAsn ($ der = null ){
If (! Is_string ($ der) or strlen ($ der) <8) {return false ;}
$ Bit = 4;
$ Len = ord ($ der [($ bit + 1)]);
$ Bytes = 0;
If ($ len & 0x80 ){
$ Bytes = $ len & 0x0f;
$ Len = 0;
For ($ I = 0; $ I <$ bytes; $ I ++ ){
$ Len = ($ len <8) | ord ($ der [$ bit + $ I + 2]);
}
}
Return substr ($ der, 4, $ len + 4 );
}
}
/**
* HTML form starts here...
*/
$ This-> answer = 'Enter PEM Encoded Certificates for the Issuer and subobject'
. 'And click Submit. Include the entire certificates, including'
. 'The begin certificate and end certificate lines .';
/*
If (isset ($ _ POST ['subjectpem']) and isset ($ _ POST ['issuerpem']) {
If (strlen ($ _ POST ['subjectpem'])> 0 and strlen ($ _ POST ['issuerpem'])> 0 ){
$ Rc = isCertSigner ($ _ POST ['subjectpem'], $ _ POST ['issuerpem']);
If ($ rc = true ){
$ Answer = 'the issuer cert DID sign The subject cert .';
} Else {
$ Answer = 'the issuer cert did not sign The subject cert .';
}
}
}
*/
?>