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