]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - tools/genssl
Replace the geoip module with geo_maxmind, geoban, and geoclass.
[user/henk/code/inspircd.git] / tools / genssl
1 #!/usr/bin/env perl
2 #
3 # InspIRCd -- Internet Relay Chat Daemon
4 #
5 #   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6 #   Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
7 #   Copyright (C) 2013 Peter Powell <petpow@saberuk.com>
8 #
9 # This file is part of InspIRCd.  InspIRCd is free software: you can
10 # redistribute it and/or modify it under the terms of the GNU General Public
11 # License as published by the Free Software Foundation, version 2.
12 #
13 # This program is distributed in the hope that it will be useful, but WITHOUT
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16 # details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21
22
23 BEGIN {
24         require 5.10.0;
25 }
26
27 use feature ':5.10';
28 use strict;
29 use warnings FATAL => qw(all);
30
31 use File::Temp();
32
33 # IMPORTANT: This script has to be able to run by itself so that it can be used
34 #            by binary distributions where the make/console.pm module will not
35 #            be available!
36
37 sub prompt($$) {
38         my ($question, $default) = @_;
39         return prompt_string(1, $question, $default) if eval 'use FindBin;use lib $FindBin::RealDir;use make::console; 1';
40         say $question;
41         print "[$default] => ";
42         chomp(my $answer = <STDIN>);
43         say '';
44         return $answer ? $answer : $default;
45 }
46
47 if ($#ARGV != 0 || $ARGV[0] !~ /^(?:auto|gnutls|openssl)$/i) {
48         say STDERR "Usage: $0 <auto|gnutls|openssl>";
49         exit 1;
50 }
51
52 # On OS X the GnuTLS certtool is prefixed to avoid collision with the system certtool.
53 my $certtool = $^O eq 'darwin' ? 'gnutls-certtool' : 'certtool';
54
55 # Check whether the user has the required tools installed.
56 my $has_gnutls = `$certtool --version v 2>/dev/null`;
57 my $has_openssl = !system 'openssl version >/dev/null 2>&1';
58
59 # The framework the user has specified.
60 my $tool = lc $ARGV[0];
61
62 # If the user has not explicitly specified a framework then detect one.
63 if ($tool eq 'auto') {
64         if ($has_gnutls) {
65                 $tool = 'gnutls';
66         } elsif ($has_openssl) {
67                 $tool = 'openssl';
68         } else {
69                 say STDERR "SSL generation failed: could not find $certtool or openssl in the PATH!";
70                 exit 1;
71         }
72 } elsif ($tool eq 'gnutls' && !$has_gnutls) {
73         say STDERR "SSL generation failed: could not find '$certtool' in the PATH!";
74         exit 1;
75 } elsif ($tool eq 'openssl' && !$has_openssl) {
76         say STDERR 'SSL generation failed: could not find \'openssl\' in the PATH!';
77         exit 1;
78 }
79
80 # Harvest information needed to generate the certificate.
81 my $common_name = prompt('What is the hostname of your server?', 'irc.example.com');
82 my $email = prompt('What email address can you be contacted at?', 'example@example.com');
83 my $unit = prompt('What is the name of your unit?', 'Server Admins');
84 my $organization = prompt('What is the name of your organization?', 'Example IRC Network');
85 my $city = prompt('What city are you located in?', 'Example City');
86 my $state = prompt('What state are you located in?', 'Example State');
87 my $country = prompt('What is the ISO 3166-1 code for the country you are located in?', 'XZ');
88 my $days = prompt('How many days do you want your certificate to be valid for?', '365');
89
90 # Contains the SSL certificate in DER form.
91 my $dercert;
92
93 # Contains the exit code of openssl/gnutls-certtool.
94 my $status = 0;
95
96 if ($tool eq 'gnutls') {
97         $has_gnutls =~ /certtool.+?(\d+\.\d+)/;
98         my $sec_param = $1 lt '2.10' ? '--bits 2048' : '--sec-param normal';
99         my $tmp = new File::Temp();
100         print $tmp <<__GNUTLS_END__;
101 cn              = "$common_name"
102 email           = "$email"
103 unit            = "$unit"
104 organization    = "$organization"
105 locality        = "$city"
106 state           = "$state"
107 country         = "$country"
108 expiration_days = $days
109 tls_www_client
110 tls_www_server
111 signing_key
112 encryption_key
113 cert_signing_key
114 crl_signing_key
115 code_signing_key
116 ocsp_signing_key
117 time_stamping_key
118 __GNUTLS_END__
119         close($tmp);
120         $status ||= system "$certtool --generate-privkey $sec_param --outfile key.pem";
121         $status ||= system "$certtool --generate-self-signed --load-privkey key.pem --outfile cert.pem --template $tmp";
122         $status ||= system "$certtool --generate-request --load-privkey key.pem --outfile csr.pem --template $tmp";
123         $status ||= system "$certtool --generate-dh-params $sec_param --outfile dhparams.pem";
124         $dercert = `$certtool --certificate-info --infile cert.pem --outder` unless $status;
125 } elsif ($tool eq 'openssl') {
126         my $tmp = new File::Temp();
127         print $tmp <<__OPENSSL_END__;
128 $country
129 $state
130 $city
131 $organization
132 $unit
133 $common_name
134 $email
135 .
136 $organization
137 __OPENSSL_END__
138         close($tmp);
139         $status ||= system "cat $tmp | openssl req -x509 -nodes -newkey rsa:2048 -keyout key.pem -out cert.pem -days $days 2>/dev/null";
140         $status ||= system "cat $tmp | openssl req -new -nodes -key key.pem -out csr.pem 2>/dev/null";
141         $status ||= system 'openssl dhparam -out dhparams.pem 2048';
142         $dercert = `openssl x509 -in cert.pem -outform DER` unless $status;
143 }
144
145 if ($status) {
146         say STDERR "SSL generation failed: $tool exited with a non-zero status!";
147         exit 1;
148 }
149
150 if (defined $dercert && eval 'use Digest::SHA; 1') {
151         my $hash = Digest::SHA->new(256);
152         $hash->add($dercert);
153         say '';
154         say 'If you are using the self-signed certificate then add this TLSA record to your domain for DANE support:';
155         say "_6697._tcp." . $common_name . " TLSA 3 0 1 " . $hash->hexdigest;
156 }