]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - tools/genssl
Update copyright headers.
[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 BEGIN {
23         require 5.10.0;
24 }
25
26 use feature ':5.10';
27 use strict;
28 use warnings FATAL => qw(all);
29
30 use File::Temp();
31
32 # IMPORTANT: This script has to be able to run by itself so that it can be used
33 #            by binary distributions where the make/console.pm module will not
34 #            be available!
35 eval {
36         use File::Basename qw(dirname);
37         use FindBin        qw($RealDir);
38
39         use lib dirname $RealDir;
40         require make::console;
41         make::console->import();
42 };
43
44 sub prompt($$) {
45         my ($question, $default) = @_;
46         return prompt_string(1, $question, $default) if defined main->can('prompt_string');
47         say $question;
48         print "[$default] => ";
49         chomp(my $answer = <STDIN>);
50         say '';
51         return $answer ? $answer : $default;
52 }
53
54 if (scalar @ARGV < 1 || $ARGV[0] !~ /^(?:auto|gnutls|openssl)$/i) {
55         say STDERR "Usage: $0 <auto|gnutls|openssl> [SSL-DIR]";
56         exit 1;
57 }
58
59 # On OS X the GnuTLS certtool is prefixed to avoid collision with the system certtool.
60 my $certtool = $^O eq 'darwin' ? 'gnutls-certtool' : 'certtool';
61
62 # Check whether the user has the required tools installed.
63 my $has_gnutls = `$certtool --version v 2>/dev/null`;
64 my $has_openssl = !system 'openssl version >/dev/null 2>&1';
65
66 # The framework the user has specified.
67 my $tool = lc $ARGV[0];
68
69 # If the user has not explicitly specified a framework then detect one.
70 if ($tool eq 'auto') {
71         if ($has_gnutls) {
72                 $tool = 'gnutls';
73         } elsif ($has_openssl) {
74                 $tool = 'openssl';
75         } else {
76                 say STDERR "SSL generation failed: could not find $certtool or openssl in the PATH!";
77                 exit 1;
78         }
79 } elsif ($tool eq 'gnutls' && !$has_gnutls) {
80         say STDERR "SSL generation failed: could not find '$certtool' in the PATH!";
81         exit 1;
82 } elsif ($tool eq 'openssl' && !$has_openssl) {
83         say STDERR 'SSL generation failed: could not find \'openssl\' in the PATH!';
84         exit 1;
85 }
86
87 # Output to the cwd unless an SSL directory is specified.
88 if (scalar @ARGV > 1 && !chdir $ARGV[1]) {
89         say STDERR "Unable to change the working directory to $ARGV[1]: $!.";
90         exit 1;
91 }
92
93 # Harvest information needed to generate the certificate.
94 my $common_name = prompt('What is the hostname of your server?', 'irc.example.com');
95 my $email = prompt('What email address can you be contacted at?', 'example@example.com');
96 my $unit = prompt('What is the name of your unit?', 'Server Admins');
97 my $organization = prompt('What is the name of your organization?', 'Example IRC Network');
98 my $city = prompt('What city are you located in?', 'Example City');
99 my $state = prompt('What state are you located in?', 'Example State');
100 my $country = prompt('What is the ISO 3166-1 code for the country you are located in?', 'XZ');
101 my $days = prompt('How many days do you want your certificate to be valid for?', '365');
102
103 # Contains the exit code of openssl/gnutls-certtool.
104 my $status = 0;
105
106 if ($tool eq 'gnutls') {
107         $has_gnutls =~ /certtool.+?(\d+\.\d+)/;
108         my $sec_param = $1 lt '2.10' ? '--bits 2048' : '--sec-param normal';
109         my $tmp = new File::Temp();
110         print $tmp <<__GNUTLS_END__;
111 cn              = "$common_name"
112 email           = "$email"
113 unit            = "$unit"
114 organization    = "$organization"
115 locality        = "$city"
116 state           = "$state"
117 country         = "$country"
118 expiration_days = $days
119 tls_www_client
120 tls_www_server
121 signing_key
122 encryption_key
123 cert_signing_key
124 crl_signing_key
125 code_signing_key
126 ocsp_signing_key
127 time_stamping_key
128 __GNUTLS_END__
129         close($tmp);
130         $status ||= system "$certtool --generate-privkey $sec_param --outfile key.pem";
131         $status ||= system "$certtool --generate-self-signed --load-privkey key.pem --outfile cert.pem --template $tmp";
132         $status ||= system "$certtool --generate-request --load-privkey key.pem --outfile csr.pem --template $tmp";
133         $status ||= system "$certtool --generate-dh-params $sec_param --outfile dhparams.pem";
134 } elsif ($tool eq 'openssl') {
135         my $tmp = new File::Temp();
136         print $tmp <<__OPENSSL_END__;
137 $country
138 $state
139 $city
140 $organization
141 $unit
142 $common_name
143 $email
144 .
145 $organization
146 __OPENSSL_END__
147         close($tmp);
148         $status ||= system "cat $tmp | openssl req -x509 -nodes -newkey rsa:2048 -keyout key.pem -out cert.pem -days $days 2>/dev/null";
149         $status ||= system "cat $tmp | openssl req -new -nodes -key key.pem -out csr.pem 2>/dev/null";
150         $status ||= system 'openssl dhparam -out dhparams.pem 2048';
151 }
152
153 if ($status) {
154         say STDERR "SSL generation failed: $tool exited with a non-zero status!";
155         exit 1;
156 }