本文整理汇总了PHP中openssl_pkey_free函数的典型用法代码示例。如果您正苦于以下问题:PHP openssl_pkey_free函数的具体用法?PHP openssl_pkey_free怎么用?PHP openssl_pkey_free使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了openssl_pkey_free函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: generate
/**
* Generates a new key pair with the given length in bits.
*
* @api
* @param int $bits length of the key
* @return KeyPair generated key pair
*/
public function generate($bits = 2048)
{
if (!is_int($bits)) {
throw new \InvalidArgumentException(sprintf("\$bits must be of type int, %s given", gettype($bits)));
}
if ($bits < 2048) {
throw new \InvalidArgumentException("Keys with fewer than 2048 bits are not allowed!");
}
$configFile = $defaultConfigFile = __DIR__ . "/../res/openssl.cnf";
if (class_exists("Phar") && !empty(Phar::running(true))) {
$configContent = file_get_contents($configFile);
$configFile = tempnam(sys_get_temp_dir(), "acme_openssl_");
file_put_contents($configFile, $configContent);
register_shutdown_function(function () use($configFile) {
@unlink($configFile);
});
}
$res = openssl_pkey_new(["private_key_type" => OPENSSL_KEYTYPE_RSA, "private_key_bits" => $bits, "config" => $configFile]);
$success = openssl_pkey_export($res, $privateKey, null, ["config" => $configFile]);
if ($configFile !== $defaultConfigFile) {
@unlink($configFile);
}
if (!$success) {
openssl_pkey_free($res);
throw new \RuntimeException("Key export failed!");
}
$publicKey = openssl_pkey_get_details($res)["key"];
openssl_pkey_free($res);
// clear error buffer, because of minimalistic openssl.cnf
while (openssl_error_string() !== false) {
}
return new KeyPair($privateKey, $publicKey);
}
示例2: generateKeys
/**
* Generate public and private key
* @param array $options
*/
public function generateKeys(array $options = self::DEFAULT_PUBLIC_KEY_OPTIONS)
{
$keys = openssl_pkey_new($options);
$this->publicKey = openssl_pkey_get_details($keys)["key"];
openssl_pkey_export($keys, $this->privateKey);
openssl_pkey_free($keys);
}
示例3: free
/**
* Frees the resource associated with this private key.
* This is automatically done on destruct.
*/
private function free()
{
if ($this->keyResource) {
openssl_pkey_free($this->keyResource);
}
$this->keyResource = null;
}
示例4: gal_service_account_upgrade
function gal_service_account_upgrade(&$option, $gal_option_name, &$existing_sa_options, $gal_sa_option_name)
{
/* Convert ga_serviceemail ga_keyfilepath
* into new separate sa options:
* ga_sakey, ga_serviceemail, ga_pkey_print
*/
if (count($existing_sa_options)) {
return;
}
$existing_sa_options = array('ga_serviceemail' => isset($option['ga_serviceemail']) ? $option['ga_serviceemail'] : '', 'ga_sakey' => '', 'ga_pkey_print' => '<unspecified>');
try {
if (version_compare(PHP_VERSION, '5.3.0') >= 0 && function_exists('openssl_x509_read')) {
if (isset($option['ga_keyfilepath']) && $option['ga_keyfilepath'] != '' && file_exists($option['ga_keyfilepath'])) {
$p12key = @file_get_contents($option['ga_keyfilepath']);
$certs = array();
if (openssl_pkcs12_read($p12key, $certs, 'notasecret')) {
if (array_key_exists("pkey", $certs) && $certs["pkey"]) {
$privateKey = openssl_pkey_get_private($certs['pkey']);
$pemString = '';
if (openssl_pkey_export($privateKey, $pemString)) {
$existing_sa_options['ga_sakey'] = $pemString;
}
openssl_pkey_free($privateKey);
@unlink($options['ga_keyfilepath']);
}
}
}
}
} catch (Exception $e) {
// Never mind
}
// Remove redundant parts of regular options
unset($option['ga_serviceemail']);
unset($option['ga_keyfilepath']);
}
示例5: rsa_decrypt
function rsa_decrypt($ciphertext, $private_key, $password)
{
// 암호문을 base64로 디코딩한다.
$ciphertext = @base64_decode($ciphertext, true);
if ($ciphertext === false) {
return false;
}
// 개인키를 사용하여 복호화한다.
$privkey_decoded = @openssl_pkey_get_private($private_key, $password);
if ($privkey_decoded === false) {
return false;
}
$plaintext = false;
$status = @openssl_private_decrypt($ciphertext, $plaintext, $privkey_decoded);
@openssl_pkey_free($privkey_decoded);
if (!$status || $plaintext === false) {
return false;
}
// 압축을 해제하여 평문을 얻는다.
$plaintext = @gzuncompress($plaintext);
if ($plaintext === false) {
return false;
}
// 이상이 없는 경우 평문을 반환한다.
return $plaintext;
}
示例6: __destruct
public function __destruct()
{
if (!is_resource($this->handle)) {
return;
}
openssl_pkey_free($this->handle);
}
示例7: removeAll
public function removeAll()
{
foreach ($this->resources as $resource) {
openssl_pkey_free($resource);
}
$this->keys = array();
$this->resources = array();
}
示例8: generateKeyPair
/**
* Generate a key pair (public / private key) with optional passphrase
* that protects the private key.
*
* @param string $passphrase
* @return KeyPair
*/
public function generateKeyPair($passphrase = null)
{
$privateKey = null;
$encrypted = $passphrase !== null;
$keyPair = openssl_pkey_new();
openssl_pkey_export($keyPair, $privateKey, $passphrase);
$keyDetails = openssl_pkey_get_details($keyPair);
$publicKey = $keyDetails['key'];
openssl_pkey_free($keyPair);
return new KeyPair($privateKey, $publicKey, $encrypted);
}
示例9: generateKeyPair
/**
* Generate a key pair (public / private key) with optional passphrase
* that protects the private key.
*
* @param string $passphrase
* @return \TYPO3\Deploy\Encryption\KeyPair
* @author Christopher Hlubek <hlubek@networkteam.com>
*/
public function generateKeyPair($passphrase = NULL)
{
$privateKey = NULL;
$encrypted = $passphrase !== NULL;
$keyPair = openssl_pkey_new();
openssl_pkey_export($keyPair, $privateKey, $passphrase);
$keyDetails = openssl_pkey_get_details($keyPair);
$publicKey = $keyDetails['key'];
openssl_pkey_free($keyPair);
return new \TYPO3\Deploy\Encryption\KeyPair($privateKey, $publicKey, $encrypted);
}
示例10: hasSigned
/**
* @param Signature $signature
*
* @return boolean Whether the signature has been signed by this certificate
*/
public function hasSigned(Signature $signature)
{
$key = openssl_pkey_get_public($this->x509Cert);
try {
$hasSigned = $signature->isSignedByKey($key);
} catch (\Excetpion $e) {
openssl_pkey_free($key);
throw $e;
}
openssl_pkey_free($key);
return $hasSigned;
}
示例11: base64_signature
/**
* Sign a string using the configured private key
*
* @param string $str The string to calculate a signature for
*/
private function base64_signature($str)
{
$key = openssl_pkey_get_private($this->private_key);
if ($key === false) {
throw new SimpleSAML_Error_Exception("Unable to load private key: " . openssl_error_string());
}
if (!openssl_sign($str, $sig, $key)) {
throw new SimpleSAML_Error_Exception("Unable to create signature: " . openssl_error_string());
}
openssl_pkey_free($key);
return base64_encode($sig);
}
示例12: load
protected function load()
{
if (false == is_file($this->filename)) {
throw new \RuntimeException(sprintf("Specified private key file '%s' does not exist", $this->filename));
}
$this->loadedContent = file_get_contents($this->filename);
$resource = openssl_pkey_get_private($this->loadedContent);
if (false == $resource) {
$this->loadedContent = null;
throw new \RuntimeException(sprintf("Specified private key '%s' is invalid", $this->filename));
}
openssl_pkey_free($resource);
}
示例13: generateKeypair
/**
* Function to generate a new RSA keypair. This is not
* used for point derivation or for generating signatures.
* Only used for assymetric data encryption, as needed.
*
* @param int
* @param string
* @return array|boolean array of keys on success, boolean false on failure
*/
public final function generateKeypair($keybits = 512, $digest_alg = 'sha512')
{
try {
/* see: http://www.php.net/manual/en/function.openssl-pkey-new.php */
if (function_exists('openssl_pkey_new')) {
$keypair = array();
/* openssl keysize can't be smaller than 384 bits */
if ((int) $keybits < 384) {
$keybits = 384;
}
if (!isset($digest_alg) || trim($digest_alg) == '') {
$digest_alg = 'sha512';
}
/*
* RSA is the only supported key type at this time
* http://www.php.net/manual/en/function.openssl-csr-new.php
*/
$config = array('digest_alg' => $digest_alg, 'private_key_bits' => (int) $keybits, 'private_key_type' => OPENSSL_KEYTYPE_RSA);
$resource = openssl_pkey_new($config);
if (!$resource) {
throw new \Exception('Error in generateOpenSSLKeypair: Could not create new OpenSSL resource.');
/* with the openssl extension, you also have it's own errors returned */
while ($msg = openssl_error_string()) {
throw new \Exception('Error in generateOpenSSLKeypair: OpenSSL reported error: ' . $msg);
}
return false;
}
if (openssl_pkey_export($resource, $keypair['pri'])) {
$publickey = openssl_pkey_get_details($resource);
$keypair['pub'] = $publickey['key'];
} else {
throw new \Exception('Error in generateOpenSSLKeypair: Private key could not be determined from OpenSSL key resource.');
while ($msg = openssl_error_string()) {
throw new \Exception('Error in generateOpenSSLKeypair: OpenSSL reported error: ' . $msg);
}
return false;
}
openssl_pkey_free($resource);
return $keypair;
} else {
throw new \Exception('Error in generateOpenSSLKeypair: OpenSSL PHP extension missing. Cannot continue.');
return false;
}
} catch (Exception $e) {
while ($msg = openssl_error_string()) {
throw new \Exception('Error in generateOpenSSLKeypair: OpenSSL reported error: ' . $msg);
}
throw $e;
return false;
}
}
示例14: parsePem
/**
* Returns a usable private and public key from a PEM encoded string.
*
* @param string $key The private key.
* @param string $pass The private key passphrase.
*
* @return array The private ([0]) and public ([1]) key.
*
* @throws InvalidArgumentException If a passphrase is required.
* @throws RuntimeException If the file could not be parsed.
*/
public function parsePem($key, $pass = null)
{
$this->clearErrors();
if (false === ($resource = openssl_pkey_get_private($key, $pass))) {
$error = openssl_error_string();
if (preg_match('/(bad password|bad decrypt)/', $error)) {
throw new InvalidArgumentException('The private key passphrase is invalid.');
}
throw new RuntimeException("The private key could not be parsed: {$error}");
}
openssl_pkey_export($resource, $private);
$details = openssl_pkey_get_details($resource);
openssl_pkey_free($resource);
return array($private, $details['key']);
}
示例15: onPasswordChange
public function onPasswordChange(FormEvent $event)
{
// Get new password
/** @var User $user */
$user = $event->getForm()->getData();
$password = $user->getPlainPassword();
// Get pkey from session
$privKey = $event->getRequest()->getSession()->get('pkey');
// Secure pkey with new password
$res = openssl_pkey_get_private($privKey);
openssl_pkey_export($res, $privKey, $password);
// Store pkey in user
$user->setPrivateKey($privKey);
unset($password);
openssl_pkey_free($res);
unset($privKey);
}