.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "RSA 3pm" .TH RSA 3pm 2024-03-07 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Crypt::OpenSSL::RSA \- RSA encoding and decoding, using the openSSL libraries .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& use Crypt::OpenSSL::Random; \& use Crypt::OpenSSL::RSA; \& \& # not necessary if we have /dev/random: \& Crypt::OpenSSL::Random::random_seed($good_entropy); \& Crypt::OpenSSL::RSA\->import_random_seed(); \& $rsa_pub = Crypt::OpenSSL::RSA\->new_public_key($key_string); \& $ciphertext = $rsa\->encrypt($plaintext); \& \& $rsa_priv = Crypt::OpenSSL::RSA\->new_private_key($key_string); \& $plaintext = $rsa\->encrypt($ciphertext); \& \& $rsa = Crypt::OpenSSL::RSA\->generate_key(1024); # or \& $rsa = Crypt::OpenSSL::RSA\->generate_key(1024, $prime); \& \& print "private key is:\en", $rsa\->get_private_key_string(); \& print "public key (in PKCS1 format) is:\en", \& $rsa\->get_public_key_string(); \& print "public key (in X509 format) is:\en", \& $rsa\->get_public_key_x509_string(); \& \& $rsa_priv\->use_md5_hash(); # insecure. use_sha256_hash or use_sha1_hash are the default \& $signature = $rsa_priv\->sign($plaintext); \& print "Signed correctly\en" if ($rsa\->verify($plaintext, $signature)); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" \&\f(CW\*(C`Crypt::OpenSSL::RSA\*(C'\fR provides the ability to RSA encrypt strings which are somewhat shorter than the block size of a key. It also allows for decryption, signatures and signature verification. .PP \&\fINOTE\fR: Many of the methods in this package can croak, so use \f(CW\*(C`eval\*(C'\fR, or Error.pm's try/catch mechanism to capture errors. Also, while some methods from earlier versions of this package return true on success, this (never documented) behavior is no longer the case. .SH "Class Methods" .IX Header "Class Methods" .IP new_public_key 4 .IX Item "new_public_key" Create a new \f(CW\*(C`Crypt::OpenSSL::RSA\*(C'\fR object by loading a public key in from a string containing Base64/DER\-encoding of either the PKCS1 or X.509 representation of the key. The string should include the \&\f(CW\*(C`\-\-\-\-\-BEGIN...\-\-\-\-\-\*(C'\fR and \f(CW\*(C`\-\-\-\-\-END...\-\-\-\-\-\*(C'\fR lines. .Sp The padding is set to PKCS1_OAEP, but can be changed with the \&\f(CW\*(C`use_xxx_padding\*(C'\fR methods. .IP new_private_key 4 .IX Item "new_private_key" Create a new \f(CW\*(C`Crypt::OpenSSL::RSA\*(C'\fR object by loading a private key in from an string containing the Base64/DER encoding of the PKCS1 representation of the key. The string should include the \&\f(CW\*(C`\-\-\-\-\-BEGIN...\-\-\-\-\-\*(C'\fR and \f(CW\*(C`\-\-\-\-\-END...\-\-\-\-\-\*(C'\fR lines. The padding is set to PKCS1_OAEP, but can be changed with \f(CW\*(C`use_xxx_padding\*(C'\fR. .Sp An optional parameter can be passed for passphase protected private key: .RS 4 .IP passphase 4 .IX Item "passphase" The passphase which protects the private key. .RE .RS 4 .RE .IP generate_key 4 .IX Item "generate_key" Create a new \f(CW\*(C`Crypt::OpenSSL::RSA\*(C'\fR object by constructing a private/public key pair. The first (mandatory) argument is the key size, while the second optional argument specifies the public exponent (the default public exponent is 65537). The padding is set to \&\f(CW\*(C`PKCS1_OAEP\*(C'\fR, but can be changed with use_xxx_padding methods. .IP new_key_from_parameters 4 .IX Item "new_key_from_parameters" Given Crypt::OpenSSL::Bignum objects for n, e, and optionally d, p, and q, where p and q are the prime factors of n, e is the public exponent and d is the private exponent, create a new Crypt::OpenSSL::RSA object using these values. If p and q are provided and d is undef, d is computed. Note that while p and q are not necessary for a private key, their presence will speed up computation. .IP import_random_seed 4 .IX Item "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. .SH "Instance Methods" .IX Header "Instance Methods" .IP DESTROY 4 .IX Item "DESTROY" Clean up after ourselves. In particular, erase and free the memory occupied by the RSA key structure. .IP get_public_key_string 4 .IX Item "get_public_key_string" Return the Base64/DER\-encoded PKCS1 representation of the public key. This string has header and footer lines: .Sp .Vb 2 \& \-\-\-\-\-BEGIN RSA PUBLIC KEY\-\-\-\-\-\- \& \-\-\-\-\-END RSA PUBLIC KEY\-\-\-\-\-\- .Ve .IP get_public_key_x509_string 4 .IX Item "get_public_key_x509_string" Return the Base64/DER\-encoded representation of the "subject public key", suitable for use in X509 certificates. This string has header and footer lines: .Sp .Vb 2 \& \-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-\- \& \-\-\-\-\-END PUBLIC KEY\-\-\-\-\-\- .Ve .Sp and is the format that is produced by running \f(CW\*(C`openssl rsa \-pubout\*(C'\fR. .IP get_private_key_string 4 .IX Item "get_private_key_string" Return the Base64/DER\-encoded PKCS1 representation of the private key. This string has header and footer lines: .Sp .Vb 2 \& \-\-\-\-\-BEGIN RSA PRIVATE KEY\-\-\-\-\-\- \& \-\-\-\-\-END RSA PRIVATE KEY\-\-\-\-\-\- .Ve .Sp 2 optional parameters can be passed for passphase protected private key string: .RS 4 .IP passphase 4 .IX Item "passphase" The passphase which protects the private key. .IP "cipher name" 4 .IX Item "cipher name" The cipher algorithm used to protect the private key. Default to \&'des3'. .RE .RS 4 .RE .IP encrypt 4 .IX Item "encrypt" Encrypt a binary "string" using the public (portion of the) key. .IP decrypt 4 .IX Item "decrypt" Decrypt a binary "string". Croaks if the key is public only. .IP private_encrypt 4 .IX Item "private_encrypt" Encrypt a binary "string" using the private key. Croaks if the key is public only. .IP public_decrypt 4 .IX Item "public_decrypt" Decrypt a binary "string" using the public (portion of the) key. .IP sign 4 .IX Item "sign" Sign a string using the secret (portion of the) key. .IP verify 4 .IX Item "verify" Check the signature on a text. .IP use_no_padding 4 .IX Item "use_no_padding" Use 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. .IP use_pkcs1_padding 4 .IX Item "use_pkcs1_padding" Use PKCS #1 v1.5 padding. This currently is the most widely used mode of padding. .IP use_pkcs1_oaep_padding 4 .IX Item "use_pkcs1_oaep_padding" Use \f(CW\*(C`EME\-OAEP\*(C'\fR padding as defined in PKCS #1 v2.0 with SHA\-1, MGF1 and an empty encoding parameter. This mode of padding is recommended for all new applications. It is the default mode used by \&\f(CW\*(C`Crypt::OpenSSL::RSA\*(C'\fR. .IP use_sslv23_padding 4 .IX Item "use_sslv23_padding" Use \f(CW\*(C`PKCS #1 v1.5\*(C'\fR padding with an SSL-specific modification that denotes that the server is SSL3 capable. .Sp Not available since OpenSSL 3. .IP use_md5_hash 4 .IX Item "use_md5_hash" Use the RFC 1321 MD5 hashing algorithm by Ron Rivest when signing and verifying messages. .Sp Note that this is considered \fBinsecure\fR. .IP use_sha1_hash 4 .IX Item "use_sha1_hash" Use the RFC 3174 Secure Hashing Algorithm (FIPS 180\-1) when signing and verifying messages. This is the default, when use_sha256_hash is not available. .IP "use_sha224_hash, use_sha256_hash, use_sha384_hash, use_sha512_hash" 4 .IX Item "use_sha224_hash, use_sha256_hash, use_sha384_hash, use_sha512_hash" These FIPS 180\-2 hash algorithms, for use when signing and verifying messages, are only available with newer openssl versions (>= 0.9.8). .Sp use_sha256_hash is the default hash mode when available. .IP use_ripemd160_hash 4 .IX Item "use_ripemd160_hash" Dobbertin, Bosselaers and Preneel's RIPEMD hashing algorithm when signing and verifying messages. .IP use_whirlpool_hash 4 .IX Item "use_whirlpool_hash" Vincent Rijmen und Paulo S. L. M. Barreto ISO/IEC 10118\-3:2004 WHIRLPOOL hashing algorithm when signing and verifying messages. .IP size 4 .IX Item "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: .RS 4 .IP pkcs1_oaep_padding 4 .IX Item "pkcs1_oaep_padding" at most 42 bytes less than this size. .IP "pkcs1_padding or sslv23_padding" 4 .IX Item "pkcs1_padding or sslv23_padding" at most 11 bytes less than this size. .IP no_padding 4 .IX Item "no_padding" exactly this size. .RE .RS 4 .RE .IP check_key 4 .IX Item "check_key" This function validates the RSA key, returning a true value if the key is valid, and a false value otherwise. Croaks if the key is public only. .IP get_key_parameters 4 .IX Item "get_key_parameters" Return \f(CW\*(C`Crypt::OpenSSL::Bignum\*(C'\fR objects representing the values of \f(CW\*(C`n\*(C'\fR, \&\f(CW\*(C`e\*(C'\fR, \f(CW\*(C`d\*(C'\fR, \f(CW\*(C`p\*(C'\fR, \f(CW\*(C`q\*(C'\fR, \f(CW\*(C`d mod (p\-1)\*(C'\fR, \f(CW\*(C`d mod (q\-1)\*(C'\fR, and \f(CW\*(C`1/q mod p\*(C'\fR, where \f(CW\*(C`p\*(C'\fR and \f(CW\*(C`q\*(C'\fR are the prime factors of \f(CW\*(C`n\*(C'\fR, \f(CW\*(C`e\*(C'\fR is the public exponent and \f(CW\*(C`d\*(C'\fR is the private exponent. Some of these values may return as \f(CW\*(C`undef\*(C'\fR; only \f(CW\*(C`n\*(C'\fR and \f(CW\*(C`e\*(C'\fR will be defined for a public key. The \&\f(CW\*(C`Crypt::OpenSSL::Bignum\*(C'\fR module must be installed for this to work. .IP is_private 4 .IX Item "is_private" Return true if this is a private key, and false if it is private only. .SH BUGS .IX Header "BUGS" There is a small memory leak when generating new keys of more than 512 bits. .SH AUTHOR .IX Header "AUTHOR" Ian Robertson, \f(CW\*(C`iroberts@cpan.org\*(C'\fR. For support, please email \&\f(CW\*(C`perl\-openssl\-users@lists.sourceforge.net\*(C'\fR. .SH ACKNOWLEDGEMENTS .IX Header "ACKNOWLEDGEMENTS" .SH LICENSE .IX Header "LICENSE" Copyright (c) 2001\-2011 Ian Robertson. Crypt::OpenSSL::RSA is free software; you may redistribute it and/or modify it under the same terms as Perl itself. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBperl\fR\|(1), Crypt::OpenSSL::Random, Crypt::OpenSSL::Bignum, \&\fBrsa\fR\|(3), \fBRSA_new\fR\|(3) , \&\fBRSA_public_encrypt\fR\|(3) , \&\fBRSA_size\fR\|(3) , \&\fBRSA_generate_key\fR\|(3) , \&\fBRSA_check_key\fR\|(3)