ovpn-key 1.1.4 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +16 -28
- data/bin/ovpn-key +93 -51
- data/defaults/ovpn-key.yml +0 -3
- data/lib/functions.rb +29 -3
- data/lib/version.rb +1 -1
- data/pre-existing-keys.md +25 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 12a8dfc02e3752450ea384809f2174f37ab15808c11112c0ffa863e2881acb6e
|
|
4
|
+
data.tar.gz: bc4a6989271bd77f26443a15df2baa4780bfdeeffd4c1100e664d6d59c26d9ce
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 04dfd8d8865bdeada6f61a8ef907cc3074310341322037e1b3d680031194a39f7954cd31f876901d29b5266247cbc68b290d506b619e7b413ab56d4f6dfa05db
|
|
7
|
+
data.tar.gz: 2f870ea24a12261deb32381405752836893713d0905e57bfbcd36a7ad7a0e6cb936ce9916b831ac4694430ba0a2e7533a693e6144ebaa0bf147fc923f5161daf
|
data/README.md
CHANGED
|
@@ -24,7 +24,7 @@ Important: `ca.key` is the CA's private key. Protect it carefully and never dist
|
|
|
24
24
|
Losing it means you can no longer issue certificates or generate updated CRLs for this CA.
|
|
25
25
|
|
|
26
26
|
There's a flat directory with configuration, generated keys and metadata:
|
|
27
|
-
* `ovpn-key.yml`: [configuration](
|
|
27
|
+
* `ovpn-key.yml`: [configuration](defaults/ovpn-key.yml)
|
|
28
28
|
* `serial`: incremental counter
|
|
29
29
|
* `crl.pem`: certificate revocation list, `crl-verify` in OpenVPN's config
|
|
30
30
|
* `ca.crt`: public key for the CA
|
|
@@ -39,7 +39,7 @@ Generating a certificate with existing name will fail, no harm will be done.
|
|
|
39
39
|
|
|
40
40
|
1. `ovpn-key --init` to initialize current directory
|
|
41
41
|
or `ovpn-key --init some/dir` to create the working directory
|
|
42
|
-
2. edit the `ovpn-key.yml` (see [docs](#
|
|
42
|
+
2. edit the `ovpn-key.yml` (see [docs](#configuration))
|
|
43
43
|
3. create the CA (once)
|
|
44
44
|
`ovpn-key --ca`
|
|
45
45
|
4. generate and sign a server certificate (once per server)
|
|
@@ -50,30 +50,10 @@ Generating a certificate with existing name will fail, no harm will be done.
|
|
|
50
50
|
6. revoke a client certificate
|
|
51
51
|
`ovpn-key --revoke alice`
|
|
52
52
|
|
|
53
|
-
###
|
|
53
|
+
### Existing certificates
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
* `ca.crt`: CA certificate
|
|
58
|
-
* `ca.key`: CA private key
|
|
59
|
-
* `crl.pem`: if the existing CA has revoked certificates, preserve its CRL.
|
|
60
|
-
Starting with a new CRL would lose the existing revocation information.
|
|
61
|
-
|
|
62
|
-
Skip the `--ca` step.
|
|
63
|
-
|
|
64
|
-
#### Certificate serial numbers
|
|
65
|
-
|
|
66
|
-
If the existing CA uses sequential integer serial numbers, initialize the `serial` file with the serial number of the latest issued certificate. For example, if the latest certificate has serial number `2`, run `echo -n 2 > serial`
|
|
67
|
-
|
|
68
|
-
If the existing CA uses random or non-sequential serial numbers (or serial numbers that cannot be represented as integers),
|
|
69
|
-
you can omit this step. `ovpn-key` will initialize the counter and start issuing certificates from `1`.
|
|
70
|
-
|
|
71
|
-
#### Revoking pre-existing certificates
|
|
72
|
-
|
|
73
|
-
Existing certificates remain valid and can continue to be used.
|
|
74
|
-
|
|
75
|
-
To revoke an existing certificate using `ovpn-key --revoke`, its `.crt` file must also be present in the working directory.
|
|
76
|
-
For example, to revoke `alice`, place `alice.crt` there.
|
|
55
|
+
We recommend to start afresh with new better keys.
|
|
56
|
+
However, you may want to import the pre-existing CA to `ovpn-key`, see [docs](pre-existing-keys.md).
|
|
77
57
|
|
|
78
58
|
### --server
|
|
79
59
|
|
|
@@ -123,19 +103,27 @@ It will contain:
|
|
|
123
103
|
|
|
124
104
|
### Configuration
|
|
125
105
|
|
|
126
|
-
It's just a single simple YAML file named [`ovpn-key.yml`](
|
|
106
|
+
It's just a single simple YAML file named [`ovpn-key.yml`](defaults/ovpn-key.yml).
|
|
127
107
|
|
|
128
|
-
ovpn-key also processes `~/.ovpn-key.yml` file
|
|
108
|
+
ovpn-key also processes `~/.ovpn-key.yml` file with three possible settings:
|
|
129
109
|
```yaml
|
|
130
110
|
cd: ~/some/path
|
|
111
|
+
zip_dir: ~
|
|
112
|
+
openvpn: openvpn
|
|
131
113
|
```
|
|
132
114
|
|
|
133
|
-
|
|
115
|
+
The `cd` setting is used as a default working directory if:
|
|
134
116
|
1. current directory does not have `ovpn-key.yml`
|
|
135
117
|
2. `--init` is not specified
|
|
136
118
|
|
|
137
119
|
If you specify the default directory, you don't need to travel to it every time you want to launch `ovpn-key`, i.e. you can sign and revoke certs without traveling to the directory with certs.
|
|
138
120
|
|
|
121
|
+
The `zip_dir` is the directory for `--zip` files, can be overridden in work directory `ovpn-key.yml`.
|
|
122
|
+
Default is `~`, i.e. user's home directory.
|
|
123
|
+
|
|
124
|
+
`openvpn` is path to OpenVPN binary for generating `--static` keys, it can be overridden in work directory `ovpn-key.yml`.
|
|
125
|
+
If you have `openvpn` in `$PATH`, no need to specify this.
|
|
126
|
+
|
|
139
127
|
### CA variants supported
|
|
140
128
|
|
|
141
129
|
We still support the old RSA mode, but it's not recommended to use in new configurations,
|
data/bin/ovpn-key
CHANGED
|
@@ -16,53 +16,58 @@ SERIAL_FILE = 'serial'
|
|
|
16
16
|
|
|
17
17
|
options = {}
|
|
18
18
|
# rubocop:disable-next Metrics/BlockLength
|
|
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
|
-
|
|
19
|
+
begin
|
|
20
|
+
OptionParser.new do |opts|
|
|
21
|
+
opts.banner = "Usage: #{File.basename $PROGRAM_NAME} <options> [--nopass]"
|
|
22
|
+
opts.on('--init [directory]', 'Init a CA directory (defaults to current)') do |v|
|
|
23
|
+
options[:init] = v || '.'
|
|
24
|
+
end
|
|
25
|
+
opts.on('--ca', 'Generate a CA (ca.crt)') do |v|
|
|
26
|
+
check_crt('ca')
|
|
27
|
+
options[:generate_ca] = v
|
|
28
|
+
end
|
|
29
|
+
opts.on('--dh', 'Generate a DH keyfile (dh.pem)') do |v|
|
|
30
|
+
# it's safe to overwrite this file
|
|
31
|
+
options[:generate_dh] = v
|
|
32
|
+
end
|
|
33
|
+
opts.on('--static', 'Generate OpenVPN static key (ta.key)') do |v|
|
|
34
|
+
options[:generate_static] = v
|
|
35
|
+
check_crt('ta')
|
|
36
|
+
end
|
|
37
|
+
opts.on('--server [name]', "Generate a server key (defaults to 'server')") do |v|
|
|
38
|
+
options[:generate_server] = v || 'server'
|
|
39
|
+
check_crt(options[:generate_server])
|
|
40
|
+
end
|
|
41
|
+
opts.on('--client [name]', 'Generate a client key and sign it') do |v|
|
|
42
|
+
check_client(v)
|
|
43
|
+
options[:generate_client] = v
|
|
44
|
+
end
|
|
45
|
+
opts.on('--zip [name]', 'Ditto plus pack it to ZIP with OpenVPN config') do |v|
|
|
46
|
+
check_client(v)
|
|
47
|
+
options[:generate_zip] = v
|
|
48
|
+
end
|
|
49
|
+
opts.on('--revoke [name]', "Revoke a certificate (using #{CRL_FILE}) and delete it") do |v|
|
|
50
|
+
abort 'Please specify what certificate to revoke' unless v
|
|
51
|
+
abort "ERROR: cannot revoke ca.crt, it's the root cert" if v == 'ca'
|
|
52
|
+
options[:revoke] = v
|
|
53
|
+
end
|
|
54
|
+
opts.on('--nopass', "Don't protect .key files with a password") do |v|
|
|
55
|
+
options[:no_password] = v
|
|
56
|
+
end
|
|
57
|
+
end.parse!
|
|
58
|
+
rescue OptionParser::ParseError => e
|
|
59
|
+
usage e.message
|
|
60
|
+
end
|
|
56
61
|
if ARGV.length.positive?
|
|
57
|
-
|
|
62
|
+
usage "invalid args: #{ARGV.join ' '}"
|
|
58
63
|
end
|
|
59
64
|
unless options[:init] || options[:generate_ca] || options[:generate_dh] || options[:generate_static] \
|
|
60
65
|
|| options[:generate_server] || options[:generate_client] || options[:generate_zip] || options[:revoke]
|
|
61
|
-
|
|
66
|
+
usage 'no action provided'
|
|
62
67
|
end
|
|
63
68
|
if options[:generate_client] && options[:generate_zip]
|
|
64
69
|
# I assume that user likely wants one of them and is confused with usage
|
|
65
|
-
abort '
|
|
70
|
+
abort 'ERROR: there can be only one: --client or --zip'
|
|
66
71
|
end
|
|
67
72
|
|
|
68
73
|
if options[:init]
|
|
@@ -74,13 +79,13 @@ if options[:init]
|
|
|
74
79
|
FileUtils.copy_file(File.expand_path("defaults/#{APP_CONF}", "#{__dir__}/.."), "./#{APP_CONF}")
|
|
75
80
|
warn "Created file: #{APP_CONF}"
|
|
76
81
|
end
|
|
77
|
-
|
|
82
|
+
else
|
|
78
83
|
begin
|
|
79
84
|
rc = YAML.load_file(File.expand_path("~/.#{APP_CONF}"))
|
|
80
85
|
rescue Errno::ENOENT
|
|
81
86
|
# no configuration file in home directory is not an error
|
|
82
87
|
end
|
|
83
|
-
Dir.chdir File.expand_path(rc['cd']) if
|
|
88
|
+
Dir.chdir File.expand_path(rc['cd']) if !File.exist?(APP_CONF) && rc['cd']
|
|
84
89
|
end
|
|
85
90
|
|
|
86
91
|
lock_file = File.join(Dir.tmpdir, "ovpn-key-#{Digest::SHA256.hexdigest(Dir.pwd)}.lock")
|
|
@@ -116,8 +121,16 @@ begin
|
|
|
116
121
|
rescue Errno::ENOENT
|
|
117
122
|
abort "Run `#{File.basename $PROGRAM_NAME} --init` before generating certificates"
|
|
118
123
|
end
|
|
119
|
-
ZIP_DIR = settings['zip_dir']
|
|
120
|
-
|
|
124
|
+
ZIP_DIR = settings['zip_dir'] || if rc && rc['zip_dir']
|
|
125
|
+
rc['zip_dir']
|
|
126
|
+
else
|
|
127
|
+
'~'
|
|
128
|
+
end
|
|
129
|
+
OPENVPN = settings['openvpn'] || if rc && rc['openvpn']
|
|
130
|
+
rc['openvpn']
|
|
131
|
+
else
|
|
132
|
+
'openvpn'
|
|
133
|
+
end
|
|
121
134
|
CN_CA = settings['ca_name'] || 'Certification Authority'
|
|
122
135
|
ENCRYPT = settings['encrypt'] || 'AES256'
|
|
123
136
|
KEY_SIZE = settings['key_size'] || 3072
|
|
@@ -172,11 +185,37 @@ if File.exist? 'openssl.ini'
|
|
|
172
185
|
warn 'WARNING: openssl.ini file is not used anymore. you can remove it'
|
|
173
186
|
end
|
|
174
187
|
|
|
188
|
+
phase_out = nil
|
|
189
|
+
unless ENCRYPT.casecmp? 'AES256'
|
|
190
|
+
warn "WARNING: your CA uses #{ENCRYPT}, not AES256"
|
|
191
|
+
phase_out = true
|
|
192
|
+
end
|
|
193
|
+
unless DIGEST&.match?(/\Asha(384|512)\z/i)
|
|
194
|
+
warn "WARNING: your CA uses #{DIGEST}; SHA384 or SHA512 are recommended instead"
|
|
195
|
+
phase_out = true
|
|
196
|
+
end
|
|
197
|
+
if KEY_TYPE && !KEY_TYPE&.match?(/\A(secp(384|521)r1|ML-DSA-87)\z/i)
|
|
198
|
+
warn "WARNING: your CA uses #{KEY_TYPE}, we recommend using secp384r1, secp521r1, ML-DSA-87 or RSA"
|
|
199
|
+
phase_out = true
|
|
200
|
+
end
|
|
201
|
+
if KEY_SIZE < 3072
|
|
202
|
+
warn "WARNING: your CA uses #{KEY_SIZE}-bit keys, 3072 is a recommended minimum"
|
|
203
|
+
phase_out = true
|
|
204
|
+
end
|
|
205
|
+
if phase_out
|
|
206
|
+
warn "WARNING: it's recommended to phase out this deployment and start afresh with better security"
|
|
207
|
+
end
|
|
208
|
+
|
|
175
209
|
if options[:generate_ca]
|
|
176
|
-
ca_pass = options[:no_password]
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
210
|
+
ca_pass = passphrase(options[:no_password], 'ca')
|
|
211
|
+
begin
|
|
212
|
+
gen_key('ca', ca_pass)
|
|
213
|
+
sign_key('ca', CN_CA, ca_pass)
|
|
214
|
+
gen_crl(ca_pass)
|
|
215
|
+
rescue Interrupt, SystemExit
|
|
216
|
+
cleanup 'ca' # delete the CA it it's not complete
|
|
217
|
+
abort_on_interrupted
|
|
218
|
+
end
|
|
180
219
|
end
|
|
181
220
|
if options[:generate_dh]
|
|
182
221
|
File.open('dh.pem', 'w') do |f|
|
|
@@ -189,10 +228,12 @@ if options[:generate_static]
|
|
|
189
228
|
exe "#{OPENVPN} --genkey --secret ta.key"
|
|
190
229
|
end
|
|
191
230
|
if options[:generate_server]
|
|
192
|
-
|
|
231
|
+
passphrase = passphrase(options[:no_password], options[:generate_server])
|
|
232
|
+
gen_and_sign('server', options[:generate_server], passphrase)
|
|
193
233
|
end
|
|
194
234
|
if options[:generate_client]
|
|
195
|
-
|
|
235
|
+
passphrase = passphrase(options[:no_password], options[:generate_client])
|
|
236
|
+
gen_and_sign('client', options[:generate_client], passphrase)
|
|
196
237
|
end
|
|
197
238
|
if options[:generate_zip]
|
|
198
239
|
ovpn_files = Dir['*.ovpn']
|
|
@@ -205,7 +246,8 @@ if options[:generate_zip]
|
|
|
205
246
|
abort 'More than one .ovpn files in current directory, aborting'
|
|
206
247
|
end
|
|
207
248
|
|
|
208
|
-
|
|
249
|
+
passphrase = passphrase(options[:no_password], options[:generate_zip])
|
|
250
|
+
gen_and_sign('client', options[:generate_zip], passphrase)
|
|
209
251
|
|
|
210
252
|
zip_file = File.join(File.expand_path(ZIP_DIR), "#{File.basename ovpn_file, '.ovpn'}.tblk.zip")
|
|
211
253
|
FileUtils.rm_f(zip_file)
|
data/defaults/ovpn-key.yml
CHANGED
data/lib/functions.rb
CHANGED
|
@@ -2,19 +2,38 @@
|
|
|
2
2
|
|
|
3
3
|
def check_crt(filename)
|
|
4
4
|
%w[key crt].each {|ext|
|
|
5
|
-
abort "#{filename}.#{ext} already exists,
|
|
5
|
+
abort "#{filename}.#{ext} already exists, aborting" if File.exist? "#{filename}.#{ext}"
|
|
6
6
|
}
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
def check_client(name)
|
|
10
|
-
abort '
|
|
10
|
+
abort 'ERROR: client should have an alphanumeric name' unless name
|
|
11
11
|
check_crt(name)
|
|
12
12
|
end
|
|
13
13
|
|
|
14
|
+
def usage(error)
|
|
15
|
+
abort "ERROR: #{error}\nSee `#{File.basename $PROGRAM_NAME} -h` for usage"
|
|
16
|
+
end
|
|
17
|
+
|
|
14
18
|
def exe(cmd)
|
|
15
19
|
system(cmd) || abort("error executing: #{cmd}")
|
|
16
20
|
end
|
|
17
21
|
|
|
22
|
+
def passphrase(no_pass, cert_name)
|
|
23
|
+
return if no_pass
|
|
24
|
+
|
|
25
|
+
begin
|
|
26
|
+
passphrase = ask_password(cert_name)
|
|
27
|
+
rescue Interrupt, SystemExit
|
|
28
|
+
abort_on_interrupted
|
|
29
|
+
end
|
|
30
|
+
passphrase
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def abort_on_interrupted
|
|
34
|
+
abort "\nInterrupted, aborting!"
|
|
35
|
+
end
|
|
36
|
+
|
|
18
37
|
def ask_password(name)
|
|
19
38
|
password = ''
|
|
20
39
|
loop do
|
|
@@ -40,6 +59,9 @@ end
|
|
|
40
59
|
def gen_and_sign(type, certname, password)
|
|
41
60
|
gen_key(certname, password)
|
|
42
61
|
sign_key(type, certname, password)
|
|
62
|
+
rescue Interrupt, SystemExit
|
|
63
|
+
cleanup certname # clean up the key if it wasn't signed due to user's interrupt
|
|
64
|
+
abort_on_interrupted
|
|
43
65
|
end
|
|
44
66
|
|
|
45
67
|
def gen_key(certname, password)
|
|
@@ -124,7 +146,7 @@ def revoke(certname)
|
|
|
124
146
|
begin
|
|
125
147
|
cert = OpenSSL::X509::Certificate.new(File.read("#{certname}.crt"))
|
|
126
148
|
rescue Errno::ENOENT
|
|
127
|
-
abort "#{certname}.crt doesn't exist"
|
|
149
|
+
abort "#{certname}.crt doesn't exist, aborting"
|
|
128
150
|
end
|
|
129
151
|
revoke = OpenSSL::X509::Revoked.new.tap {|rev|
|
|
130
152
|
rev.serial = cert.serial
|
|
@@ -133,6 +155,10 @@ def revoke(certname)
|
|
|
133
155
|
crl.next_update = time_after_days(EXPIRE['crl'])
|
|
134
156
|
crl.add_revoked(revoke)
|
|
135
157
|
update_crl(crl, '')
|
|
158
|
+
cleanup certname
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def cleanup(certname)
|
|
136
162
|
%w[crt key].each {|ext|
|
|
137
163
|
begin
|
|
138
164
|
File.delete "#{certname}.#{ext}"
|
data/lib/version.rb
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Importing a pre-existing CA
|
|
2
|
+
|
|
3
|
+
To import an existing CA instead of creating a new one,
|
|
4
|
+
place the following files in the working directory:
|
|
5
|
+
|
|
6
|
+
* `ca.crt`: CA certificate
|
|
7
|
+
* `ca.key`: CA private key
|
|
8
|
+
* `crl.pem`: if the existing CA has revoked certificates, preserve its CRL.
|
|
9
|
+
Starting with a new CRL would lose the existing revocation information.
|
|
10
|
+
|
|
11
|
+
Skip the `--ca` step.
|
|
12
|
+
|
|
13
|
+
## Certificate serial numbers
|
|
14
|
+
|
|
15
|
+
If the existing CA uses sequential integer serial numbers, initialize the `serial` file with the serial number of the latest issued certificate. For example, if the latest certificate has serial number `2`, run `echo -n 2 > serial`
|
|
16
|
+
|
|
17
|
+
If the existing CA uses random or non-sequential serial numbers (or serial numbers that cannot be represented as integers),
|
|
18
|
+
you can omit this step. `ovpn-key` will initialize the counter and start issuing certificates from `1`.
|
|
19
|
+
|
|
20
|
+
## Revoking pre-existing certificates
|
|
21
|
+
|
|
22
|
+
Existing certificates remain valid and can continue to be used.
|
|
23
|
+
|
|
24
|
+
To revoke an existing certificate using `ovpn-key --revoke`, its `.crt` file must also be present in the working directory.
|
|
25
|
+
For example, to revoke `alice`, place `alice.crt` there.
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ovpn-key
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Vasily Korytov
|
|
@@ -45,6 +45,7 @@ files:
|
|
|
45
45
|
- defaults/ovpn-key.yml
|
|
46
46
|
- lib/functions.rb
|
|
47
47
|
- lib/version.rb
|
|
48
|
+
- pre-existing-keys.md
|
|
48
49
|
homepage: https://github.com/chillum/ovpn-key
|
|
49
50
|
licenses:
|
|
50
51
|
- Apache-2.0
|