| 1 | 1 | ▶ | use v5.36; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | package Protocol::HAP::Session; | ||||||||||||||||||||
| 4 | 1 | our $VERSION = '0.1.0'; | |||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | use Protocol::HAP; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use Protocol::HAP::Crypto; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 0 | ▶ | sub new ( $class, %args ) | ||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 10 | { | ||||||||||||||||||||
| 11 | # The id is required, and the server allocates it from an | ||||||||||||||||||||
| 12 | # instance counter. The id is not the descriptor: the kernel | ||||||||||||||||||||
| 13 | # gives a closed descriptor to the next connection, so a | ||||||||||||||||||||
| 14 | # subscription that a purge missed would arrive at whoever | ||||||||||||||||||||
| 15 | # inherits the number. A counter never repeats. | ||||||||||||||||||||
| 16 | 0 | ▶ | my $id = $args{id} // die 'session id required'; | ||||||||||||||||||
|
Condition0%0/2
$args{"id"} // die("session id required")
MC/DC0%0/1
$args{"id"} // die("session id required")
$args{"id"}
Truth table0%0/2
A
A= $args{"id"}
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | my $self = bless { | ||||||||||||||||||||
| 19 | id => $id, | ||||||||||||||||||||
| 20 | 0 | ▶ | logger => $args{logger} // Protocol::HAP->null_logger, | ||||||||||||||||||
|
Condition0%0/3
$args{"logger"} // "Protocol::HAP"->null_logger
MC/DC0%0/2
$args{"logger"} // "Protocol::HAP"->null_logger
$args{"logger"}
"Protocol::HAP"->null_logger
Truth table0%0/3
A // B
A= $args{"logger"}B= "Protocol::HAP"->null_logger
| |||||||||||||||||||||
| 21 | encrypted => 0, | ||||||||||||||||||||
| 22 | verified => 0, | ||||||||||||||||||||
| 23 | controller_id => undef, | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | # Session keys. Pair-verify sets them. | ||||||||||||||||||||
| 26 | encrypt_key => undef, | ||||||||||||||||||||
| 27 | decrypt_key => undef, | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | # Counters for nonce generation | ||||||||||||||||||||
| 30 | encrypt_count => 0, | ||||||||||||||||||||
| 31 | decrypt_count => 0, | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # Temporary pairing state | ||||||||||||||||||||
| 34 | pairing_state => {}, | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | # The event keys this connection subscribed to. The | ||||||||||||||||||||
| 37 | # server deletes them one by one when the connection | ||||||||||||||||||||
| 38 | # closes, instead of sweeping every key it holds. | ||||||||||||||||||||
| 39 | subscriptions => {}, | ||||||||||||||||||||
| 40 | }, $class; | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 0 | return $self; | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | # $self->id: | ||||||||||||||||||||
| 46 | # The key that the server files this session under. | ||||||||||||||||||||
| 47 | 0 | ▶ | sub id ($self) | ||||||||||||||||||
|
Subroutine
id: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 48 | { | ||||||||||||||||||||
| 49 | 0 | return $self->{id}; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | ▶ | sub set_encryption ( $self, $encrypt_key, $decrypt_key ) | ||||||||||||||||||
|
Subroutine
set_encryption: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 53 | { | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | $self->{encrypt_key} = $encrypt_key; | |||||||||||||||||||
| 56 | 0 | $self->{decrypt_key} = $decrypt_key; | |||||||||||||||||||
| 57 | 0 | $self->{encrypted} = 1; | |||||||||||||||||||
| 58 | 0 | $self->{encrypt_count} = 0; | |||||||||||||||||||
| 59 | 0 | $self->{decrypt_count} = 0; | |||||||||||||||||||
| 60 | 0 | $self->{logger}->debug('Session encryption enabled'); | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | ▶ | sub encrypt ( $self, $data ) | ||||||||||||||||||
|
Subroutine
encrypt: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 64 | { | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 0 | ▶ | return $data unless $self->{encrypted}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 0 | my $encrypted = ''; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | # HAP encrypts data in chunks. The AAD contains the length. | ||||||||||||||||||||
| 71 | 0 | ▶ | while ( length($data) > 0 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 0 | my $chunk = substr( $data, 0, 1024, '' ); | |||||||||||||||||||
| 73 | 0 | my $length = length($chunk); | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # The AAD is the 2-byte length in little-endian | ||||||||||||||||||||
| 76 | 0 | my $aad = pack( 'v', $length ); | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | # The nonce is 4 bytes zero + 8 bytes counter | ||||||||||||||||||||
| 79 | # (little-endian) | ||||||||||||||||||||
| 80 | 0 | my $nonce = pack( 'x[4]Q<', $self->{encrypt_count}++ ); | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | my ( $ciphertext, $tag ) = | ||||||||||||||||||||
| 83 | Protocol::HAP::Crypto->chacha20poly1305_encrypt( | ||||||||||||||||||||
| 84 | $self->{encrypt_key}, | ||||||||||||||||||||
| 85 | 0 | $nonce, $chunk, $aad ); | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | # Frame format: length (2 bytes) + ciphertext + tag (16 bytes) | ||||||||||||||||||||
| 88 | 0 | $encrypted .= $aad . $ciphertext . $tag; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 0 | return $encrypted; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | ▶ | sub decrypt ( $self, $data ) | ||||||||||||||||||
|
Subroutine
decrypt: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||
| 95 | { | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | ▶ | return $data unless $self->{encrypted}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 0 | my $decrypted = ''; | |||||||||||||||||||
| 100 | 0 | my $pos = 0; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | # HAP decrypts data in frames. A truncated frame is an | ||||||||||||||||||||
| 103 | # error. A frame that claims more than 1024 bytes of | ||||||||||||||||||||
| 104 | # plaintext is also an error (HAP-Encryption.md ยง9). | ||||||||||||||||||||
| 105 | 0 | ▶ | while ( $pos < length($data) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | # Read the frame header (2-byte length) | ||||||||||||||||||||
| 108 | 0 | ▶ | return if $pos + 2 > length($data); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | 0 | my $length = unpack( 'v', substr( $data, $pos, 2 ) ); | |||||||||||||||||||
| 110 | 0 | my $aad = substr( $data, $pos, 2 ); | |||||||||||||||||||
| 111 | 0 | $pos += 2; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 0 | ▶ | return if $length > 1024; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | # Read the ciphertext and the tag | ||||||||||||||||||||
| 116 | 0 | ▶ | return if $pos + $length + 16 > length($data); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 0 | my $ciphertext = substr( $data, $pos, $length ); | |||||||||||||||||||
| 118 | 0 | $pos += $length; | |||||||||||||||||||
| 119 | 0 | my $tag = substr( $data, $pos, 16 ); | |||||||||||||||||||
| 120 | 0 | $pos += 16; | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | # The nonce is 4 bytes zero + 8 bytes counter | ||||||||||||||||||||
| 123 | # (little-endian) | ||||||||||||||||||||
| 124 | 0 | my $nonce = pack( 'x[4]Q<', $self->{decrypt_count}++ ); | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | my $plaintext = | ||||||||||||||||||||
| 127 | Protocol::HAP::Crypto->chacha20poly1305_decrypt( | ||||||||||||||||||||
| 128 | 0 | $self->{decrypt_key}, $nonce, $ciphertext, $tag, $aad ); | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 0 | ▶ | return unless defined $plaintext; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | 0 | $decrypted .= $plaintext; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 0 | return $decrypted; | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 0 | ▶ | sub is_encrypted ($self) | ||||||||||||||||||
|
Subroutine
is_encrypted: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 138 | { | ||||||||||||||||||||
| 139 | 0 | return $self->{encrypted}; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 0 | ▶ | sub is_verified ($self) | ||||||||||||||||||
|
Subroutine
is_verified: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 143 | { | ||||||||||||||||||||
| 144 | 0 | return $self->{verified}; | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 0 | ▶ | sub set_verified ( $self, $controller_id ) | ||||||||||||||||||
|
Subroutine
set_verified: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 148 | { | ||||||||||||||||||||
| 149 | 0 | $self->{verified} = 1; | |||||||||||||||||||
| 150 | 0 | $self->{controller_id} = $controller_id; | |||||||||||||||||||
| 151 | $self->{logger} | ||||||||||||||||||||
| 152 | 0 | ->debug( 'Session verified for controller: %s', $controller_id ); | |||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 0 | return; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 0 | ▶ | sub controller_id ($self) | ||||||||||||||||||
|
Subroutine
controller_id: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 158 | { | ||||||||||||||||||||
| 159 | 0 | return $self->{controller_id}; | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 1 | 1; | |||||||||||||||||||