NAME

Crypt::OpenSSL::RSA - RSA encoding and decoding, using the openSSL libraries

SYNOPSIS

use Crypt::OpenSSL::Random;
use Crypt::OpenSSL::RSA;

# not necessary if we have /dev/random:
Crypt::OpenSSL::Random::random_seed($good_entropy);

$rsa_pub = new Crypt::OpenSSL::RSA();
$rsa_pub->import_random_seed();

$rsa_pub->load_public_key($key_string);
$ciphertext = $rsa->encrypt($plaintext);

$rsa_priv = new Crypt::OpenSSL::RSA();

$rsa_priv->load_private_key($key_string);
$plaintext = $rsa->encrypt($ciphertext);

$rsa = new Crypt::OpenSSL::RSA();

$rsa->generate_key(1024); # or
$rsa->generate_key(1024, $prime);

print "private key is:\n", $rsa->get_private_key_string();
print "public key is:\n", $rsa->get_public_key_string();

DESCRIPTION

Crypt::OpenSSL::RSA provides the ability to RSA encrypt strings which are somewhat shorter than the block size of a key. It also allows for decryption.

Instance Methods

new

The standard constructor for an RSA object takes no arguments; the key should either be created by generate_key, or loaded in by load_public_key or load_private_key.

DESTROY

Clean up after ourselves. In particular, erase and free the memory occupied by the RSA key structure.

load_public_key

Load a public key in from an X509 encoded string. The string should include the -----BEGIN...----- and -----END...----- lines. The padding is set to PKCS1_OAEP, but can be changed with set_padding.

load_private_key

Load a private key in from an X509 encoded string. The string should include the -----BEGIN...----- and -----END...----- lines. The padding is set to PKCS1_OAEP, but can be changed with set_padding.

get_public_key_string

Return the public portion of the key as an X509 encoded string.

get_private_key_string

Return the X509 encoding of the private key.

generate_key

Generate a private/public key pair. The padding is set to PKCS1_OAEP, but can be changed with set_padding.

encrypt

Encrypt a string using the public (portion of the) key

decrypt

Decrypt a binary "string". Croaks if the key is public only.

set_padding_mode

Set the padding mode. The choices are

$RSA_PKCS1_PADDING

PKCS #1 v1.5 padding. This currently is the most widely used mode.

$RSA_PKCS1_OAEP_PADDING

EME-OAEP as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty encoding parameter. This mode is recommended for all new applications.

$RSA_SSLV23_PADDING

PKCS #1 v1.5 padding with an SSL-specific modification that denotes that the server is SSL3 capable.

$RSA_NO_PADDING

Raw RSA encryption. This mode should only be used to implement cryptographically sound padding modes in the application code. Encrypting user data directly with RSA is insecure.

By default, $RSA_PKCS1_OAEP_PADDING is used.

get_padding_mode

Get the padding mode.

size

Returns the size, in bytes, of the key. All encrypted text will be of this size, and depending on the padding mode used, the length of the text to be encrypted should be

$RSA_PKCS1_OAEP_PADDING

at most 42 bytes less than this size.

$RSA_PKCS1_PADDING or $RSA_SSLV23_PADDING

at most 11 bytes less than this size.

$RSA_NO_PADDING

exactly this size.

check_key

This function validates the RSA key, returning 1 if the key is valid, 0 otherwise.

import_random_seed

Import a random seed from Crypt::OpenSSL::Random, since the OpenSSL libraries won't allow sharing of random structures across perl XS modules.

BUGS

Currently many XS routines croak rather than trying to intelligently signal an error. This is mostly in cases where a routine is called without adequate preparation, such as asking to encrypt before setting a key.

There appears to be some small memory leaks in functions (notably _load_key, probably others). This is presumably due to failure to decrement a reference count somewhere - I just haven't found out where yet.

RSA_NO_PADDING_MODE does not work - I don't know yet if it's a problem with encryption, decryption, or both.

AUTHOR

Ian Robertson, iroberts@cpan.com

SEE ALSO

perl(1), Crypt::OpenSSL::Random(3), rsa(3), RSA_new(3), RSA_public_encrypt(3), RSA_size(3), RSA_generate_key(3), RSA_check_key(3)

1 POD Error

The following errors were encountered while parsing the POD:

Around line 258:

'=item' outside of any '=over'