-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck_ssl_cert_and_key.rb
More file actions
executable file
·50 lines (38 loc) · 1014 Bytes
/
Copy pathcheck_ssl_cert_and_key.rb
File metadata and controls
executable file
·50 lines (38 loc) · 1014 Bytes
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
#!/usr/bin/env ruby
##
# Check the provided SSL certificate and private key to see if they are a valid pair.
#
# The certificate and key are expected to be DER or PEM-encoded.
# https://ruby-doc.org/stdlib-2.7.1/libdoc/openssl/rdoc/OpenSSL/X509/Certificate.html
# https://ruby-doc.org/stdlib-2.7.1/libdoc/openssl/rdoc/OpenSSL/PKey/RSA.html
#
require 'openssl'
cert_file = ARGV[0]
key_file = ARGV[1]
if cert_file.nil? || key_file.nil?
puts 'Please provide the certificate and key files as arguments.'
exit 1
end
key = OpenSSL::PKey::RSA.new File.read(key_file)
cert = OpenSSL::X509::Certificate.new File.read(cert_file)
##
# Color methods for strings
#
module ColorizableString
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
def red
colorize(31)
end
def green
colorize(32)
end
end
String.include ColorizableString
if cert.check_private_key key
puts "It's a match!".green
exit 0
end
puts "The provided certificate and key do not appear to match.".red
exit 1