forked from skoerfgen/CertLE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CertLE.php
235 lines (197 loc) · 5.99 KB
/
CertLE.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/*
CertLE - A Let's Encrypt PHP Command Line ACME Client
Copyright (C) 2016 S.Körfgen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
set_time_limit(0);
require('CertLE.inc.php');
function exception_handler($e){
$err=error_get_last();
echo 'Error: '.$e->getMessage()."\n".($err['message']?$err['message']."\n":'');
exit(1);
}
set_exception_handler('exception_handler');
if (!extension_loaded('openssl')) {
throw new Exception('PHP OpenSSL Extension is required but not installed/loaded !');
}
function get_args($offset) {
global $argv;
$lists=array(array(),array());
foreach(array_slice($argv,$offset) as $idx=>$item){
$lists[$idx%2==0?0:1][]=$item;
}
$out=array();
foreach($lists[0] as $k=>$v){
$o=isset($lists[1][$k])?$lists[1][$k]:null;
$out[]=array(ltrim($v,'-')=>$o);
}
return $out;
}
if (isset($argv[1])){
switch($argv[1]) {
case 'genrsa':
$bits=2048;
if (isset($argv[2])){
$bits=intval($argv[2]);
}
if (false===($fn=tempnam(sys_get_temp_dir(), "CNF_"))){
throw new Exception('Failed to create temp file !');
}
if (false===@file_put_contents($fn,
'HOME = .'."\n".
'RANDFILE=$ENV::HOME/.rnd'."\n".
'[v3_ca]'."\n"
)){
throw new Exception('Failed to write tmp file: '.$fn);
}
$config=array(
'config'=>$fn,
'private_key_bits'=>$bits,
'private_key_type'=>OPENSSL_KEYTYPE_RSA,
);
$key=openssl_pkey_new($config);
if ($key===false) {
throw new Exception('Could not generate key');
}
if (false===openssl_pkey_export($key,$pem,null,$config)){
throw new Exception('Could not export key');
}
unlink($fn);
echo $pem;
break;
case 'register':
if (!isset($argv[2])){
throw new Exception('Account-Key expected !');
}
$certLE=new CertLE($argv[2]);
$certLE->register(isset($argv[3])?$argv[3]:null);
break;
case 'auto-register':
if (!isset($argv[2])){
throw new Exception('Account-Key expected !');
}
$certLE=new CertLE($argv[2]);
$certLE->register(isset($argv[3])?$argv[3]:null,true);
break;
case 'cert':
if (!isset($argv[2])){
throw new Exception('Account-Key expected !');
}
if (!isset($argv[3])){
throw new Exception('Domain-Key expected !');
}
$args=get_args(4);
$opts=array();
$webroot=null;
$domains=array();
foreach($args as $item){
$value=reset($item);
$arg=key($item);
switch($arg){
case 'webroot':
case 'w':
$webroot=rtrim($value,'/').'/';
break;
case 'domain':
case 'd':
if ($webroot===null) {
throw new Exception('-w, --webroot must be specified in front of -d, --domain !');
}
$domains[$value]=$webroot;
break;
case 'csr':
case 'cert':
case 'chain':
case 'fullchain':
$opts[$arg]=$value;
break;
default:
throw new Exception('Unknown Parameter: '.$arg);
break;
}
}
if ($webroot===null){
throw new Exception('-w , --webroot parameter missing !');
}
if (empty($domains)){
throw new Exception('-d , --domain parameter missing !');
}
if ( (!isset($opts['cert'])) && (!isset($opts['fullchain'])) ) {
throw new Exception('--cert or --fullchain parameter missing !');
}
$certLE=new CertLE($argv[2]);
$ret=$certLE->get_cert(
$argv[3],
$domains,
$opts
);
break;
case 'revoke':
if (!isset($argv[2])){
throw new Exception('Account-Key or Domain-Key expected !');
}
if (!isset($argv[3])){
throw new Exception('Cert or Fullchain expected !');
}
$certLE=new CertLE($argv[2]);
$certLE->revoke($argv[3]);
break;
case 'deactivate':
if (!isset($argv[2])){
throw new Exception('Account-Key expected !');
}
if (!isset($argv[3])){
throw new Exception('Account-ID (URL) expected !');
}
$certLE=new CertLE($argv[2]);
$certLE->deactivate($argv[3]);
break;
default:
throw new Exception('Unknown subcommand: '.$argv[1]);
break;
}
}else{
echo <<<EOD
CertLE - Let's Encrypt PHP Command Line ACME Client
Copyright (C) 2016 S.Körfgen
Usage:
certle SUBCOMMAND
SUBCOMMANDS: (All keys are in PEM-Format)
genrsa <bits> Generate new RSA-Key
<bits> RSA-Key size in bits (default: 2048)
register <account_key> <email> Register key with ACME-Server
auto-register <account_key> <email> Same as above but auto-accepts TOS
<account_key> Account-Key
<email> Contact E-Mail
cert <account_key> <domain_key> options Issue Certificate
<account_key> Account-Key
<domain_key> Private-Key (public part of key is used to generate CSR)
options:
-w, --webroot Path to webroot/docroot
must be specified in front of -d, --domain; can be repeated
for each domain otherwise the previous one is used
-d, --domain Domainname
can be repeated up to 100 times
--cert Output Certificate
--chain Output Intermediate CA Certificate
--fullchain Output cert + chain
--csr Output CSR
revoke <key> <cert> Revoke Certificate
<key> Acount-Key or Domain-Key
<cert> cert or fullchain
deactivate <account_key> <account_id> Deactivate Account
<account_key> Account-Key
<account_id> Account ID (URL)
this URL is displayed when running the 'register' subcommand
EOD;
}