Skip to content

Commit ea97ca1

Browse files
committed
🔐 👊 💥 🔥
0 parents  commit ea97ca1

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed

create-cert

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
# Usage: create-cert [CNAME=localhost] [ORGANIZATION]
3+
#
4+
# Creates a self-signed certificate and outputs it and its private key.
5+
set -e
6+
7+
name="${1:-localhost}"
8+
org="$2"
9+
temp="${TMPDIR:-/tmp}/openssl-req.$$.$(date +%s)"
10+
11+
openssl req \
12+
-x509 -nodes -days 365 \
13+
-subj "/O=$org/CN=$name" \
14+
-newkey rsa:1024 -keyout "$temp" -out "$temp" 2>/dev/null
15+
16+
cat "$temp"
17+
rm -f "$temp"

der2pem

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
set -e
3+
while [ -n "$1" ]; do
4+
if ! [ -f "$1" ]; then
5+
echo "invalid .cer file: \`$1'" >&2
6+
exit 1
7+
fi
8+
openssl x509 -inform der -in "$1" -outform pem
9+
shift 1
10+
done

doctor.rb

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Usage: ruby doctor.rb [HOST=status.github.com[:PORT=443]]
2+
require 'rbconfig'
3+
require 'net/https'
4+
5+
if ARGV[0] =~ /^[^-]/
6+
host, port = ARGV[0].split(':', 2)
7+
else
8+
host = 'status.github.com'
9+
end
10+
port ||= 443
11+
12+
ruby = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
13+
ruby_version = RUBY_VERSION
14+
if patch = RbConfig::CONFIG['PATCHLEVEL']
15+
ruby_version += "-p#{patch}"
16+
end
17+
puts "%s (%s)" % [ruby, ruby_version]
18+
19+
openssl_dir = OpenSSL::X509::DEFAULT_CERT_AREA
20+
mac_openssl = '/System/Library/OpenSSL' == openssl_dir
21+
puts "%s: %s" % [OpenSSL::OPENSSL_VERSION, openssl_dir]
22+
[OpenSSL::X509::DEFAULT_CERT_DIR_ENV, OpenSSL::X509::DEFAULT_CERT_FILE_ENV].each do |key|
23+
puts "%s=%s" % [key, ENV[key].to_s.inspect]
24+
end
25+
26+
ca_file = ENV[OpenSSL::X509::DEFAULT_CERT_FILE_ENV] || OpenSSL::X509::DEFAULT_CERT_FILE
27+
ca_path = (ENV[OpenSSL::X509::DEFAULT_CERT_DIR_ENV] || OpenSSL::X509::DEFAULT_CERT_DIR).chomp('/')
28+
29+
puts "\nHEAD https://#{host}:#{port}"
30+
http = Net::HTTP.new(host, port)
31+
http.use_ssl = true
32+
33+
# Explicitly setting cert_store like this is not needed in most cases but it
34+
# seems necessary in edge cases such as when using `verify_callback` in some
35+
# combination of Ruby + OpenSSL versions.
36+
http.cert_store = OpenSSL::X509::Store.new
37+
http.cert_store.set_default_paths
38+
39+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
40+
failed_cert = failed_cert_reason = nil
41+
42+
if mac_openssl
43+
warn "warning: unable show failed certificate info on OS X's OpenSSL"
44+
# This drives me absolutely nuts. It seems that on Rubies compiled against OS X's
45+
# system OpenSSL, the mere fact of defining a `verify_callback` makes the
46+
# cert verification fail for requests that would otherwise be successful.
47+
else
48+
http.verify_callback = lambda { |verify_ok, store_context|
49+
if !verify_ok
50+
failed_cert = store_context.current_cert
51+
failed_cert_reason = "%d: %s" % [ store_context.error, store_context.error_string ]
52+
end
53+
verify_ok
54+
}
55+
end
56+
57+
user_agent = "net/http #{ruby_version}"
58+
req = Net::HTTP::Head.new('/', 'user-agent' => user_agent)
59+
60+
begin
61+
res = http.start { http.request(req) }
62+
abort res.inspect if res.code.to_i >= 500
63+
puts "OK"
64+
rescue Errno::ECONNREFUSED
65+
puts "Error: connection refused"
66+
exit 1
67+
rescue OpenSSL::SSL::SSLError => e
68+
puts "#{e.class}: #{e.message}"
69+
70+
if failed_cert
71+
puts "\nThe server presented a certificate that could not be verified:"
72+
puts "subject: #{failed_cert.subject}"
73+
puts "issuer: #{failed_cert.issuer}"
74+
puts "error code %s" % failed_cert_reason
75+
end
76+
77+
ca_file_missing = !File.exist?(ca_file) && !mac_openssl
78+
ca_path_empty = Dir["#{ca_path}/*"].empty?
79+
80+
if ca_file_missing || ca_path_empty
81+
puts "\nPossible causes:"
82+
puts "- `%s' does not exist" % ca_file if ca_file_missing
83+
puts "- `%s/' is empty" % ca_path if ca_path_empty
84+
end
85+
86+
exit 1
87+
end

readme.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# SSL tools
2+
3+
der2pem FILES...
4+
5+
remote-cert status.github.com | show-cert
6+
7+
create-cert example.com "My Organization" > cert_and_key.pem
8+
9+
ruby doctor.rb example.com:443
10+
11+
Sample output of `doctor.rb`:
12+
13+
```
14+
/Users/mislav/.rbenv/versions/2.0.0-p247/bin/ruby (2.0.0-p247)
15+
OpenSSL 1.0.1e 11 Feb 2013: /usr/local/etc/openssl
16+
SSL_CERT_DIR=""
17+
SSL_CERT_FILE=""
18+
19+
HEAD https://status.github.com:443
20+
OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
21+
22+
The server presented a certificate that could not be verified:
23+
subject: /C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert High Assurance CA-3
24+
issuer: /C=US/O=DigiCert Inc/OU=www.digicert.com/CN=DigiCert High Assurance EV Root CA
25+
error code 20: unable to get local issuer certificate
26+
27+
Possible causes:
28+
- `/usr/local/etc/openssl/cert.pem' does not exist
29+
- `/usr/local/etc/openssl/certs/' is empty
30+
```

remote-cert

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
# Usage: remote-cert HOST[:PORT] [HOST[:PORT] ...]
3+
set -e
4+
5+
while [ -n "$1" ]; do
6+
host="${1%:*}"
7+
port="${1#*:}"
8+
[ "$host" = "$port" ] && port=443
9+
10+
echo | openssl s_client -connect ${host}:${port} 2>&1 |\
11+
sed -nEe '/^-+BEGIN/,/^-+END/p'
12+
13+
shift 1
14+
done

show-cert

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
# Display human-readable information from a certificate.
3+
set -e
4+
5+
file="$1"
6+
[ "$file" = "-" ] && file=""
7+
8+
if [ -n "$file" -a ! -f "$file" ] || [ -z "$file" -a -t 0 ]; then
9+
echo "Usage: show-cert <file>" >&2
10+
exit 1
11+
fi
12+
13+
format=pem
14+
[ "${file##*.}" = "cer" ] && format=der
15+
16+
exec openssl x509 -inform "$format" ${1:+-in "$1"} -noout -text \
17+
-nameopt multiline -certopt no_pubkey,no_sigdump

0 commit comments

Comments
 (0)