]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - tools/genssl
b658efb117d927ee1f6e5fcca2ad79f1f13262ae
[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         print "$question\n";
39         print "[$default] => ";
40         chomp(my $answer = <STDIN>);
41         print "\n";
42         return $answer ? $answer : $default;
43 }
44
45 if ($#ARGV != 0 || $ARGV[0] !~ /gnutls|openssl/i) {
46         print "Syntax: genssl <gnutls|openssl>\n";
47         exit 1;
48 }
49
50 my $common_name = prompt('What is the hostname of your server?', 'irc.example.com');
51 my $email = prompt('What email address can you be contacted at?', 'example@example.com');
52 my $unit = prompt('What is the name of your unit?', 'Server Admins');
53 my $organization = prompt('What is the name of your organization?', 'Example IRC Network');
54 my $city = prompt('What city are you located in?', 'Example City');
55 my $state = prompt('What state are you located in?', 'Example State');
56 my $country = prompt('What is the ISO 3166-1 code for the country you are located in?', 'XZ');
57 my $days = prompt('How many days do you want your certificate to be valid for?', '365');
58
59 # Contains the exit code of openssl/gnutls-certtool.
60 my $status = 0;
61
62 if (lc $ARGV[0] eq 'gnutls') {
63         my $tmp = new File::Temp();
64         print $tmp <<__GNUTLS_END__;
65 cn              = "$common_name"
66 email           = "$email"
67 unit            = "$unit"
68 organization    = "$organization"
69 locality        = "$city"
70 state           = "$state"
71 country         = "$country"
72 expiration_days = $days
73 tls_www_client
74 tls_www_server
75 signing_key
76 encryption_key
77 cert_signing_key
78 crl_signing_key
79 code_signing_key
80 ocsp_signing_key
81 time_stamping_key
82 __GNUTLS_END__
83         close($tmp);
84         my $certtool = `uname -s` eq "Darwin\n" ? 'gnutls-certtool' : 'certtool';
85         $status ||= system "$certtool --version  >/dev/null 2>&1";
86         $status ||= system "$certtool --generate-privkey --outfile key.pem";
87         $status ||= system "$certtool --generate-self-signed --load-privkey key.pem --outfile cert.pem --template $tmp";
88         $status ||= system "$certtool --generate-dh-params --bits 2048 --outfile dhparams.pem";
89 } elsif (lc $ARGV[0] eq 'openssl') {
90         my $tmp = new File::Temp();
91         print $tmp <<__OPENSSL_END__;
92 $country
93 $state
94 $city
95 $organization
96 $unit
97 $common_name
98 $email
99 __OPENSSL_END__
100         close($tmp);
101         $status ||= system 'openssl version >/dev/null 2>&1';
102         $status ||= system "cat $tmp | openssl req -x509 -nodes -newkey rsa:2048 -keyout key.pem -out cert.pem -days $days 2>/dev/null";
103         $status ||= system 'openssl dhparam -out dhparams.pem 2048';
104 }
105
106 if ($status) {
107         print "SSL generation failed! Are you missing an $ARGV[0] binary package?\n";
108         exit 1;
109 }
110