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