From: <iro...@us...> - 2004-02-13 23:08:18
|
Update of /cvsroot/perl-openssl/Crypt/OpenSSL/RSA In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30377 Modified Files: RSA.pm RSA.xs Log Message: Add support for X.509 encodings of public keys Index: RSA.pm =================================================================== RCS file: /cvsroot/perl-openssl/Crypt/OpenSSL/RSA/RSA.pm,v retrieving revision 1.25 retrieving revision 1.26 diff -C2 -d -r1.25 -r1.26 *** RSA.pm 27 Apr 2003 20:04:25 -0000 1.25 --- RSA.pm 13 Feb 2004 23:02:13 -0000 1.26 *************** *** 56,60 **** print "private key is:\n", $rsa->get_private_key_string(); ! print "public key is:\n", $rsa->get_public_key_string(); $rsa_priv->use_md5_hash(); # use_sha1_hash is the default --- 56,63 ---- print "private key is:\n", $rsa->get_private_key_string(); ! print "public key (in PKCS1 format) is:\n", ! $rsa->get_public_key_string(); ! print "public key (in X509 format) is:\n", ! $rsa->get_public_key_pem_string(); $rsa_priv->use_md5_hash(); # use_sha1_hash is the default *************** *** 80,86 **** Create a new Crypt::OpenSSL::RSA object by loading 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 the use_xxx_padding methods =cut --- 83,92 ---- Create a new Crypt::OpenSSL::RSA 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 ! -----BEGIN...----- and -----END...----- lines. ! ! The padding is set to PKCS1_OAEP, but can be changed with the ! use_xxx_padding methods =cut *************** *** 88,93 **** sub new_public_key { ! my $self = shift->_new(); ! $self->load_public_key( @_ ); return $self; } --- 94,100 ---- sub new_public_key { ! my ($proto, $p_string) = @_; ! my $self = $proto->_new(); ! $self->load_public_key($p_string); return $self; } *************** *** 96,100 **** Create a new Crypt::OpenSSL::RSA object by loading 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 use_xxx_padding. --- 103,108 ---- Create a new Crypt::OpenSSL::RSA 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 -----BEGIN...----- and -----END...----- lines. The padding is set to PKCS1_OAEP, but can be changed with use_xxx_padding. *************** *** 222,227 **** sub load_public_key { ! my($self, $key_string) = @_; ! $self->_load_key(0, $key_string); } --- 230,246 ---- sub load_public_key { ! my ($self, $p_key_string) = @_; ! if ($p_key_string =~ /^-----BEGIN RSA PUBLIC KEY-----/) ! { ! $self->_load_public_pkcs1_key($p_key_string); ! } ! elsif ($p_key_string =~ /^-----BEGIN PUBLIC KEY-----/) ! { ! $self->_load_public_pem_key($p_key_string); ! } ! else ! { ! croak "unrecognized key format"; ! } } *************** *** 230,265 **** I<DEPRECATED> - use new_private_key instead - =cut - - sub load_private_key - { - my($self, $key_string) = @_; - $self->_load_key(1, $key_string); - } - =item get_public_key_string ! Return the public portion of the key as an X509 encoded string. ! =cut ! sub get_public_key_string ! { ! my ($self) = @_; ! return $self->_get_key_string(0); ! } ! =item get_private_key_string ! Return the X509 encoding of the private key. ! =cut ! sub get_private_key_string ! { ! my ($self) = @_; ! return $self->_get_key_string(1); ! } =item encrypt --- 249,275 ---- I<DEPRECATED> - use new_private_key instead =item get_public_key_string ! Return the Base64/DER-encoded PKCS1 representation of the public ! key. This string has ! header and footer lines: ! -----BEGIN RSA PUBLIC KEY------ ! -----END RSA PUBLIC KEY------ ! =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: ! -----BEGIN PUBLIC KEY------ ! -----END PUBLIC KEY------ ! and is the format that is produced by running C<openssl rsa -pubout>. ! =item get_private_key_string + Return the DER-encoded PKCS1 representation of the private key. =item encrypt Index: RSA.xs =================================================================== RCS file: /cvsroot/perl-openssl/Crypt/OpenSSL/RSA/RSA.xs,v retrieving revision 1.27 retrieving revision 1.28 diff -C2 -d -r1.27 -r1.28 *** RSA.xs 27 Apr 2003 20:02:22 -0000 1.27 --- RSA.xs 13 Feb 2004 23:02:13 -0000 1.28 *************** *** 191,235 **** } ! MODULE = Crypt::OpenSSL::RSA PACKAGE = Crypt::OpenSSL::RSA ! PROTOTYPES: DISABLE ! BOOT: ! ERR_load_crypto_strings(); - void - _load_key(rsa_HV, private_flag_SV, key_string_SV) - HV* rsa_HV; - SV* private_flag_SV; - SV* key_string_SV; - PREINIT: - int key_string_length; /* Needed to pass to SvPV */ - char* key_string; - char private_flag; RSA* rsa; BIO* stringBIO; ! CODE: /* First; remove any old rsa structures, to avoid leakage */ ! free_RSA_key(rsa_HV); ! private_flag = SvTRUE( private_flag_SV ); ! key_string = SvPV( key_string_SV, key_string_length ); ! if( (stringBIO = BIO_new_mem_buf(key_string, key_string_length)) == NULL ) ! { ! croak( "Failed to create memory BIO" ); ! } ! rsa = private_flag ! ? PEM_read_bio_RSAPrivateKey( stringBIO, NULL, NULL, NULL ) ! : PEM_read_bio_RSAPublicKey( stringBIO, NULL, NULL, NULL ); BIO_set_close(stringBIO, BIO_CLOSE); ! BIO_free( stringBIO ); ! if ( rsa == NULL ) { ! croak( "Failed to read key" ); } ! set_RSA_key(rsa_HV, rsa); void --- 191,263 ---- } ! SV* extractBioString(BIO* p_stringBio) ! { ! SV* sv; ! BUF_MEM* bptr; ! BIO_flush(p_stringBio); ! BIO_get_mem_ptr(p_stringBio, &bptr); ! sv = newSVpv(bptr->data, bptr->length); ! ! BIO_set_close(p_stringBio, BIO_CLOSE); ! BIO_free(p_stringBio); ! return sv; ! } ! ! void _load_rsa_key(HV* p_rsaHv, ! SV* p_keyStringSv, ! RSA*(*p_loader)(BIO*, RSA**, pem_password_cb*, void*)) ! { ! int keyStringLength; /* Needed to pass to SvPV */ ! char* keyString; RSA* rsa; BIO* stringBIO; ! /* First; remove any old rsa structures, to avoid leakage */ ! free_RSA_key(p_rsaHv); ! keyString = SvPV(p_keyStringSv, keyStringLength); ! checkOpenSslCall(stringBIO = BIO_new_mem_buf(keyString, keyStringLength)); ! rsa = p_loader(stringBIO, NULL, NULL, NULL); BIO_set_close(stringBIO, BIO_CLOSE); ! BIO_free(stringBIO); ! if (rsa == NULL) { ! croak("OpenSSL error: %s", ERR_reason_error_string(ERR_get_error())); } ! set_RSA_key(p_rsaHv, rsa); ! } ! ! MODULE = Crypt::OpenSSL::RSA PACKAGE = Crypt::OpenSSL::RSA ! PROTOTYPES: DISABLE ! ! BOOT: ! ERR_load_crypto_strings(); ! ! void ! load_private_key(rsa_HV, key_string_SV) ! HV* rsa_HV; ! SV* key_string_SV; ! CODE: ! _load_rsa_key(rsa_HV, key_string_SV, PEM_read_bio_RSAPrivateKey); ! ! void ! _load_public_pkcs1_key(rsa_HV, key_string_SV) ! HV* rsa_HV; ! SV* key_string_SV; ! CODE: ! _load_rsa_key(rsa_HV, key_string_SV, PEM_read_bio_RSAPublicKey); ! ! void ! _load_public_pem_key(rsa_HV, key_string_SV) ! HV* rsa_HV; ! SV* key_string_SV; ! CODE: ! _load_rsa_key(rsa_HV, key_string_SV, PEM_read_bio_RSA_PUBKEY); void *************** *** 240,274 **** SV* ! _get_key_string(rsa_HV, private_flag_SV) HV* rsa_HV; - SV* private_flag_SV; PREINIT: - BUF_MEM* bptr; BIO* stringBIO; RSA* rsa; CODE: ! stringBIO = BIO_new( BIO_s_mem() ); ! if (stringBIO == NULL) ! { ! croak( "Failed to create memory BIO" ); ! } ! rsa = get_RSA_key( rsa_HV ); ! if( SvTRUE( private_flag_SV ) ) ! { ! PEM_write_bio_RSAPrivateKey(stringBIO, rsa, NULL, NULL, 0, NULL, NULL); ! } ! else ! { ! PEM_write_bio_RSAPublicKey(stringBIO, rsa); ! } ! BIO_flush(stringBIO); ! BIO_get_mem_ptr(stringBIO, &bptr); ! RETVAL = newSVpv( bptr->data, bptr->length ); - BIO_set_close(stringBIO, BIO_CLOSE); - BIO_free(stringBIO); OUTPUT: RETVAL --- 268,308 ---- SV* ! get_private_key_string(rsa_HV) HV* rsa_HV; PREINIT: BIO* stringBIO; RSA* rsa; CODE: ! checkOpenSslCall(stringBIO = BIO_new(BIO_s_mem())); ! rsa = get_RSA_key(rsa_HV); ! PEM_write_bio_RSAPrivateKey(stringBIO, rsa, NULL, NULL, 0, NULL, NULL); ! RETVAL = extractBioString(stringBIO); ! OUTPUT: ! RETVAL ! SV* ! get_public_key_string(rsa_HV) ! HV* rsa_HV; ! PREINIT: ! BIO* stringBIO; ! CODE: ! checkOpenSslCall(stringBIO = BIO_new(BIO_s_mem())); ! PEM_write_bio_RSAPublicKey(stringBIO, get_RSA_key(rsa_HV)); ! RETVAL = extractBioString(stringBIO); ! OUTPUT: ! RETVAL ! ! SV* ! get_public_key_x509_string(rsa_HV) ! HV* rsa_HV; ! PREINIT: ! BIO* stringBIO; ! CODE: ! checkOpenSslCall(stringBIO = BIO_new(BIO_s_mem())); ! PEM_write_bio_RSA_PUBKEY(stringBIO, get_RSA_key(rsa_HV)); ! RETVAL = extractBioString(stringBIO); OUTPUT: RETVAL |