/** * @return true if the signature is valid, false otherwise */ public static function verif(TOGoS_RSAUtil_Signature $sig, $blobSource) { $pubKeyData = $blobSource->getBlob($sig->getPublicKeyUri()); $pubKeyPem = TOGoS_RSAUtil_Util::looksLikePem($pubKeyData) ? $pubKeyData : TOGoS_RSAUtil_Util::derToPem($pubKeyData); $pubKey = openssl_pkey_get_public($pubKeyPem); if ($pubKey === false) { throw new Exception("Failed to parse public key data"); } $data = TOGoS_RSAUtil_Util::getSignaturePayload($sig, $blobSource); $verified = openssl_verify($data, $sig->getSignatureBytes(), $pubKey, TOGoS_RSAUtil_Util::rsaAlgoIdFromName($sig->getAlgorithmName())); openssl_free_key($pubKey); return (bool) $verified; }
public static function generate($options = array(), $dataStore = null) { $bits = isset($options['size']) ? $options['size'] : 4096; $key = openssl_pkey_new(array('digest_alg' => 'sha1', 'private_key_bits' => $bits, 'private_key_type' => OPENSSL_KEYTYPE_RSA)); if (!openssl_pkey_export($key, $privateKeyPem)) { throw new Exception("openssl_pkey_export failed with no explanation"); } $privateKeyDer = TOGoS_RSAUtil_Util::pemToDer($privateKeyPem); $det = openssl_pkey_get_details($key); /** PEM-formatted public key */ $publicKeyPem = $det['key']; $publicKeyDer = TOGoS_RSAUtil_Util::pemToDer($publicKeyPem); if ($dataStore !== null) { $dataStore->store($publicKeyDer); } return self::create2($privateKeyDer, $publicKeyDer); }