Proxy-Impersonate
view release on metacpan or search on metacpan
lib/Proxy/Impersonate/Cert.pm view on Meta::CPAN
Net::SSLeay::X509_NAME_add_entry_by_txt(
Net::SSLeay::X509_get_subject_name($x), 'CN', &Net::SSLeay::MBSTRING_UTF8, $cn, -1, 0);
Net::SSLeay::X509_set_issuer_name($x, Net::SSLeay::X509_get_subject_name($x)); # self
Net::SSLeay::P_X509_add_extensions($x, $x,
&Net::SSLeay::NID_basic_constraints => 'CA:FALSE',
&Net::SSLeay::NID_subject_alt_name => $san,
&Net::SSLeay::NID_key_usage => 'digitalSignature,keyEncipherment',
&Net::SSLeay::NID_ext_key_usage => 'serverAuth');
Net::SSLeay::X509_sign($x, $pkey, Net::SSLeay::EVP_sha256());
return $x;
}
sub _load_or_generate {
my ($self) = @_;
my ($cf, $kf) = ($self->cert_path, $self->key_path);
if (-e $cf || -l $cf || -e $kf || -l $kf) {
Carp::croak('Cert: incomplete certificate/key pair') unless -r $cf && -r $kf;
# Adopting a key we did not write is the interesting case: cert_dir is a
# 0700 File::Temp dir by default, but the option exists precisely so a
# caller can persist the cert somewhere of their choosing -- and a
# shared location is where this goes wrong. A key that is group- or
# world-accessible, or owned by somebody else, means whoever put it
# there can impersonate this proxy to the browser it fronts (which is
# configured to accept its certificate). Refuse rather than adopt it.
# Symlink-safe: lstat, so a symlink to a 0600 file is judged on the
# LINK, and O_NOFOLLOW-equivalent semantics apply to the check.
my @st = lstat $kf;
if (@st) {
my ($mode, $uid) = @st[2, 4];
Carp::croak("Cert: refusing to use $kf -- it is a symlink")
if -l _;
Carp::croak("Cert: refusing to use $kf -- not a regular file") unless -f _;
Carp::croak(sprintf 'Cert: refusing to use %s -- mode %04o is accessible '
. 'beyond its owner (want 0600)', $kf, $mode & 07777)
if $mode & 077;
Carp::croak("Cert: refusing to use $kf -- it is owned by uid $uid, not $<")
if $uid != $<;
}
my $bc = Net::SSLeay::BIO_new_file($cf, 'r') or Carp::croak("Cert: read $cf failed");
$self->{cert} = Net::SSLeay::PEM_read_bio_X509($bc);
Net::SSLeay::BIO_free($bc);
Carp::croak("Cert: invalid certificate in $cf") unless $self->{cert};
my $bk = Net::SSLeay::BIO_new_file($kf, 'r') or Carp::croak("Cert: read $kf failed");
$self->{key} = Net::SSLeay::PEM_read_bio_PrivateKey($bk);
Net::SSLeay::BIO_free($bk);
Carp::croak("Cert: invalid private key in $kf") unless $self->{key};
return;
}
my $key = _gen_key();
my $cert = _mk_self_signed('Proxy::Impersonate', $key, 'DNS:localhost');
$self->{key} = $key; $self->{cert} = $cert;
_spew($cf, Net::SSLeay::PEM_get_string_X509($cert), 0644);
_spew($kf, Net::SSLeay::PEM_get_string_PrivateKey($key), 0600);
}
# Use the validated in-memory pair for every handshake.
sub apply_to_ctx {
my ($self, $ctx) = @_;
Net::SSLeay::CTX_use_certificate($ctx, $self->{cert})
&& Net::SSLeay::CTX_use_PrivateKey($ctx, $self->{key})
&& Net::SSLeay::CTX_check_private_key($ctx)
or Carp::croak('Cert: invalid or mismatched certificate/key pair');
}
sub _spew {
my ($path, $data, $mode) = @_;
$mode //= 0644;
# create at the target mode so the private key never exists world-readable
# in an open->chmod window
sysopen(my $fh, $path, O_CREAT | O_WRONLY | O_EXCL, $mode)
or Carp::croak("Cert: write $path: $!");
my $ok = print $fh $data;
$ok = close($fh) && $ok;
if (!$ok) {
my $err = $!;
unlink $path;
Carp::croak("Cert: write $path: $err");
}
}
sub DESTROY {
my ($self) = @_;
Net::SSLeay::X509_free($self->{cert}) if $self->{cert};
Net::SSLeay::EVP_PKEY_free($self->{key}) if $self->{key}; # frees the assigned RSA too
}
1;
__END__
=head1 NAME
Proxy::Impersonate::Cert - the self-signed cert the proxy terminates TLS with
=head1 DESCRIPTION
Generates (once) and persists a self-signed CA-ish certificate under the
proxy's C<cert_dir>, and installs it into an OpenSSL context. Used by
L<Proxy::Impersonate::Connection>. You do not normally touch it directly.
Because the proxy MITMs the client's TLS, the client must be told to accept
this certificate -- L<EV::WebKit> does that with
C<set_tls_errors_policy('ignore')>, which is only reasonable because the
browser-to-proxy hop is loopback. See L<Proxy::Impersonate/TRUST MODEL>.
=cut
( run in 0.722 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )