-
Notifications
You must be signed in to change notification settings - Fork 20
/
phar-generate-cert.php
83 lines (65 loc) · 2.31 KB
/
phar-generate-cert.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env php
<?php
/**
* Generate OpenSSL certificate
* This is the equivalent of these openssl commands:
* $ openssl genrsa -out priv.pem 1024
* $ openssl rsa -in priv.pem -pubout -out pub.pem
*
* This file is part of the PharUtil library.
* @author Krzysztof Kotowicz <kkotowicz at gmail dot com>
* @package PharUtil
*/
// Include the Console_CommandLine package.
require_once 'Console/CommandLine.php';
// create the parser
$parser = new Console_CommandLine(array(
'description' => 'Generate OpenSSL certificate',
'version' => '@package_version@',
'name' => 'phar-generate-cert',
));
$parser->addOption('public', array(
'short_name' => '-P',
'long_name' => '--public',
'action' => 'StoreString',
'default' => './pub.pem',
'description' => "Path to public key file (PEM) to generate\n(./pub.pem by default)"
));
$parser->addOption('private', array(
'short_name' => '-p',
'long_name' => '--private',
'action' => 'StoreString',
'default' => './priv.pem',
'description' => "Path to private key file (PEM) to generate\n(./priv.pem by default)"
));
// run the parser
try {
$result = $parser->parse();
} catch (Exception $exc) {
$parser->displayError($exc->getMessage());
}
$options = $result->options;
$args = $result->args;
echo $parser->name . ' ' . $parser->version . PHP_EOL . PHP_EOL;
try {
$res = openssl_pkey_new();
if (!$res) {
throw new Exception("Couldn't create key. Check that OpenSSL in PHP is configured properly - an openssl.cnf file is needed. Consult http://www.php.net/manual/en/openssl.installation.php");
}
$privkey = null;
openssl_pkey_export($res, $privkey);
$pubkey = openssl_pkey_get_details($res);
$pubkey = $pubkey["key"];
if (!@file_put_contents($options['private'], $privkey)) {
throw new Exception("Error writing private key to {$options['private']}!");
}
echo "Private key written to {$options['private']} " . PHP_EOL;
if (!@file_put_contents($options['public'], $pubkey)) {
throw new Exception("Error writing public key to {$options['public']}!");
}
echo "Public key written to {$options['public']} " . PHP_EOL;
} catch (Exception $e) {
$parser->displayError($e->getMessage(), 1);
}
echo PHP_EOL . "All done, exiting." . PHP_EOL;
?>