]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - tools/genssl
Merge pull request #941 from SaberUK/master+test-build
[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.8.0;
25 }
26
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/utilities.pm module will not
34 #            be available!
35
36 sub prompt($$) {
37         my ($question, $default) = @_;
38         return prompt_string(1, $question, $default) if eval 'use make::console; 1';
39         print "$question\n";
40         print "[$default] => ";
41         chomp(my $answer = <STDIN>);
42         print "\n";
43         return $answer ? $answer : $default;
44 }
45
46 if ($#ARGV != 0 || $ARGV[0] !~ /^(?:auto|gnutls|openssl)$/i) {
47         print "Syntax: genssl <auto|gnutls|openssl>\n";
48         exit 1;
49 }
50
51 # On OS X the GnuTLS certtool is prefixed to avoid collision with the system certtool.
52 my $certtool = $^O eq 'darwin' ? 'gnutls-certtool' : 'certtool';
53
54 # Check whether the user has the required tools installed.
55 my $has_gnutls = `$certtool --version v 2>/dev/null`;
56 my $has_openssl = !system 'openssl version >/dev/null 2>&1';
57
58 # The framework the user has specified.
59 my $tool = lc $ARGV[0];
60
61 # If the user has not explicitly specified a framework then detect one.
62 if ($tool eq 'auto') {
63         if ($has_gnutls) {
64                 $tool = 'gnutls';
65         } elsif ($has_openssl) {
66                 $tool = 'openssl';
67         } else {
68                 print STDERR "SSL generation failed: could not find $certtool or openssl in the PATH!\n";
69                 exit 1;
70         }
71 } elsif ($tool eq 'gnutls' && !$has_gnutls) {
72         print STDERR "SSL generation failed: could not find '$certtool' in the PATH!\n";
73         exit 1;
74 } elsif ($tool eq 'openssl' && !$has_openssl) {
75         print STDERR "SSL generation failed: could not find 'openssl' in the PATH!\n";
76         exit 1;
77 }
78
79 # Harvest information needed to generate the certificate.
80 my $common_name = prompt('What is the hostname of your server?', 'irc.example.com');
81 my $email = prompt('What email address can you be contacted at?', 'example@example.com');
82 my $unit = prompt('What is the name of your unit?', 'Server Admins');
83 my $organization = prompt('What is the name of your organization?', 'Example IRC Network');
84 my $city = prompt('What city are you located in?', 'Example City');
85 my $state = prompt('What state are you located in?', 'Example State');
86 my $country = prompt('What is the ISO 3166-1 code for the country you are located in?', 'XZ');
87 my $days = prompt('How many days do you want your certificate to be valid for?', '365');
88
89 # Contains the SSL certificate in DER form.
90 my $dercert;
91
92 # Contains the exit code of openssl/gnutls-certtool.
93 my $status = 0;
94
95 if ($tool eq 'gnutls') {
96         $has_gnutls =~ /certtool.+?(\d+\.\d+)/;
97         my $sec_param = $1 lt '2.10' ? '--bits 2048' : '--sec-param normal';
98         my $tmp = new File::Temp();
99         print $tmp <<__GNUTLS_END__;
100 cn              = "$common_name"
101 email           = "$email"
102 unit            = "$unit"
103 organization    = "$organization"
104 locality        = "$city"
105 state           = "$state"
106 country         = "$country"
107 expiration_days = $days
108 tls_www_client
109 tls_www_server
110 signing_key
111 encryption_key
112 cert_signing_key
113 crl_signing_key
114 code_signing_key
115 ocsp_signing_key
116 time_stamping_key
117 __GNUTLS_END__
118         close($tmp);
119         $status ||= system "$certtool --generate-privkey $sec_param --outfile key.pem";
120         $status ||= system "$certtool --generate-self-signed --load-privkey key.pem --outfile cert.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 __OPENSSL_END__
134         close($tmp);
135         $status ||= system "cat $tmp | openssl req -x509 -nodes -newkey rsa:2048 -keyout key.pem -out cert.pem -days $days 2>/dev/null";
136         $status ||= system 'openssl dhparam -out dhparams.pem 2048';
137         $dercert = `openssl x509 -in cert.pem -outform DER` unless $status;
138 }
139
140 if ($status) {
141         print STDERR "SSL generation failed: $tool exited with a non-zero status!\n";
142         exit 1;
143 }
144
145 if (defined $dercert && eval 'use Digest::SHA; 1') {
146         my $hash = Digest::SHA->new(256);
147         $hash->add($dercert);
148         print "\nAdd this TLSA record to your domain for DANE support:\n";
149         print "_6697._tcp." . $common_name . " TLSA 3 0 1 " . $hash->hexdigest . "\n";
150 }