| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | ## MIME Email Builder - ~/lib/Mail/Make/SMIME.pm | ||||||||||||||||||||||||||||
| 3 | ## Version v0.1.3 | ||||||||||||||||||||||||||||
| 4 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||||||||||||||||||||||||||||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||||||||||||||||||||
| 6 | ## Created 2026/05/11 | ||||||||||||||||||||||||||||
| 7 | ## Modified 2026/05/11 | ||||||||||||||||||||||||||||
| 8 | ## All rights reserved | ||||||||||||||||||||||||||||
| 9 | ## | ||||||||||||||||||||||||||||
| 10 | ## | ||||||||||||||||||||||||||||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 12 | ## under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 13 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 14 | package Mail::Make::SMIME; | ||||||||||||||||||||||||||||
| 15 | BEGIN | ||||||||||||||||||||||||||||
| 16 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | warnings::register_categories( 'Mail::Make' ); | |||||||||||||||||||||||||||
| 20 | 1 | ▶ | use parent qw( Module::Generic ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use vars qw( $VERSION $EXCEPTION_CLASS ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 1 | our $EXCEPTION_CLASS = 'Mail::Make::Exception'; | |||||||||||||||||||||||||||
| 23 | 1 | our $VERSION = 'v0.1.3'; | |||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # init( %opts ) | ||||||||||||||||||||||||||||
| 30 | # Initialises attributes. Accepted constructor options (all optional): | ||||||||||||||||||||||||||||
| 31 | # ca_cert => $pem_string_or_file CA certificate(s) for chain verification | ||||||||||||||||||||||||||||
| 32 | # cert => $pem_string_or_file Signer certificate (PEM) | ||||||||||||||||||||||||||||
| 33 | # key => $pem_string_or_file Private key (PEM) | ||||||||||||||||||||||||||||
| 34 | # key_password => $string_or_coderef Passphrase for encrypted private key | ||||||||||||||||||||||||||||
| 35 | sub init | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 38 | 0 | $self->{ca_cert} = undef; # PEM string or file path: CA cert(s) for verification | |||||||||||||||||||||||||||
| 39 | 0 | $self->{cert} = undef; # PEM string or file path: signer certificate | |||||||||||||||||||||||||||
| 40 | 0 | $self->{key} = undef; # PEM string or file path: private key | |||||||||||||||||||||||||||
| 41 | 0 | $self->{key_password} = undef; # string or CODE ref; undef = unencrypted key | |||||||||||||||||||||||||||
| 42 | 0 | $self->{_exception_class} = $EXCEPTION_CLASS; | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | return( $self ); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # ca_cert( [$pem_or_file] ) | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub ca_cert { return( shift->_set_get_scalar( 'ca_cert', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
ca_cert: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # cert( [$pem_or_file] ) | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub cert { return( shift->_set_get_scalar( 'cert', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
cert: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # encrypt( entity => $mail_make, RecipientCert => $cert_or_arrayref [, %opts] ) | ||||||||||||||||||||||||||||
| 54 | # Encrypts $mail_make for one or more recipients. Returns a new Mail::Make object whose | ||||||||||||||||||||||||||||
| 55 | # entity is a RFC 5751 application/pkcs7-mime enveloped message. | ||||||||||||||||||||||||||||
| 56 | # | ||||||||||||||||||||||||||||
| 57 | # Required options: | ||||||||||||||||||||||||||||
| 58 | # entity => Mail::Make object | ||||||||||||||||||||||||||||
| 59 | # RecipientCert => PEM string, file path, or arrayref of either | ||||||||||||||||||||||||||||
| 60 | # | ||||||||||||||||||||||||||||
| 61 | # Optional options: | ||||||||||||||||||||||||||||
| 62 | # Cipher => 'DES3' | 'AES128' | 'AES256' (default: AES256) | ||||||||||||||||||||||||||||
| 63 | sub encrypt | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
encrypt: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 66 | 0 | my $opts = $self->_get_args_as_hash( @_ ); | |||||||||||||||||||||||||||
| 67 | my $entity = $opts->{entity} || | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | return( $self->error( 'Entity option is required.' ) ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{'entity'} || (return $self->error('Entity option is required.'))
MC/DC0%0/1
$opts->{'entity'} || (return $self->error('Entity option is required.'))
$opts->{'entity'}
Truth table0%0/2
A
A= $opts->{'entity'}
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | my $recipient_cert = $opts->{RecipientCert} || | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | return( $self->error( 'RecipientCert option is required.' ) ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{'RecipientCert'} || (return $self->error('RecipientCert option is required.'))
MC/DC0%0/1
$opts->{'RecipientCert'} || (return $self->error('RecipientCert option is required.'))
$opts->{'RecipientCert'}
Truth table0%0/2
A
A= $opts->{'RecipientCert'}
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # Ensure Date and Message-ID exist before serialising | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | $self->_ensure_envelope_headers( $entity ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my $smime = $self->_make_crypt_smime || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->_make_crypt_smime || (return $self->pass_error)
MC/DC0%0/1
$self->_make_crypt_smime || (return $self->pass_error)
$self->_make_crypt_smime
Truth table0%0/2
A
A= $self->_make_crypt_smime
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Load recipient certificate(s) as public key(s) | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my @certs = ref( $recipient_cert ) eq 'ARRAY' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | ? @$recipient_cert | ||||||||||||||||||||||||||||
| 81 | : ( $recipient_cert ); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | my @pem_certs; | |||||||||||||||||||||||||||
| 84 | 0 | for my $cert ( @certs ) | |||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my $pem = $self->_read_pem( $cert ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->_read_pem($cert) || (return $self->pass_error)
MC/DC0%0/1
$self->_read_pem($cert) || (return $self->pass_error)
$self->_read_pem($cert)
Truth table0%0/2
A
A= $self->_read_pem($cert)
| |||||||||||||||||||||||||||||
| 87 | 0 | push( @pem_certs, $pem ); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | local $@; | |||||||||||||||||||||||||||
| 91 | 0 | eval{ $smime->setPublicKey( \@pem_certs ) }; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | return( $self->error( "Failed to load recipient certificate(s): $@" ) ) if( $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Serialise the full message | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | my $raw = $self->_serialise_for_smime( $entity ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->_serialise_for_smime($entity) || (return $self->pass_error)
MC/DC0%0/1
$self->_serialise_for_smime($entity) || (return $self->pass_error)
$self->_serialise_for_smime($entity)
Truth table0%0/2
A
A= $self->_serialise_for_smime($entity)
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | my $encrypted; | |||||||||||||||||||||||||||
| 98 | 0 | eval{ $encrypted = $smime->encrypt( $raw ) }; | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | return( $self->error( "Crypt::SMIME::encrypt() failed: $@" ) ) if( $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | unless( defined( $encrypted ) && CORE::length( $encrypted ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $encrypted and length $encrypted
MC/DC0%0/2
defined $encrypted and length $encrypted
defined $encrypted
length $encrypted
Truth table0%0/3
A and B
A= defined $encryptedB= length $encrypted
| |||||||||||||||||||||||||||||
| 101 | { | ||||||||||||||||||||||||||||
| 102 | 0 | return( $self->error( 'Crypt::SMIME returned empty result.' ) ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | return( $self->_build_from_smime_output( $entity, $encrypted ) ); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # key( [$pem_or_file] ) | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub key { return( shift->_set_get_scalar( 'key', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
key: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # key_password( [$string_or_coderef] ) | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub key_password { return( shift->_set_get_scalar( 'key_password', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
key_password: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # sign( entity => $mail_make [, %opts] ) | ||||||||||||||||||||||||||||
| 115 | # Signs $mail_make with a detached S/MIME signature. Returns a new Mail::Make | ||||||||||||||||||||||||||||
| 116 | # object whose entity is a RFC 5751 multipart/signed message. | ||||||||||||||||||||||||||||
| 117 | # | ||||||||||||||||||||||||||||
| 118 | # Required option (or set via constructor / accessors): | ||||||||||||||||||||||||||||
| 119 | # entity => Mail::Make object | ||||||||||||||||||||||||||||
| 120 | # Cert => PEM string or file path (overrides $self->{cert}) | ||||||||||||||||||||||||||||
| 121 | # Key => PEM string or file path (overrides $self->{key}) | ||||||||||||||||||||||||||||
| 122 | # | ||||||||||||||||||||||||||||
| 123 | # Optional options: | ||||||||||||||||||||||||||||
| 124 | # KeyPassword => string or CODE ref (overrides $self->{key_password}) | ||||||||||||||||||||||||||||
| 125 | # CACert => PEM string or file path | ||||||||||||||||||||||||||||
| 126 | sub sign | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
sign: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 129 | 0 | my $opts = $self->_get_args_as_hash( @_ ); | |||||||||||||||||||||||||||
| 130 | my $entity = $opts->{entity} || | ||||||||||||||||||||||||||||
| 131 | 0 | ▶ | return( $self->error( "The option 'entity' is required." ) ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{'entity'} || (return $self->error(q[The option 'entity' is required.]))
MC/DC0%0/1
$opts->{'entity'} || (return $self->error(q[The option 'entity' is required.]))
$opts->{'entity'}
Truth table0%0/2
A
A= $opts->{'entity'}
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | $self->_ensure_envelope_headers( $entity ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my $smime = $self->_make_crypt_smime || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->_make_crypt_smime || (return $self->pass_error)
MC/DC0%0/1
$self->_make_crypt_smime || (return $self->pass_error)
$self->_make_crypt_smime
Truth table0%0/2
A
A= $self->_make_crypt_smime
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | $self->_load_private_key( $smime, $opts ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | $self->_load_ca_cert( $smime, $opts ); # optional; ignore error | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | my $raw = $self->_serialise_for_smime( $entity ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->_serialise_for_smime($entity) || (return $self->pass_error)
MC/DC0%0/1
$self->_serialise_for_smime($entity) || (return $self->pass_error)
$self->_serialise_for_smime($entity)
Truth table0%0/2
A
A= $self->_serialise_for_smime($entity)
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | my $signed; | |||||||||||||||||||||||||||
| 144 | 0 | local $@; | |||||||||||||||||||||||||||
| 145 | 0 | eval{ $signed = $smime->sign( $raw ) }; | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | return( $self->error( "Crypt::SMIME::sign() failed: $@" ) ) if( $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | unless( defined( $signed ) && CORE::length( $signed ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $signed and length $signed
MC/DC0%0/2
defined $signed and length $signed
defined $signed
length $signed
Truth table0%0/3
A and B
A= defined $signedB= length $signed
| |||||||||||||||||||||||||||||
| 148 | { | ||||||||||||||||||||||||||||
| 149 | 0 | return( $self->error( 'Crypt::SMIME returned empty result.' ) ); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | return( $self->_build_from_smime_output( $entity, $signed ) ); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # sign_encrypt( entity => $mail_make, RecipientCert => $cert [, %opts] ) | ||||||||||||||||||||||||||||
| 156 | # Signs then encrypts $mail_make. Returns a new Mail::Make object. | ||||||||||||||||||||||||||||
| 157 | # | ||||||||||||||||||||||||||||
| 158 | # Required options: | ||||||||||||||||||||||||||||
| 159 | # entity => Mail::Make object | ||||||||||||||||||||||||||||
| 160 | # Cert => PEM string or file path | ||||||||||||||||||||||||||||
| 161 | # Key => PEM string or file path | ||||||||||||||||||||||||||||
| 162 | # RecipientCert => PEM string, file path, or arrayref | ||||||||||||||||||||||||||||
| 163 | # | ||||||||||||||||||||||||||||
| 164 | # Optional options: | ||||||||||||||||||||||||||||
| 165 | # KeyPassword => string or CODE ref | ||||||||||||||||||||||||||||
| 166 | # CACert => PEM string or file path | ||||||||||||||||||||||||||||
| 167 | # Cipher => 'DES3' | 'AES128' | 'AES256' | ||||||||||||||||||||||||||||
| 168 | sub sign_encrypt | ||||||||||||||||||||||||||||
| 169 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
sign_encrypt: not called CC=17 SCAR=57.2
Pod
documented
| |||||||||||||||||||||||||||||
| 170 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 171 | 0 | my $opts = $self->_get_args_as_hash( @_ ); | |||||||||||||||||||||||||||
| 172 | my $entity = $opts->{entity} || | ||||||||||||||||||||||||||||
| 173 | 0 | ▶ | return( $self->error( "The option 'entity' option is required." ) ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{'entity'} || (return $self->error(q[The option 'entity' option is required.]))
MC/DC0%0/1
$opts->{'entity'} || (return $self->error(q[The option 'entity' option is required.]))
$opts->{'entity'}
Truth table0%0/2
A
A= $opts->{'entity'}
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | $opts->{RecipientCert} || | ||||||||||||||||||||||||||||
| 176 | 0 | ▶ | return( $self->error( 'RecipientCert option is required.' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | $self->_ensure_envelope_headers( $entity ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | my $smime = $self->_make_crypt_smime || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->_make_crypt_smime || (return $self->pass_error)
MC/DC0%0/1
$self->_make_crypt_smime || (return $self->pass_error)
$self->_make_crypt_smime
Truth table0%0/2
A
A= $self->_make_crypt_smime
| |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | $self->_load_private_key( $smime, $opts ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | $self->_load_ca_cert( $smime, $opts ); # optional | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | # Load recipient certificate(s) | ||||||||||||||||||||||||||||
| 187 | my @certs = ref( $opts->{RecipientCert} ) eq 'ARRAY' | ||||||||||||||||||||||||||||
| 188 | 0 | ? @{$opts->{RecipientCert}} | |||||||||||||||||||||||||||
| 189 | 0 | ▶ | : ( $opts->{RecipientCert} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | my @pem_certs; | |||||||||||||||||||||||||||
| 192 | 0 | for my $cert ( @certs ) | |||||||||||||||||||||||||||
| 193 | { | ||||||||||||||||||||||||||||
| 194 | 0 | ▶ | my $pem = $self->_read_pem( $cert ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->_read_pem($cert) || (return $self->pass_error)
MC/DC0%0/1
$self->_read_pem($cert) || (return $self->pass_error)
$self->_read_pem($cert)
Truth table0%0/2
A
A= $self->_read_pem($cert)
| |||||||||||||||||||||||||||||
| 195 | 0 | push( @pem_certs, $pem ); | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 0 | local $@; | |||||||||||||||||||||||||||
| 199 | 0 | eval{ $smime->setPublicKey( \@pem_certs ) }; | |||||||||||||||||||||||||||
| 200 | 0 | ▶ | return( $self->error( "Failed to load recipient certificate(s): $@" ) ) if( $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | my $raw = $self->_serialise_for_smime( $entity ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->_serialise_for_smime($entity) || (return $self->pass_error)
MC/DC0%0/1
$self->_serialise_for_smime($entity) || (return $self->pass_error)
$self->_serialise_for_smime($entity)
Truth table0%0/2
A
A= $self->_serialise_for_smime($entity)
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # Crypt::SMIME has no signAndEncrypt() method. RFC 5751 sign-then-encrypt is | ||||||||||||||||||||||||||||
| 205 | # implemented by signing first, then encrypting the signed output. | ||||||||||||||||||||||||||||
| 206 | # The signed intermediate is a full RFC 2822 message string; we pass it directly to | ||||||||||||||||||||||||||||
| 207 | # encrypt() which operates on the same format. | ||||||||||||||||||||||||||||
| 208 | 0 | my $signed; | |||||||||||||||||||||||||||
| 209 | 0 | eval{ $signed = $smime->sign( $raw ) }; | |||||||||||||||||||||||||||
| 210 | 0 | ▶ | return( $self->error( "Crypt::SMIME::sign() failed: $@" ) ) if( $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 0 | ▶ | unless( defined( $signed ) && CORE::length( $signed ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $signed and length $signed
MC/DC0%0/2
defined $signed and length $signed
defined $signed
length $signed
Truth table0%0/3
A and B
A= defined $signedB= length $signed
| |||||||||||||||||||||||||||||
| 212 | { | ||||||||||||||||||||||||||||
| 213 | 0 | return( $self->error( 'Crypt::SMIME::sign() returned empty result.' ) ); | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # Re-load recipient public key(s) on a fresh instance for the encrypt step. | ||||||||||||||||||||||||||||
| 217 | # The same $smime object already has the private key loaded; calling setPublicKey() | ||||||||||||||||||||||||||||
| 218 | # again on it works, but to be explicit and avoid any state confusion we reuse $smime | ||||||||||||||||||||||||||||
| 219 | # (Crypt::SMIME accumulates public keys). | ||||||||||||||||||||||||||||
| 220 | 0 | my $result; | |||||||||||||||||||||||||||
| 221 | 0 | eval{ $result = $smime->encrypt( $signed ) }; | |||||||||||||||||||||||||||
| 222 | 0 | ▶ | return( $self->error( "Crypt::SMIME::encrypt() failed: $@" ) ) if( $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 0 | ▶ | unless( defined( $result ) && CORE::length( $result ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $result and length $result
MC/DC0%0/2
defined $result and length $result
defined $result
length $result
Truth table0%0/3
A and B
A= defined $resultB= length $result
| |||||||||||||||||||||||||||||
| 224 | { | ||||||||||||||||||||||||||||
| 225 | 0 | return( $self->error( 'Crypt::SMIME::encrypt() returned empty result.' ) ); | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 0 | return( $self->_build_from_smime_output( $entity, $result ) ); | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | # _build_from_smime_output( $original_mail, $smime_string ) → Mail::Make | ||||||||||||||||||||||||||||
| 232 | # Parses the S/MIME output string from Crypt::SMIME (which already contains all the correct | ||||||||||||||||||||||||||||
| 233 | # headers) into a new Mail::Make object that smtpsend() can use directly. | ||||||||||||||||||||||||||||
| 234 | # | ||||||||||||||||||||||||||||
| 235 | # Crypt::SMIME::sign() and encrypt() return a fully formed RFC 2822 message string. We | ||||||||||||||||||||||||||||
| 236 | # wrap it in a Mail::Make object by parsing it into an Entity and storing it as | ||||||||||||||||||||||||||||
| 237 | # _smime_entity, mirroring what _gpg_entity does for GPG. | ||||||||||||||||||||||||||||
| 238 | sub _build_from_smime_output | ||||||||||||||||||||||||||||
| 239 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_build_from_smime_output: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 240 | 0 | my( $self, $original, $smime_str ) = @_; | |||||||||||||||||||||||||||
| 241 | 0 | require Mail::Make; | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | # Canonicalise line endings to CRLF | ||||||||||||||||||||||||||||
| 244 | 0 | ( my $canon = $smime_str ) =~ s/\015?\012/\015\012/g; | |||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | # Locate the header / body separator | ||||||||||||||||||||||||||||
| 247 | 0 | my $pos = index( $canon, "\015\012\015\012" ); | |||||||||||||||||||||||||||
| 248 | 0 | ▶ | if( $pos < 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | { | ||||||||||||||||||||||||||||
| 250 | 0 | return( $self->error( 'No header/body separator in Crypt::SMIME output.' ) ); | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | # Parse outer headers into a plain hash (case-insensitive, last-value wins for duplicates) | ||||||||||||||||||||||||||||
| 254 | # so that the structure test can call headers->get(). | ||||||||||||||||||||||||||||
| 255 | 0 | my $hdr_block = substr( $canon, 0, $pos + 2 ); | |||||||||||||||||||||||||||
| 256 | 0 | my %hdrs; | |||||||||||||||||||||||||||
| 257 | 0 | my $cur_name = ''; | |||||||||||||||||||||||||||
| 258 | 0 | my $cur_value = ''; | |||||||||||||||||||||||||||
| 259 | 0 | for my $line ( split( /(?<=\015\012)/, $hdr_block ) ) | |||||||||||||||||||||||||||
| 260 | { | ||||||||||||||||||||||||||||
| 261 | 0 | ▶ | if( $line =~ /^[ \t]/ ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 262 | { | ||||||||||||||||||||||||||||
| 263 | 0 | ( my $cont = $line ) =~ s/^\015\012$//; # strip trailing CRLF | |||||||||||||||||||||||||||
| 264 | 0 | ▶ | $cur_value .= $line if( CORE::length( $cur_name ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | elsif( $line =~ /^([\x21-\x39\x3B-\x7E]+):\s*(.*?)\015\012$/ ) | ||||||||||||||||||||||||||||
| 267 | { | ||||||||||||||||||||||||||||
| 268 | 0 | ▶ | $hdrs{ $cur_name } = $cur_value if( CORE::length( $cur_name ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 0 | ( $cur_name, $cur_value ) = ( $1, $2 ); | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | 0 | ▶ | $hdrs{ $cur_name } = $cur_value if( CORE::length( $cur_name ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | # _RawEntity wraps the complete Crypt::SMIME output string and exposes just enough of | ||||||||||||||||||||||||||||
| 275 | # the Entity interface for smtpsend() and the test suite: | ||||||||||||||||||||||||||||
| 276 | # headers->get( $name ) - used by structure tests | ||||||||||||||||||||||||||||
| 277 | # headers->remove( $name ) - called by smtpsend() to strip Bcc | ||||||||||||||||||||||||||||
| 278 | # as_string() - called by smtpsend() for SMTP DATA | ||||||||||||||||||||||||||||
| 279 | # | ||||||||||||||||||||||||||||
| 280 | # We deliberately do NOT subclass Mail::Make::Entity here. Entity::print_body | ||||||||||||||||||||||||||||
| 281 | # branches on is_multipart() and iterates _parts (which would be empty), producing a | ||||||||||||||||||||||||||||
| 282 | # message with an empty body. Bypassing Entity entirely is the correct fix. | ||||||||||||||||||||||||||||
| 283 | 0 | my $entity = Mail::Make::SMIME::_RawEntity->new( \%hdrs, $canon ); | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | # Build the wrapper Mail::Make object | ||||||||||||||||||||||||||||
| 286 | 0 | ▶ | my $new = Mail::Make->new || | ||||||||||||||||||||||||||
|
Condition0%0/2
'Mail::Make'->new || (return $self->pass_error('Mail::Make'->error))
MC/DC0%0/1
'Mail::Make'->new || (return $self->pass_error('Mail::Make'->error))
'Mail::Make'->new
Truth table0%0/2
A
A= 'Mail::Make'->new
| |||||||||||||||||||||||||||||
| 287 | return( $self->pass_error( Mail::Make->error ) ); | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | # Copy envelope headers (From, To, Subject, Date, Message-ID …) from the original | ||||||||||||||||||||||||||||
| 290 | # Mail::Make object so that smtpsend() can derive the SMTP envelope | ||||||||||||||||||||||||||||
| 291 | # (MAIL FROM / RCPT TO) without inspecting the entity. | ||||||||||||||||||||||||||||
| 292 | $original->headers->scan( sub | ||||||||||||||||||||||||||||
| 293 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 294 | 0 | my( $name, $value ) = @_; | |||||||||||||||||||||||||||
| 295 | 0 | $new->headers->set( $name => $value ); | |||||||||||||||||||||||||||
| 296 | 0 | return(1); | |||||||||||||||||||||||||||
| 297 | 0 | }); | |||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | # Store pre-assembled entity; as_entity() in Mail::Make returns it directly via the | ||||||||||||||||||||||||||||
| 300 | # _smime_entity hook. | ||||||||||||||||||||||||||||
| 301 | 0 | $new->{_smime_entity} = $entity; | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 0 | return( $new ); | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | # _ensure_envelope_headers( $mail_make_obj ) | ||||||||||||||||||||||||||||
| 307 | # Generates Date and Message-ID on the Mail::Make object without calling as_entity(), to | ||||||||||||||||||||||||||||
| 308 | # avoid polluting $self->{_parts}[0] with RFC 2822 headers. | ||||||||||||||||||||||||||||
| 309 | sub _ensure_envelope_headers | ||||||||||||||||||||||||||||
| 310 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_ensure_envelope_headers: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 311 | 0 | my( $self, $mail ) = @_; | |||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 0 | ▶ | unless( $mail->{_headers}->exists( 'Date' ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 314 | { | ||||||||||||||||||||||||||||
| 315 | $mail->{_headers}->init_header( Date => $mail->_format_date ) || | ||||||||||||||||||||||||||||
| 316 | 0 | ▶ | return( $self->pass_error( $mail->{_headers}->error ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 0 | ▶ | unless( $mail->{_headers}->exists( 'Message-ID' ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | { | ||||||||||||||||||||||||||||
| 321 | $mail->{_headers}->message_id( | ||||||||||||||||||||||||||||
| 322 | { generate => 1, domain => $mail->_default_domain } | ||||||||||||||||||||||||||||
| 323 | 0 | ▶ | ) || return( $self->pass_error( $mail->{_headers}->error ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 0 | return(1); | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | # _load_ca_cert( $smime_obj, \%opts ) | ||||||||||||||||||||||||||||
| 330 | # Loads the CA certificate into a Crypt::SMIME instance for chain verification. | ||||||||||||||||||||||||||||
| 331 | # Source priority: option CACert > constructor ca_cert. | ||||||||||||||||||||||||||||
| 332 | # Silently returns 1 if no CA cert is provided (CA cert is optional for signing). | ||||||||||||||||||||||||||||
| 333 | sub _load_ca_cert | ||||||||||||||||||||||||||||
| 334 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_load_ca_cert: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 335 | 0 | my( $self, $smime, $opts_ref ) = @_; | |||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 0 | ▶ | my $source = $opts_ref->{CACert} // $self->{ca_cert}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$opts_ref->{"CACert"} // $self->{"ca_cert"}
MC/DC0%0/2
$opts_ref->{"CACert"} // $self->{"ca_cert"}
$opts_ref->{"CACert"}
$self->{"ca_cert"}
Truth table0%0/3
A // B
A= $opts_ref->{"CACert"}B= $self->{"ca_cert"}
| |||||||||||||||||||||||||||||
| 338 | 0 | ▶ | return(1) unless( defined( $source ) && CORE::length( $source ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $source and length $source
MC/DC0%0/2
defined $source and length $source
defined $source
length $source
Truth table0%0/3
A and B
A= defined $sourceB= length $source
| |||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 0 | ▶ | my $pem = $self->_read_pem( $source ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->_read_pem($source) || (return $self->pass_error)
MC/DC0%0/1
$self->_read_pem($source) || (return $self->pass_error)
$self->_read_pem($source)
Truth table0%0/2
A
A= $self->_read_pem($source)
| |||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 0 | local $@; | |||||||||||||||||||||||||||
| 343 | 0 | eval{ $smime->setPublicKey( [$pem] ) }; | |||||||||||||||||||||||||||
| 344 | 0 | ▶ | return( $self->error( "Failed to load CA certificate: $@" ) ) if( $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 0 | return(1); | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | # _load_private_key( $smime_obj, \%opts ) | ||||||||||||||||||||||||||||
| 350 | # Loads the private key and signing certificate into a Crypt::SMIME instance. | ||||||||||||||||||||||||||||
| 351 | # Source priority: option Cert/Key > constructor cert/key. | ||||||||||||||||||||||||||||
| 352 | # Handles key_password as string or CODE ref. | ||||||||||||||||||||||||||||
| 353 | sub _load_private_key | ||||||||||||||||||||||||||||
| 354 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_load_private_key: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||
| 355 | 0 | my( $self, $smime, $opts_ref ) = @_; | |||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 0 | ▶ | my $cert_source = $opts_ref->{Cert} // $self->{cert}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$opts_ref->{"Cert"} // $self->{"cert"}
MC/DC0%0/2
$opts_ref->{"Cert"} // $self->{"cert"}
$opts_ref->{"Cert"}
$self->{"cert"}
Truth table0%0/3
A // B
A= $opts_ref->{"Cert"}B= $self->{"cert"}
| |||||||||||||||||||||||||||||
| 358 | 0 | ▶ | my $key_source = $opts_ref->{Key} // $self->{key}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$opts_ref->{"Key"} // $self->{"key"}
MC/DC0%0/2
$opts_ref->{"Key"} // $self->{"key"}
$opts_ref->{"Key"}
$self->{"key"}
Truth table0%0/3
A // B
A= $opts_ref->{"Key"}B= $self->{"key"}
| |||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 0 | ▶ | unless( defined( $cert_source ) && CORE::length( $cert_source ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $cert_source and length $cert_source
MC/DC0%0/2
defined $cert_source and length $cert_source
defined $cert_source
length $cert_source
Truth table0%0/3
A and B
A= defined $cert_sourceB= length $cert_source
| |||||||||||||||||||||||||||||
| 361 | { | ||||||||||||||||||||||||||||
| 362 | 0 | return( $self->error( 'No certificate provided. Set Cert option or cert() accessor.' ) ); | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 0 | ▶ | unless( defined( $key_source ) && CORE::length( $key_source ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $key_source and length $key_source
MC/DC0%0/2
defined $key_source and length $key_source
defined $key_source
length $key_source
Truth table0%0/3
A and B
A= defined $key_sourceB= length $key_source
| |||||||||||||||||||||||||||||
| 366 | { | ||||||||||||||||||||||||||||
| 367 | 0 | return( $self->error( 'No private key provided. Set Key option or key() accessor.' ) ); | |||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 0 | ▶ | my $cert_pem = $self->_read_pem( $cert_source ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->_read_pem($cert_source) || (return $self->pass_error)
MC/DC0%0/1
$self->_read_pem($cert_source) || (return $self->pass_error)
$self->_read_pem($cert_source)
Truth table0%0/2
A
A= $self->_read_pem($cert_source)
| |||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 0 | ▶ | my $key_pem = $self->_read_pem( $key_source ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->_read_pem($key_source) || (return $self->pass_error)
MC/DC0%0/1
$self->_read_pem($key_source) || (return $self->pass_error)
$self->_read_pem($key_source)
Truth table0%0/2
A
A= $self->_read_pem($key_source)
| |||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | # Resolve key password | ||||||||||||||||||||||||||||
| 375 | 0 | ▶ | my $password_src = $opts_ref->{KeyPassword} // $self->{key_password}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$opts_ref->{"KeyPassword"} // $self->{"key_password"}
MC/DC0%0/2
$opts_ref->{"KeyPassword"} // $self->{"key_password"}
$opts_ref->{"KeyPassword"}
$self->{"key_password"}
Truth table0%0/3
A // B
A= $opts_ref->{"KeyPassword"}B= $self->{"key_password"}
| |||||||||||||||||||||||||||||
| 376 | 0 | my $password; | |||||||||||||||||||||||||||
| 377 | 0 | ▶ | if( defined( $password_src ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 378 | { | ||||||||||||||||||||||||||||
| 379 | 0 | ▶ | if( ref( $password_src ) eq 'CODE' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | { | ||||||||||||||||||||||||||||
| 381 | 0 | local $@; | |||||||||||||||||||||||||||
| 382 | 0 | $password = eval{ $password_src->() }; | |||||||||||||||||||||||||||
| 383 | 0 | ▶ | return( $self->error( "KeyPassword CODE ref died: $@" ) ) if( $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | else | ||||||||||||||||||||||||||||
| 386 | { | ||||||||||||||||||||||||||||
| 387 | 0 | $password = $password_src; | |||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 0 | local $@; | |||||||||||||||||||||||||||
| 392 | 0 | ▶ | if( defined( $password ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | { | ||||||||||||||||||||||||||||
| 394 | 0 | eval{ $smime->setPrivateKey( $key_pem, $cert_pem, $password ) }; | |||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | else | ||||||||||||||||||||||||||||
| 397 | { | ||||||||||||||||||||||||||||
| 398 | 0 | eval{ $smime->setPrivateKey( $key_pem, $cert_pem ) }; | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | 0 | ▶ | return( $self->error( "Failed to load private key/certificate: $@" ) ) if( $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 0 | return(1); | |||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | # _make_crypt_smime() → Crypt::SMIME instance | ||||||||||||||||||||||||||||
| 406 | # Loads Crypt::SMIME and returns a new instance, with a clear error if the module is not | ||||||||||||||||||||||||||||
| 407 | # installed. | ||||||||||||||||||||||||||||
| 408 | sub _make_crypt_smime | ||||||||||||||||||||||||||||
| 409 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_make_crypt_smime: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 410 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 411 | 0 | ▶ | $self->_load_class( 'Crypt::SMIME' ) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 412 | return( $self->error( 'Crypt::SMIME is required for S/MIME operations. Install it with: cpan Crypt::SMIME' ) ); | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 0 | my $smime; | |||||||||||||||||||||||||||
| 415 | 0 | eval{ $smime = Crypt::SMIME->new }; | |||||||||||||||||||||||||||
| 416 | 0 | ▶ | return( $self->error( "Failed to instantiate Crypt::SMIME: $@" ) ) if( $@ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | 0 | return( $smime ); | |||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | # _read_pem( $source ) → $pem_string | ||||||||||||||||||||||||||||
| 422 | # Accepts either a PEM string (contains '-----BEGIN') or a file path and returns the PEM | ||||||||||||||||||||||||||||
| 423 | # content as a string. Dies gracefully with a proper error. | ||||||||||||||||||||||||||||
| 424 | sub _read_pem | ||||||||||||||||||||||||||||
| 425 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_read_pem: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 426 | 0 | my( $self, $source ) = @_; | |||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | 0 | ▶ | unless( defined( $source ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 429 | { | ||||||||||||||||||||||||||||
| 430 | 0 | return( $self->error( 'Undefined source.' ) ); | |||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | # Already a PEM string | ||||||||||||||||||||||||||||
| 434 | 0 | ▶ | return( $source ) if( $source =~ /-----BEGIN/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | # File path | ||||||||||||||||||||||||||||
| 437 | 0 | ▶ | unless( -f $source ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 438 | { | ||||||||||||||||||||||||||||
| 439 | 0 | return( $self->error( "File not found: $source" ) ); | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | 0 | ▶ | unless( -r $source ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 443 | { | ||||||||||||||||||||||||||||
| 444 | 0 | return( $self->error( "File not readable: $source" ) ); | |||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 0 | ▶ | open( my $fh, '<', $source ) || | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 448 | return( $self->error( "Cannot open '$source': $!" ) ); | ||||||||||||||||||||||||||||
| 449 | 0 | local $/; | |||||||||||||||||||||||||||
| 450 | 0 | my $pem = <$fh>; | |||||||||||||||||||||||||||
| 451 | 0 | close( $fh ); | |||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | 0 | ▶ | unless( defined( $pem ) && $pem =~ /-----BEGIN/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $pem and $pem =~ /-----BEGIN/
MC/DC0%0/2
defined $pem and $pem =~ /-----BEGIN/
defined $pem
$pem =~ /-----BEGIN/
Truth table0%0/3
A and B
A= defined $pemB= $pem =~ /-----BEGIN/
| |||||||||||||||||||||||||||||
| 454 | { | ||||||||||||||||||||||||||||
| 455 | 0 | return( $self->error( "File '$source' does not contain PEM data." ) ); | |||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | 0 | return( $pem ); | |||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | # _serialise_for_smime( $mail_make_obj ) → $string | ||||||||||||||||||||||||||||
| 462 | # Serialises the Mail::Make object to a full RFC 2822 message string | ||||||||||||||||||||||||||||
| 463 | # (headers + body, CRLF line endings). | ||||||||||||||||||||||||||||
| 464 | # Unlike _serialise_for_gpg, we pass the COMPLETE message to Crypt::SMIME; it handles | ||||||||||||||||||||||||||||
| 465 | # RFC 5751 header separation internally. | ||||||||||||||||||||||||||||
| 466 | sub _serialise_for_smime | ||||||||||||||||||||||||||||
| 467 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_serialise_for_smime: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 468 | 0 | my( $self, $mail ) = @_; | |||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | 0 | ▶ | unless( defined( $mail ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 471 | { | ||||||||||||||||||||||||||||
| 472 | 0 | return( $self->error( 'No Mail::Make object supplied.' ) ); | |||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 0 | ▶ | unless( $mail->can( 'as_entity' ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 476 | { | ||||||||||||||||||||||||||||
| 477 | 0 | return( $self->error( 'Argument must be a Mail::Make object.' ) ); | |||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 0 | ▶ | my $entity = $mail->as_entity || return( $self->pass_error( $mail->error ) ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$mail->as_entity || (return $self->pass_error($mail->error))
MC/DC0%0/1
$mail->as_entity || (return $self->pass_error($mail->error))
$mail->as_entity
Truth table0%0/2
A
A= $mail->as_entity
| |||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | 0 | ▶ | my $full = $entity->as_string || return( $self->pass_error( $entity->error ) ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$entity->as_string || (return $self->pass_error($entity->error))
MC/DC0%0/1
$entity->as_string || (return $self->pass_error($entity->error))
$entity->as_string
Truth table0%0/2
A
A= $entity->as_string
| |||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | # Canonicalise line endings to CRLF | ||||||||||||||||||||||||||||
| 485 | 0 | $full =~ s/\015?\012/\015\012/g; | |||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | 0 | return( $full ); | |||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | # STORABLE_freeze / STORABLE_thaw both satisfy Module::Generic serialisation hooks | ||||||||||||||||||||||||||||
| 491 | 0 | ▶ | sub STORABLE_freeze { return( $_[0] ) } | ||||||||||||||||||||||||||
|
Subroutine
STORABLE_freeze: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | 0 | ▶ | sub STORABLE_thaw { return( $_[0] ) } | ||||||||||||||||||||||||||
|
Subroutine
STORABLE_thaw: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | # NOTE: package Mail::Make::SMIME::_RawEntity | ||||||||||||||||||||||||||||
| 497 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 498 | ## Mail::Make::SMIME::_RawEntity | ||||||||||||||||||||||||||||
| 499 | ## Lightweight entity wrapper for Crypt::SMIME output strings. | ||||||||||||||||||||||||||||
| 500 | ## | ||||||||||||||||||||||||||||
| 501 | ## Exposes just enough of the Mail::Make::Entity interface to satisfy | ||||||||||||||||||||||||||||
| 502 | ## Mail::Make::smtpsend() and the test suite: | ||||||||||||||||||||||||||||
| 503 | ## | ||||||||||||||||||||||||||||
| 504 | ## headers->get( $name ) - returns the header value | ||||||||||||||||||||||||||||
| 505 | ## headers->remove( $name ) - removes a header (no-op if absent) | ||||||||||||||||||||||||||||
| 506 | ## as_string() - returns the complete RFC 2822 message verbatim | ||||||||||||||||||||||||||||
| 507 | ## | ||||||||||||||||||||||||||||
| 508 | ## We deliberately bypass Mail::Make::Entity because Entity::print_body() | ||||||||||||||||||||||||||||
| 509 | ## branches on is_multipart() and iterates _parts. For a multipart/signed | ||||||||||||||||||||||||||||
| 510 | ## entity the _parts array would be empty, producing a message with only a | ||||||||||||||||||||||||||||
| 511 | ## closing boundary and no body. Storing the raw Crypt::SMIME string and | ||||||||||||||||||||||||||||
| 512 | ## emitting it verbatim is the correct approach. | ||||||||||||||||||||||||||||
| 513 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 514 | # Hide it from CPAN | ||||||||||||||||||||||||||||
| 515 | package | ||||||||||||||||||||||||||||
| 516 | Mail::Make::SMIME::_RawEntity; | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 519 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | # new( \%headers, $raw_string ) → _RawEntity | ||||||||||||||||||||||||||||
| 522 | sub new | ||||||||||||||||||||||||||||
| 523 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 524 | 0 | my( $class, $hdrs_ref, $raw ) = @_; | |||||||||||||||||||||||||||
| 525 | return( bless( | ||||||||||||||||||||||||||||
| 526 | { | ||||||||||||||||||||||||||||
| 527 | 0 | _hdrs => { map { lc( $_ ) => $hdrs_ref->{ $_ } } keys( %$hdrs_ref ) }, | |||||||||||||||||||||||||||
| 528 | _raw => $raw, | ||||||||||||||||||||||||||||
| 529 | }, $class ) ); | ||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | # as_string() → the complete RFC 2822 message string (CRLF line endings) | ||||||||||||||||||||||||||||
| 533 | 0 | ▶ | sub as_string { return( $_[0]->{_raw} ) } | ||||||||||||||||||||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | # headers() → a _RawHeaders proxy object | ||||||||||||||||||||||||||||
| 536 | sub headers | ||||||||||||||||||||||||||||
| 537 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
headers: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 538 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 539 | 0 | return( Mail::Make::SMIME::_RawHeaders->new( $self->{_hdrs} ) ); | |||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | # NOTE: package Mail::Make::SMIME::_RawHeaders | ||||||||||||||||||||||||||||
| 543 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 544 | ## Mail::Make::SMIME::_RawHeaders | ||||||||||||||||||||||||||||
| 545 | ## Minimal headers proxy used by _RawEntity. | ||||||||||||||||||||||||||||
| 546 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 547 | # Hide it from CPAN | ||||||||||||||||||||||||||||
| 548 | package | ||||||||||||||||||||||||||||
| 549 | Mail::Make::SMIME::_RawHeaders; | ||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 552 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | sub new | ||||||||||||||||||||||||||||
| 555 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 556 | 0 | my( $class, $hdrs_ref ) = @_; | |||||||||||||||||||||||||||
| 557 | 0 | return( bless( { _h => $hdrs_ref }, $class ) ); | |||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | # get( $name ) → value string or undef | ||||||||||||||||||||||||||||
| 561 | sub get | ||||||||||||||||||||||||||||
| 562 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 563 | 0 | my( $self, $name ) = @_; | |||||||||||||||||||||||||||
| 564 | 0 | return( $self->{_h}->{ lc( $name ) } ); | |||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | # remove( $name ) → removes the header (no-op if absent) | ||||||||||||||||||||||||||||
| 568 | sub remove | ||||||||||||||||||||||||||||
| 569 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
remove: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 570 | 0 | my( $self, $name ) = @_; | |||||||||||||||||||||||||||
| 571 | 0 | delete( $self->{_h}->{ lc( $name ) } ); | |||||||||||||||||||||||||||
| 572 | 0 | return( $self ); | |||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | 1 | 1; | |||||||||||||||||||||||||||
| 576 | # NOTE: POD | ||||||||||||||||||||||||||||
| 577 | __END__ | ||||||||||||||||||||||||||||