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