]>
Commit | Line | Data |
---|---|---|
b8adb2d5 WB |
1 | package PVE::U2F; |
2 | ||
3 | use 5.024000; | |
4 | use strict; | |
5 | use warnings; | |
6 | ||
7 | require Exporter; | |
8 | ||
9 | our @ISA = qw(Exporter); | |
10 | ||
11 | # Items to export into callers namespace by default. Note: do not export | |
12 | # names by default without a very good reason. Use EXPORT_OK instead. | |
13 | # Do not simply export all your public functions/methods/constants. | |
14 | ||
15 | # This allows declaration use PVE::U2F::XS ':all'; | |
16 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | |
17 | # will save memory. | |
18 | our %EXPORT_TAGS = ( 'all' => [] ); | |
19 | ||
20 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |
21 | our @EXPORT = (); | |
22 | our $VERSION = '1.0'; | |
23 | ||
24 | require XSLoader; | |
25 | XSLoader::load('PVE::U2F', $VERSION); | |
26 | ||
27 | #### Context creation | |
28 | ||
29 | my $global_init = 0; | |
30 | sub new($) { | |
31 | my ($class) = @_; | |
32 | if (!$global_init) { | |
33 | $global_init = 1; | |
34 | do_global_init(); | |
35 | } | |
36 | if (my $lib = new_impl()) { | |
37 | return bless { ctx => $lib }, $class; | |
38 | } | |
39 | return undef; | |
40 | } | |
41 | ||
42 | sub DESTROY { | |
43 | my ($self) = @_; | |
44 | done_impl($self->{ctx}); | |
45 | } | |
46 | ||
47 | #### Error handling | |
48 | ||
49 | my @errcodes = ( | |
50 | qw(memory json base64 crypto origin challenge signature format) | |
51 | ); | |
52 | sub checkrc($) { | |
53 | my ($rc) = @_; | |
54 | return if $rc == 0; | |
55 | die "u2fs: $errcodes[-$rc-1] error\n" if $rc < 0 && $rc >= -8; | |
56 | die "u2fs: unknown error\n"; | |
57 | } | |
58 | ||
59 | #### Context initialization | |
60 | ||
61 | sub origin($) { return $_[0]->{origin}; } | |
62 | sub set_origin($$) { | |
63 | my ($self, $origin) = @_; | |
64 | checkrc(set_origin_impl($self->{ctx}, $origin)); | |
65 | return $self->{origin} = $origin; | |
66 | } | |
67 | ||
68 | sub appid($) { return $_[0]->{appid}; } | |
69 | sub set_appid($$) { | |
70 | my ($self, $appid) = @_; | |
71 | checkrc(set_appid_impl($self->{ctx}, $appid)); | |
72 | return $self->{appid} = $appid; | |
73 | } | |
74 | ||
75 | sub challenge($) { return $_[0]->{challenge}; } | |
76 | sub set_challenge($$) { | |
77 | my ($self, $challenge) = @_; | |
78 | checkrc(set_challenge_impl($self->{ctx}, $challenge)); | |
79 | return $self->{challenge} = $challenge; | |
80 | } | |
81 | ||
82 | sub keyHandle($) { return $_[0]->{keyHandle}; } | |
83 | sub set_keyHandle($$) { | |
84 | my ($self, $keyHandle) = @_; | |
85 | checkrc(set_keyHandle_impl($self->{ctx}, $keyHandle)); | |
86 | return $self->{keyHandle} = $keyHandle; | |
87 | } | |
88 | ||
89 | sub publicKey($) { return $_[0]->{publicKey}; } | |
90 | sub set_publicKey($$) { | |
91 | my ($self, $publicKey) = @_; | |
92 | checkrc(set_publicKey_impl($self->{ctx}, $publicKey)); | |
93 | return $self->{publicKey} = $publicKey; | |
94 | } | |
95 | ||
96 | #### Registration | |
97 | ||
98 | sub registration_challenge($) { | |
99 | my ($self) = @_; | |
100 | checkrc(registration_challenge_impl($self->{ctx}, my $challenge)); | |
101 | return $challenge; | |
102 | } | |
103 | ||
104 | sub registration_verify($$) { | |
105 | my ($self, $response) = @_; | |
106 | checkrc(registration_verify_impl($self->{ctx}, $response, my $kh, my $pk)); | |
107 | return ($kh, $pk); | |
108 | } | |
109 | ||
110 | #### Authentication | |
111 | ||
112 | sub auth_challenge($) { | |
113 | my ($self) = @_; | |
114 | checkrc(auth_challenge_impl($self->{ctx}, my $challenge)); | |
115 | return $challenge; | |
116 | } | |
117 | ||
118 | sub auth_verify($$) { | |
119 | my ($self, $response) = @_; | |
120 | checkrc(auth_verify_impl($self->{ctx}, $response, | |
121 | my $verified, | |
122 | my $counter, | |
123 | my $presence)); | |
124 | checkrc($verified); | |
125 | return wantarray ? ($counter, $presence) : 1; | |
126 | } | |
127 | ||
128 | 1; | |
129 | __END__ | |
130 | ||
131 | =head1 NAME | |
132 | ||
133 | PVE::U2F - Perl bindings for libu2f-server | |
134 | ||
135 | =head1 SYNOPSIS | |
136 | ||
137 | use PVE::U2F; | |
138 | ||
139 | =head1 DESCRIPTION | |
140 | ||
141 | Perl bindings for libu2f-server | |
142 | ||
143 | =head2 EXPORT | |
144 | ||
145 | None by default. | |
146 | ||
147 | =head1 SEE ALSO | |
148 | ||
149 | TODO | |
150 | ||
151 | =head1 AUTHOR | |
152 | ||
153 | Proxmox Server Solutions GmbH <support@proxmox.com> | |
154 | ||
155 | =cut |