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