|
39 | 39 | # The client's private key must not be world-readable, so take a copy
|
40 | 40 | # of the key stored in the code tree and update its permissions.
|
41 | 41 | #
|
42 |
| -# This changes ssl/client.key to ssl/client_tmp.key etc for the rest |
43 |
| -# of the tests. |
| 42 | +# This changes to using keys stored in a temporary path for the rest of |
| 43 | +# the tests. To get the full path for inclusion in connection strings, the |
| 44 | +# %key hash can be interrogated. |
| 45 | +my %key; |
44 | 46 | my @keys = (
|
45 |
| - "client", "client-revoked", |
46 |
| - "client-der", "client-encrypted-pem", |
47 |
| - "client-encrypted-der", "client-dn"); |
48 |
| -foreach my $key (@keys) |
| 47 | + "client.key", "client-revoked.key", |
| 48 | + "client-der.key", "client-encrypted-pem.key", |
| 49 | + "client-encrypted-der.key", "client-dn.key"); |
| 50 | +foreach my $keyfile (@keys) |
49 | 51 | {
|
50 |
| - copy("ssl/${key}.key", "ssl/${key}_tmp.key") |
| 52 | + copy("ssl/${keyfile}", "${PostgreSQL::Test::Utils::tmp_check}/${keyfile}") |
51 | 53 | or die
|
52 |
| - "couldn't copy ssl/${key}.key to ssl/${key}_tmp.key for permissions change: $!"; |
53 |
| - chmod 0600, "ssl/${key}_tmp.key" |
54 |
| - or die "failed to change permissions on ssl/${key}_tmp.key: $!"; |
| 54 | + "couldn't copy ssl/${keyfile} to ${PostgreSQL::Test::Utils::tmp_check}/${keyfile} for permissions change: $!"; |
| 55 | + chmod 0600, "${PostgreSQL::Test::Utils::tmp_check}/${keyfile}" |
| 56 | + or die "failed to change permissions on ${PostgreSQL::Test::Utils::tmp_check}/${keyfile}: $!"; |
| 57 | + |
| 58 | + $key{$keyfile} = "${PostgreSQL::Test::Utils::tmp_check}/$keyfile"; |
55 | 59 | }
|
56 | 60 |
|
57 | 61 | # Also make a copy of that explicitly world-readable. We can't
|
58 | 62 | # necessarily rely on the file in the source tree having those
|
59 |
| -# permissions. Add it to @keys to include it in the final clean |
60 |
| -# up phase. |
61 |
| -copy("ssl/client.key", "ssl/client_wrongperms_tmp.key"); |
62 |
| -chmod 0644, "ssl/client_wrongperms_tmp.key"; |
63 |
| -push @keys, 'client_wrongperms'; |
| 63 | +# permissions. |
| 64 | +copy("ssl/client.key", "${PostgreSQL::Test::Utils::tmp_check}/client_wrongperms.key"); |
| 65 | +chmod 0644, "${PostgreSQL::Test::Utils::tmp_check}/client_wrongperms.key"; |
| 66 | +$key{'client_wrongperms.key'} = "${PostgreSQL::Test::Utils::tmp_check}/client_wrongperms.key"; |
64 | 67 |
|
65 | 68 | #### Set up the server.
|
66 | 69 |
|
|
399 | 402 |
|
400 | 403 | # correct client cert in unencrypted PEM
|
401 | 404 | $node->connect_ok(
|
402 |
| - "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=ssl/client_tmp.key", |
| 405 | + "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=$key{'client.key'}", |
403 | 406 | "certificate authorization succeeds with correct client cert in PEM format"
|
404 | 407 | );
|
405 | 408 |
|
406 | 409 | # correct client cert in unencrypted DER
|
407 | 410 | $node->connect_ok(
|
408 |
| - "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=ssl/client-der_tmp.key", |
| 411 | + "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=$key{'client-der.key'}", |
409 | 412 | "certificate authorization succeeds with correct client cert in DER format"
|
410 | 413 | );
|
411 | 414 |
|
412 | 415 | # correct client cert in encrypted PEM
|
413 | 416 | $node->connect_ok(
|
414 |
| - "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=ssl/client-encrypted-pem_tmp.key sslpassword='dUmmyP^#+'", |
| 417 | + "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=$key{'client-encrypted-pem.key'} sslpassword='dUmmyP^#+'", |
415 | 418 | "certificate authorization succeeds with correct client cert in encrypted PEM format"
|
416 | 419 | );
|
417 | 420 |
|
418 | 421 | # correct client cert in encrypted DER
|
419 | 422 | $node->connect_ok(
|
420 |
| - "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=ssl/client-encrypted-der_tmp.key sslpassword='dUmmyP^#+'", |
| 423 | + "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=$key{'client-encrypted-der.key'} sslpassword='dUmmyP^#+'", |
421 | 424 | "certificate authorization succeeds with correct client cert in encrypted DER format"
|
422 | 425 | );
|
423 | 426 |
|
424 | 427 | # correct client cert in encrypted PEM with wrong password
|
425 | 428 | $node->connect_fails(
|
426 |
| - "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=ssl/client-encrypted-pem_tmp.key sslpassword='wrong'", |
| 429 | + "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=$key{'client-encrypted-pem.key'} sslpassword='wrong'", |
427 | 430 | "certificate authorization fails with correct client cert and wrong password in encrypted PEM format",
|
428 | 431 | expected_stderr =>
|
429 |
| - qr!\Qprivate key file "ssl/client-encrypted-pem_tmp.key": bad decrypt\E! |
| 432 | + qr!\Qprivate key file "$key{'client-encrypted-pem.key'}": bad decrypt\E! |
430 | 433 | );
|
431 | 434 |
|
432 | 435 |
|
433 | 436 | # correct client cert using whole DN
|
434 | 437 | my $dn_connstr = "$common_connstr dbname=certdb_dn";
|
435 | 438 |
|
436 | 439 | $node->connect_ok(
|
437 |
| - "$dn_connstr user=ssltestuser sslcert=ssl/client-dn.crt sslkey=ssl/client-dn_tmp.key", |
| 440 | + "$dn_connstr user=ssltestuser sslcert=ssl/client-dn.crt sslkey=$key{'client-dn.key'}", |
438 | 441 | "certificate authorization succeeds with DN mapping",
|
439 | 442 | log_like => [
|
440 | 443 | qr/connection authenticated: identity="CN=ssltestuser-dn,OU=Testing,OU=Engineering,O=PGDG" method=cert/
|
|
444 | 447 | $dn_connstr = "$common_connstr dbname=certdb_dn_re";
|
445 | 448 |
|
446 | 449 | $node->connect_ok(
|
447 |
| - "$dn_connstr user=ssltestuser sslcert=ssl/client-dn.crt sslkey=ssl/client-dn_tmp.key", |
| 450 | + "$dn_connstr user=ssltestuser sslcert=ssl/client-dn.crt sslkey=$key{'client-dn.key'}", |
448 | 451 | "certificate authorization succeeds with DN regex mapping");
|
449 | 452 |
|
450 | 453 | # same thing but using explicit CN
|
451 | 454 | $dn_connstr = "$common_connstr dbname=certdb_cn";
|
452 | 455 |
|
453 | 456 | $node->connect_ok(
|
454 |
| - "$dn_connstr user=ssltestuser sslcert=ssl/client-dn.crt sslkey=ssl/client-dn_tmp.key", |
| 457 | + "$dn_connstr user=ssltestuser sslcert=ssl/client-dn.crt sslkey=$key{'client-dn.key'}", |
455 | 458 | "certificate authorization succeeds with CN mapping",
|
456 | 459 | # the full DN should still be used as the authenticated identity
|
457 | 460 | log_like => [
|
|
469 | 472 |
|
470 | 473 | # correct client cert in encrypted PEM with empty password
|
471 | 474 | $node->connect_fails(
|
472 |
| - "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=ssl/client-encrypted-pem_tmp.key sslpassword=''", |
| 475 | + "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=$key{'client-encrypted-pem.key'} sslpassword=''", |
473 | 476 | "certificate authorization fails with correct client cert and empty password in encrypted PEM format",
|
474 | 477 | expected_stderr =>
|
475 |
| - qr!\Qprivate key file "ssl/client-encrypted-pem_tmp.key": processing error\E! |
| 478 | + qr!\Qprivate key file "$key{'client-encrypted-pem.key'}": processing error\E! |
476 | 479 | );
|
477 | 480 |
|
478 | 481 | # correct client cert in encrypted PEM with no password
|
479 | 482 | $node->connect_fails(
|
480 |
| - "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=ssl/client-encrypted-pem_tmp.key", |
| 483 | + "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=$key{'client-encrypted-pem.key'}", |
481 | 484 | "certificate authorization fails with correct client cert and no password in encrypted PEM format",
|
482 | 485 | expected_stderr =>
|
483 |
| - qr!\Qprivate key file "ssl/client-encrypted-pem_tmp.key": processing error\E! |
| 486 | + qr!\Qprivate key file "$key{'client-encrypted-pem.key'}": processing error\E! |
484 | 487 | );
|
485 | 488 |
|
486 | 489 | }
|
|
522 | 525 | '-P',
|
523 | 526 | 'null=_null_',
|
524 | 527 | '-d',
|
525 |
| - "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=ssl/client_tmp.key", |
| 528 | + "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=$key{'client.key'}", |
526 | 529 | '-c',
|
527 | 530 | "SELECT * FROM pg_stat_ssl WHERE pid = pg_backend_pid()"
|
528 | 531 | ],
|
|
536 | 539 | skip "Permissions check not enforced on Windows", 2 if ($windows_os);
|
537 | 540 |
|
538 | 541 | $node->connect_fails(
|
539 |
| - "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=ssl/client_wrongperms_tmp.key", |
| 542 | + "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=$key{'client_wrongperms.key'}", |
540 | 543 | "certificate authorization fails because of file permissions",
|
541 | 544 | expected_stderr =>
|
542 |
| - qr!\Qprivate key file "ssl/client_wrongperms_tmp.key" has group or world access\E! |
| 545 | + qr!\Qprivate key file "$key{'client_wrongperms.key'}" has group or world access\E! |
543 | 546 | );
|
544 | 547 | }
|
545 | 548 |
|
546 | 549 | # client cert belonging to another user
|
547 | 550 | $node->connect_fails(
|
548 |
| - "$common_connstr user=anotheruser sslcert=ssl/client.crt sslkey=ssl/client_tmp.key", |
| 551 | + "$common_connstr user=anotheruser sslcert=ssl/client.crt sslkey=$key{'client.key'}", |
549 | 552 | "certificate authorization fails with client cert belonging to another user",
|
550 | 553 | expected_stderr =>
|
551 | 554 | qr/certificate authentication failed for user "anotheruser"/,
|
|
555 | 558 |
|
556 | 559 | # revoked client cert
|
557 | 560 | $node->connect_fails(
|
558 |
| - "$common_connstr user=ssltestuser sslcert=ssl/client-revoked.crt sslkey=ssl/client-revoked_tmp.key", |
| 561 | + "$common_connstr user=ssltestuser sslcert=ssl/client-revoked.crt sslkey=$key{'client-revoked.key'}", |
559 | 562 | "certificate authorization fails with revoked client cert",
|
560 | 563 | expected_stderr => qr/SSL error: sslv3 alert certificate revoked/,
|
561 | 564 | # revoked certificates should not authenticate the user
|
|
568 | 571 | "sslrootcert=ssl/root+server_ca.crt sslmode=require dbname=verifydb hostaddr=$SERVERHOSTADDR";
|
569 | 572 |
|
570 | 573 | $node->connect_ok(
|
571 |
| - "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=ssl/client_tmp.key", |
| 574 | + "$common_connstr user=ssltestuser sslcert=ssl/client.crt sslkey=$key{'client.key'}", |
572 | 575 | "auth_option clientcert=verify-full succeeds with matching username and Common Name",
|
573 | 576 | # verify-full does not provide authentication
|
574 | 577 | log_unlike => [qr/connection authenticated:/],);
|
575 | 578 |
|
576 | 579 | $node->connect_fails(
|
577 |
| - "$common_connstr user=anotheruser sslcert=ssl/client.crt sslkey=ssl/client_tmp.key", |
| 580 | + "$common_connstr user=anotheruser sslcert=ssl/client.crt sslkey=$key{'client.key'}", |
578 | 581 | "auth_option clientcert=verify-full fails with mismatching username and Common Name",
|
579 | 582 | expected_stderr =>
|
580 | 583 | qr/FATAL: .* "trust" authentication failed for user "anotheruser"/,
|
|
584 | 587 | # Check that connecting with auth-optionverify-ca in pg_hba :
|
585 | 588 | # works, when username doesn't match Common Name
|
586 | 589 | $node->connect_ok(
|
587 |
| - "$common_connstr user=yetanotheruser sslcert=ssl/client.crt sslkey=ssl/client_tmp.key", |
| 590 | + "$common_connstr user=yetanotheruser sslcert=ssl/client.crt sslkey=$key{'client.key'}", |
588 | 591 | "auth_option clientcert=verify-ca succeeds with mismatching username and Common Name",
|
589 | 592 | # verify-full does not provide authentication
|
590 | 593 | log_unlike => [qr/connection authenticated:/],);
|
591 | 594 |
|
592 | 595 | # intermediate client_ca.crt is provided by client, and isn't in server's ssl_ca_file
|
593 | 596 | switch_server_cert($node, 'server-cn-only', 'root_ca');
|
594 | 597 | $common_connstr =
|
595 |
| - "user=ssltestuser dbname=certdb sslkey=ssl/client_tmp.key sslrootcert=ssl/root+server_ca.crt hostaddr=$SERVERHOSTADDR"; |
| 598 | + "user=ssltestuser dbname=certdb sslkey=$key{'client.key'} sslrootcert=ssl/root+server_ca.crt hostaddr=$SERVERHOSTADDR"; |
596 | 599 |
|
597 | 600 | $node->connect_ok(
|
598 | 601 | "$common_connstr sslmode=require sslcert=ssl/client+client_ca.crt",
|
|
608 | 611 |
|
609 | 612 | # revoked client cert
|
610 | 613 | $node->connect_fails(
|
611 |
| - "$common_connstr user=ssltestuser sslcert=ssl/client-revoked.crt sslkey=ssl/client-revoked_tmp.key", |
| 614 | + "$common_connstr user=ssltestuser sslcert=ssl/client-revoked.crt sslkey=$key{'client-revoked.key'}", |
612 | 615 | "certificate authorization fails with revoked client cert with server-side CRL directory",
|
613 | 616 | expected_stderr => qr/SSL error: sslv3 alert certificate revoked/);
|
614 |
| - |
615 |
| -# clean up |
616 |
| -foreach my $key (@keys) |
617 |
| -{ |
618 |
| - unlink("ssl/${key}_tmp.key"); |
619 |
| -} |
0 commit comments