| 1 | |||
| 2 | use v5.26; | ||
| 3 | use warnings; | ||
| 4 | use experimental 'signatures'; | ||
| 5 | use Future::AsyncAwait; | ||
| 6 | use Object::Pad ':experimental(inherit_field)'; | ||
| 7 | |||
| 8 | class Sys::Async::Virt::Connection::TLS v0.0.4; | ||
| 9 | |||
| 10 | inherit Sys::Async::Virt::Connection::TCP '$_socket', '$_url'; | ||
| 11 | |||
| 12 | use Carp qw(croak); | ||
| 13 | use Future::IO::TLS; | ||
| 14 | use Log::Any qw($log); | ||
| 15 | |||
| 16 | use Crypt::OpenSSL3::SSL; | ||
| 17 | use Crypt::OpenSSL3::SSL::Context; | ||
| 18 | use Protocol::Sys::Virt::URI; # imports parse_url | ||
| 19 | |||
| 20 | field $_tls = undef; | ||
| 21 | field $_need_tls_confirm = 1; | ||
| 22 | |||
| 23 | method _default_port() { | ||
| 24 | return 16514; | ||
| 25 | } | ||
| 26 | |||
| 27 | async method connect() { | ||
| 28 | # disect URL | ||
| 29 | my %components = parse_url( $_url ); | ||
| 30 | |||
| 31 | await $self->SUPER::connect(); | ||
| 32 | |||
| 33 | my $no_verify = $components{query}->{no_verify}; | ||
| 34 | my $pkipath = $components{query}->{pkipath}; | ||
| 35 | my $tls_priority = $components{query}->{tls_priority}; | ||
| 36 | my $cacert; | ||
| 37 | my $clientcert; | ||
| 38 | my $clientkey; | ||
| 39 | |||
| 40 | if ($pkipath) { | ||
| 41 | $cacert = "$pkipath/cacert.pem"; | ||
| 42 | $clientcert = "$pkipath/clientcert.pem"; | ||
| 43 | $clientkey = "$pkipath/clientkey.pem"; | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | if ($> != 0) { | ||
| 47 | if (-r "$ENV{HOME}/.pki/cacert.pem") { | ||
| 48 | $cacert = "$ENV{HOME}/.pki/cacert.pem"; | ||
| 49 | } | ||
| 50 | if (-r "$ENV{HOME}/.pki/libvirt/clientcert.pem" | ||
| 51 | and -r "$ENV{HOME}/.pki/libvirt/clientkey.pem") { | ||
| 52 | $clientcert = "$ENV{HOME}/.pki/libvirt/clientcert.pem"; | ||
| 53 | $clientkey = "$ENV{HOME}/.pki/libvirt/clientkey.pem"; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | $cacert = '/etc/pki/CA/cacert.pem'; | ||
| 57 | $clientcert = '/etc/pki/libvirt/clientcert.pem'; | ||
| 58 | $clientkey = '/etc/pki/libvirt/clientkey.pem'; | ||
| 59 | } | ||
| 60 | |||
| 61 | my $ctx = Crypt::OpenSSL3::SSL::Context->new; | ||
| 62 | $ctx->load_verify_file( $cacert );; | ||
| 63 | $ctx->use_PrivateKey_file( $clientkey, Crypt::OpenSSL3::SSL::FILETYPE_PEM ); | ||
| 64 | $ctx->use_certificate_file( $clientcert, Crypt::OpenSSL3::SSL::FILETYPE_PEM ); | ||
| 65 | |||
| 66 | $_tls = await Future::IO::TLS->start_TLS( | ||
| 67 | $_socket, | ||
| 68 | hostname => $components{host}, | ||
| 69 | context => $ctx, | ||
| 70 | ); | ||
| 71 | } | ||
| 72 | |||
| 73 | async method _read_internal( $len ) { | ||
| 74 | # Once we can have transitive inheritance, we can | ||
| 75 | # add the first-byte verification to $_read_f in | ||
| 76 | # connect(). | ||
| 77 | if ($_need_tls_confirm) { | ||
| 78 | $_need_tls_confirm = 0; | ||
| 79 | my $buf = await $_tls->read( $_socket, 1 ); | ||
| 80 | croak 'Server failed TLS verification' | ||
| 81 | if $buf ne "\1"; | ||
| 82 | } | ||
| 83 | my $data = ''; | ||
| 84 | do { | ||
| 85 | my $read = await $_tls->read( $_socket, $len ); | ||
| 86 | unless (defined $read) { | ||
| 87 | return ($data ? $data : undef); | ||
| 88 | } | ||
| 89 | $len -= length($read); | ||
| 90 | } while ($len > 0); | ||
| 91 | return $data; | ||
| 92 | } | ||
| 93 | |||
| 94 | async method _write_internal( $data ) { | ||
| 95 | my $len = length($data); | ||
| 96 | my $idx = 0; | ||
| 97 | while ($len > 0) { | ||
| 98 | my $written = await $_tls->write( $_socket, substr( $data, $idx ) ); | ||
| 99 | $idx += $written; | ||
| 100 | $len -= $written; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | method is_secure() { | ||
| 105 | return 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | 1; | ||
| 109 | |||
| 110 | |||
| 111 | __END__ |