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