#################### main pod documentation begin ################### =head1 NAME Zymonic::Decryptor - Zymonic Decryptor base module. =head1 SYNOPSIS TODO =head1 DESCRIPTION TODO =head1 USAGE TODO =head1 BUGS None we're aware of... =head1 SUPPORT As in the license, Zymonic is provided without warranty or support unless purchased separately, however... If you email zymonic-support@zednax.com your issue will be noted and may receive a response. For security issues, please contact zymonic-security@zednax.com and someone will respond within 8 working hours. =head1 AUTHOR Alex Masidlover et al. CPAN ID: MODAUTHOR Zednax Limited alex.masidlover@zednax.com http://www.zednax.com =head1 COPYRIGHT This program is free software licensed under the... Alfresco Public License 1.0 The full text of the license can be found in the LICENSE file included with this module. Other licenses may be acceptable if including parts of Zymonic in larger projects, please contact Zednax for details. =head1 SEE ALSO perl(1). =cut #################### main pod documentation end ################### package Zymonic::Decryptor; use strict; use warnings; BEGIN { use Exporter (); use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = '0.01'; @ISA = qw(Exporter); #Give a hoot don't pollute, do not export more than needed by default @EXPORT = qw(); @EXPORT_OK = qw(); %EXPORT_TAGS = (); } use MIME::Base64; use Storable qw(nfreeze thaw); #################### subroutine header begin #################### =head2 new Usage : not to be called directly. Purpose : This is the constructor for the Zymonic Decryptor. Returns : a Zymonic::Decryptor object Argument : nothing Throws : nothing Comment : See Also : Zymonic::Decryptor::init =cut #################### subroutine header end #################### sub new { my $invocant = shift; my $class = ref($invocant) || $invocant; my $self = {@_}; # Remaining args become attributes $self->{zz_decryptor_fields} = ' hostname, port, d_posix_process_id, has_key, in_use, id, reenc_pid, nofork '; bless $self, $class; $self->init; return $self; } #################### subroutine header begin #################### =head2 init Usage : $zd->init Purpose : This is called by the constructor for the Zymonic Decryptor to perform initialisation tasks. Returns : nothing Argument : nothing Throws : Zymonic::Exception::Decryptor::MissingConfig Comment : See Also : Zymonic::Decryptor::new =cut #################### subroutine header end #################### sub init { my $self = shift; # Exception if no Config Zymonic::Exception::Decryptor::MissingConfig->throw() unless $self->{config}; } #################### subroutine header begin #################### =head2 load_key_file Usage : $zd->load_key_file($keytype, $keydir) Purpose : This loads a key file. Returns : the key Argument : the keytype (public or private) Throws : Zymonic::Exception::Decryptor::MissingKeyFile Comment : See Also : Zymonic::Decryptor::new =cut #################### subroutine header end #################### sub load_key_file { my $self = shift; my $keytype = shift || ''; my $keydir = shift || ''; my $keyversion = shift || ''; my $keyfile = ''; my $base64 = 0; if ( -f $keydir . '/' . $keytype . 'key' . $keyversion . '.txt' ) { $keyfile = $keydir . '/' . $keytype . 'key' . $keyversion . '.txt'; } elsif ( -f $keydir . '/' . $keytype . 'key' . $keyversion . '.bin' ) { $keyfile = $keydir . '/' . $keytype . 'key' . $keyversion . '.bin'; } elsif ( -f $keydir . '/' . $keytype . 'key' . $keyversion . '.base64' ) { $keyfile = $keydir . '/' . $keytype . 'key' . $keyversion . '.base64'; $base64 = 1; } else { Zymonic::Exception::Decryptor::MissingKeyFile->throw( keytype => $keytype ); } open( FLE, '<', $keyfile ); my $ret = ''; while () { $ret .= ( $base64 ? decode_base64($_) : $_ ); } close(FLE); return $ret; } #################### subroutine header begin #################### =head2 freeze_and_pack Usage : $zd->freeze_and_pack($message) Purpose : This uses pack and freeze to create a string suitable for sending over an SSL socket. Returns : a string suitable for sending over an SSL socket Argument : a hashref Throws : nothing Comment : See Also : Zymonic::Decryptor::new =cut #################### subroutine header end #################### sub freeze_and_pack { my $self = shift; my $message = shift; my $packet = nfreeze($message); $packet = pack( "N/a*", $packet ); return $packet; } #################### subroutine header begin #################### =head2 unpack_and_thaw Usage : $zd->unpack_and_thaw($socket) Purpose : This uses unpack and thaw to receive data from an SSL socket. Returns : a hashref Argument : a socket Throws : Zymonic::Exception::Decryptor::ReceiveFailed Comment : See Also : Zymonic::Decryptor::new =cut #################### subroutine header end #################### sub unpack_and_thaw { my $self = shift; my $handle = shift; Zymonic::Exception::Decryptor::ReceiveFailed->throw() unless 4 == read( $handle, my $buffer, 4 ); my $length = unpack( "N", $buffer ); Zymonic::Exception::Decryptor::ReceiveFailed->throw() unless $length == read( $handle, $buffer, $length ); return thaw($buffer); } 1;