Skip to content

Commit 77fa35a

Browse files
committed
Tests: basic QUIC key update tests.
1 parent e337f00 commit 77fa35a

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

lib/Test/Nginx/HTTP3.pm

+24-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ sub new {
6565
sub init {
6666
my ($self) = @_;
6767
$self->{keys} = [];
68+
$self->{key_phase} = 0;
6869
$self->{pn} = [[-1, -1, -1, -1], [-1, -1, -1, -1]];
6970
$self->{crypto_in} = [[],[],[],[]];
7071
$self->{stream_in} = [];
@@ -1775,7 +1776,9 @@ sub encrypt_aead_f {
17751776
sub encrypt_aead {
17761777
my ($self, $payload, $level) = @_;
17771778
my $pn = ++$self->{pn}[0][$level];
1778-
my $ad = pack("C", $level == 3 ? 0x40 : 0xc + $level << 4) | "\x03";
1779+
my $ad = pack("C", $level == 3
1780+
? 0x40 | ($self->{key_phase} << 2)
1781+
: 0xc + $level << 4) | "\x03";
17791782
$ad .= "\x00\x00\x00\x01" unless $level == 3;
17801783
$ad .= $level == 3 ? $self->{dcid} :
17811784
pack("C", length($self->{dcid})) . $self->{dcid}
@@ -1867,6 +1870,26 @@ sub set_traffic_keys {
18671870
$self->{keys}[$level]{$direction}{hp} = $hp;
18681871
}
18691872

1873+
sub key_update {
1874+
my ($self) = @_;
1875+
my ($prk, $key, $iv);
1876+
my $klen = $self->{cipher} == 0x1301 || $self->{cipher} == 0x1304
1877+
? 16 : 32;
1878+
my ($hash, $hlen) = $self->{cipher} == 0x1302 ?
1879+
('SHA384', 48) : ('SHA256', 32);
1880+
$self->{key_phase} ^= 1;
1881+
1882+
for my $direction ('r', 'w') {
1883+
$prk = $self->{keys}[3]{$direction}{prk};
1884+
$prk = hkdf_expand_label("tls13 quic ku", $hash, $hlen, $prk);
1885+
$key = hkdf_expand_label("tls13 quic key", $hash, $klen, $prk);
1886+
$iv = hkdf_expand_label("tls13 quic iv", $hash, 12, $prk);
1887+
$self->{keys}[3]{$direction}{prk} = $prk;
1888+
$self->{keys}[3]{$direction}{key} = $key;
1889+
$self->{keys}[3]{$direction}{iv} = $iv;
1890+
}
1891+
}
1892+
18701893
sub hmac_finished {
18711894
my ($hash, $hlen, $key, $digest) = @_;
18721895
my $expand = hkdf_expand_label("tls13 finished", $hash, $hlen, $key);

quic_key_update.t

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Sergey Kandaurov
4+
# (C) Nginx, Inc.
5+
6+
# Tests for QUIC key update.
7+
8+
###############################################################################
9+
10+
use warnings;
11+
use strict;
12+
13+
use Test::More;
14+
15+
BEGIN { use FindBin; chdir($FindBin::Bin); }
16+
17+
use lib 'lib';
18+
use Test::Nginx;
19+
use Test::Nginx::HTTP3;
20+
21+
###############################################################################
22+
23+
select STDERR; $| = 1;
24+
select STDOUT; $| = 1;
25+
26+
my $t = Test::Nginx->new()->has(qw/http http_v3 cryptx/)
27+
->has_daemon('openssl')->plan(3);
28+
29+
$t->write_file_expand('nginx.conf', <<'EOF');
30+
31+
%%TEST_GLOBALS%%
32+
33+
daemon off;
34+
35+
events {
36+
}
37+
38+
http {
39+
%%TEST_GLOBALS_HTTP%%
40+
41+
ssl_certificate_key localhost.key;
42+
ssl_certificate localhost.crt;
43+
44+
server {
45+
listen 127.0.0.1:%%PORT_8980_UDP%% quic;
46+
server_name localhost;
47+
48+
location / { }
49+
}
50+
}
51+
52+
EOF
53+
54+
$t->write_file('openssl.conf', <<EOF);
55+
[ req ]
56+
default_bits = 2048
57+
encrypt_key = no
58+
distinguished_name = req_distinguished_name
59+
[ req_distinguished_name ]
60+
EOF
61+
62+
my $d = $t->testdir();
63+
64+
foreach my $name ('localhost') {
65+
system('openssl req -x509 -new '
66+
. "-config $d/openssl.conf -subj /CN=$name/ "
67+
. "-out $d/$name.crt -keyout $d/$name.key "
68+
. ">>$d/openssl.out 2>&1") == 0
69+
or die "Can't create certificate for $name: $!\n";
70+
}
71+
72+
$t->run();
73+
74+
###############################################################################
75+
76+
my ($s, $sid, $frames, $frame);
77+
78+
$s = Test::Nginx::HTTP3->new();
79+
ok(get($s), 'request');
80+
81+
# sets the Key Phase bit
82+
83+
$s->key_update();
84+
ok(get($s), 'key update 1');
85+
86+
# clears the Key Phase bit
87+
88+
$s->key_update();
89+
ok(get($s), 'key update 2');
90+
91+
###############################################################################
92+
93+
sub get {
94+
my ($s) = @_;
95+
my $frames = $s->read(all => [{ sid => $s->new_stream(), fin => 1 }]);
96+
grep { $_->{type} eq "HEADERS" } @$frames;
97+
}
98+
99+
###############################################################################

0 commit comments

Comments
 (0)