| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # crypto::gpg Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Crypto::Gpg; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::System::Package); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable pgp gnupg) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | public_keyring => [ qw(file.gpg) ], | ||
| 20 | secret_keyring => [ qw(file.gpg) ], | ||
| 21 | passphrase => [ qw(passphrase) ], | ||
| 22 | type_key => [ qw(RSA|DSA) ], | ||
| 23 | type_subkey => [ qw(RSA|ELG-E) ], | ||
| 24 | length_key => [ qw(1024|2048|3072|4096) ], | ||
| 25 | length_subkey => [ qw(1024|2048|3072|4096) ], | ||
| 26 | expire_key => [ qw(count_y|0) ], | ||
| 27 | _gnupg => [ qw(INTERNAL) ], | ||
| 28 | }, | ||
| 29 | attributes_default => { | ||
| 30 | public_keyring => $ENV{HOME}."/.gnupg/pubring.gpg", | ||
| 31 | secret_keyring => $ENV{HOME}."/.gnupg/secring.gpg", | ||
| 32 | type_key => 'DSA', | ||
| 33 | type_subkey => 'ELG-E', | ||
| 34 | length_key => 2048, | ||
| 35 | length_subkey => 3072, | ||
| 36 | expire_key => '5y', | ||
| 37 | }, | ||
| 38 | commands => { | ||
| 39 | install => [ ], # Inherited | ||
| 40 | list_public_keys => [ ], | ||
| 41 | list_secret_keys => [ ], | ||
| 42 | get_public_keys => [ qw(keys_list) ], | ||
| 43 | get_secret_keys => [ qw(keys_list) ], | ||
| 44 | import_keys => [ qw(file) ], | ||
| 45 | delete_key => [ qw(key_id) ], | ||
| 46 | generate_key => [ qw(email description|OPTIONAL comment|OPTIONAL) ], | ||
| 47 | encrypt => [ qw($data email_recipient_list) ], | ||
| 48 | decrypt => [ qw($data) ], | ||
| 49 | decrypt_from_file => [ qw(file) ], | ||
| 50 | export_keys => [ qw(key_id) ], | ||
| 51 | }, | ||
| 52 | require_modules => { | ||
| 53 | 'IO::Handle' => [ ], | ||
| 54 | 'GnuPG::Interface' => [ ], | ||
| 55 | 'GnuPG::Handles' => [ ], | ||
| 56 | 'Metabrik::File::Text' => [ ], | ||
| 57 | 'Metabrik::String::Random' => [ ], | ||
| 58 | 'Metabrik::String::Password' => [ ], | ||
| 59 | }, | ||
| 60 | require_binaries => { | ||
| 61 | 'rngd' => [ ], | ||
| 62 | }, | ||
| 63 | need_packages => { | ||
| 64 | ubuntu => [ qw(rng-tools) ], | ||
| 65 | debian => [ qw(rng-tools) ], | ||
| 66 | kali => [ qw(rng-tools) ], | ||
| 67 | }, | ||
| 68 | }; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub brik_init { | ||
| 72 | my $self = shift; | ||
| 73 | |||
| 74 | my $gnupg = GnuPG::Interface->new; | ||
| 75 | if (! $gnupg) { | ||
| 76 | return $self->log->error("brik_init: GnuPG::Interface failed"); | ||
| 77 | } | ||
| 78 | $gnupg->options->hash_init(armor => 1); | ||
| 79 | |||
| 80 | $self->_gnupg($gnupg); | ||
| 81 | |||
| 82 | return $self->SUPER::brik_init; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub generate_key { | ||
| 86 | my $self = shift; | ||
| 87 | my ($email, $description, $comment) = @_; | ||
| 88 | |||
| 89 | $self->brik_help_run_undef_arg('generate_key', $email) or return; | ||
| 90 | |||
| 91 | my $passphrase = $self->passphrase; | ||
| 92 | $self->brik_help_set_undef_arg('passphrase', $passphrase) or return; | ||
| 93 | |||
| 94 | $description ||= $email; | ||
| 95 | $comment ||= $email; | ||
| 96 | |||
| 97 | my $type_key = $self->type_key; | ||
| 98 | my $type_subkey = $self->type_subkey; | ||
| 99 | my $length_key = $self->length_key; | ||
| 100 | my $length_subkey = $self->length_subkey; | ||
| 101 | my $expire_key = $self->expire_key; | ||
| 102 | |||
| 103 | my $sr = Metabrik::String::Random->new_from_brik_init($self) or return; | ||
| 104 | my $filename = $sr->filename | ||
| 105 | or return $self->log->error("generate_key: filename failed"); | ||
| 106 | |||
| 107 | my $ft = Metabrik::File::Text->new_from_brik_init($self) or return; | ||
| 108 | $ft->output($filename); | ||
| 109 | |||
| 110 | # If key is RSA, subkey will be RSA. | ||
| 111 | # If key is DSA, subkey will be Elgamal. | ||
| 112 | #my $subkey = $type_key; | ||
| 113 | #if ($type_key eq 'DSA') { | ||
| 114 | #$subkey = 'Elgamal'; | ||
| 115 | #} | ||
| 116 | |||
| 117 | $ft->write([ | ||
| 118 | '%echo Generating a standard key', "\n", | ||
| 119 | "Key-Type: $type_key", "\n", | ||
| 120 | "Key-Length: $length_key", "\n", | ||
| 121 | "Subkey-Type: $type_subkey", "\n", | ||
| 122 | "Subkey-Length: $length_subkey", "\n", | ||
| 123 | "Name-Real: $description", "\n", | ||
| 124 | "Name-Email: $email", "\n", | ||
| 125 | "Expire-Date: $expire_key", "\n", | ||
| 126 | "Passphrase: $passphrase", "\n", | ||
| 127 | '#%pubring foo.pub', "\n", | ||
| 128 | '#%secring foo.sec', "\n", | ||
| 129 | '%commit', "\n", | ||
| 130 | '%echo done', "\n", | ||
| 131 | '' | ||
| 132 | ]); | ||
| 133 | |||
| 134 | my $gnupg = $self->_gnupg; | ||
| 135 | |||
| 136 | my $stdin = IO::Handle->new; | ||
| 137 | my $stdout = IO::Handle->new; | ||
| 138 | my $stderr = IO::Handle->new; | ||
| 139 | my $handles = GnuPG::Handles->new( | ||
| 140 | stdin => $stdin, | ||
| 141 | stdout => $stdout, | ||
| 142 | stderr => $stderr, | ||
| 143 | ); | ||
| 144 | |||
| 145 | my $pid = $gnupg->wrap_call( | ||
| 146 | commands => [ qw(--batch --gen-key) ], | ||
| 147 | command_args => [ $filename ], | ||
| 148 | handles => $handles, | ||
| 149 | ); | ||
| 150 | |||
| 151 | my @out = <$stdout>; | ||
| 152 | close($stdout); | ||
| 153 | my @err = <$stderr>; | ||
| 154 | close($stderr); | ||
| 155 | waitpid($pid, 0); | ||
| 156 | |||
| 157 | unlink($filename); | ||
| 158 | |||
| 159 | for my $this (@err) { | ||
| 160 | chomp($this); | ||
| 161 | $self->log->verbose("generate_key: $this"); | ||
| 162 | } | ||
| 163 | |||
| 164 | return \@out; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub delete_key { | ||
| 168 | my $self = shift; | ||
| 169 | my ($id) = @_; | ||
| 170 | |||
| 171 | $self->brik_help_run_undef_arg('delete_key', $id) or return; | ||
| 172 | |||
| 173 | my $gnupg = $self->_gnupg; | ||
| 174 | |||
| 175 | my $stdin = IO::Handle->new; | ||
| 176 | my $stdout = IO::Handle->new; | ||
| 177 | my $stderr = IO::Handle->new; | ||
| 178 | my $handles = GnuPG::Handles->new( | ||
| 179 | stdin => $stdin, | ||
| 180 | stdout => $stdout, | ||
| 181 | stderr => $stderr, | ||
| 182 | ); | ||
| 183 | |||
| 184 | my $pid = $gnupg->wrap_call( | ||
| 185 | commands => [ qw(--delete-secret-and-public-key) ], | ||
| 186 | command_args => [ $id ], | ||
| 187 | handles => $handles, | ||
| 188 | ); | ||
| 189 | |||
| 190 | my @lines = (); | ||
| 191 | while (<$stdout>) { | ||
| 192 | chomp; | ||
| 193 | push @lines, $_; | ||
| 194 | } | ||
| 195 | close($stdout); | ||
| 196 | waitpid($pid, 0); | ||
| 197 | |||
| 198 | return \@lines; | ||
| 199 | } | ||
| 200 | |||
| 201 | sub import_keys { | ||
| 202 | my $self = shift; | ||
| 203 | my ($file) = @_; | ||
| 204 | |||
| 205 | $self->brik_help_run_undef_arg('import_keys', $file) or return; | ||
| 206 | |||
| 207 | my $gnupg = $self->_gnupg; | ||
| 208 | |||
| 209 | my $stdin = IO::Handle->new; | ||
| 210 | my $stdout = IO::Handle->new; | ||
| 211 | my $stderr = IO::Handle->new; | ||
| 212 | my $handles = GnuPG::Handles->new( | ||
| 213 | stdin => $stdin, | ||
| 214 | stdout => $stdout, | ||
| 215 | stderr => $stderr, | ||
| 216 | ); | ||
| 217 | |||
| 218 | my $pid = $gnupg->import_keys(handles => $handles); | ||
| 219 | if (! $pid) { | ||
| 220 | return $self->log->error("import_keys: import_keys failed"); | ||
| 221 | } | ||
| 222 | |||
| 223 | my $ft = Metabrik::File::Text->new_from_brik_init($self) or return; | ||
| 224 | my $data = $ft->read($file) | ||
| 225 | or return $self->log->error("import_keys: read failed"); | ||
| 226 | |||
| 227 | print $stdin $data; | ||
| 228 | close($stdin); | ||
| 229 | |||
| 230 | my @lines = (); | ||
| 231 | while (<$stdout>) { | ||
| 232 | chomp; | ||
| 233 | push @lines, $_; | ||
| 234 | } | ||
| 235 | close($stdout); | ||
| 236 | waitpid($pid, 0); | ||
| 237 | |||
| 238 | return \@lines; | ||
| 239 | |||
| 240 | } | ||
| 241 | |||
| 242 | sub list_public_keys { | ||
| 243 | my $self = shift; | ||
| 244 | |||
| 245 | my $gnupg = $self->_gnupg; | ||
| 246 | |||
| 247 | my $stdin = IO::Handle->new; | ||
| 248 | my $stdout = IO::Handle->new; | ||
| 249 | my $stderr = IO::Handle->new; | ||
| 250 | my $handles = GnuPG::Handles->new( | ||
| 251 | stdin => $stdin, | ||
| 252 | stdout => $stdout, | ||
| 253 | stderr => $stderr, | ||
| 254 | ); | ||
| 255 | |||
| 256 | my $pid = $gnupg->list_public_keys(handles => $handles); | ||
| 257 | if (! $pid) { | ||
| 258 | return $self->log->error("list_public_keys: list_public_keys failed"); | ||
| 259 | } | ||
| 260 | |||
| 261 | my @lines = (); | ||
| 262 | while (<$stdout>) { | ||
| 263 | chomp; | ||
| 264 | push @lines, $_; | ||
| 265 | } | ||
| 266 | close($stdout); | ||
| 267 | waitpid($pid, 0); | ||
| 268 | |||
| 269 | return \@lines; | ||
| 270 | } | ||
| 271 | |||
| 272 | sub get_public_keys { | ||
| 273 | my $self = shift; | ||
| 274 | my ($keys) = @_; | ||
| 275 | |||
| 276 | $self->brik_help_run_undef_arg('get_public_keys', $keys) or return; | ||
| 277 | $self->brik_help_run_invalid_arg('get_public_keys', $keys, 'ARRAY') or return; | ||
| 278 | |||
| 279 | my $gnupg = $self->_gnupg; | ||
| 280 | |||
| 281 | my @keys = $gnupg->get_public_keys_with_sigs(@$keys); | ||
| 282 | |||
| 283 | return \@keys; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub list_secret_keys { | ||
| 287 | my $self = shift; | ||
| 288 | |||
| 289 | my $gnupg = $self->_gnupg; | ||
| 290 | |||
| 291 | my $stdin = IO::Handle->new; | ||
| 292 | my $stdout = IO::Handle->new; | ||
| 293 | my $stderr = IO::Handle->new; | ||
| 294 | my $handles = GnuPG::Handles->new( | ||
| 295 | stdin => $stdin, | ||
| 296 | stdout => $stdout, | ||
| 297 | stderr => $stderr, | ||
| 298 | ); | ||
| 299 | |||
| 300 | my $pid = $gnupg->list_secret_keys(handles => $handles); | ||
| 301 | if (! $pid) { | ||
| 302 | return $self->log->error("list_secret_keys: list_secret_keys failed"); | ||
| 303 | } | ||
| 304 | |||
| 305 | my @lines = (); | ||
| 306 | while (<$stdout>) { | ||
| 307 | chomp; | ||
| 308 | push @lines, $_; | ||
| 309 | } | ||
| 310 | close($stdout); | ||
| 311 | waitpid($pid, 0); | ||
| 312 | |||
| 313 | return \@lines; | ||
| 314 | } | ||
| 315 | |||
| 316 | sub get_secret_keys { | ||
| 317 | my $self = shift; | ||
| 318 | my ($keys) = @_; | ||
| 319 | |||
| 320 | $self->brik_help_run_undef_arg('get_secret_keys', $keys) or return; | ||
| 321 | $self->brik_help_run_invalid_arg('get_secret_keys', $keys, 'ARRAY') or return; | ||
| 322 | |||
| 323 | my $gnupg = $self->_gnupg; | ||
| 324 | |||
| 325 | # XXX: does not work | ||
| 326 | #my $saved = $gnupg->options->copy; | ||
| 327 | |||
| 328 | my @keys = $gnupg->get_secret_keys(@$keys); | ||
| 329 | |||
| 330 | #$gnupg->options($saved); | ||
| 331 | |||
| 332 | return \@keys; | ||
| 333 | } | ||
| 334 | |||
| 335 | sub encrypt { | ||
| 336 | my $self = shift; | ||
| 337 | my ($data, $recipient_list) = @_; | ||
| 338 | |||
| 339 | $self->brik_help_run_undef_arg('encrypt', $data) or return; | ||
| 340 | $self->brik_help_run_undef_arg('encrypt', $recipient_list) or return; | ||
| 341 | $self->brik_help_run_invalid_arg('encrypt', $recipient_list, 'ARRAY') or return; | ||
| 342 | |||
| 343 | my @data = (); | ||
| 344 | if (ref($data) eq 'ARRAY') { | ||
| 345 | for my $this (@$data) { | ||
| 346 | push @data, $this; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | else { | ||
| 350 | if (ref($data) eq 'SCALAR') { | ||
| 351 | push @data, $$data; | ||
| 352 | } | ||
| 353 | else { | ||
| 354 | push @data, $data; | ||
| 355 | } | ||
| 356 | } | ||
| 357 | |||
| 358 | my $gnupg = $self->_gnupg; | ||
| 359 | |||
| 360 | my $stdin = IO::Handle->new; | ||
| 361 | my $stdout = IO::Handle->new; | ||
| 362 | my $stderr = IO::Handle->new; | ||
| 363 | my $handles = GnuPG::Handles->new( | ||
| 364 | stdin => $stdin, | ||
| 365 | stdout => $stdout, | ||
| 366 | stderr => $stderr, | ||
| 367 | ); | ||
| 368 | |||
| 369 | for my $email (@$recipient_list) { | ||
| 370 | $gnupg->options->push_recipients($email); | ||
| 371 | } | ||
| 372 | |||
| 373 | my $pid = $gnupg->encrypt(handles => $handles); | ||
| 374 | print $stdin @data; | ||
| 375 | close($stdin); | ||
| 376 | |||
| 377 | my @lines = (); | ||
| 378 | while (<$stdout>) { | ||
| 379 | chomp; | ||
| 380 | push @lines, $_; | ||
| 381 | } | ||
| 382 | close($stdout); | ||
| 383 | waitpid($pid, 0); | ||
| 384 | |||
| 385 | return \@lines; | ||
| 386 | } | ||
| 387 | |||
| 388 | sub decrypt { | ||
| 389 | my $self = shift; | ||
| 390 | my ($data) = @_; | ||
| 391 | |||
| 392 | $self->brik_help_run_undef_arg('decrypt', $data) or return; | ||
| 393 | |||
| 394 | my $sp = Metabrik::String::Password->new_from_brik_init($self) or return; | ||
| 395 | |||
| 396 | my $passphrase = $sp->prompt; | ||
| 397 | if (! defined($passphrase)) { | ||
| 398 | return $self->log->error("decrypt: invalid passphrase entered"); | ||
| 399 | } | ||
| 400 | |||
| 401 | my @data = (); | ||
| 402 | if (ref($data) eq 'ARRAY') { | ||
| 403 | for my $this (@$data) { | ||
| 404 | push @data, $this; | ||
| 405 | } | ||
| 406 | } | ||
| 407 | else { | ||
| 408 | if (ref($data) eq 'SCALAR') { | ||
| 409 | push @data, $$data; | ||
| 410 | } | ||
| 411 | else { | ||
| 412 | push @data, $data; | ||
| 413 | } | ||
| 414 | } | ||
| 415 | |||
| 416 | my $gnupg = $self->_gnupg; | ||
| 417 | |||
| 418 | my $stdin = IO::Handle->new; | ||
| 419 | my $stdout = IO::Handle->new; | ||
| 420 | my $stderr = IO::Handle->new; | ||
| 421 | my $stdpass = IO::Handle->new; | ||
| 422 | my $handles = GnuPG::Handles->new( | ||
| 423 | stdin => $stdin, | ||
| 424 | stdout => $stdout, | ||
| 425 | stderr => $stderr, | ||
| 426 | passphrase => $stdpass, | ||
| 427 | ); | ||
| 428 | |||
| 429 | my $pid = $gnupg->decrypt(handles => $handles); | ||
| 430 | |||
| 431 | # Print passphrase | ||
| 432 | print $stdpass $passphrase; | ||
| 433 | close($stdpass); | ||
| 434 | |||
| 435 | # Then data to decrypt | ||
| 436 | print $stdin @data; | ||
| 437 | close($stdin); | ||
| 438 | |||
| 439 | my @lines = (); | ||
| 440 | while (<$stdout>) { | ||
| 441 | chomp; | ||
| 442 | push @lines, $_; | ||
| 443 | } | ||
| 444 | close($stdout); | ||
| 445 | waitpid($pid, 0); | ||
| 446 | |||
| 447 | return \@lines; | ||
| 448 | } | ||
| 449 | |||
| 450 | sub decrypt_from_file { | ||
| 451 | my $self = shift; | ||
| 452 | my ($file) = @_; | ||
| 453 | |||
| 454 | $self->brik_help_run_undef_arg('decrypt_from_file', $file) or return; | ||
| 455 | $self->brik_help_run_file_not_found('decrypt_from_file', $file) or return; | ||
| 456 | |||
| 457 | my $ft = Metabrik::File::Text->new_from_brik_init($self) or return; | ||
| 458 | my $data = $ft->read($file) or return; | ||
| 459 | |||
| 460 | return $self->decrypt($data); | ||
| 461 | } | ||
| 462 | |||
| 463 | sub export_keys { | ||
| 464 | my $self = shift; | ||
| 465 | my ($key_id) = @_; | ||
| 466 | |||
| 467 | $self->brik_help_run_undef_arg('export_keys', $key_id) or return; | ||
| 468 | |||
| 469 | my $gnupg = $self->_gnupg; | ||
| 470 | |||
| 471 | my $stdin = IO::Handle->new; | ||
| 472 | my $stdout = IO::Handle->new; | ||
| 473 | my $stderr = IO::Handle->new; | ||
| 474 | my $handles = GnuPG::Handles->new( | ||
| 475 | stdin => $stdin, | ||
| 476 | stdout => $stdout, | ||
| 477 | stderr => $stderr, | ||
| 478 | ); | ||
| 479 | |||
| 480 | my $pid = $gnupg->export_keys( | ||
| 481 | handles => $handles, | ||
| 482 | command_args => $key_id, | ||
| 483 | ); | ||
| 484 | if (! $pid) { | ||
| 485 | return $self->log->error("export_keys: export_keys failed"); | ||
| 486 | } | ||
| 487 | |||
| 488 | my @lines = (); | ||
| 489 | while (<$stdout>) { | ||
| 490 | chomp; | ||
| 491 | push @lines, $_; | ||
| 492 | } | ||
| 493 | close($stdout); | ||
| 494 | waitpid($pid, 0); | ||
| 495 | |||
| 496 | return \@lines; | ||
| 497 | } | ||
| 498 | |||
| 499 | 1; | ||
| 500 | |||
| 501 | __END__ | ||