| 1 | package Net::IPMessenger::Encrypt; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 2 | ▶ | use Net::IPMessenger::EncryptOption; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 2 | ▶ | use base qw( Class::Accessor::Fast ); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | 2 | __PACKAGE__->mk_accessors( | |||||||||||||||||
| 9 | qw( exponent modulus private_key | ||||||||||||||||||
| 10 | support_encryption attach ) | ||||||||||||||||||
| 11 | ); | ||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | 2 | my $RSA_KEY_SIZE = 1024; | |||||||||||||||||
| 14 | 2 | my $IV = "\0\0\0\0\0\0\0\0"; | |||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | 3 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=10.1
Pod
documented
| |||||||||||||||||||
| 17 | 3 | my $class = shift; | |||||||||||||||||
| 18 | 3 | my %args = @_; | |||||||||||||||||
| 19 | # needs those modules for encryption support | ||||||||||||||||||
| 20 | 3 | eval { | |||||||||||||||||
| 21 | 3 | require Crypt::Blowfish; | |||||||||||||||||
| 22 | 0 | require Crypt::CBC; | |||||||||||||||||
| 23 | 0 | require Crypt::OpenSSL::Bignum; | |||||||||||||||||
| 24 | 0 | require Crypt::OpenSSL::RSA; | |||||||||||||||||
| 25 | }; | ||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | 3 | ▶ | return if $@; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 28 | 0 | my $self = {}; | |||||||||||||||||
| 29 | 0 | bless $self, $class; | |||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | 0 | $self->support_encryption( $self->option->set_rsa_1024->set_blowfish_128 ); | |||||||||||||||||
| 32 | 0 | return $self; | |||||||||||||||||
| 33 | } | ||||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | 0 | ▶ | sub option { | ||||||||||||||||
|
Subroutine
option: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 36 | 0 | my $self = shift; | |||||||||||||||||
| 37 | 0 | return Net::IPMessenger::EncryptOption->new(shift); | |||||||||||||||||
| 38 | } | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | 0 | ▶ | sub generate_keys { | ||||||||||||||||
|
Subroutine
generate_keys: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||
| 41 | 0 | my $self = shift; | |||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | 0 | ▶ | if ( $self->private_key ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 44 | 0 | return ( $self->exponent, $self->modulus ); | |||||||||||||||||
| 45 | } | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | 0 | my $rsa_key_size; | |||||||||||||||||
| 48 | 0 | my $option = $self->support_encryption; | |||||||||||||||||
| 49 | 0 | ▶ | if ( $option->get_rsa_2048 ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 50 | 0 | $rsa_key_size = 2048; | |||||||||||||||||
| 51 | } | ||||||||||||||||||
| 52 | elsif ( $option->get_rsa_1024 ) { | ||||||||||||||||||
| 53 | 0 | $rsa_key_size = 1024; | |||||||||||||||||
| 54 | } | ||||||||||||||||||
| 55 | elsif ( $option->get_rsa_512 ) { | ||||||||||||||||||
| 56 | 0 | $rsa_key_size = 512; | |||||||||||||||||
| 57 | } | ||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | 0 | my $rsa = Crypt::OpenSSL::RSA->generate_key($rsa_key_size); | |||||||||||||||||
| 60 | 0 | my( $modulus, $exponent ) = $rsa->get_key_parameters; | |||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | 0 | $self->private_key( $rsa->get_private_key_string ); | |||||||||||||||||
| 63 | return ( | ||||||||||||||||||
| 64 | 0 | $self->exponent( $exponent->to_hex ), | |||||||||||||||||
| 65 | $self->modulus( $modulus->to_hex ) | ||||||||||||||||||
| 66 | ); | ||||||||||||||||||
| 67 | } | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | 0 | ▶ | sub public_key_string { | ||||||||||||||||
|
Subroutine
public_key_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 70 | 0 | my $self = shift; | |||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | 0 | my( $exponent, $modulus ) = $self->generate_keys; | |||||||||||||||||
| 73 | 0 | my $option = sprintf "%x:%d-%s", | |||||||||||||||||
| 74 | $self->support_encryption, $exponent, $modulus; | ||||||||||||||||||
| 75 | } | ||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | 0 | ▶ | sub encrypt_message { | ||||||||||||||||
|
Subroutine
encrypt_message: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||
| 78 | 0 | my( $self, $message, $pubkey ) = @_; | |||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | 0 | my $option = $self->option( hex $pubkey->{option} ); | |||||||||||||||||
| 81 | 0 | my $blowfish_key_size; | |||||||||||||||||
| 82 | 0 | ▶ | if ( $option->get_blowfish_128 ) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 83 | 0 | $blowfish_key_size = 128 / 8; | |||||||||||||||||
| 84 | } | ||||||||||||||||||
| 85 | elsif ( $option->get_blowfish_256 ) { | ||||||||||||||||||
| 86 | 0 | $blowfish_key_size = 256 / 8; | |||||||||||||||||
| 87 | } | ||||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | 0 | my $shared_key = Crypt::CBC->random_bytes($blowfish_key_size); | |||||||||||||||||
| 90 | 0 | my $blowfish = Crypt::CBC->new( | |||||||||||||||||
| 91 | -literal_key => 1, | ||||||||||||||||||
| 92 | -key => $shared_key, | ||||||||||||||||||
| 93 | -keysize => length $shared_key, | ||||||||||||||||||
| 94 | -cipher => 'Blowfish', | ||||||||||||||||||
| 95 | -padding => 'standard', | ||||||||||||||||||
| 96 | -iv => $IV, | ||||||||||||||||||
| 97 | -header => 'none', | ||||||||||||||||||
| 98 | ); | ||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | 0 | my $exponent = Crypt::OpenSSL::Bignum->new_from_hex( $pubkey->{exponent} ); | |||||||||||||||||
| 101 | 0 | my $modulus = Crypt::OpenSSL::Bignum->new_from_hex( $pubkey->{modulus} ); | |||||||||||||||||
| 102 | 0 | my $rsa_pub = | |||||||||||||||||
| 103 | Crypt::OpenSSL::RSA->new_key_from_parameters( $modulus, $exponent ); | ||||||||||||||||||
| 104 | 0 | $rsa_pub->use_pkcs1_padding; | |||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | # encrypt key and message | ||||||||||||||||||
| 107 | 0 | my $cipher_key = $rsa_pub->encrypt($shared_key); | |||||||||||||||||
| 108 | 0 | my $cipher_text = $blowfish->encrypt($message); | |||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | 0 | return sprintf "%s:%s:%s", $pubkey->{option}, unpack( "H*", $cipher_key ), | |||||||||||||||||
| 111 | unpack( "H*", $cipher_text ); | ||||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | 0 | ▶ | sub decrypt_message { | ||||||||||||||||
|
Subroutine
decrypt_message: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||
| 115 | 0 | my( $self, $message ) = @_; | |||||||||||||||||
| 116 | 0 | ▶ | return $message unless defined $self->private_key; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | 0 | my( $enc_opt, $cipher_key, $cipher_text ) = split /\:/, $message, 3; | |||||||||||||||||
| 119 | 0 | my $rsa = Crypt::OpenSSL::RSA->new_private_key( $self->private_key ); | |||||||||||||||||
| 120 | 0 | $rsa->use_pkcs1_padding; | |||||||||||||||||
| 121 | 0 | my $shared_key = $rsa->decrypt( pack( "H*", $cipher_key ) ); | |||||||||||||||||
| 122 | 0 | my $blowfish = Crypt::CBC->new( | |||||||||||||||||
| 123 | -literal_key => 1, | ||||||||||||||||||
| 124 | -key => $shared_key, | ||||||||||||||||||
| 125 | -keysize => length $shared_key, | ||||||||||||||||||
| 126 | -cipher => 'Blowfish', | ||||||||||||||||||
| 127 | -padding => 'standard', | ||||||||||||||||||
| 128 | -iv => $IV, | ||||||||||||||||||
| 129 | -header => 'none', | ||||||||||||||||||
| 130 | ); | ||||||||||||||||||
| 131 | # XXX attach info not encrypted | ||||||||||||||||||
| 132 | 0 | my( $fileid, $attach ) = split /:/, $cipher_text, 2; | |||||||||||||||||
| 133 | 0 | $fileid = substr $fileid, -1; | |||||||||||||||||
| 134 | 0 | $attach = $fileid . ':' . $attach; | |||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | 0 | my $decrypted = $blowfish->decrypt( pack( "H*", $cipher_text ) ); | |||||||||||||||||
| 137 | # delete null string | ||||||||||||||||||
| 138 | 0 | my($text) = split /\0/, $decrypted; | |||||||||||||||||
| 139 | 0 | $self->attach($attach); | |||||||||||||||||
| 140 | 0 | return $text; | |||||||||||||||||
| 141 | } | ||||||||||||||||||
| 142 | |||||||||||||||||||
| 143 | 2 | 1; | |||||||||||||||||
| 144 | __END__ | ||||||||||||||||||