| 1 | package KeyNub::LicDongle; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp qw(croak); | ||
| 6 | use FFI::Platypus 2.00; | ||
| 7 | use FFI::Platypus::Buffer qw(scalar_to_buffer); | ||
| 8 | use Exporter qw(import); | ||
| 9 | |||
| 10 | our $VERSION = '1.1.1'; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | KeyNub::LicDongle - Perl binding for the KeyNub USB-C license dongle | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use KeyNub::LicDongle; | ||
| 19 | |||
| 20 | my $dongle = KeyNub::LicDongle->open; # first dongle, or ->open($serial) | ||
| 21 | $dongle->verify_genuine; # dies unless genuine | ||
| 22 | $dongle->session_open; | ||
| 23 | my $data = $dongle->app_decrypt($blob); # <- build the licence check on this | ||
| 24 | $dongle->session_close; | ||
| 25 | $dongle->close; | ||
| 26 | |||
| 27 | =head1 DESIGN | ||
| 28 | |||
| 29 | This binding calls the SDK's B<flat companion API> (C<keynub_licdongle_flat>) | ||
| 30 | rather than the core ABI, which is what COBOL and Fortran do and for the same | ||
| 31 | reason. The flat API has no structs, no library-allocated buffers and no | ||
| 32 | callbacks, so nothing here hand-writes a struct layout with computed padding — | ||
| 33 | the one class of mistake in an FFI binding that produces plausible wrong values | ||
| 34 | instead of a crash. | ||
| 35 | |||
| 36 | The cost is that there is no progress reporting: the flat API has no callbacks. | ||
| 37 | Records are read in one call. | ||
| 38 | |||
| 39 | Requires L<FFI::Platypus>, the one dependency. Set | ||
| 40 | C<KEYNUB_LICDONGLE_FLAT_LIBRARY> to point at a specific library. | ||
| 41 | |||
| 42 | =head1 SECURITY | ||
| 43 | |||
| 44 | Read F<docs/integration-security.md> before writing the check. | ||
| 45 | C<exit unless $dongle-E<gt>is_genuine> is one line to delete, and Perl ships as | ||
| 46 | source. What cannot be deleted is data the program needs and only the dongle can | ||
| 47 | decrypt — put it through C<app_encrypt>/C<app_decrypt>. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | # --- status codes ----------------------------------------------------------- | ||
| 52 | |||
| 53 | our %STATUS = ( | ||
| 54 | OK => 0, | ||
| 55 | INVALID_ARGUMENT => -1, | ||
| 56 | NO_DEVICE => -2, | ||
| 57 | ACCESS_DENIED => -3, | ||
| 58 | IO => -4, | ||
| 59 | TIMEOUT => -5, | ||
| 60 | PROTOCOL => -6, | ||
| 61 | NOT_GENUINE => -7, | ||
| 62 | CERTIFICATE_INVALID => -8, | ||
| 63 | SESSION_EXPIRED => -9, | ||
| 64 | TAG_MISMATCH => -10, | ||
| 65 | RANGE => -11, | ||
| 66 | STORAGE_FULL => -12, | ||
| 67 | BUSY => -13, | ||
| 68 | NOT_FOUND => -14, | ||
| 69 | AUTH_REQUIRED => -15, | ||
| 70 | FIRMWARE_INCOMPATIBLE => -16, | ||
| 71 | SDK_TOO_OLD => -17, | ||
| 72 | CANCELLED => -18, | ||
| 73 | NOT_IMPLEMENTED => -19, | ||
| 74 | INTERNAL => -20, | ||
| 75 | ); | ||
| 76 | |||
| 77 | # Bits in the flags value from get_info. | ||
| 78 | our $FLAG_SE_READY = 1; | ||
| 79 | our $FLAG_PROVISIONED = 2; | ||
| 80 | our $FLAG_WATCHDOG_REBOOT = 4; | ||
| 81 | our $FLAG_ISOLATED = 8; | ||
| 82 | # The write-auth key has been rotated away from the factory one, which is | ||
| 83 | # public: a dongle without this bit takes writes from anyone holding it. | ||
| 84 | our $FLAG_WRITEAUTH_ROTATED = 16; | ||
| 85 | |||
| 86 | # Who can decrypt data produced by app_encrypt. | ||
| 87 | our $SCOPE_DEVICE = 0; # only this one physical dongle | ||
| 88 | our $SCOPE_DEVELOPER = 1; # any dongle issued by the same developer | ||
| 89 | |||
| 90 | our @EXPORT_OK = qw( | ||
| 91 | %STATUS $FLAG_SE_READY $FLAG_PROVISIONED $FLAG_WATCHDOG_REBOOT | ||
| 92 | $FLAG_ISOLATED $FLAG_WRITEAUTH_ROTATED | ||
| 93 | $SCOPE_DEVICE $SCOPE_DEVELOPER | ||
| 94 | ); | ||
| 95 | |||
| 96 | # --- library discovery ------------------------------------------------------ | ||
| 97 | |||
| 98 | sub _default_library { | ||
| 99 | return 'keynub_licdongle_flat.dll' if $^O eq 'MSWin32'; | ||
| 100 | return 'libkeynub_licdongle_flat.dylib' if $^O eq 'darwin'; | ||
| 101 | return 'libkeynub_licdongle_flat.so'; | ||
| 102 | } | ||
| 103 | |||
| 104 | my $FFI; | ||
| 105 | |||
| 106 | sub _ffi { | ||
| 107 | return $FFI if $FFI; | ||
| 108 | |||
| 109 | my $lib = $ENV{KEYNUB_LICDONGLE_FLAT_LIBRARY} || _default_library(); | ||
| 110 | my $ffi = eval { FFI::Platypus->new(api => 2, lib => $lib) } | ||
| 111 | or croak "could not load the KeyNub flat library '$lib': $@"; | ||
| 112 | |||
| 113 | # int32 in by value, int32 out by reference, strings in as 'string', and every | ||
| 114 | # caller-provided buffer as 'opaque' with a pointer taken from a Perl scalar. | ||
| 115 | $ffi->attach(licdf_version => ['sint32*','sint32*','sint32*'] => 'sint32'); | ||
| 116 | $ffi->attach(licdf_strerror => ['sint32','opaque','sint32'] => 'sint32'); | ||
| 117 | |||
| 118 | $ffi->attach(licdf_device_count => ['sint32*'] => 'sint32'); | ||
| 119 | $ffi->attach(licdf_device_serial => ['sint32','opaque','sint32'] => 'sint32'); | ||
| 120 | $ffi->attach(licdf_device_path => ['sint32','opaque','sint32'] => 'sint32'); | ||
| 121 | |||
| 122 | $ffi->attach(licdf_open => ['string'] => 'sint32'); | ||
| 123 | $ffi->attach(licdf_open_path => ['string'] => 'sint32'); | ||
| 124 | $ffi->attach(licdf_close => ['sint32'] => 'sint32'); | ||
| 125 | $ffi->attach(licdf_set_trust_root => ['sint32','opaque','sint32'] => 'sint32'); | ||
| 126 | |||
| 127 | $ffi->attach(licdf_get_serial => ['sint32','opaque','sint32'] => 'sint32'); | ||
| 128 | $ffi->attach(licdf_get_info => ['sint32', map { 'sint32*' } 1..8] => 'sint32'); | ||
| 129 | $ffi->attach(licdf_verify_genuine => | ||
| 130 | ['sint32','sint32*','opaque','sint32','opaque','sint32'] => 'sint32'); | ||
| 131 | |||
| 132 | $ffi->attach(licdf_session_open => ['sint32'] => 'sint32'); | ||
| 133 | $ffi->attach(licdf_session_close => ['sint32'] => 'sint32'); | ||
| 134 | $ffi->attach(licdf_write_auth => ['sint32','opaque','sint32'] => 'sint32'); | ||
| 135 | $ffi->attach(licdf_write_auth_rotate => ['sint32','opaque','sint32'] => 'sint32'); | ||
| 136 | |||
| 137 | $ffi->attach(licdf_record_count => ['sint32','sint32*'] => 'sint32'); | ||
| 138 | $ffi->attach(licdf_record_name => | ||
| 139 | ['sint32','sint32','opaque','sint32','sint32*'] => 'sint32'); | ||
| 140 | $ffi->attach(licdf_record_size => ['sint32','string','sint32*'] => 'sint32'); | ||
| 141 | $ffi->attach(licdf_record_read => | ||
| 142 | ['sint32','string','opaque','sint32','sint32*'] => 'sint32'); | ||
| 143 | $ffi->attach(licdf_record_write => | ||
| 144 | ['sint32','string','opaque','sint32'] => 'sint32'); | ||
| 145 | $ffi->attach(licdf_record_erase => ['sint32','string'] => 'sint32'); | ||
| 146 | $ffi->attach(licdf_record_erase_all => ['sint32'] => 'sint32'); | ||
| 147 | |||
| 148 | $ffi->attach(licdf_counter_read => ['sint32','sint32','sint32*'] => 'sint32'); | ||
| 149 | $ffi->attach(licdf_counter_increment => ['sint32','sint32','sint32*'] => 'sint32'); | ||
| 150 | |||
| 151 | $ffi->attach(licdf_app_encrypt => | ||
| 152 | ['sint32','sint32','opaque','sint32','opaque','sint32','sint32*'] => 'sint32'); | ||
| 153 | $ffi->attach(licdf_app_decrypt => | ||
| 154 | ['sint32','opaque','sint32','opaque','sint32','sint32*'] => 'sint32'); | ||
| 155 | |||
| 156 | $ffi->attach(licdf_last_error => ['sint32','opaque','sint32'] => 'sint32'); | ||
| 157 | |||
| 158 | $FFI = $ffi; | ||
| 159 | return $FFI; | ||
| 160 | } | ||
| 161 | |||
| 162 | # --- errors ----------------------------------------------------------------- | ||
| 163 | |||
| 164 | =head2 Errors | ||
| 165 | |||
| 166 | Failures die with a L<KeyNub::LicDongle::Error> object, which stringifies for a | ||
| 167 | plain C<die> handler and carries C<status>, C<operation> and C<detail> for code | ||
| 168 | that wants to branch. Compare C<$err-E<gt>status> against C<$STATUS{NO_DEVICE}> | ||
| 169 | and friends. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | { | ||
| 174 | package KeyNub::LicDongle::Error; | ||
| 175 | use overload '""' => sub { | ||
| 176 | my $self = shift; | ||
| 177 | my $text = "$self->{operation}: $self->{message}"; | ||
| 178 | $text .= " ($self->{detail})" if length $self->{detail}; | ||
| 179 | return "$text\n"; | ||
| 180 | }, fallback => 1; | ||
| 181 | |||
| 182 | sub new { my ($c,%a) = @_; bless {%a}, $c } | ||
| 183 | sub status { $_[0]{status} } | ||
| 184 | sub operation { $_[0]{operation} } | ||
| 185 | sub detail { $_[0]{detail} } | ||
| 186 | sub message { $_[0]{message} } | ||
| 187 | } | ||
| 188 | |||
| 189 | sub _status_text { | ||
| 190 | my ($status) = @_; | ||
| 191 | my $buffer = "\0" x 256; | ||
| 192 | my ($ptr, $size) = scalar_to_buffer $buffer; | ||
| 193 | return "status $status" if licdf_strerror($status, $ptr, $size) != 0; | ||
| 194 | return unpack 'Z*', $buffer; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub _throw { | ||
| 198 | my ($status, $operation, $detail) = @_; | ||
| 199 | die KeyNub::LicDongle::Error->new( | ||
| 200 | status => $status, | ||
| 201 | operation => $operation, | ||
| 202 | message => _status_text($status), | ||
| 203 | detail => defined $detail ? $detail : '', | ||
| 204 | ); | ||
| 205 | } | ||
| 206 | |||
| 207 | sub _check { | ||
| 208 | my ($self, $status, $operation) = @_; | ||
| 209 | return if $status == 0; | ||
| 210 | my $detail = ''; | ||
| 211 | if (ref $self && $self->{handle}) { | ||
| 212 | my $buffer = "\0" x 256; | ||
| 213 | my ($ptr, $size) = scalar_to_buffer $buffer; | ||
| 214 | $detail = unpack 'Z*', $buffer | ||
| 215 | if licdf_last_error($self->{handle}, $ptr, $size) == 0; | ||
| 216 | } | ||
| 217 | _throw($status, $operation, $detail); | ||
| 218 | } | ||
| 219 | |||
| 220 | # --- module-level ----------------------------------------------------------- | ||
| 221 | |||
| 222 | =head2 library_version | ||
| 223 | |||
| 224 | my ($major, $minor, $patch) = KeyNub::LicDongle::library_version(); | ||
| 225 | |||
| 226 | =cut | ||
| 227 | |||
| 228 | sub library_version { | ||
| 229 | _ffi(); | ||
| 230 | my ($major, $minor, $patch) = (0, 0, 0); | ||
| 231 | licdf_version(\$major, \$minor, \$patch); | ||
| 232 | return ($major, $minor, $patch); | ||
| 233 | } | ||
| 234 | |||
| 235 | =head2 device_count / device_serial | ||
| 236 | |||
| 237 | Enumerates without opening. C<device_count> takes the snapshot that | ||
| 238 | C<device_serial> indexes into, so call them back to back. | ||
| 239 | |||
| 240 | =cut | ||
| 241 | |||
| 242 | sub device_count { | ||
| 243 | _ffi(); | ||
| 244 | my $count = 0; | ||
| 245 | _check(undef, licdf_device_count(\$count), 'licdf_device_count'); | ||
| 246 | return $count; | ||
| 247 | } | ||
| 248 | |||
| 249 | sub device_serial { | ||
| 250 | my ($index) = @_; | ||
| 251 | _ffi(); | ||
| 252 | my $buffer = "\0" x 15; | ||
| 253 | my ($ptr, $size) = scalar_to_buffer $buffer; | ||
| 254 | _check(undef, licdf_device_serial($index, $ptr, $size), 'licdf_device_serial'); | ||
| 255 | return unpack 'Z*', $buffer; | ||
| 256 | } | ||
| 257 | |||
| 258 | # --- constructor ------------------------------------------------------------ | ||
| 259 | |||
| 260 | =head2 open | ||
| 261 | |||
| 262 | my $dongle = KeyNub::LicDongle->open; # first dongle found | ||
| 263 | my $dongle = KeyNub::LicDongle->open($serial); | ||
| 264 | |||
| 265 | Dies with C<$STATUS{NO_DEVICE}> when none is attached. | ||
| 266 | |||
| 267 | =cut | ||
| 268 | |||
| 269 | sub open { | ||
| 270 | my ($class, $serial) = @_; | ||
| 271 | _ffi(); | ||
| 272 | my $handle = licdf_open(defined $serial ? $serial : ''); | ||
| 273 | _throw($handle, 'licdf_open') if $handle <= 0; | ||
| 274 | return bless { handle => $handle, session => 0 }, ref($class) || $class; | ||
| 275 | } | ||
| 276 | |||
| 277 | =head2 open_simulated | ||
| 278 | |||
| 279 | Opens a dongle backed by the in-process software simulator. Present only when the | ||
| 280 | loaded library was built with the simulator compiled in, which the shipping | ||
| 281 | library is not. | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||
| 285 | sub open_simulated { | ||
| 286 | my ($class) = @_; | ||
| 287 | my $ffi = _ffi(); | ||
| 288 | $ffi->attach(licdf_open_simulated => [] => 'sint32') | ||
| 289 | unless defined &licdf_open_simulated; | ||
| 290 | my $handle = licdf_open_simulated(); | ||
| 291 | _throw($handle, 'licdf_open_simulated') if $handle <= 0; | ||
| 292 | return bless { handle => $handle, session => 0 }, ref($class) || $class; | ||
| 293 | } | ||
| 294 | |||
| 295 | sub _handle { | ||
| 296 | my ($self) = @_; | ||
| 297 | croak 'the dongle has been closed' unless $self->{handle}; | ||
| 298 | return $self->{handle}; | ||
| 299 | } | ||
| 300 | |||
| 301 | =head2 close | ||
| 302 | |||
| 303 | Releases the dongle. Safe to call more than once, and called from C<DESTROY>. | ||
| 304 | The library holds 32 handles at a time, so a loop that forgets will notice. | ||
| 305 | |||
| 306 | =cut | ||
| 307 | |||
| 308 | sub close { | ||
| 309 | my ($self) = @_; | ||
| 310 | if ($self->{handle}) { | ||
| 311 | my $handle = $self->{handle}; | ||
| 312 | $self->{handle} = 0; | ||
| 313 | licdf_close($handle); | ||
| 314 | } | ||
| 315 | return; | ||
| 316 | } | ||
| 317 | |||
| 318 | sub DESTROY { | ||
| 319 | my ($self) = @_; | ||
| 320 | local ($@, $!); | ||
| 321 | eval { $self->close }; # a destructor must not die | ||
| 322 | return; | ||
| 323 | } | ||
| 324 | |||
| 325 | =head2 last_error_detail | ||
| 326 | |||
| 327 | The SDK's diagnostic detail for the most recent failure. Log it; do not parse it. | ||
| 328 | |||
| 329 | =cut | ||
| 330 | |||
| 331 | sub last_error_detail { | ||
| 332 | my ($self) = @_; | ||
| 333 | my $buffer = "\0" x 256; | ||
| 334 | my ($ptr, $size) = scalar_to_buffer $buffer; | ||
| 335 | return '' unless licdf_last_error($self->_handle, $ptr, $size) == 0; | ||
| 336 | return unpack 'Z*', $buffer; | ||
| 337 | } | ||
| 338 | |||
| 339 | =head2 set_trust_root | ||
| 340 | |||
| 341 | Overrides the CA root that C<verify_genuine> checks against. Applications do not | ||
| 342 | need this: a release build embeds the KeyNub production root. It exists for | ||
| 343 | dongles provisioned against a different CA, and for vendor tooling. | ||
| 344 | |||
| 345 | =cut | ||
| 346 | |||
| 347 | sub set_trust_root { | ||
| 348 | my ($self, $der) = @_; | ||
| 349 | my $copy = defined $der ? $der : ''; | ||
| 350 | my ($ptr, $size) = scalar_to_buffer $copy; | ||
| 351 | $self->_check(licdf_set_trust_root($self->_handle, $size ? $ptr : undef, $size), | ||
| 352 | 'licdf_set_trust_root'); | ||
| 353 | return; | ||
| 354 | } | ||
| 355 | |||
| 356 | # --- plaintext info --------------------------------------------------------- | ||
| 357 | |||
| 358 | =head2 serial | ||
| 359 | |||
| 360 | The dongle serial as hex. | ||
| 361 | |||
| 362 | =cut | ||
| 363 | |||
| 364 | sub serial { | ||
| 365 | my ($self) = @_; | ||
| 366 | my $buffer = "\0" x 15; | ||
| 367 | my ($ptr, $size) = scalar_to_buffer $buffer; | ||
| 368 | $self->_check(licdf_get_serial($self->_handle, $ptr, $size), 'licdf_get_serial'); | ||
| 369 | return unpack 'Z*', $buffer; | ||
| 370 | } | ||
| 371 | |||
| 372 | =head2 info | ||
| 373 | |||
| 374 | A hashref of the plaintext device info. C<watchdog_reboot> means the dongle's | ||
| 375 | I<previous> boot ended in a watchdog timeout: the firmware hung and reset itself. | ||
| 376 | It is the only trace a field hang leaves behind, and a power cycle clears it, so | ||
| 377 | log it. | ||
| 378 | |||
| 379 | =cut | ||
| 380 | |||
| 381 | sub info { | ||
| 382 | my ($self) = @_; | ||
| 383 | my @out = (0) x 8; | ||
| 384 | $self->_check( | ||
| 385 | licdf_get_info($self->_handle, map { \$out[$_] } 0 .. 7), | ||
| 386 | 'licdf_get_info' | ||
| 387 | ); | ||
| 388 | my ($pmaj, $pmin, $fmaj, $fmin, $fpat, $flags, $capacity, $free) = @out; | ||
| 389 | return { | ||
| 390 | protocol_version => [ $pmaj, $pmin ], | ||
| 391 | firmware_version => [ $fmaj, $fmin, $fpat ], | ||
| 392 | se_ready => ($flags & $FLAG_SE_READY) ? 1 : 0, | ||
| 393 | provisioned => ($flags & $FLAG_PROVISIONED) ? 1 : 0, | ||
| 394 | watchdog_reboot => ($flags & $FLAG_WATCHDOG_REBOOT) ? 1 : 0, | ||
| 395 | isolated => ($flags & $FLAG_ISOLATED) ? 1 : 0, | ||
| 396 | writeauth_rotated => ($flags & $FLAG_WRITEAUTH_ROTATED) ? 1 : 0, | ||
| 397 | data_capacity => $capacity, | ||
| 398 | data_free => $free, | ||
| 399 | }; | ||
| 400 | } | ||
| 401 | |||
| 402 | =head2 verify_genuine | ||
| 403 | |||
| 404 | Proves authenticity: the certificate chain to the trusted root plus a live ECDSA | ||
| 405 | challenge-response. Dies unless the dongle is genuine; returns a hashref with | ||
| 406 | C<genuine> and C<serial>. | ||
| 407 | |||
| 408 | =cut | ||
| 409 | |||
| 410 | sub verify_genuine { | ||
| 411 | my ($self) = @_; | ||
| 412 | my $genuine = 0; | ||
| 413 | my $serial = "\0" x 15; | ||
| 414 | my ($sptr, $ssize) = scalar_to_buffer $serial; | ||
| 415 | my $date = "\0" x 11; | ||
| 416 | my ($dptr, $dsize) = scalar_to_buffer $date; | ||
| 417 | $self->_check( | ||
| 418 | licdf_verify_genuine($self->_handle, \$genuine, $sptr, $ssize, $dptr, $dsize), | ||
| 419 | 'licdf_verify_genuine' | ||
| 420 | ); | ||
| 421 | _throw($STATUS{NOT_GENUINE}, 'licdf_verify_genuine') unless $genuine; | ||
| 422 | return { | ||
| 423 | genuine => 1, | ||
| 424 | serial => unpack('Z*', $serial), | ||
| 425 | provisioned_date => unpack('Z*', $date), | ||
| 426 | }; | ||
| 427 | } | ||
| 428 | |||
| 429 | =head2 is_genuine | ||
| 430 | |||
| 431 | The non-dying form, for a licence gate. B<Fails closed>: a missing dongle, an I/O | ||
| 432 | error and an invalid certificate all return false. | ||
| 433 | |||
| 434 | =cut | ||
| 435 | |||
| 436 | sub is_genuine { | ||
| 437 | my ($self) = @_; | ||
| 438 | my $ok = eval { $self->verify_genuine; 1 }; | ||
| 439 | return $ok ? 1 : 0; | ||
| 440 | } | ||
| 441 | |||
| 442 | # --- session ---------------------------------------------------------------- | ||
| 443 | |||
| 444 | =head2 session_open / session_close | ||
| 445 | |||
| 446 | Opens and ends the encrypted session (P-256 ECDH, HKDF-SHA256, AES-256-GCM). | ||
| 447 | |||
| 448 | =cut | ||
| 449 | |||
| 450 | sub session_open { | ||
| 451 | my ($self) = @_; | ||
| 452 | $self->_check(licdf_session_open($self->_handle), 'licdf_session_open'); | ||
| 453 | $self->{session} = 1; | ||
| 454 | return; | ||
| 455 | } | ||
| 456 | |||
| 457 | sub session_close { | ||
| 458 | my ($self) = @_; | ||
| 459 | if ($self->{session} && $self->{handle}) { | ||
| 460 | $self->{session} = 0; | ||
| 461 | licdf_session_close($self->{handle}); | ||
| 462 | } | ||
| 463 | return; | ||
| 464 | } | ||
| 465 | |||
| 466 | sub _in_session { | ||
| 467 | my ($self) = @_; | ||
| 468 | croak 'no session is open; call session_open first' unless $self->{session}; | ||
| 469 | return $self->_handle; | ||
| 470 | } | ||
| 471 | |||
| 472 | =head2 authorize_write | ||
| 473 | |||
| 474 | Elevates to the write role with the developer master key (a DER EC private key). | ||
| 475 | This belongs in your licence-issuing tooling; never ship that key | ||
| 476 | in the application your users run. | ||
| 477 | |||
| 478 | =cut | ||
| 479 | |||
| 480 | sub authorize_write { | ||
| 481 | my ($self, $der) = @_; | ||
| 482 | my $copy = $der; | ||
| 483 | my ($ptr, $size) = scalar_to_buffer $copy; | ||
| 484 | $self->_check(licdf_write_auth($self->_in_session, $ptr, $size), 'licdf_write_auth'); | ||
| 485 | return; | ||
| 486 | } | ||
| 487 | |||
| 488 | =head2 rotate_write_key($new_key_der) | ||
| 489 | |||
| 490 | Replaces the dongle's write-auth key with your own (a DER EC private key). Call | ||
| 491 | C<authorize_write> with the current key first. From the next session on, only the | ||
| 492 | new key elevates. | ||
| 493 | |||
| 494 | =cut | ||
| 495 | |||
| 496 | sub rotate_write_key { | ||
| 497 | my ($self, $der) = @_; | ||
| 498 | my $copy = $der; | ||
| 499 | my ($ptr, $size) = scalar_to_buffer $copy; | ||
| 500 | $self->_check(licdf_write_auth_rotate($self->_in_session, $ptr, $size), | ||
| 501 | 'licdf_write_auth_rotate'); | ||
| 502 | return; | ||
| 503 | } | ||
| 504 | |||
| 505 | # --- records ---------------------------------------------------------------- | ||
| 506 | |||
| 507 | =head2 records | ||
| 508 | |||
| 509 | An arrayref of C<{ name, size }> hashrefs. | ||
| 510 | |||
| 511 | =cut | ||
| 512 | |||
| 513 | sub records { | ||
| 514 | my ($self) = @_; | ||
| 515 | my $handle = $self->_in_session; | ||
| 516 | my $count = 0; | ||
| 517 | $self->_check(licdf_record_count($handle, \$count), 'licdf_record_count'); | ||
| 518 | my @out; | ||
| 519 | for my $index (0 .. $count - 1) { | ||
| 520 | my $buffer = "\0" x 64; | ||
| 521 | my ($ptr, $size) = scalar_to_buffer $buffer; | ||
| 522 | my $record_size = 0; | ||
| 523 | $self->_check(licdf_record_name($handle, $index, $ptr, $size, \$record_size), | ||
| 524 | 'licdf_record_name'); | ||
| 525 | push @out, { name => unpack('Z*', $buffer), size => $record_size }; | ||
| 526 | } | ||
| 527 | return \@out; | ||
| 528 | } | ||
| 529 | |||
| 530 | =head2 read_record | ||
| 531 | |||
| 532 | my $data = $dongle->read_record('license'); | ||
| 533 | |||
| 534 | Two calls under the hood: the flat API answers a zero-capacity read with | ||
| 535 | C<RANGE> and the size needed, so nothing has to guess a buffer size. | ||
| 536 | |||
| 537 | =cut | ||
| 538 | |||
| 539 | sub read_record { | ||
| 540 | my ($self, $name) = @_; | ||
| 541 | croak 'the record name must not be empty' unless defined $name && length $name; | ||
| 542 | my $handle = $self->_in_session; | ||
| 543 | |||
| 544 | my $needed = 0; | ||
| 545 | my $probe = "\0"; | ||
| 546 | my ($pptr) = scalar_to_buffer $probe; | ||
| 547 | my $status = licdf_record_read($handle, $name, $pptr, 0, \$needed); | ||
| 548 | $self->_check($status, 'licdf_record_read') | ||
| 549 | if $status != 0 && $status != $STATUS{RANGE}; | ||
| 550 | return '' if $needed == 0; | ||
| 551 | |||
| 552 | my $data = "\0" x $needed; | ||
| 553 | my ($ptr, $size) = scalar_to_buffer $data; | ||
| 554 | my $got = 0; | ||
| 555 | $self->_check(licdf_record_read($handle, $name, $ptr, $size, \$got), | ||
| 556 | 'licdf_record_read'); | ||
| 557 | return substr $data, 0, $got; | ||
| 558 | } | ||
| 559 | |||
| 560 | =head2 write_record | ||
| 561 | |||
| 562 | Atomically replaces a record. Requires the write role. | ||
| 563 | |||
| 564 | =cut | ||
| 565 | |||
| 566 | sub write_record { | ||
| 567 | my ($self, $name, $data) = @_; | ||
| 568 | croak 'the record name must not be empty' unless defined $name && length $name; | ||
| 569 | my $copy = defined $data ? $data : ''; | ||
| 570 | my ($ptr, $size) = scalar_to_buffer $copy; | ||
| 571 | $self->_check(licdf_record_write($self->_in_session, $name, $size ? $ptr : undef, $size), | ||
| 572 | 'licdf_record_write'); | ||
| 573 | return; | ||
| 574 | } | ||
| 575 | |||
| 576 | =head2 erase_record / erase_all_records | ||
| 577 | |||
| 578 | Erasing everything is a separate method on purpose: in the C API a null name means | ||
| 579 | "erase every record", and an accidentally empty Perl variable must not do that. | ||
| 580 | |||
| 581 | =cut | ||
| 582 | |||
| 583 | sub erase_record { | ||
| 584 | my ($self, $name) = @_; | ||
| 585 | croak 'the record name must not be empty; use erase_all_records' | ||
| 586 | unless defined $name && length $name; | ||
| 587 | $self->_check(licdf_record_erase($self->_in_session, $name), 'licdf_record_erase'); | ||
| 588 | return; | ||
| 589 | } | ||
| 590 | |||
| 591 | sub erase_all_records { | ||
| 592 | my ($self) = @_; | ||
| 593 | $self->_check(licdf_record_erase_all($self->_in_session), 'licdf_record_erase_all'); | ||
| 594 | return; | ||
| 595 | } | ||
| 596 | |||
| 597 | # --- counters --------------------------------------------------------------- | ||
| 598 | |||
| 599 | =head2 read_counter / increment_counter | ||
| 600 | |||
| 601 | Hardware monotonic counters. Incrementing is irreversible and needs the write role. | ||
| 602 | |||
| 603 | =cut | ||
| 604 | |||
| 605 | sub read_counter { | ||
| 606 | my ($self, $id) = @_; | ||
| 607 | my $value = 0; | ||
| 608 | $self->_check(licdf_counter_read($self->_in_session, $id, \$value), 'licdf_counter_read'); | ||
| 609 | return $value; | ||
| 610 | } | ||
| 611 | |||
| 612 | sub increment_counter { | ||
| 613 | my ($self, $id) = @_; | ||
| 614 | my $value = 0; | ||
| 615 | $self->_check(licdf_counter_increment($self->_in_session, $id, \$value), | ||
| 616 | 'licdf_counter_increment'); | ||
| 617 | return $value; | ||
| 618 | } | ||
| 619 | |||
| 620 | # --- app-data envelope encryption ------------------------------------------- | ||
| 621 | |||
| 622 | =head2 app_encrypt / app_decrypt | ||
| 623 | |||
| 624 | my $blob = $dongle->app_encrypt($SCOPE_DEVELOPER, $plaintext); | ||
| 625 | my $data = $dongle->app_decrypt($blob); | ||
| 626 | |||
| 627 | The pair to build a licence check on: put something the program genuinely needs | ||
| 628 | through it, so removing the check removes the data. C<$SCOPE_DEVELOPER> lets any | ||
| 629 | dongle you have issued decrypt, so one blob ships to every customer; | ||
| 630 | C<$SCOPE_DEVICE> locks it to one dongle. | ||
| 631 | |||
| 632 | =cut | ||
| 633 | |||
| 634 | sub _two_call { | ||
| 635 | my ($self, $operation, $call) = @_; | ||
| 636 | my $needed = 0; | ||
| 637 | my $probe = "\0"; | ||
| 638 | my ($pptr) = scalar_to_buffer $probe; | ||
| 639 | my $status = $call->($pptr, 0, \$needed); | ||
| 640 | $self->_check($status, $operation) if $status != 0 && $status != $STATUS{RANGE}; | ||
| 641 | return '' if $needed == 0; | ||
| 642 | |||
| 643 | my $out = "\0" x $needed; | ||
| 644 | my ($ptr, $size) = scalar_to_buffer $out; | ||
| 645 | my $got = 0; | ||
| 646 | $self->_check($call->($ptr, $size, \$got), $operation); | ||
| 647 | return substr $out, 0, $got; | ||
| 648 | } | ||
| 649 | |||
| 650 | sub app_encrypt { | ||
| 651 | my ($self, $scope, $plaintext) = @_; | ||
| 652 | croak 'scope must be 0 (device) or 1 (developer)' | ||
| 653 | unless defined $scope && ($scope == $SCOPE_DEVICE || $scope == $SCOPE_DEVELOPER); | ||
| 654 | my $handle = $self->_in_session; | ||
| 655 | my $copy = defined $plaintext ? $plaintext : ''; | ||
| 656 | my ($inPtr, $inSize) = scalar_to_buffer $copy; | ||
| 657 | return $self->_two_call('licdf_app_encrypt', sub { | ||
| 658 | my ($ptr, $size, $lenRef) = @_; | ||
| 659 | licdf_app_encrypt($handle, $scope, $inSize ? $inPtr : undef, $inSize, | ||
| 660 | $ptr, $size, $lenRef); | ||
| 661 | }); | ||
| 662 | } | ||
| 663 | |||
| 664 | sub app_decrypt { | ||
| 665 | my ($self, $packed) = @_; | ||
| 666 | my $handle = $self->_in_session; | ||
| 667 | my $copy = defined $packed ? $packed : ''; | ||
| 668 | my ($inPtr, $inSize) = scalar_to_buffer $copy; | ||
| 669 | return $self->_two_call('licdf_app_decrypt', sub { | ||
| 670 | my ($ptr, $size, $lenRef) = @_; | ||
| 671 | licdf_app_decrypt($handle, $inSize ? $inPtr : undef, $inSize, | ||
| 672 | $ptr, $size, $lenRef); | ||
| 673 | }); | ||
| 674 | } | ||
| 675 | |||
| 676 | 1; | ||
| 677 | |||
| 678 | __END__ |