| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use 5.008001; |
| 4 |
use strict; |
| 5 |
use warnings; |
| 6 |
|
| 7 |
use English qw( |
| 8 |
$EVAL_ERROR $EXCEPTIONS_BEING_CAUGHT $EXECUTABLE_NAME $OS_ERROR $RS |
| 9 |
-no_match_vars |
| 10 |
); |
| 11 |
use File::Basename qw(dirname); |
| 12 |
use File::Spec::Functions qw( catfile splitpath ); |
| 13 |
use File::Temp; |
| 14 |
use Getopt::Long qw(GetOptionsFromArray); |
| 15 |
use IPC::Run qw( start finish timeout ); |
| 16 |
|
| 17 |
our $VERSION = '1.96'; |
| 18 |
|
| 19 |
local $SIG{__DIE__} = sub { |
| 20 |
my ($cause) = @_; |
| 21 |
|
| 22 |
if ($EXCEPTIONS_BEING_CAUGHT) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
print STDERR $cause, "\n"; |
| 27 |
|
| 28 |
exit 1; |
| 29 |
}; |
| 30 |
|
| 31 |
my ( $args, $entities ) = eval { parse_options( \@ARGV ) } |
| 32 |
or fatal( 'Error while parsing command line options', $EVAL_ERROR ); |
| 33 |
|
| 34 |
eval { |
| 35 |
check_openssl_version( |
| 36 |
{ |
| 37 |
min_version => '3.0.0-alpha7', |
| 38 |
min_version_match => qr{^3\.(?!0\.0-alpha[1-6])}, |
| 39 |
} |
| 40 |
); |
| 41 |
} or fatal( 'OpenSSL minimum version check failed', $EVAL_ERROR ); |
| 42 |
|
| 43 |
my $tmp = eval { |
| 44 |
File::Temp->newdir( |
| 45 |
TEMPLATE => 'test-pki-XXXXXXXX', |
| 46 |
TMPDIR => 1, |
| 47 |
CLEANUP => 1, |
| 48 |
); |
| 49 |
} or fatal( 'Could not create temporary working directory', $EVAL_ERROR ); |
| 50 |
|
| 51 |
my $pki_config = eval { pki_config() } |
| 52 |
or fatal( 'Could not load PKI configuration file', $EVAL_ERROR ); |
| 53 |
|
| 54 |
my $pki_tree = eval { pki_tree() } |
| 55 |
or fatal( 'Error while building PKI tree', $EVAL_ERROR ); |
| 56 |
|
| 57 |
generate_tree( |
| 58 |
$pki_tree, |
| 59 |
@{ $entities } |
| 60 |
? { map { $_ => 1 } @{ $entities } } |
| 61 |
: undef |
| 62 |
); |
| 63 |
|
| 64 |
|
| 65 |
sub parse_options { |
| 66 |
my ($argv) = @_; |
| 67 |
|
| 68 |
my $opts = { |
| 69 |
'config' => catfile( dirname(__FILE__), 'pki.cfg' ), |
| 70 |
'openssl-binary' => 'openssl', |
| 71 |
'output' => undef, |
| 72 |
'verbose' => 0, |
| 73 |
}; |
| 74 |
|
| 75 |
GetOptionsFromArray( |
| 76 |
$argv, |
| 77 |
$opts, |
| 78 |
'config|c=s', |
| 79 |
'openssl-binary|b=s', |
| 80 |
'output|o=s', |
| 81 |
'verbose|v', |
| 82 |
); |
| 83 |
|
| 84 |
if ( !-e $opts->{config} ) { |
| 85 |
fatal("PKI configuration file $opts->{config} does not exist"); |
| 86 |
} |
| 87 |
|
| 88 |
if ( !defined $opts->{output} ) { |
| 89 |
fatal("an output directory must be given"); |
| 90 |
} |
| 91 |
|
| 92 |
if ( !-d $opts->{output} ) { |
| 93 |
fatal("output directory $opts->{output} does not exist"); |
| 94 |
} |
| 95 |
|
| 96 |
return wantarray |
| 97 |
? ( $opts, $argv ) |
| 98 |
: $opts; |
| 99 |
} |
| 100 |
|
| 101 |
sub pki_config { |
| 102 |
open my $fh, '<:encoding(UTF-8)', $args->{config} |
| 103 |
or fatal( $args->{config}, $OS_ERROR ); |
| 104 |
|
| 105 |
my $config = do { |
| 106 |
local $RS = undef; |
| 107 |
eval <$fh> |
| 108 |
or do { |
| 109 |
( my $error = $EVAL_ERROR ) |
| 110 |
=~ s{ at \(eval .+?\) }{ at $args->{config} }g; |
| 111 |
|
| 112 |
fatal( 'syntax error', $error ); |
| 113 |
}; |
| 114 |
}; |
| 115 |
|
| 116 |
close $fh; |
| 117 |
|
| 118 |
return $config; |
| 119 |
} |
| 120 |
|
| 121 |
sub pki_tree { |
| 122 |
my $children = {}; |
| 123 |
my $tree = {}; |
| 124 |
|
| 125 |
for my $entity ( keys %{$pki_config} ) { |
| 126 |
my $issuer = $pki_config->{$entity}->{cert}->{issuer}; |
| 127 |
|
| 128 |
if ( !exists $children->{$entity} ) { |
| 129 |
$children->{$entity} = {}; |
| 130 |
} |
| 131 |
|
| 132 |
if ( defined $issuer ) { |
| 133 |
if ( !exists $pki_config->{$issuer} ) { |
| 134 |
fatal("entity '$entity': issuer '$issuer' is not defined"); |
| 135 |
} |
| 136 |
|
| 137 |
$children->{$issuer}->{$entity} = $children->{$entity}; |
| 138 |
} |
| 139 |
else { |
| 140 |
$tree->{$entity} = $children->{$entity}; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return $tree; |
| 145 |
} |
| 146 |
|
| 147 |
sub openssl_config { |
| 148 |
my (%tmpl) = @_; |
| 149 |
|
| 150 |
my $start = tell DATA; |
| 151 |
|
| 152 |
my $config = do { local $RS = undef; <DATA> }; |
| 153 |
|
| 154 |
$config =~ s/\{\{ \s* (\w+) \s* \}\}/defined $tmpl{$1} ? $tmpl{$1} : ''/xeg; |
| 155 |
|
| 156 |
seek DATA, $start, 0; |
| 157 |
|
| 158 |
return $config; |
| 159 |
} |
| 160 |
|
| 161 |
sub subject_string { |
| 162 |
my (@rdns) = @_; |
| 163 |
|
| 164 |
my $string = q{}; |
| 165 |
|
| 166 |
while (@rdns) { |
| 167 |
my ( $key, $value ) = ( shift @rdns, shift @rdns ); |
| 168 |
|
| 169 |
if ( !defined $key |
| 170 |
|| !defined $value ) |
| 171 |
{ |
| 172 |
fatal('invalid key/value pair given in subject'); |
| 173 |
} |
| 174 |
|
| 175 |
# Certain characters in an RDN value must be escaped |
| 176 |
$value =~ s{([,\#+<>;"=/])}{\\$1}g; |
| 177 |
|
| 178 |
# Any leading space in an RDN value must be escaped |
| 179 |
$value =~ s{^ }{\\ }; |
| 180 |
|
| 181 |
$string .= "/$key=$value"; |
| 182 |
} |
| 183 |
|
| 184 |
return $string; |
| 185 |
} |
| 186 |
|
| 187 |
sub time_string { |
| 188 |
my ($time) = @_; |
| 189 |
|
| 190 |
if ( $time !~ m{^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$} ) { |
| 191 |
fatal('invalid timestamp'); |
| 192 |
} |
| 193 |
|
| 194 |
$time =~ s/[^\d]//g; |
| 195 |
$time =~ s/Z$//; |
| 196 |
|
| 197 |
return $time . 'Z'; |
| 198 |
} |
| 199 |
|
| 200 |
sub short_time_string { |
| 201 |
my ($time) = @_; |
| 202 |
|
| 203 |
( $time = time_string($time) ) =~ s/^\d{2}//; |
| 204 |
|
| 205 |
return $time; |
| 206 |
} |
| 207 |
|
| 208 |
sub extensions_section { |
| 209 |
my ($exts) = @_; |
| 210 |
|
| 211 |
my @section; |
| 212 |
|
| 213 |
for my $ext ( sort keys %{$exts} ) { |
| 214 |
push @section, |
| 215 |
sprintf '%s = %s', |
| 216 |
$ext, |
| 217 |
ref $exts->{$ext} eq 'ARRAY' |
| 218 |
? join ',', @{ $exts->{$ext} } |
| 219 |
: $exts->{$ext}; |
| 220 |
} |
| 221 |
|
| 222 |
return join "\n", @section; |
| 223 |
} |
| 224 |
|
| 225 |
sub issuer_chain { |
| 226 |
my ( $entity, $opts ) = @_; |
| 227 |
|
| 228 |
my @chain = ($entity); |
| 229 |
|
| 230 |
while ( defined $pki_config->{$entity}->{cert}->{issuer} ) { |
| 231 |
push @chain, $pki_config->{$entity}->{cert}->{issuer}; |
| 232 |
|
| 233 |
$entity = $pki_config->{$entity}->{cert}->{issuer}; |
| 234 |
} |
| 235 |
|
| 236 |
return \@chain; |
| 237 |
} |
| 238 |
|
| 239 |
sub generate_tree { |
| 240 |
my ( $tree, $entities ) = @_; |
| 241 |
|
| 242 |
for my $root ( sort keys %{$tree} ) { |
| 243 |
if ( defined $entities ) { |
| 244 |
if ( exists $entities->{$root} ) { |
| 245 |
for my $child ( keys %{ $tree->{$root} } ) { |
| 246 |
$entities->{$child} = 1; |
| 247 |
} |
| 248 |
|
| 249 |
generate_entity( $root, $entities ) |
| 250 |
or return 0; |
| 251 |
} |
| 252 |
|
| 253 |
generate_tree( $tree->{$root}, $entities ) |
| 254 |
or return 0; |
| 255 |
} |
| 256 |
else { |
| 257 |
generate_entity($root) |
| 258 |
or return 0; |
| 259 |
|
| 260 |
generate_tree( $tree->{$root} ) |
| 261 |
or return 0; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
return 1; |
| 266 |
} |
| 267 |
|
| 268 |
sub generate_entity { |
| 269 |
my ($entity) = @_; |
| 270 |
|
| 271 |
print "Generating PKI for entity '$entity'\n"; |
| 272 |
|
| 273 |
my $entity_cfg = $pki_config->{$entity}; |
| 274 |
my $entity_root = catfile( $args->{output}, $entity ); |
| 275 |
|
| 276 |
if ( !-e "$entity_root.key.pem" ) { |
| 277 |
print "\tPEM key: $entity_root.key.pem\n"; |
| 278 |
|
| 279 |
eval { |
| 280 |
generate_key( |
| 281 |
"$entity_root.key.pem", |
| 282 |
{ |
| 283 |
algorithm => $entity_cfg->{key}->{algorithm}, |
| 284 |
size => $entity_cfg->{key}->{size}, |
| 285 |
} |
| 286 |
) |
| 287 |
} or fatal( "Could not generate '$entity_root.key.pem'", $EVAL_ERROR ); |
| 288 |
|
| 289 |
print "\tEncrypted PEM key: $entity_root.key.enc.pem\n"; |
| 290 |
|
| 291 |
eval { |
| 292 |
convert_key( |
| 293 |
"$entity_root.key.pem", |
| 294 |
"$entity_root.key.enc.pem", |
| 295 |
{ |
| 296 |
( |
| 297 |
exists $entity_cfg->{key}->{passphrase} |
| 298 |
? ( passphrase => $entity_cfg->{key}->{passphrase} ) |
| 299 |
: () |
| 300 |
) |
| 301 |
} |
| 302 |
) |
| 303 |
} or fatal( "Could not generate '$entity_root.key.enc.pem'", |
| 304 |
$EVAL_ERROR ); |
| 305 |
|
| 306 |
print "\tDER key: $entity_root.key.der\n"; |
| 307 |
|
| 308 |
eval { |
| 309 |
convert_key( |
| 310 |
"$entity_root.key.pem", |
| 311 |
"$entity_root.key.der", |
| 312 |
{ |
| 313 |
format => 'der', |
| 314 |
} |
| 315 |
); |
| 316 |
} or fatal( "Could not generate '$entity_root.key.der'", $EVAL_ERROR ); |
| 317 |
|
| 318 |
print "\tEncrypted DER key: $entity_root.key.enc.der\n"; |
| 319 |
|
| 320 |
eval { |
| 321 |
convert_key( |
| 322 |
"$entity_root.key.pem", |
| 323 |
"$entity_root.key.enc.der", |
| 324 |
{ |
| 325 |
format => 'der', |
| 326 |
passphrase => 'test', |
| 327 |
} |
| 328 |
); |
| 329 |
} or fatal( "Could not generate '$entity_root.key.enc.der'", |
| 330 |
$EVAL_ERROR ); |
| 331 |
} |
| 332 |
|
| 333 |
print "\tPEM CSR: $entity_root.csr.pem\n"; |
| 334 |
|
| 335 |
eval { |
| 336 |
generate_csr( |
| 337 |
"$entity_root.key.pem", |
| 338 |
"$entity_root.csr.pem", |
| 339 |
{ |
| 340 |
md_algorithm => $entity_cfg->{csr}->{md_algorithm}, |
| 341 |
subject => $entity_cfg->{cert}->{subject}, |
| 342 |
} |
| 343 |
); |
| 344 |
} or fatal( "Could not generate '$entity_root.csr.pem'", $EVAL_ERROR ); |
| 345 |
|
| 346 |
print "\tDER CSR: $entity_root.csr.der\n"; |
| 347 |
|
| 348 |
eval { |
| 349 |
convert_csr( |
| 350 |
"$entity_root.csr.pem", |
| 351 |
"$entity_root.csr.der", |
| 352 |
{ |
| 353 |
format => 'der', |
| 354 |
} |
| 355 |
); |
| 356 |
} or fatal( "Could not generate '$entity_root.csr.der'", $EVAL_ERROR ); |
| 357 |
|
| 358 |
print "\tPEM certificate: $entity_root.cert.pem\n"; |
| 359 |
|
| 360 |
my $issuer_root = defined $entity_cfg->{cert}->{issuer} |
| 361 |
? catfile( $args->{output}, $entity_cfg->{cert}->{issuer} ) |
| 362 |
: $entity_root; |
| 363 |
|
| 364 |
my $issuer_cfg = defined $entity_cfg->{cert}->{issuer} |
| 365 |
? $pki_config->{ $entity_cfg->{cert}->{issuer} } |
| 366 |
: undef; |
| 367 |
|
| 368 |
my @issuer_opts = defined $entity_cfg->{cert}->{issuer} |
| 369 |
? ( |
| 370 |
issuer_cert_path => "$issuer_root.cert.pem", |
| 371 |
) |
| 372 |
: (); |
| 373 |
|
| 374 |
eval { |
| 375 |
my $valid_from = time_string( $entity_cfg->{cert}->{valid_from} ) |
| 376 |
or fatal( 'valid_from', $EVAL_ERROR ); |
| 377 |
|
| 378 |
my $valid_until = time_string( $entity_cfg->{cert}->{valid_until} ) |
| 379 |
or fatal( 'valid_until', $EVAL_ERROR ); |
| 380 |
|
| 381 |
generate_cert( |
| 382 |
"$entity_root.csr.pem", |
| 383 |
"$issuer_root.key.pem", |
| 384 |
"$entity_root.cert.pem", |
| 385 |
{ |
| 386 |
extensions => $entity_cfg->{cert}->{extensions}, |
| 387 |
md_algorithm => $entity_cfg->{cert}->{md_algorithm}, |
| 388 |
purpose => $entity_cfg->{cert}->{purpose}, |
| 389 |
serial => $entity_cfg->{cert}->{serial}, |
| 390 |
valid_from => $valid_from, |
| 391 |
valid_until => $valid_until, |
| 392 |
@issuer_opts, |
| 393 |
} |
| 394 |
); |
| 395 |
} or fatal( "Could not generate '$entity_root.cert.pem'", $EVAL_ERROR ); |
| 396 |
|
| 397 |
print "\tDER certificate: $entity_root.cert.der\n"; |
| 398 |
|
| 399 |
eval { |
| 400 |
convert_cert( |
| 401 |
"$entity_root.cert.pem", |
| 402 |
"$entity_root.cert.der", |
| 403 |
{ |
| 404 |
format => 'der', |
| 405 |
} |
| 406 |
); |
| 407 |
} or fatal( "Could not generate '$entity_root.cert.der'", $EVAL_ERROR ); |
| 408 |
|
| 409 |
print "\tCertificate info: $entity_root.cert.dump\n"; |
| 410 |
|
| 411 |
eval { |
| 412 |
dump_cert_info( |
| 413 |
"$entity_root.cert.pem", |
| 414 |
"$entity_root.cert.dump" |
| 415 |
); |
| 416 |
} or fatal( "Could not generate '$entity_root.cert.dump'", $EVAL_ERROR ); |
| 417 |
|
| 418 |
print "\tPEM certificate chain: $entity_root.certchain.pem\n"; |
| 419 |
|
| 420 |
eval { |
| 421 |
generate_cert_chain( |
| 422 |
[ |
| 423 |
map |
| 424 |
{ catfile( $args->{output}, "$_.cert.pem" ) } |
| 425 |
@{ issuer_chain( $entity ) } |
| 426 |
], |
| 427 |
"$entity_root.certchain.pem" |
| 428 |
); |
| 429 |
} or fatal( "Could not generate '$entity_root.certchain.pem'", |
| 430 |
$EVAL_ERROR ); |
| 431 |
|
| 432 |
print "\tDER certificate chain: $entity_root.certchain.der\n"; |
| 433 |
|
| 434 |
eval { |
| 435 |
generate_cert_chain( |
| 436 |
[ |
| 437 |
map |
| 438 |
{ catfile( $args->{output}, "$_.cert.der" ) } |
| 439 |
@{ issuer_chain( $entity ) } |
| 440 |
], |
| 441 |
"$entity_root.certchain.der" |
| 442 |
) |
| 443 |
} or fatal( "Could not generate '$entity_root.certchain.der'", |
| 444 |
$EVAL_ERROR ); |
| 445 |
|
| 446 |
if ( exists $entity_cfg->{cert}->{revoke_reason} ) { |
| 447 |
print "\tPEM CRL for signing entity: $issuer_root.crl.pem\n"; |
| 448 |
|
| 449 |
eval { |
| 450 |
revoke_cert( |
| 451 |
"$entity_root.cert.pem", |
| 452 |
"$issuer_root.key.pem", |
| 453 |
"$issuer_root.cert.pem", |
| 454 |
"$issuer_root.crl.pem", |
| 455 |
{ |
| 456 |
crl_last_update => $issuer_cfg->{crl}->{last_update}, |
| 457 |
crl_md_algorithm => $issuer_cfg->{crl}->{md_algorithm}, |
| 458 |
crl_next_update => $issuer_cfg->{crl}->{next_update}, |
| 459 |
crl_number => $issuer_cfg->{crl}->{number}, |
| 460 |
reason => $entity_cfg->{cert}->{revoke_reason}, |
| 461 |
time => $entity_cfg->{cert}->{revoke_time}, |
| 462 |
} |
| 463 |
); |
| 464 |
} or fatal( "Could not generate '$issuer_root.crl.pem'", $EVAL_ERROR ); |
| 465 |
|
| 466 |
print "\tDER CRL for signing entity: $issuer_root.crl.der\n"; |
| 467 |
|
| 468 |
eval { |
| 469 |
convert_crl( |
| 470 |
"$issuer_root.crl.pem", |
| 471 |
"$issuer_root.crl.der", |
| 472 |
{ |
| 473 |
format => 'der', |
| 474 |
} |
| 475 |
); |
| 476 |
} or fatal( "Could not generate '$issuer_root.crl.der'", $EVAL_ERROR ); |
| 477 |
} |
| 478 |
|
| 479 |
my @extra_certs = map |
| 480 |
{ catfile( $args->{output}, "$_.cert.pem" ) } |
| 481 |
@{ issuer_chain( $entity ) }; |
| 482 |
|
| 483 |
# The certificate for this entity is at the start of the chain, but we just |
| 484 |
# want the certificates in the issuer chain |
| 485 |
shift @extra_certs; |
| 486 |
|
| 487 |
my @extra_certs_opts = @extra_certs |
| 488 |
? ( |
| 489 |
extra_certs => \@extra_certs, |
| 490 |
) |
| 491 |
: (); |
| 492 |
|
| 493 |
print "\tPKCS#12 archive: $entity_root.p12\n"; |
| 494 |
|
| 495 |
eval { |
| 496 |
generate_pkcs12( |
| 497 |
"$entity_root.key.pem", |
| 498 |
"$entity_root.cert.pem", |
| 499 |
"$entity_root.p12", |
| 500 |
{ |
| 501 |
name => "$entity: unencrypted, no certificate chain", |
| 502 |
} |
| 503 |
); |
| 504 |
} or fatal( "Could not generate '$entity_root.p12'", $EVAL_ERROR ); |
| 505 |
|
| 506 |
print "\tPKCS#12 archive with certificate chain: ", |
| 507 |
"$entity_root.certchain.p12\n"; |
| 508 |
|
| 509 |
eval { |
| 510 |
generate_pkcs12( |
| 511 |
"$entity_root.key.pem", |
| 512 |
"$entity_root.cert.pem", |
| 513 |
"$entity_root.certchain.p12", |
| 514 |
{ |
| 515 |
name => "$entity: unencrypted, certificate chain", |
| 516 |
@extra_certs_opts, |
| 517 |
} |
| 518 |
); |
| 519 |
} or fatal( "Could not generate '$entity_root.certchain.p12'", |
| 520 |
$EVAL_ERROR ); |
| 521 |
|
| 522 |
print "\tEncrypted PKCS#12 archive: $entity_root.enc.p12\n"; |
| 523 |
|
| 524 |
eval { |
| 525 |
generate_pkcs12( |
| 526 |
"$entity_root.key.pem", |
| 527 |
"$entity_root.cert.pem", |
| 528 |
"$entity_root.enc.p12", |
| 529 |
{ |
| 530 |
name => "$entity: encrypted, no certificate chain", |
| 531 |
passphrase => $entity_cfg->{pkcs12}->{passphrase}, |
| 532 |
} |
| 533 |
); |
| 534 |
} or fatal( "Could not generate '$entity_root.enc.p12'", $EVAL_ERROR ); |
| 535 |
|
| 536 |
print "\tEncrypted PKCS#12 archive with certificate chain: ", |
| 537 |
"$entity_root.certchain.enc.p12\n"; |
| 538 |
|
| 539 |
eval { |
| 540 |
generate_pkcs12( |
| 541 |
"$entity_root.key.pem", |
| 542 |
"$entity_root.cert.pem", |
| 543 |
"$entity_root.certchain.enc.p12", |
| 544 |
{ |
| 545 |
name => "$entity: encrypted, certificate chain", |
| 546 |
passphrase => $entity_cfg->{pkcs12}->{passphrase}, |
| 547 |
@extra_certs_opts, |
| 548 |
} |
| 549 |
); |
| 550 |
} or fatal( "Could not generate '$entity_root.certchain.enc.p12'", |
| 551 |
$EVAL_ERROR ); |
| 552 |
|
| 553 |
return 1; |
| 554 |
} |
| 555 |
|
| 556 |
sub generate_key { |
| 557 |
my ( $out_key_path, $params ) = @_; |
| 558 |
|
| 559 |
my $algorithms = { |
| 560 |
'ec' => { |
| 561 |
openssl_name => 'EC', |
| 562 |
}, |
| 563 |
'ed25519' => { |
| 564 |
openssl_name => 'ED25519', |
| 565 |
}, |
| 566 |
'ed448' => { |
| 567 |
openssl_name => 'ED448', |
| 568 |
}, |
| 569 |
'rsa' => { |
| 570 |
openssl_name => 'RSA', |
| 571 |
size_param => 'rsa_keygen_bits', |
| 572 |
}, |
| 573 |
'rsa-pss' => { |
| 574 |
openssl_name => 'RSA-PSS', |
| 575 |
size_param => 'rsa_keygen_bits', |
| 576 |
}, |
| 577 |
'x25519' => { |
| 578 |
openssl_name => 'X25519', |
| 579 |
}, |
| 580 |
'x448' => { |
| 581 |
openssl_name => 'X448', |
| 582 |
}, |
| 583 |
}; |
| 584 |
|
| 585 |
if ( !exists $params->{algorithm} ) { |
| 586 |
fatal('missing key algorithm'); |
| 587 |
} |
| 588 |
|
| 589 |
if ( !exists $algorithms->{ $params->{algorithm} } ) { |
| 590 |
fatal("unknown key algorithm '$params->{algorithm}'"); |
| 591 |
} |
| 592 |
|
| 593 |
my $algorithm = $algorithms->{ $params->{algorithm} }; |
| 594 |
my @genpkey_opts; |
| 595 |
|
| 596 |
if ( exists $algorithm->{size_param} ) { |
| 597 |
if ( !exists $params->{size} ) { |
| 598 |
fatal("key algorithm '$params->{algorithm}' requires a key size"); |
| 599 |
} |
| 600 |
|
| 601 |
@genpkey_opts = ( |
| 602 |
'-pkeyopt', "$algorithm->{size_param}:$params->{size}" |
| 603 |
); |
| 604 |
} |
| 605 |
|
| 606 |
# "openssl genpkey" exports keys in PKCS#8 format (which isn't recognised by |
| 607 |
# OpenSSL 0.9.8), and there's no way to export in traditional SSLeay format |
| 608 |
# directly - write the PKCS#8-formatted key to a temporary file, and then |
| 609 |
# use "openssl pkey" to convert it to SSLeay format |
| 610 |
my $out_key_name = ( splitpath($out_key_path) )[2]; |
| 611 |
my $tmp_key_path = catfile( $tmp->dirname(), $out_key_name ); |
| 612 |
|
| 613 |
openssl_cmd( |
| 614 |
[ |
| 615 |
'genpkey', |
| 616 |
'-out', $tmp_key_path, |
| 617 |
'-algorithm', $algorithm->{openssl_name}, |
| 618 |
@genpkey_opts, |
| 619 |
] |
| 620 |
); |
| 621 |
|
| 622 |
return openssl_cmd( |
| 623 |
[ |
| 624 |
'pkey', |
| 625 |
'-in', $tmp_key_path, |
| 626 |
'-out', $out_key_path, |
| 627 |
'-traditional', |
| 628 |
] |
| 629 |
); |
| 630 |
} |
| 631 |
|
| 632 |
sub convert_key { |
| 633 |
my ( $in_key_path, $out_key_path, $params ) = @_; |
| 634 |
|
| 635 |
my $formats = { |
| 636 |
pem => 'PEM', |
| 637 |
der => 'DER', |
| 638 |
}; |
| 639 |
|
| 640 |
( my $in_format = $in_key_path ) =~ s{.*\.}{}; |
| 641 |
|
| 642 |
my $out_format = delete $params->{format} || 'pem'; |
| 643 |
|
| 644 |
if ( !exists $formats->{$in_format} ) { |
| 645 |
fatal("unknown key input format '$in_format'"); |
| 646 |
} |
| 647 |
|
| 648 |
if ( !exists $formats->{$out_format} ) { |
| 649 |
fatal("unknown key output format '$out_format'"); |
| 650 |
} |
| 651 |
|
| 652 |
my @encrypt_opts = exists $params->{passphrase} |
| 653 |
? ( |
| 654 |
'-aes128', |
| 655 |
'-passout', 'stdin', |
| 656 |
) |
| 657 |
: (); |
| 658 |
|
| 659 |
return openssl_cmd( |
| 660 |
[ |
| 661 |
'pkey', |
| 662 |
'-in', $in_key_path, |
| 663 |
'-inform', $formats->{$in_format}, |
| 664 |
'-out', $out_key_path, |
| 665 |
'-outform', $formats->{$out_format}, |
| 666 |
'-traditional', |
| 667 |
@encrypt_opts, |
| 668 |
], |
| 669 |
$params->{passphrase} |
| 670 |
); |
| 671 |
} |
| 672 |
|
| 673 |
sub generate_csr { |
| 674 |
my ( $in_key_path, $out_csr_path, $params ) = @_; |
| 675 |
|
| 676 |
my $formats = { |
| 677 |
pem => 'PEM', |
| 678 |
der => 'DER', |
| 679 |
}; |
| 680 |
|
| 681 |
my $format = delete $params->{format} || 'pem'; |
| 682 |
|
| 683 |
if ( !exists $formats->{$format} ) { |
| 684 |
fatal("unknown CSR output format '$format'"); |
| 685 |
} |
| 686 |
|
| 687 |
if ( !exists $params->{md_algorithm} ) { |
| 688 |
fatal('missing message digest algorithm'); |
| 689 |
} |
| 690 |
|
| 691 |
my $digest_opt = '-' . $params->{md_algorithm}; |
| 692 |
|
| 693 |
return openssl_cmd( |
| 694 |
[ |
| 695 |
'req', |
| 696 |
'-config', '-', |
| 697 |
'-new', |
| 698 |
'-key', $in_key_path, |
| 699 |
'-out', $out_csr_path, |
| 700 |
'-outform', $formats->{$format}, |
| 701 |
'-subj', subject_string( @{ $params->{subject} } ), |
| 702 |
'-multivalue-rdn', |
| 703 |
$digest_opt, |
| 704 |
], |
| 705 |
openssl_config() |
| 706 |
); |
| 707 |
} |
| 708 |
|
| 709 |
sub convert_csr { |
| 710 |
my ( $in_csr_path, $out_csr_path, $params ) = @_; |
| 711 |
|
| 712 |
my $formats = { |
| 713 |
pem => 'PEM', |
| 714 |
der => 'DER', |
| 715 |
}; |
| 716 |
|
| 717 |
( my $in_format = $in_csr_path ) =~ s{.*\.}{}; |
| 718 |
|
| 719 |
my $out_format = delete $params->{format} || 'pem'; |
| 720 |
|
| 721 |
if ( !exists $formats->{$in_format} ) { |
| 722 |
fatal("unknown CSR input format '$in_format'"); |
| 723 |
} |
| 724 |
|
| 725 |
if ( !exists $formats->{$out_format} ) { |
| 726 |
fatal("unknown CSR output format '$out_format'"); |
| 727 |
} |
| 728 |
|
| 729 |
return openssl_cmd( |
| 730 |
[ |
| 731 |
'req', |
| 732 |
'-in', $in_csr_path, |
| 733 |
'-inform', $formats->{$in_format}, |
| 734 |
'-out', $out_csr_path, |
| 735 |
'-outform', $formats->{$out_format}, |
| 736 |
] |
| 737 |
); |
| 738 |
} |
| 739 |
|
| 740 |
sub generate_cert { |
| 741 |
my ( $in_csr_path, $issuer_key_path, $out_cert_path, $params ) = @_; |
| 742 |
|
| 743 |
if ( !exists $params->{md_algorithm} ) { |
| 744 |
fatal('missing message digest algorithm'); |
| 745 |
} |
| 746 |
|
| 747 |
my @signing_opts = exists $params->{issuer_cert_path} |
| 748 |
? ( |
| 749 |
'-cert', $params->{issuer_cert_path}, |
| 750 |
) |
| 751 |
: ( |
| 752 |
'-selfsign', |
| 753 |
'-cert', 'ignored', |
| 754 |
); |
| 755 |
|
| 756 |
my $tmp_root = do { |
| 757 |
my $file = ( splitpath($issuer_key_path) )[2]; |
| 758 |
|
| 759 |
$file =~ s/(?:\.key)?\.(?:pem|der)$//; |
| 760 |
|
| 761 |
my $dir = catfile( $tmp->dirname(), $file ); |
| 762 |
|
| 763 |
if ( !-d $dir ) { |
| 764 |
mkdir $dir |
| 765 |
or fatal( "could not create directory $dir", $OS_ERROR ); |
| 766 |
} |
| 767 |
|
| 768 |
$dir; |
| 769 |
}; |
| 770 |
|
| 771 |
my $serial_file = catfile( $tmp_root, 'serial' ); |
| 772 |
open my $serial_fh, '>', $serial_file |
| 773 |
or fatal( "could not write serial file $serial_file", $OS_ERROR ); |
| 774 |
printf {$serial_fh} '%02x', $params->{serial}; |
| 775 |
close $serial_fh; |
| 776 |
|
| 777 |
my $db_file = catfile( $tmp_root, 'db' ); |
| 778 |
open my $db_fh, '>>', $db_file |
| 779 |
or fatal( "could not touch database file $db_file", $OS_ERROR ); |
| 780 |
close $db_fh; |
| 781 |
|
| 782 |
return openssl_cmd( |
| 783 |
[ |
| 784 |
'ca', |
| 785 |
'-verbose', |
| 786 |
'-batch', |
| 787 |
'-config', '-', |
| 788 |
'-name', 'ca_conf', |
| 789 |
'-in', $in_csr_path, |
| 790 |
'-out', $out_cert_path, |
| 791 |
'-keyfile', $issuer_key_path, |
| 792 |
'-startdate', $params->{valid_from}, |
| 793 |
'-enddate', $params->{valid_until}, |
| 794 |
'-md', $params->{md_algorithm}, |
| 795 |
'-extensions', 'exts_' . $params->{purpose}, |
| 796 |
'-notext', |
| 797 |
'-utf8', |
| 798 |
'-multivalue-rdn', |
| 799 |
@signing_opts, |
| 800 |
], |
| 801 |
openssl_config( |
| 802 |
extensions => ( |
| 803 |
exists $params->{extensions} |
| 804 |
? extensions_section( $params->{extensions} ) |
| 805 |
: q{} |
| 806 |
), |
| 807 |
certs_path => $tmp_root, |
| 808 |
database_path => $db_file, |
| 809 |
serial_path => $serial_file, |
| 810 |
) |
| 811 |
); |
| 812 |
} |
| 813 |
|
| 814 |
sub convert_cert { |
| 815 |
my ( $in_cert_path, $out_cert_path, $params ) = @_; |
| 816 |
|
| 817 |
my $formats = { |
| 818 |
pem => 'PEM', |
| 819 |
der => 'DER', |
| 820 |
}; |
| 821 |
|
| 822 |
( my $in_format = $in_cert_path ) =~ s{.*\.}{}; |
| 823 |
|
| 824 |
my $out_format = delete $params->{format} || 'pem'; |
| 825 |
|
| 826 |
if ( !exists $formats->{$in_format} ) { |
| 827 |
fatal("unknown certificate input format '$in_format'"); |
| 828 |
} |
| 829 |
|
| 830 |
if ( !exists $formats->{$out_format} ) { |
| 831 |
fatal("unknown certificate output format '$out_format'"); |
| 832 |
} |
| 833 |
|
| 834 |
return openssl_cmd( |
| 835 |
[ |
| 836 |
'x509', |
| 837 |
'-in', $in_cert_path, |
| 838 |
'-inform', $formats->{$in_format}, |
| 839 |
'-out', $out_cert_path, |
| 840 |
'-outform', $formats->{$out_format}, |
| 841 |
] |
| 842 |
); |
| 843 |
} |
| 844 |
|
| 845 |
sub dump_cert_info { |
| 846 |
my ( $in_cert_path, $out_dump_path ) = @_; |
| 847 |
|
| 848 |
my $cwd = dirname(__FILE__); |
| 849 |
|
| 850 |
open my $out_fh, '>', $out_dump_path |
| 851 |
or fatal( "could not write $out_dump_path", $OS_ERROR ); |
| 852 |
|
| 853 |
my $run = eval { |
| 854 |
start( |
| 855 |
[ |
| 856 |
$EXECUTABLE_NAME, |
| 857 |
catfile( $cwd, '..', 'examples', 'x509_cert_details.pl' ), |
| 858 |
'-dump', |
| 859 |
'-pem', $in_cert_path |
| 860 |
], |
| 861 |
'>', |
| 862 |
sub { |
| 863 |
print {$out_fh} $_[0]; |
| 864 |
}, |
| 865 |
'2>', |
| 866 |
sub { |
| 867 |
if ( $args->{verbose} ) { |
| 868 |
printf "[x509_cert_details.pl stderr] %s\n", $_[0]; |
| 869 |
} |
| 870 |
} |
| 871 |
); |
| 872 |
} or fatal( 'could not run examples/x509_cert_details.pl', $EVAL_ERROR ); |
| 873 |
|
| 874 |
$run->finish(); |
| 875 |
|
| 876 |
close $out_fh; |
| 877 |
|
| 878 |
if ( $run->result() != 0 ) { |
| 879 |
fatal( 'examples/x509_cert_details.pl exited with exit code ' |
| 880 |
. $run->result() ); |
| 881 |
} |
| 882 |
|
| 883 |
return 1; |
| 884 |
} |
| 885 |
|
| 886 |
sub generate_cert_chain { |
| 887 |
my ( $in_cert_paths, $out_cert_path ) = @_; |
| 888 |
|
| 889 |
open my $out_fh, '>', $out_cert_path |
| 890 |
or fatal( "could not write certificate chain file $out_cert_path", |
| 891 |
$OS_ERROR ); |
| 892 |
|
| 893 |
for my $in ( @{$in_cert_paths} ) { |
| 894 |
open my $in_fh, '<', $in |
| 895 |
or fatal( "could not read certificate file $in", $OS_ERROR ); |
| 896 |
|
| 897 |
my $cert = do { local $RS = undef; <$in_fh> }; |
| 898 |
|
| 899 |
print {$out_fh} $cert; |
| 900 |
|
| 901 |
close $in_fh; |
| 902 |
} |
| 903 |
|
| 904 |
close $out_fh; |
| 905 |
|
| 906 |
return 1; |
| 907 |
} |
| 908 |
|
| 909 |
sub revoke_cert { |
| 910 |
my ( $in_cert_path, $issuer_key_path, $issuer_cert_path, $out_crl_path, |
| 911 |
$params ) = @_; |
| 912 |
|
| 913 |
my $tmp_root = do { |
| 914 |
my ( undef, undef, $file ) = splitpath($issuer_key_path); |
| 915 |
|
| 916 |
$file =~ s/(?:\.key)?\.(?:pem|der)$//; |
| 917 |
|
| 918 |
my $dir = catfile( $tmp->dirname(), $file ); |
| 919 |
|
| 920 |
if ( !-d $dir ) { |
| 921 |
mkdir $dir |
| 922 |
or fatal( "could not create directory $dir", $OS_ERROR ); |
| 923 |
} |
| 924 |
|
| 925 |
$dir; |
| 926 |
}; |
| 927 |
|
| 928 |
my $serial_file = catfile( $tmp_root, 'serial' ); |
| 929 |
|
| 930 |
my ( $stdout, $stderr ) = openssl_cmd( |
| 931 |
[ |
| 932 |
'x509', |
| 933 |
'-in', $in_cert_path, |
| 934 |
'-noout', |
| 935 |
'-serial', |
| 936 |
] |
| 937 |
); |
| 938 |
|
| 939 |
( my $in_cert_serial = join "\n", @{$stdout} ) =~ s/^serial=//; |
| 940 |
|
| 941 |
if ( $in_cert_serial !~ /^[\da-f]+$/i ) { |
| 942 |
fatal('could not get serial number for revoked certificate'); |
| 943 |
} |
| 944 |
|
| 945 |
my $db_file = catfile( $tmp_root, 'db' ); |
| 946 |
open my $db_fh, '<', $db_file |
| 947 |
or fatal( "could not read database file $db_file", $OS_ERROR ); |
| 948 |
|
| 949 |
my @entries; |
| 950 |
|
| 951 |
while ( defined( my $entry = <$db_fh> ) ) { |
| 952 |
chomp $entry; |
| 953 |
my @fields = split /\t/, $entry; |
| 954 |
|
| 955 |
if ( $fields[3] eq $in_cert_serial ) { |
| 956 |
$fields[0] = 'R'; |
| 957 |
$fields[2] = short_time_string( $params->{time} ); |
| 958 |
|
| 959 |
if ( defined $params->{reason} ) { |
| 960 |
$fields[2] .= ',' . $params->{reason}; |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
push @entries, join "\t", @fields; |
| 965 |
} |
| 966 |
|
| 967 |
close $db_fh; |
| 968 |
|
| 969 |
open $db_fh, '>', $db_file |
| 970 |
or fatal( "could not write database file $db_file", $OS_ERROR ); |
| 971 |
|
| 972 |
for my $entry (@entries) { |
| 973 |
print {$db_fh} $entry, "\n"; |
| 974 |
} |
| 975 |
|
| 976 |
close $db_fh; |
| 977 |
|
| 978 |
my $crl_number_file = catfile( $tmp_root, 'crl_number' ); |
| 979 |
open my $crl_number_fh, '>', $crl_number_file |
| 980 |
or fatal( "could not write CRL number file $crl_number_file", $OS_ERROR ); |
| 981 |
printf {$crl_number_fh} '%02x', $params->{crl_number}; |
| 982 |
close $crl_number_fh; |
| 983 |
|
| 984 |
return openssl_cmd( |
| 985 |
[ |
| 986 |
'ca', |
| 987 |
'-verbose', |
| 988 |
'-batch', |
| 989 |
'-gencrl', |
| 990 |
'-config', '-', |
| 991 |
'-name', 'ca_conf', |
| 992 |
'-keyfile', $issuer_key_path, |
| 993 |
'-cert', $issuer_cert_path, |
| 994 |
'-out', $out_crl_path, |
| 995 |
'-crl_lastupdate', time_string( $params->{crl_last_update} ), |
| 996 |
'-crl_nextupdate', time_string( $params->{crl_next_update} ), |
| 997 |
'-md', $params->{crl_md_algorithm}, |
| 998 |
], |
| 999 |
openssl_config( |
| 1000 |
certs_path => $tmp_root, |
| 1001 |
crl_number_path => $crl_number_file, |
| 1002 |
database_path => $db_file, |
| 1003 |
serial_path => $serial_file, |
| 1004 |
) |
| 1005 |
); |
| 1006 |
} |
| 1007 |
|
| 1008 |
sub convert_crl { |
| 1009 |
my ( $in_crl_path, $out_crl_path, $params ) = @_; |
| 1010 |
|
| 1011 |
my $formats = { |
| 1012 |
pem => 'PEM', |
| 1013 |
der => 'DER', |
| 1014 |
}; |
| 1015 |
|
| 1016 |
( my $in_format = $in_crl_path ) =~ s{.*\.}{}; |
| 1017 |
|
| 1018 |
my $out_format = delete $params->{format} || 'pem'; |
| 1019 |
|
| 1020 |
if ( !exists $formats->{$in_format} ) { |
| 1021 |
fatal("unknown CRL input format '$in_format'"); |
| 1022 |
} |
| 1023 |
|
| 1024 |
if ( !exists $formats->{$out_format} ) { |
| 1025 |
fatal("unknown CRL output format '$out_format'"); |
| 1026 |
} |
| 1027 |
|
| 1028 |
return openssl_cmd( |
| 1029 |
[ |
| 1030 |
'crl', |
| 1031 |
'-in', $in_crl_path, |
| 1032 |
'-inform', $formats->{$in_format}, |
| 1033 |
'-out', $out_crl_path, |
| 1034 |
'-outform', $formats->{$out_format}, |
| 1035 |
] |
| 1036 |
); |
| 1037 |
} |
| 1038 |
|
| 1039 |
sub generate_pkcs12 { |
| 1040 |
my ( $in_key_path, $in_cert_path, $out_p12_path, $params ) = @_; |
| 1041 |
|
| 1042 |
my $cert_chain_path; |
| 1043 |
|
| 1044 |
if ( exists $params->{extra_certs} ) { |
| 1045 |
my ( undef, undef, $file ) = splitpath($in_key_path); |
| 1046 |
|
| 1047 |
$file =~ s/(?:\.key)?\.(?:pem|der)$//; |
| 1048 |
|
| 1049 |
my $dir = catfile( $tmp->dirname(), $file ); |
| 1050 |
|
| 1051 |
if ( !-d $dir ) { |
| 1052 |
mkdir $dir |
| 1053 |
or fatal( 'could not create directory $dir', $OS_ERROR ); |
| 1054 |
} |
| 1055 |
|
| 1056 |
$cert_chain_path = catfile( $dir, 'pkcs12_cert_chain.pem' ); |
| 1057 |
|
| 1058 |
generate_cert_chain( |
| 1059 |
$params->{extra_certs}, |
| 1060 |
$cert_chain_path |
| 1061 |
); |
| 1062 |
} |
| 1063 |
|
| 1064 |
my @name_opt = exists $params->{name} |
| 1065 |
? ( |
| 1066 |
'-name', $params->{name}, |
| 1067 |
) |
| 1068 |
: (); |
| 1069 |
|
| 1070 |
my @cert_opt = exists $params->{extra_certs} |
| 1071 |
? ( |
| 1072 |
'-certfile', $cert_chain_path, |
| 1073 |
) |
| 1074 |
: (); |
| 1075 |
|
| 1076 |
my @encrypt_opts = exists $params->{passphrase} |
| 1077 |
? ( |
| 1078 |
'-passout', 'stdin', |
| 1079 |
'-keypbe', 'pbeWithSHA1And3-KeyTripleDES-CBC', |
| 1080 |
'-certpbe', 'pbeWithSHA1And3-KeyTripleDES-CBC', |
| 1081 |
) |
| 1082 |
: ( |
| 1083 |
'-passout', 'pass:', |
| 1084 |
'-keypbe', 'NONE', |
| 1085 |
'-certpbe', 'NONE', |
| 1086 |
'-nomaciter', |
| 1087 |
); |
| 1088 |
|
| 1089 |
return openssl_cmd( |
| 1090 |
[ |
| 1091 |
'pkcs12', |
| 1092 |
'-export', |
| 1093 |
'-inkey', $in_key_path, |
| 1094 |
'-in', $in_cert_path, |
| 1095 |
'-out', $out_p12_path, |
| 1096 |
'-rand', $in_key_path, |
| 1097 |
'-no-CAfile', |
| 1098 |
'-no-CApath', |
| 1099 |
@name_opt, |
| 1100 |
@cert_opt, |
| 1101 |
@encrypt_opts, |
| 1102 |
], |
| 1103 |
$params->{passphrase} |
| 1104 |
); |
| 1105 |
} |
| 1106 |
|
| 1107 |
sub check_openssl_version { |
| 1108 |
my ($params) = @_; |
| 1109 |
|
| 1110 |
my $min_version = delete $params->{min_version} |
| 1111 |
or fatal('missing minimum OpenSSL version'); |
| 1112 |
|
| 1113 |
my $min_version_match = delete $params->{min_version_match} |
| 1114 |
or fatal('missing minimum OpenSSL version regex'); |
| 1115 |
|
| 1116 |
my ( $stdout, $stderr ); |
| 1117 |
|
| 1118 |
my $run = eval { |
| 1119 |
start( |
| 1120 |
[ 'openssl', 'version' ], |
| 1121 |
\undef, |
| 1122 |
\$stdout, |
| 1123 |
\$stderr, |
| 1124 |
timeout( 3 ) |
| 1125 |
); |
| 1126 |
} or fatal( "could not run `openssl version`", $EVAL_ERROR ); |
| 1127 |
|
| 1128 |
$run->finish(); |
| 1129 |
|
| 1130 |
if ( $run->result() != 0 ) { |
| 1131 |
fatal( "`openssl version` exited with exit code " . $run->result() ); |
| 1132 |
} |
| 1133 |
|
| 1134 |
my ($openssl_version) = $stdout =~ m{^OpenSSL (.+?) } |
| 1135 |
or fatal("`openssl` is not the OpenSSL command line utility"); |
| 1136 |
|
| 1137 |
if ( $openssl_version !~ $min_version_match ) { |
| 1138 |
fatal( "OpenSSL >= $min_version required, but `openssl` is version " |
| 1139 |
. $openssl_version ); |
| 1140 |
} |
| 1141 |
|
| 1142 |
my $net_ssleay_version = eval { |
| 1143 |
use Net::SSLeay; |
| 1144 |
Net::SSLeay::SSLeay_version( Net::SSLeay::SSLEAY_VERSION() ); |
| 1145 |
} or fatal( 'could not load Net::SSLeay', $EVAL_ERROR ); |
| 1146 |
|
| 1147 |
($net_ssleay_version) = $net_ssleay_version =~ m{^OpenSSL (.+?) } |
| 1148 |
or fatal('Net::SSLeay was not built against OpenSSL'); |
| 1149 |
|
| 1150 |
if ( $net_ssleay_version !~ $min_version_match ) { |
| 1151 |
fatal( "Net::SSLeay must be built against OpenSSL >= $min_version, but " |
| 1152 |
. "it is built against version $net_ssleay_version" ); |
| 1153 |
} |
| 1154 |
|
| 1155 |
return 1; |
| 1156 |
} |
| 1157 |
|
| 1158 |
sub openssl_cmd { |
| 1159 |
my ( $opts, $stdin ) = @_; |
| 1160 |
|
| 1161 |
my $wantarray = wantarray; |
| 1162 |
|
| 1163 |
my $stdout = []; |
| 1164 |
my $stderr = []; |
| 1165 |
|
| 1166 |
my $print = sub { |
| 1167 |
my ( $prefix, $data ) = @_; |
| 1168 |
|
| 1169 |
for my $line ( split /\r?\n/, $data ) { |
| 1170 |
printf "[OpenSSL %s] %s\n", $prefix, $line; |
| 1171 |
} |
| 1172 |
}; |
| 1173 |
|
| 1174 |
if ( $args->{verbose} ) { |
| 1175 |
print "Running `openssl ", join( q{ }, @{$opts} ), "`\n"; |
| 1176 |
} |
| 1177 |
|
| 1178 |
my $cmd = [ 'openssl', @{$opts} ]; |
| 1179 |
my $cmd_string = join ' ', @{$cmd}; |
| 1180 |
|
| 1181 |
my $run = eval { |
| 1182 |
start( |
| 1183 |
$cmd, |
| 1184 |
\$stdin, |
| 1185 |
sub { |
| 1186 |
if ($wantarray) { |
| 1187 |
chomp $_[0]; |
| 1188 |
push @{$stdout}, $_[0]; |
| 1189 |
} |
| 1190 |
elsif ( $args->{verbose} ) { |
| 1191 |
$print->( 'stdout', $_[0] ); |
| 1192 |
} |
| 1193 |
}, |
| 1194 |
sub { |
| 1195 |
if ($wantarray) { |
| 1196 |
chomp $_[0]; |
| 1197 |
push @{$stderr}, $_[0]; |
| 1198 |
} |
| 1199 |
elsif ( $args->{verbose} ) { |
| 1200 |
$print->( 'stderr', $_[0] ); |
| 1201 |
} |
| 1202 |
} |
| 1203 |
); |
| 1204 |
} or fatal( "failed to run `$cmd_string`", $EVAL_ERROR ); |
| 1205 |
|
| 1206 |
$run->finish(); |
| 1207 |
|
| 1208 |
if ( $run->result() != 0 ) { |
| 1209 |
fatal( "`$cmd_string` exited with exit code " . $run->result() ); |
| 1210 |
} |
| 1211 |
|
| 1212 |
return $wantarray |
| 1213 |
? ( $stdout, $stderr ) |
| 1214 |
: 1; |
| 1215 |
} |
| 1216 |
|
| 1217 |
sub fatal { |
| 1218 |
my ( $message, $cause ) = @_; |
| 1219 |
|
| 1220 |
die Error->new( $message, $cause ); |
| 1221 |
} |
| 1222 |
|
| 1223 |
package Error; |
| 1224 |
|
| 1225 |
use overload ( |
| 1226 |
q{""} => sub { |
| 1227 |
my ($self) = @_; |
| 1228 |
|
| 1229 |
return defined $self->{cause} |
| 1230 |
? "$self->{message}: $self->{cause}" |
| 1231 |
: $self->{message}; |
| 1232 |
}, |
| 1233 |
fallback => 1, |
| 1234 |
); |
| 1235 |
|
| 1236 |
sub new { |
| 1237 |
my ( $class, $message, $cause ) = @_; |
| 1238 |
|
| 1239 |
return bless { |
| 1240 |
message => $message, |
| 1241 |
cause => $cause, |
| 1242 |
}, $class; |
| 1243 |
} |
| 1244 |
|
| 1245 |
package main; |
| 1246 |
|
| 1247 |
=pod |
| 1248 |
|
| 1249 |
=encoding utf-8 |
| 1250 |
|
| 1251 |
=head1 NAME |
| 1252 |
|
| 1253 |
C<generate-test-pki> - Generate a PKI for the Net-SSLeay test suite |
| 1254 |
|
| 1255 |
=head1 VERSION |
| 1256 |
|
| 1257 |
This document describes version 1.96 of C<generate-test-pki>. |
| 1258 |
|
| 1259 |
=head1 USAGE |
| 1260 |
|
| 1261 |
# With openssl >= 3.0.0-alpha7 in PATH, and a version of Net::SSLeay built |
| 1262 |
# against OpenSSL >= 3.0.0-alpha7 in PERL5LIB: |
| 1263 |
generate-test-pki \ |
| 1264 |
-c pki.cfg \ |
| 1265 |
-o pki-output-dir |
| 1266 |
|
| 1267 |
=head1 DESCRIPTION |
| 1268 |
|
| 1269 |
The Net-SSLeay test suite relies on a dummy X.509 public key infrastructure |
| 1270 |
(PKI). Occasionally, this PKI needs to be modified - for example, to add a |
| 1271 |
certificate with certain properties when writing a new test - but maintaining it |
| 1272 |
by hand is time-consuming, difficult, and error-prone. |
| 1273 |
|
| 1274 |
C<generate-test-pki> simplifies maintenance of the PKI by generating it from |
| 1275 |
scratch using the OpenSSL command line utility, based on the structure defined |
| 1276 |
in a simple configuration file. The files it generates can then be used in |
| 1277 |
Net-SSLeay test scripts. |
| 1278 |
|
| 1279 |
=head1 DEPENDENCIES |
| 1280 |
|
| 1281 |
C<generate-test-pki> requires at least version 3.0.0-alpha7 of the OpenSSL |
| 1282 |
command line utility to be present either in I<PATH> as C<openssl> or at the |
| 1283 |
path given by the B<-b> option (see L</OPTIONS>). Additionally, the first |
| 1284 |
occurrance of Net::SSLeay in I<PERL5LIB> must be built against at least version |
| 1285 |
3.0.0-alpha7 of OpenSSL. |
| 1286 |
|
| 1287 |
LibreSSL is not supported, since its command line utility lacks some of the |
| 1288 |
functionality relied on by this program. |
| 1289 |
|
| 1290 |
=head1 OPTIONS |
| 1291 |
|
| 1292 |
C<generate-test-pki> accepts the following command line options: |
| 1293 |
|
| 1294 |
=over 4 |
| 1295 |
|
| 1296 |
=item * |
| 1297 |
|
| 1298 |
B<-b I<FILE>>, B<--openssl-binary=I<FILE>>: the path to the OpenSSL binary to |
| 1299 |
invoke when performing PKI generation operations. Defaults to C<openssl> (i.e. |
| 1300 |
the first occurrence of C<openssl> in I<PATH>). |
| 1301 |
|
| 1302 |
=item * |
| 1303 |
|
| 1304 |
B<-c I<FILE>>, B<--config=I<FILE>>: the path to the configuration file defining |
| 1305 |
the PKI to generate. See L</CONFIGURATION> for a description of the expected |
| 1306 |
format. |
| 1307 |
|
| 1308 |
=item * |
| 1309 |
|
| 1310 |
B<-o I<DIR>>, B<--output=I<DIR>>: the path to the directory to which the PKI's |
| 1311 |
files (see L</OUTPUT>) will be written. The directory must already exist. |
| 1312 |
Existing files whose names collide with files written by this program will be |
| 1313 |
overwritten without warning; other existing files will be left alone. |
| 1314 |
|
| 1315 |
=item * |
| 1316 |
|
| 1317 |
B<-v>, B<--verbose>: show the output of C<openssl> and |
| 1318 |
C<examples/x509_cert_details.pl> when they are invoked. |
| 1319 |
|
| 1320 |
=back |
| 1321 |
|
| 1322 |
=head1 CONFIGURATION |
| 1323 |
|
| 1324 |
The configuration file is an anonymous Perl hash whose keys define the names of |
| 1325 |
the PKI's entities and whose values define each entity's properties: |
| 1326 |
|
| 1327 |
{ |
| 1328 |
'entity-name' => { |
| 1329 |
'key' => { ... }, # Private key properties |
| 1330 |
'csr' => { ... }, # Certificate signing request (CSR) properties |
| 1331 |
'cert' => { ... }, # Certificate properties |
| 1332 |
'pkcs12' => { ... }, # PKCS#12 archive properties |
| 1333 |
'crl' => { ... }, # Certificate revocation list (CRL) properties |
| 1334 |
# (optional; for CA entities only) |
| 1335 |
}, |
| 1336 |
... |
| 1337 |
} |
| 1338 |
|
| 1339 |
=head2 key |
| 1340 |
|
| 1341 |
An anonymous hash defining properties relating to the entity's private key. |
| 1342 |
|
| 1343 |
Valid keys: |
| 1344 |
|
| 1345 |
=over 4 |
| 1346 |
|
| 1347 |
=item * |
| 1348 |
|
| 1349 |
B<algorithm>: the public key algorithm to use when generating the private key. |
| 1350 |
Must be one of C<ec>, C<ed25519>, C<ed448>, C<rsa>, C<rsa-pss>, C<x25519>, or |
| 1351 |
C<x448>. |
| 1352 |
|
| 1353 |
=item * |
| 1354 |
|
| 1355 |
B<passphrase>: the passphrase under which to encrypt the private key. Used only |
| 1356 |
when generating encrypted forms of the key. |
| 1357 |
|
| 1358 |
=item * |
| 1359 |
|
| 1360 |
B<size>: the size of the public key to generate, in bits. Used only when |
| 1361 |
B<algorithm> is C<ec>, C<rsa>, or C<rsa-pss>. |
| 1362 |
|
| 1363 |
=back |
| 1364 |
|
| 1365 |
=head2 csr |
| 1366 |
|
| 1367 |
An anonymous hash defining properties relating to the entity's PKCS#10 |
| 1368 |
certificate signing request (CSR). The value of the B<subject> key in L</cert> |
| 1369 |
will be used to generate a subject name for the CSR. |
| 1370 |
|
| 1371 |
Valid keys: |
| 1372 |
|
| 1373 |
=over 4 |
| 1374 |
|
| 1375 |
=item * |
| 1376 |
|
| 1377 |
B<md_algorithm>: the message digest algorithm used to sign the CSR. May be any |
| 1378 |
value supported by C<openssl dgst>; commonly-supported values include C<md5>, |
| 1379 |
C<sha1>, and C<sha256>. |
| 1380 |
|
| 1381 |
=back |
| 1382 |
|
| 1383 |
=head2 cert |
| 1384 |
|
| 1385 |
An anonymous hash defining properties relating to the entity's X.509 v3 |
| 1386 |
certificate. |
| 1387 |
|
| 1388 |
Valid keys: |
| 1389 |
|
| 1390 |
=over 4 |
| 1391 |
|
| 1392 |
=item * |
| 1393 |
|
| 1394 |
B<extensions>: optional; an anonymous hash defining the X.509 v3 extensions that |
| 1395 |
should be specified in the certificate. Keys are expected to be extension field |
| 1396 |
names as they appear in L<x509v3_config(5)>, and values are expected to be |
| 1397 |
either strings or anonymous arrays of strings (whose elements will be |
| 1398 |
concatenated and delimited with commas), e.g.: |
| 1399 |
|
| 1400 |
{ |
| 1401 |
basicConstraints => 'critical,CA:false', |
| 1402 |
certificatePolicies => [ '1.2.3', '4.5.6' ], # Becomes '1.2.3,4.5.6' |
| 1403 |
} |
| 1404 |
|
| 1405 |
=item * |
| 1406 |
|
| 1407 |
B<issuer>: a top-level key denoting the entity that should sign this |
| 1408 |
certificate. If undefined, the entity's certificate will be self-signed. |
| 1409 |
|
| 1410 |
=item * |
| 1411 |
|
| 1412 |
B<md_algorithm>: the message digest algorithm used to sign the certificate. May |
| 1413 |
be any value supported by C<openssl dgst>; commonly-supported values include |
| 1414 |
C<md5>, C<sha1>, and C<sha256>. |
| 1415 |
|
| 1416 |
=item * |
| 1417 |
|
| 1418 |
B<purpose>: a string describing the purpose of the certificate. The value given |
| 1419 |
here will define reasonable values for the I<keyUsage>, I<extendedKeyUsage>, |
| 1420 |
I<basicConstraints>, and/or I<subjectKeyIdentifier> X.509 v3 extension fields. |
| 1421 |
Must be one of C<ca>, C<server>, C<client>, C<email>, or C<custom> (in which |
| 1422 |
case no default values will be defined for any of the aforementioned fields, |
| 1423 |
allowing for complete control of the fields that appear in the certificate via |
| 1424 |
the B<extensions> key). |
| 1425 |
|
| 1426 |
=item * |
| 1427 |
|
| 1428 |
B<revoke_reason>: optional; the reason for revoking the certificate. Must be one |
| 1429 |
of C<affiliationChanged>, C<CACompromise>, C<certificateHold>, |
| 1430 |
C<cessationOfOperation>, C<keyCompromise>, C<superseded>, or C<unspecified>. |
| 1431 |
|
| 1432 |
=item * |
| 1433 |
|
| 1434 |
B<revoke_time>: optional; a timestamp string in I<YYYY-MM-DD hh:mm:ss> format |
| 1435 |
denoting the time at which the certificate was revoked, in the UTC time zone. |
| 1436 |
Must be specified if B<revoke_reason> is specified. |
| 1437 |
|
| 1438 |
=item * |
| 1439 |
|
| 1440 |
B<serial>: a decimal integer denoting the certificate's serial number. Must be |
| 1441 |
unique among the serial numbers of all certificates issued by the entity given |
| 1442 |
in B<issuer>. |
| 1443 |
|
| 1444 |
=item * |
| 1445 |
|
| 1446 |
B<subject>: an anonymous array denoting the certificate's subject name; elements |
| 1447 |
are expected to alternate between field names in either short or long format and |
| 1448 |
values for those fields, e.g.: |
| 1449 |
|
| 1450 |
[ |
| 1451 |
C => 'PL', |
| 1452 |
O => 'Net-SSLeay', |
| 1453 |
OU => 'Test Suite', |
| 1454 |
commonName => 'test.net-ssleay.example', |
| 1455 |
] |
| 1456 |
|
| 1457 |
The order of the fields is preserved when generating the Distinguished Name |
| 1458 |
string. |
| 1459 |
|
| 1460 |
=item * |
| 1461 |
|
| 1462 |
B<valid_from>: a timestamp string in I<YYYY-MM-DD hh:mm:ss> format denoting the |
| 1463 |
time from which the certificate is valid, in the UTC time zone. |
| 1464 |
|
| 1465 |
=item * |
| 1466 |
|
| 1467 |
B<valid_to>: a timestamp string in I<YYYY-MM-DD hh:mm:ss> format denoting the |
| 1468 |
time until which the certificate is valid, in the UTC time zone. |
| 1469 |
|
| 1470 |
=back |
| 1471 |
|
| 1472 |
=head2 pkcs12 |
| 1473 |
|
| 1474 |
An anonymous hash defining properties relating to the entity's PKCS#12 archives. |
| 1475 |
|
| 1476 |
Valid keys: |
| 1477 |
|
| 1478 |
=over 4 |
| 1479 |
|
| 1480 |
=item * |
| 1481 |
|
| 1482 |
B<passphrase>: the passphrase under which to encrypt the private key stored in |
| 1483 |
the archive. Used only when generating archives that contain encrypted forms of |
| 1484 |
the private key. |
| 1485 |
|
| 1486 |
=back |
| 1487 |
|
| 1488 |
=head2 crl |
| 1489 |
|
| 1490 |
An anonymous hash defining properties relating to the entity's certificate |
| 1491 |
revocation list (CRL). Only used when the entity is a certificate authority and |
| 1492 |
at least one of the certificates it issues requires revocation. |
| 1493 |
|
| 1494 |
Valid keys: |
| 1495 |
|
| 1496 |
=over 4 |
| 1497 |
|
| 1498 |
=item * |
| 1499 |
|
| 1500 |
B<last_update>: a timestamp string in I<YYYY-MM-DD hh:mm:ss> format denoting the |
| 1501 |
time at which the CRL was last updated, in the UTC time zone. |
| 1502 |
|
| 1503 |
=item * |
| 1504 |
|
| 1505 |
B<md_algorithm>: the message digest algorithm used to sign the CRL. May be any |
| 1506 |
value supported by C<openssl dgst>; commonly-supported values include C<md5>, |
| 1507 |
C<sha1>, and C<sha256>. |
| 1508 |
|
| 1509 |
=item * |
| 1510 |
|
| 1511 |
B<next_update>: a timestamp string in I<YYYY-MM-DD hh:mm:ss> format denoting the |
| 1512 |
time at which the CRL is next expected to be updated, in the UTC time zone. |
| 1513 |
|
| 1514 |
=item * |
| 1515 |
|
| 1516 |
B<number>: a decimal integer denoting the CRL number. |
| 1517 |
|
| 1518 |
=back |
| 1519 |
|
| 1520 |
=head1 OUTPUT |
| 1521 |
|
| 1522 |
For each entity I<E> declared in the configuration file, C<generate-test-pki> |
| 1523 |
ensures the following set of files exists: |
| 1524 |
|
| 1525 |
=over 4 |
| 1526 |
|
| 1527 |
=item * |
| 1528 |
|
| 1529 |
B<E.key.pem>: a private key in PEM format. Will not be generated if it already |
| 1530 |
exists; the key in the existing file will be used instead. |
| 1531 |
|
| 1532 |
=item * |
| 1533 |
|
| 1534 |
B<E.key.enc.pem>: the file above, encrypted with AES-128 using the passphrase |
| 1535 |
given in the configuration file (see L</CONFIGURATION>). |
| 1536 |
|
| 1537 |
=item * |
| 1538 |
|
| 1539 |
B<E.key.der>: B<E.key.pem> in DER format. |
| 1540 |
|
| 1541 |
=item * |
| 1542 |
|
| 1543 |
B<E.key.enc.der>: the file above, encrypted with AES-128 using the passphrase |
| 1544 |
given in the configuration file (see L</CONFIGURATION>). |
| 1545 |
|
| 1546 |
=item * |
| 1547 |
|
| 1548 |
B<E.csr.pem>: a certificate signing request in PEM format. |
| 1549 |
|
| 1550 |
=item * |
| 1551 |
|
| 1552 |
B<E.csr.der>: the file above in DER format. |
| 1553 |
|
| 1554 |
=item * |
| 1555 |
|
| 1556 |
B<E.cert.pem>: a certificate in PEM format, signed by the entity given in the |
| 1557 |
configuration file (see L</CONFIGURATION>). |
| 1558 |
|
| 1559 |
=item * |
| 1560 |
|
| 1561 |
B<E.cert.der>: the file above in DER format. |
| 1562 |
|
| 1563 |
=item * |
| 1564 |
|
| 1565 |
B<E.cert.dump>: the output of |
| 1566 |
C<examples/x509_cert_details.pl -dump -pem E.cert.pem>. C<x509_cert_details.pl> |
| 1567 |
is a Net-SSLeay example script whose output is used by the test suite to verify |
| 1568 |
the correct operation of various libssl certificate information functions. |
| 1569 |
|
| 1570 |
=item * |
| 1571 |
|
| 1572 |
B<E.cert.certchain.pem>: the certificate chain in PEM format, starting with |
| 1573 |
I<E>'s certificate and ending with the root CA certificate. |
| 1574 |
|
| 1575 |
=item * |
| 1576 |
|
| 1577 |
B<E.cert.certchain.der>: the file above, with certificates in DER format. |
| 1578 |
|
| 1579 |
=item * |
| 1580 |
|
| 1581 |
B<E.p12>: a PKCS#12 archive containing a private key and a certificate. |
| 1582 |
|
| 1583 |
=item * |
| 1584 |
|
| 1585 |
B<E.enc.p12>: the file above, with the private key encrypted with AES-128 using |
| 1586 |
the passphrase given in the configuration file (see L</CONFIGURATION>). |
| 1587 |
|
| 1588 |
=item * |
| 1589 |
|
| 1590 |
B<E.certchain.p12>: a PKCS#12 archive containing a private key and a certificate |
| 1591 |
chain starting with I<E>'s certificate and ending with the root CA certificate. |
| 1592 |
|
| 1593 |
=item * |
| 1594 |
|
| 1595 |
B<E.certchain.enc.p12>: the file above, with the private key encrypted with |
| 1596 |
AES-128 using the passphrase given in the configuration file (see |
| 1597 |
L</CONFIGURATION>). |
| 1598 |
|
| 1599 |
=back |
| 1600 |
|
| 1601 |
Additionally, for entities that sign and then revoke at least one certificate, |
| 1602 |
C<generate-test-pki> outputs the following files: |
| 1603 |
|
| 1604 |
=over 4 |
| 1605 |
|
| 1606 |
=item * |
| 1607 |
|
| 1608 |
B<E.crl.pem>: a certificate revocation list (version 2) in PEM format. |
| 1609 |
|
| 1610 |
=item * |
| 1611 |
|
| 1612 |
B<E.crl.der>: the file above in DER format. |
| 1613 |
|
| 1614 |
=back |
| 1615 |
|
| 1616 |
=head1 DIAGNOSTICS |
| 1617 |
|
| 1618 |
C<generate-test-pki> outputs a diagnostic message to stderr and immediately |
| 1619 |
exits with exit code 1 if an error occurs. Error messages listed below indicate |
| 1620 |
invalid input or a problem with the state of the system that can usually be |
| 1621 |
fixed. Error messages not listed below are internal and should never be |
| 1622 |
encountered under normal operation; please report any occurrences of such errors |
| 1623 |
as bugs (see L</BUGS>). |
| 1624 |
|
| 1625 |
=over |
| 1626 |
|
| 1627 |
=item B<Error while parsing command line options: PKI configuration file I<PATH> |
| 1628 |
does not exist> |
| 1629 |
|
| 1630 |
The PKI configuration file at I<PATH>, as specified by the B<-c> command line |
| 1631 |
option (or C<pki.cfg> in the same directory as C<generate-test-pki> if a value |
| 1632 |
for B<-c> was not specified), does not exist. Ensure C<pki.cfg> exists, or |
| 1633 |
speicify an alternative path with B<-c I<PATH>>. |
| 1634 |
|
| 1635 |
=item B<Error while parsing command line options: an output directory must be |
| 1636 |
given> |
| 1637 |
|
| 1638 |
The B<-o> option is compulsory, and has no default value. Pass the path to a |
| 1639 |
directory in which the output files described in L</OUTPUT> should be written |
| 1640 |
with B<-o I<PATH>>. |
| 1641 |
|
| 1642 |
=item B<Error while parsing command line options: output directory I<PATH> does |
| 1643 |
not exist> |
| 1644 |
|
| 1645 |
C<generate-test-pki> does not attempt to create the directory at the path given |
| 1646 |
by the B<-o> option; it must already exist and be writable. |
| 1647 |
|
| 1648 |
=item B<Could not load PKI configuration file: I<PATH>: I<REASON>> |
| 1649 |
|
| 1650 |
The configuration file at I<PATH> could not be loaded because of I<REASON>, |
| 1651 |
which is probably an OS-level error. Ensure the file at I<PATH> is readable. |
| 1652 |
|
| 1653 |
=item B<Could not load PKI configuration file: syntax error: I<REASON>> |
| 1654 |
|
| 1655 |
The configuration file could not be parsed because of I<REASON>, which is likely |
| 1656 |
a Perl syntax error. Ensure the configuration file is valid Perl and meets the |
| 1657 |
specification given in L</CONFIGURATION>. |
| 1658 |
|
| 1659 |
=item B<OpenSSL minimum version check failed: `openssl version` exited with exit |
| 1660 |
code I<N>> |
| 1661 |
|
| 1662 |
C<generate-test-pki> attempted to check the version of the OpenSSL command line |
| 1663 |
utility currently in use by invoking C<openssl version>, and expected it to exit |
| 1664 |
with exit code 0 (indicating success) but it actually exited with exit code I<N> |
| 1665 |
(indicating failure). Check that the first occurrence of C<openssl> in I<PATH> |
| 1666 |
is in fact the OpenSSL command line utility, then run C<generate-test-pki> with |
| 1667 |
the B<-v> option to see the full output from C<openssl version>, which may help |
| 1668 |
diagnose the problem further. |
| 1669 |
|
| 1670 |
=item B<OpenSSL minimum version check failed: `openssl` is not the OpenSSL |
| 1671 |
command line utility> |
| 1672 |
|
| 1673 |
C<generate-test-pki> attempted to check the version of the OpenSSL command line |
| 1674 |
utility currently in use by invoking C<openssl version>, but its output was |
| 1675 |
inconsistent with the output format known to be used by OpenSSL. Check that the |
| 1676 |
first occurrence of C<openssl> in I<PATH> is in fact the OpenSSL command line |
| 1677 |
utility (and not the LibreSSL command line utility), then run |
| 1678 |
C<generate-test-pki> with the B<-v> option to see the full output from |
| 1679 |
C<openssl version>, which may help diagnose the problem further. |
| 1680 |
|
| 1681 |
=item B<OpenSSL minimum version check failed: OpenSSL E<gt>= I<MINVER> required, |
| 1682 |
but `openssl` is version I<VER>> |
| 1683 |
|
| 1684 |
C<generate-test-pki> relies on features of the OpenSSL command line utility that |
| 1685 |
were added in version I<MINVER>, but the first occurrence of C<openssl> in |
| 1686 |
I<PATH> is version I<VER>, which is insufficient. It may be necessary to compile |
| 1687 |
a newer version of OpenSSL from the source code and prepend the directory |
| 1688 |
containing the command line utility to I<PATH> in order to solve this problem. |
| 1689 |
|
| 1690 |
=item B<OpenSSL minimum version check failed: could not load Net::SSLeay: |
| 1691 |
I<REASON>> |
| 1692 |
|
| 1693 |
C<generate-test-pki> attempted to check the version of OpenSSL that Net::SSLeay |
| 1694 |
is built against, but was unable to import Net::SSLeay because of I<REASON>. |
| 1695 |
Ensure the first occurrence of Net::SSLeay in I<PERL5LIB> can be imported by |
| 1696 |
Perl. |
| 1697 |
|
| 1698 |
=item B<OpenSSL minimum version check failed: Net::SSLeay was not built against |
| 1699 |
OpenSSL> |
| 1700 |
|
| 1701 |
C<generate-test-pki> relies on features of Net::SSLeay that are only available |
| 1702 |
when it is built against OpenSSL, but the first occurrence of Net::SSLeay in |
| 1703 |
I<PERL5LIB> is built against LibreSSL. Rebuild Net::SSLeay against OpenSSL and |
| 1704 |
ensure the rebuilt version is the first occurrence of Net::SSLeay in |
| 1705 |
I<PERL5LIB>. |
| 1706 |
|
| 1707 |
=item B<OpenSSL minimum version check failed: Net::SSLeay must be built against |
| 1708 |
OpenSSL E<gt>= I<MINVER>, but it is built against version I<VER>> |
| 1709 |
|
| 1710 |
C<generate-test-pki> relies on features of Net::SSLeay that are only available |
| 1711 |
when it is built against OpenSSL version I<MINVER>, but the first occurrence of |
| 1712 |
Net::SSLeay in I<PERL5LIB> is built against OpenSSL I<VER>. Rebuild Net::SSLeay |
| 1713 |
against a newer version OpenSSL - ideally the same version as the OpenSSL |
| 1714 |
command line utility - and ensure the rebuilt version is the first occurrence of |
| 1715 |
Net::SSLeay in I<PERL5LIB>. |
| 1716 |
|
| 1717 |
=item B<Could not create temporary working directory: I<REASON>> |
| 1718 |
|
| 1719 |
C<generate-test-pki> attempted to create a directory to store some temporary |
| 1720 |
files that are necessary to generate the output files, but was unable to create |
| 1721 |
the directory because of I<REASON> (which is probably an OS-level error). Ensure |
| 1722 |
the system's temporary directory is writable. |
| 1723 |
|
| 1724 |
=item B<Error while building PKI tree: entity 'I<E>': issuer 'I<I>' is not |
| 1725 |
defined> |
| 1726 |
|
| 1727 |
The configuration file defines an entity I<E> whose issuer (per the the value of |
| 1728 |
its C<{cert}-E<gt>{issuer}> key) does not exist. Check that I<I> is not |
| 1729 |
misnamed and that the value of C<{cert}-E<gt>{issuer}> for I<E> is correct. |
| 1730 |
|
| 1731 |
=item B<Could not generate 'I<E>.key.pem': missing key algorithm> |
| 1732 |
|
| 1733 |
The configuration file defines an entity I<E> with no value for |
| 1734 |
C<{key}-E<gt>{algorithm}>. See L</key> for a list of acceptable values. |
| 1735 |
|
| 1736 |
=item B<Could not generate 'I<E>.key.pem': unknown key algorithm 'I<ALGORITHM>'> |
| 1737 |
|
| 1738 |
The configuration file defines an entity I<E> with the value I<ALGORITHM> for |
| 1739 |
C<{key}-E<gt>{algorithm}>, but this is not a known public key algorithm. See |
| 1740 |
L</key> for a list of acceptable values. |
| 1741 |
|
| 1742 |
=item B<Could not generate 'I<E>.key.pem': key algorithm 'I<ALGORITHM>' requires |
| 1743 |
a key size> |
| 1744 |
|
| 1745 |
The configuration file defines an entity I<E> with the value I<ALGORITHM> for |
| 1746 |
C<{key}-E<gt>{algorithm}>, but I<ALGORITHM> requires a key size to be defined |
| 1747 |
in C<{key}-E<gt>{size}>. Define a valid key size for this entity's private key. |
| 1748 |
See L</key> for more information. |
| 1749 |
|
| 1750 |
=item B<Could not generate 'I<E>.csr.pem': invalid key/value pair given in |
| 1751 |
subject> |
| 1752 |
|
| 1753 |
The configuration file defines an entity I<E> with at least one undefined |
| 1754 |
element in its value for C<{cert}-E<gt>{subject}>. Undefined elements cannot be |
| 1755 |
stringified, so the subject could not be transformed into a Distinguished Name |
| 1756 |
string. See L</cert> for more information of the expected format for |
| 1757 |
C<{cert}-E<gt>{subject}>. |
| 1758 |
|
| 1759 |
=item B<Could not generate 'I<E>.csr.pem': missing message digest algorithm> |
| 1760 |
|
| 1761 |
The configuration file defines an entity I<E> with no value for |
| 1762 |
C<{csr}-E<gt>{md_algorithm}>. See L</csr> for possible values. |
| 1763 |
|
| 1764 |
=item B<Could not generate 'I<E>.cert.pem': valid_from: invalid timestamp> |
| 1765 |
|
| 1766 |
The configuration file defines an entity I<E> with an invalid timestamp for its |
| 1767 |
value of C<{cert}-E<gt>{valid_from}>. See L</cert> for more information on the |
| 1768 |
expected timestamp format. |
| 1769 |
|
| 1770 |
=item B<Could not generate 'I<E>.cert.pem': valid_until: invalid timestamp> |
| 1771 |
|
| 1772 |
The configuration file defines an entity I<E> with an invalid timestamp for its |
| 1773 |
value of C<{cert}-E<gt>{valid_to}>. See L</cert> for more information on the |
| 1774 |
expected timestamp format. |
| 1775 |
|
| 1776 |
=item B<Could not generate 'I<E>.cert.pem': missing message digest algorithm> |
| 1777 |
|
| 1778 |
The configuration file defines an entity I<E> with no value for |
| 1779 |
C<{cert}-E<gt>{md_algorithm}>. See L</cert> for possible values. |
| 1780 |
|
| 1781 |
=item B<Could not generate 'I<E>.cert.pem': could not create directory I<PATH>: |
| 1782 |
I<REASON>> |
| 1783 |
|
| 1784 |
C<generate-test-pki> attempted to create a temporary directory at I<PATH> to |
| 1785 |
store intermediate files that are necessary to generate I<E>'s certificate, but |
| 1786 |
was unable to do so because of I<REASON>, which is probably an OS-level error. |
| 1787 |
Ensure the system's temporary directory is writable. |
| 1788 |
|
| 1789 |
=item B<Could not generate 'I<E>.cert.pem': could not write serial file I<PATH>: |
| 1790 |
I<REASON>> |
| 1791 |
|
| 1792 |
C<generate-test-pki> attempted to write an intermediate file to I<PATH> (a |
| 1793 |
subdirectory of a temporary directory it created earlier) that is necessary to |
| 1794 |
generate I<E>'s certificate, but was unable to do so because of I<REASON>, which |
| 1795 |
is probably an OS-level error. Ensure the system's temporary directory is |
| 1796 |
writable. |
| 1797 |
|
| 1798 |
=item B<Could not generate 'I<E>.cert.dump': could not write I<PATH>: I<REASON>> |
| 1799 |
|
| 1800 |
C<generate-test-pki> attempted to write information about I<E>'s certificate to |
| 1801 |
the file at I<PATH>, but was unable to do so because of I<REASON>, which is |
| 1802 |
probably an OS-level error. Ensure the file at I<PATH> is writable. |
| 1803 |
|
| 1804 |
=item B<Could not generate 'I<E>.cert.dump': could not run |
| 1805 |
examples/x509_cert_details.pl: I<REASON>> |
| 1806 |
|
| 1807 |
C<generate-test-pki> attempted to invoke the Perl script |
| 1808 |
C<examples/x509_cert_details.pl> (part of the Net-SSLeay source distribution) to |
| 1809 |
produce an output file containing information about I<E>'s certificate, but was |
| 1810 |
unable to invoke the script because of I<REASON>. Ensure that the script is |
| 1811 |
located at C<../examples/x509_cert_details.pl> relative to the path to |
| 1812 |
C<generate-test-pki>, that it can be executed given the values of I<PATH> and |
| 1813 |
I<PERL5LIB> that are inherited by C<generate-test-pki>, and that a suitable |
| 1814 |
version of Net::SSLeay is present in I<PERL5LIB> (see L</DEPENDENCIES> for more |
| 1815 |
information). |
| 1816 |
|
| 1817 |
=item B<Could not generate 'I<E>.cert.dump': examples/x509_cert_details.pl |
| 1818 |
exited with exit code I<N>> |
| 1819 |
|
| 1820 |
C<generate-test-pki> invoked the Perl script C<examples/x509_cert_details.pl> |
| 1821 |
(part of the Net-SSLeay source distribution) to produce an output file |
| 1822 |
containing information about I<E>'s certificate, and expected it to exit |
| 1823 |
with exit code 0 (indicating success) but it actually exited with exit code I<N> |
| 1824 |
(indicating failure). Run C<generate-test-pki> with the B<-v> option to see the |
| 1825 |
full output from C<examples/x509_cert_details.pl>, which may help diagnose the |
| 1826 |
problem further. |
| 1827 |
|
| 1828 |
=item B<Could not generate 'I<E>.certchain.pem': could not write certificate |
| 1829 |
chain file I<PATH>: I<REASON>> |
| 1830 |
|
| 1831 |
=item B<Could not generate 'I<E>.certchain.der': could not write certificate |
| 1832 |
chain file I<PATH>: I<REASON>> |
| 1833 |
|
| 1834 |
C<generate-test-pki> attempted to concatenate the certificates in I<E>'s issuer |
| 1835 |
chain (in either format) and write them to I<PATH>, but was unable to do so |
| 1836 |
because of I<REASON>, which is probably an OS-level error. Ensure the file at |
| 1837 |
I<PATH> is writable. |
| 1838 |
|
| 1839 |
=item B<Could not generate 'I<E>.certchain.pem': could not read certificate file |
| 1840 |
I<PATH>: I<REASON>> |
| 1841 |
|
| 1842 |
=item B<Could not generate 'I<E>.certchain.der': could not read certificate file |
| 1843 |
I<PATH>: I<REASON>> |
| 1844 |
|
| 1845 |
C<generate-test-pki> attempted to read a certificate in I<E>'s issuer chain (in |
| 1846 |
either format) at I<PATH>, but was unable to do so because of I<REASON>, which |
| 1847 |
is probably an OS-level error. Ensure the file at I<PATH> is readable. |
| 1848 |
|
| 1849 |
=item B<Could not generate 'I<E>.crl.pem': could not create directory I<PATH>: |
| 1850 |
I<REASON>> |
| 1851 |
|
| 1852 |
C<generate-test-pki> attempted to create a temporary directory at I<PATH> to |
| 1853 |
store intermediate files that are necessary to generate I<E>'s CRL, but was |
| 1854 |
unable to do so because of I<REASON>, which is probably an OS-level error. |
| 1855 |
Ensure the system's temporary directory is writable. |
| 1856 |
|
| 1857 |
=item B<Could not generate 'I<E>.crl.pem': could not read database file I<PATH>: |
| 1858 |
I<REASON>> |
| 1859 |
|
| 1860 |
When revoking a certificate, C<generate-test-pki> looks up the certificate's |
| 1861 |
serial number in its issuing entity's database file, which is created by OpenSSL |
| 1862 |
in a temporary directory created earlier by C<generate-test-pki>. It was unable |
| 1863 |
to read this file on this occasion because of I<REASON>, which is probably an |
| 1864 |
OS-level error. Ensure the system's temporary directory is readable. |
| 1865 |
|
| 1866 |
=item B<Could not generate 'I<E>.crl.pem': could not write database file |
| 1867 |
I<PATH>: I<REASON>> |
| 1868 |
|
| 1869 |
To revoke a certificate, C<generate-test-pki> updates the certificate's entry in |
| 1870 |
its issuing entity's database file, which is created by OpenSSL in a temporary |
| 1871 |
directory created earlier by C<generate-test-pki>. It was unable to update the |
| 1872 |
file on this occasion because of I<REASON>, which is probably an OS-level error. |
| 1873 |
Ensure the system's temporary directory is writable. |
| 1874 |
|
| 1875 |
=item B<Could not generate 'I<E>.crl.pem': could not write CRL number file |
| 1876 |
I<PATH>: I<REASON>> |
| 1877 |
|
| 1878 |
When revoking a certificate, C<generate-test-pki> stores the CRL number for the |
| 1879 |
CRL it outputs in a file in a temporary directory it created earlier. It was |
| 1880 |
unable to write this file on this occasion because of I<REASON>, which is |
| 1881 |
probably an OS-level error. Ensure the system's temporary directory is writable. |
| 1882 |
|
| 1883 |
=item B<Could not generate 'I<E>.certchain.p12': could not create directory |
| 1884 |
I<PATH>: I<REASON>> |
| 1885 |
|
| 1886 |
=item B<Could not generate 'I<E>.certchain.enc.p12': could not create directory |
| 1887 |
I<PATH>: I<REASON>> |
| 1888 |
|
| 1889 |
When generating a PKCS#12 archive containing multiple certificates, |
| 1890 |
C<generate-test-pki> concatenates the certificates and writes them to a file in |
| 1891 |
a temporary directory it creates before passing the path to that file in a |
| 1892 |
command line option to C<openssl>. It was unable to create the temporary |
| 1893 |
directory on this occasion because of I<REASON>, which is probably an OS-level |
| 1894 |
error. Ensure the system's temporary directory is writable. |
| 1895 |
|
| 1896 |
=item B<OpenSSL minimum version check failed: could not run `openssl version`: |
| 1897 |
I<REASON>> |
| 1898 |
|
| 1899 |
=item B<Could not generate 'I<PATH>': failed to run `openssl I<COMMAND>`: |
| 1900 |
I<REASON>> |
| 1901 |
|
| 1902 |
C<generate-test-pki> attempted to invoke the OpenSSL command line utility, but |
| 1903 |
failed to spawn a new process because of I<REASON>, which is probably an |
| 1904 |
OS-level error. |
| 1905 |
|
| 1906 |
=item B<Could not generate 'I<PATH>': `openssl I<COMMAND>` failed with exit code |
| 1907 |
I<N>> |
| 1908 |
|
| 1909 |
C<generate-test-pki> attempted to generate an output file by invoking the |
| 1910 |
OpenSSL command line utility, and expected it to exit with exit code 0 |
| 1911 |
(indicating success) but it actually exited with exit code I<N> (indicating |
| 1912 |
failure). Check that the PKI defined in the configuration file is sensible, then |
| 1913 |
run C<generate-test-pki> with the B<-v> option to see the full output from |
| 1914 |
C<openssl>. |
| 1915 |
|
| 1916 |
=back |
| 1917 |
|
| 1918 |
=head1 LIMITATIONS |
| 1919 |
|
| 1920 |
Although its interface is almost identical to the OpenSSL command line utility, |
| 1921 |
C<generate-test-pki> is incompatible with the LibreSSL command line utility, |
| 1922 |
since it relies on features currently only found in the OpenSSL command line |
| 1923 |
utility. |
| 1924 |
|
| 1925 |
Only limited error checking is performed on the configuration file; in |
| 1926 |
particular, C<generate-test-pki> will not always complain if required keys are |
| 1927 |
missing. It is recommended to run the program with the B<-v> option after |
| 1928 |
editing the configuration file to ensure C<openssl> is being invoked as |
| 1929 |
expected. |
| 1930 |
|
| 1931 |
Entities can have their certificates issued by one and only one entity; |
| 1932 |
cross-signed certificates cannot currently be generated. |
| 1933 |
|
| 1934 |
The uniqueness of serial numbers among the certificates signed by any given |
| 1935 |
issuer is not enforced, and duplication will likely cause odd output from |
| 1936 |
C<generate-test-pki> and breakage when certificates are revoked. Care should be |
| 1937 |
taken when editing serial numbers in the configuration file. |
| 1938 |
|
| 1939 |
While as much effort as possible has been put into generating output files |
| 1940 |
deterministically, C<generate-test-pki> will still generate different private |
| 1941 |
keys and PKCS#12 archives on every invocation, even when the PKI configuration |
| 1942 |
file has not changed between invocations. C<generate-test-pki> will avoid |
| 1943 |
overwriting the private key for an entity if one already exists, but cannot |
| 1944 |
recreate a private key that has been deleted. PKCS#12 archives cannot be |
| 1945 |
generated deterministically because the PKCS#12 file format uses salts and IVs |
| 1946 |
that the OpenSSL command line utiltity randomly generates on each invocation. |
| 1947 |
|
| 1948 |
=head1 SEE ALSO |
| 1949 |
|
| 1950 |
The man pages for the OpenSSL command line utility subcommands invoked by |
| 1951 |
C<generate-test-pki>: L<openssl-ca(1)>, L<openssl-crl(1)>, |
| 1952 |
L<openssl-genpkey(1)>, L<openssl-pkey(1)>, L<openssl-req(1)>, and |
| 1953 |
L<openssl-x509(1)>. |
| 1954 |
|
| 1955 |
=head1 BUGS |
| 1956 |
|
| 1957 |
If you encounter a problem with this program that you believe is a bug, please |
| 1958 |
L<create a new issue|https://github.com/radiator-software/p5-net-ssleay/issues/new> |
| 1959 |
in the Net-SSLeay GitHub repository. Please make sure your bug report includes |
| 1960 |
the following information: |
| 1961 |
|
| 1962 |
=over |
| 1963 |
|
| 1964 |
=item * |
| 1965 |
|
| 1966 |
the list of command line options passed to C<generate-test-pki>; |
| 1967 |
|
| 1968 |
=item * |
| 1969 |
|
| 1970 |
the full configuration file given by the C<-c> command line option; |
| 1971 |
|
| 1972 |
=item * |
| 1973 |
|
| 1974 |
the full output of C<generate-test-pki>; |
| 1975 |
|
| 1976 |
=item * |
| 1977 |
|
| 1978 |
your operating system name and version; |
| 1979 |
|
| 1980 |
=item * |
| 1981 |
|
| 1982 |
the output of C<perl -V>; |
| 1983 |
|
| 1984 |
=item * |
| 1985 |
|
| 1986 |
the version of Net-SSLeay you are using; |
| 1987 |
|
| 1988 |
=item * |
| 1989 |
|
| 1990 |
the version of OpenSSL you are using. |
| 1991 |
|
| 1992 |
=back |
| 1993 |
|
| 1994 |
=head1 AUTHORS |
| 1995 |
|
| 1996 |
Originally written by Chris Novakovic. |
| 1997 |
|
| 1998 |
Maintained by Chris Novakovic and Heikki Vatiainen. |
| 1999 |
|
| 2000 |
=head1 COPYRIGHT AND LICENSE |
| 2001 |
|
| 2002 |
Copyright 2020- Chris Novakovic <[email protected]>. |
| 2003 |
|
| 2004 |
Copyright 2020- Heikki Vatiainen <[email protected]>. |
| 2005 |
|
| 2006 |
This module is released under the terms of the Artistic License 2.0. For |
| 2007 |
details, see the C<LICENSE> file distributed with Net-SSLeay's source code. |
| 2008 |
|
| 2009 |
=cut |
| 2010 |
|
| 2011 |
__DATA__ |
| 2012 |
#----------------------------------------------------------------------- |
| 2013 |
# openssl req |
| 2014 |
#----------------------------------------------------------------------- |
| 2015 |
|
| 2016 |
[ req ] |
| 2017 |
utf8 = yes |
| 2018 |
string_mask = utf8only |
| 2019 |
prompt = no |
| 2020 |
distinguished_name = req_dn |
| 2021 |
|
| 2022 |
[ req_dn ] |
| 2023 |
# This section is intentionally left blank - distinguished_name must be |
| 2024 |
# defined in the [ req ] section, but the distinguished name is actually |
| 2025 |
# specified in the -subj option to `openssl req` |
| 2026 |
|
| 2027 |
#----------------------------------------------------------------------- |
| 2028 |
# openssl ca |
| 2029 |
#----------------------------------------------------------------------- |
| 2030 |
|
| 2031 |
[ ca_conf ] |
| 2032 |
database = {{ database_path }} |
| 2033 |
serial = {{ serial_path }} |
| 2034 |
new_certs_dir = {{ certs_path }} |
| 2035 |
unique_subject = no |
| 2036 |
email_in_dn = yes |
| 2037 |
default_days = 3650 |
| 2038 |
policy = ca_policy |
| 2039 |
crlnumber = {{ crl_number_path }} |
| 2040 |
crl_extensions = crlexts |
| 2041 |
|
| 2042 |
[ ca_policy ] |
| 2043 |
domainComponent = optional |
| 2044 |
countryName = optional |
| 2045 |
organizationName = optional |
| 2046 |
organizationalUnitName = optional |
| 2047 |
dnQualifier = optional |
| 2048 |
stateOrProvinceName = optional |
| 2049 |
commonName = optional |
| 2050 |
serialNumber = optional |
| 2051 |
localityName = optional |
| 2052 |
title = optional |
| 2053 |
name = optional |
| 2054 |
givenName = optional |
| 2055 |
initials = optional |
| 2056 |
pseudonym = optional |
| 2057 |
generationQualifier = optional |
| 2058 |
emailAddress = optional |
| 2059 |
|
| 2060 |
[ exts_ca ] |
| 2061 |
keyUsage = critical,keyCertSign,cRLSign |
| 2062 |
basicConstraints = critical,CA:true |
| 2063 |
subjectKeyIdentifier = hash |
| 2064 |
{{ extensions }} |
| 2065 |
|
| 2066 |
[ exts_server ] |
| 2067 |
keyUsage = critical,digitalSignature,keyEncipherment |
| 2068 |
extendedKeyUsage = serverAuth,clientAuth |
| 2069 |
subjectKeyIdentifier = hash |
| 2070 |
{{ extensions }} |
| 2071 |
|
| 2072 |
[ exts_client ] |
| 2073 |
keyUsage = critical,digitalSignature |
| 2074 |
extendedKeyUsage = clientAuth |
| 2075 |
subjectKeyIdentifier = hash |
| 2076 |
{{ extensions }} |
| 2077 |
|
| 2078 |
[ exts_email ] |
| 2079 |
keyUsage = critical,digitalSignature,keyEncipherment |
| 2080 |
extendedKeyUsage = emailProtection,clientAuth |
| 2081 |
subjectKeyIdentifier = hash |
| 2082 |
{{ extensions }} |
| 2083 |
|
| 2084 |
[ exts_custom ] |
| 2085 |
{{ extensions }} |
| 2086 |
|
| 2087 |
[ crlexts ] |
| 2088 |
# This section is intentionally left blank - if crl_extensions is |
| 2089 |
# defined in the [ ca_conf ] section (even if it is empty), OpenSSL |
| 2090 |
# writes a V2 CRL instead of a V1 CRL |