]>
Commit | Line | Data |
---|---|---|
8ecb08d7 DM |
1 | package PVE::Ticket; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use Crypt::OpenSSL::Random; | |
7 | use Crypt::OpenSSL::RSA; | |
8 | use MIME::Base64; | |
8ecb08d7 DM |
9 | use Digest::SHA; |
10 | use Time::HiRes qw(gettimeofday); | |
11 | ||
12 | use PVE::Exception qw(raise_perm_exc); | |
13 | ||
14 | Crypt::OpenSSL::RSA->import_random_seed(); | |
15 | ||
16 | sub assemble_csrf_prevention_token { | |
17 | my ($secret, $username) = @_; | |
18 | ||
19 | my $timestamp = sprintf("%08X", time()); | |
20 | ||
21 | my $digest = Digest::SHA::sha1_base64("$timestamp:$username", $secret); | |
22 | ||
23 | return "$timestamp:$digest"; | |
24 | } | |
25 | ||
26 | sub verify_csrf_prevention_token { | |
27 | my ($secret, $username, $token, $min_age, $max_age, $noerr) = @_; | |
28 | ||
29 | if ($token =~ m/^([A-Z0-9]{8}):(\S+)$/) { | |
30 | my $sig = $2; | |
31 | my $timestamp = $1; | |
32 | my $ttime = hex($timestamp); | |
33 | ||
34 | my $digest = Digest::SHA::sha1_base64("$timestamp:$username", $secret); | |
35 | ||
36 | my $age = time() - $ttime; | |
37 | return 1 if ($digest eq $sig) && ($age > $min_age) && | |
38 | ($age < $max_age); | |
39 | } | |
40 | ||
41 | raise_perm_exc("Permission denied - invalid csrf token") if !$noerr; | |
42 | ||
43 | return undef; | |
44 | } | |
45 | ||
46 | # Note: data may not contain white spaces (verify fails in that case) | |
47 | sub assemble_rsa_ticket { | |
48 | my ($rsa_priv, $prefix, $data, $secret_data) = @_; | |
49 | ||
50 | my $timestamp = sprintf("%08X", time()); | |
51 | ||
52 | my $plain = "$prefix:"; | |
53 | ||
54 | $plain .= "$data:" if defined($data); | |
55 | ||
56 | $plain .= $timestamp; | |
57 | ||
58 | my $full = defined($secret_data) ? "$plain:$secret_data" : $plain; | |
59 | ||
60 | my $ticket = $plain . "::" . encode_base64($rsa_priv->sign($full), ''); | |
61 | ||
62 | return $ticket; | |
63 | } | |
64 | ||
65 | sub verify_rsa_ticket { | |
66 | my ($rsa_pub, $prefix, $ticket, $secret_data, $min_age, $max_age, $noerr) = @_; | |
67 | ||
68 | if ($ticket && $ticket =~ m/^(\Q$prefix\E:\S+)::([^:\s]+)$/) { | |
69 | my $plain = $1; | |
70 | my $sig = $2; | |
71 | ||
72 | my $full = defined($secret_data) ? "$plain:$secret_data" : $plain; | |
73 | ||
74 | if ($rsa_pub->verify($full, decode_base64($sig))) { | |
75 | if ($plain =~ m/^\Q$prefix\E:(?:(\S+):)?([A-Z0-9]{8})$/) { | |
76 | my $data = $1; # Note: not all tickets contains data | |
77 | my $timestamp = $2; | |
78 | my $ttime = hex($timestamp); | |
79 | ||
80 | my $age = time() - $ttime; | |
81 | ||
82 | if (($age > $min_age) && ($age < $max_age)) { | |
83 | if (defined($data)) { | |
84 | return wantarray ? ($data, $age) : $data; | |
85 | } else { | |
86 | return wantarray ? (1, $age) : 1; | |
87 | } | |
88 | } | |
89 | } | |
90 | } | |
91 | } | |
92 | ||
93 | raise_perm_exc("permission denied - invalid $prefix ticket") if !$noerr; | |
94 | ||
95 | return undef; | |
96 | } | |
97 | ||
98 | sub assemble_spice_ticket { | |
99 | my ($secret, $username, $vmid, $node) = @_; | |
100 | ||
101 | my ($seconds, $microseconds) = gettimeofday; | |
102 | ||
103 | my $timestamp = sprintf("%08x", $seconds); | |
104 | ||
105 | my $randomstr = "PVESPICE:$timestamp:$username:$vmid:$node:$secret:" . | |
106 | ':' . sprintf("%08x", $microseconds) . | |
107 | ':' . sprintf("%08x", $$) . | |
108 | ':' . rand(1); | |
109 | ||
110 | # this should be used as one-time password | |
111 | # max length is 60 chars (spice limit) | |
112 | # we pass this to qemu set_pasword and limit lifetime there | |
113 | # keep this secret | |
114 | my $ticket = Digest::SHA::sha1_hex($randomstr); | |
115 | ||
116 | # Note: spice proxy connects with HTTP, so $proxyticket is exposed to public | |
117 | # we use a signature/timestamp to make sure nobody can fake such a ticket | |
118 | # an attacker can use this $proxyticket, but he will fail because $ticket is | |
119 | # private. | |
120 | # The proxy needs to be able to extract/verify the ticket | |
121 | # Note: data needs to be lower case only, because virt-viewer needs that | |
122 | # Note: RSA signature are too long (>=256 charaters) and make problems with remote-viewer | |
123 | ||
124 | my $plain = "pvespiceproxy:$timestamp:$vmid:" . lc($node); | |
125 | ||
126 | # produces 40 characters | |
127 | my $sig = unpack("H*", Digest::SHA::sha1($plain, $secret)); | |
128 | ||
129 | #my $sig = unpack("H*", $rsa_priv->sign($plain)); # this produce too long strings (512) | |
130 | ||
131 | my $proxyticket = "$plain::$sig"; | |
132 | ||
133 | return ($ticket, $proxyticket); | |
134 | } | |
135 | ||
136 | sub verify_spice_connect_url { | |
137 | my ($secret, $connect_str) = @_; | |
138 | ||
139 | # Note: we pass the spice ticket as 'host', so the | |
140 | # spice viewer connects with "$ticket:$port" | |
141 | ||
142 | return undef if !$connect_str; | |
143 | ||
144 | if ($connect_str =~m/^pvespiceproxy:([a-z0-9]{8}):(\d+):(\S+)::([a-z0-9]{40}):(\d+)$/) { | |
145 | my ($timestamp, $vmid, $node, $hexsig, $port) = ($1, $2, $3, $4, $5, $6); | |
146 | my $ttime = hex($timestamp); | |
147 | my $age = time() - $ttime; | |
148 | ||
149 | # use very limited lifetime - is this enough? | |
150 | return undef if !(($age > -20) && ($age < 40)); | |
151 | ||
152 | my $plain = "pvespiceproxy:$timestamp:$vmid:$node"; | |
153 | my $sig = unpack("H*", Digest::SHA::sha1($plain, $secret)); | |
154 | ||
155 | if ($sig eq $hexsig) { | |
156 | return ($vmid, $node, $port); | |
157 | } | |
158 | } | |
159 | ||
160 | return undef; | |
161 | } | |
162 | ||
163 | 1; |