Skip to content

Instantly share code, notes, and snippets.

@sbingner
Last active April 28, 2016 11:31
Show Gist options
  • Save sbingner/9c8f95363caff2aefb5c221c41a1864c to your computer and use it in GitHub Desktop.
Save sbingner/9c8f95363caff2aefb5c221c41a1864c to your computer and use it in GitHub Desktop.
StartSSL Certificate Creation
#!/usr/bin/perl
# USAGE: startSSL.pl DOMAIN1 DOMAIN2 ...
#
# Requires:
# - client.crt and client.key in same directory as startSSL.pl
# - DOMAIN.csr in the current directory for each domain (in PEM format)
#
# Outputs: DOMAIN.crt to current directory in PEM format
#
use strict;
use WWW::Mechanize;
use File::Slurp;
use FindBin;
my $mech = WWW::Mechanize->new(autocheck => 1);
my $dir = $FindBin::Bin;
$mech->ssl_opts(
SSL_verify_mode => 0,
SSL_cert_file => $dir . '/client.crt',
SSL_key_file => $dir . '/client.key',
#SSL_passwd_cb => sub { return "secretpassword"; }
);
sub errorOut($) {
print $_[0] . "\n";
exit 1;
}
$|++;
print "Logging in.";
my $res = $mech->get( "https://startssl.com/Certificates" );
errorOut('Fetching https://startssl.com/Certificates failed') unless ($res->is_success);
print ".";
$res = $mech->follow_link( text_regex => qr/Authenticate/i );
errorOut('Authenticating failed') unless ($res->is_success);
print ".\nAuthenticated!\n";
foreach my $domain (@ARGV) {
print "Trying to get a cert for ${domain}\n";
unless (-f "${domain}.csr") {
print("${domain}.csr does not exist in the current directory.\n");
next;
}
print "Reading ${domain}.csr\n";
my $csr = read_file("${domain}.csr");
unless ($csr) {
print "Couldn't read in ${domain}.csr\n";
next;
}
print "Working.";
$res = $mech->get( "https://startssl.com/Certificates/ApplySSLCert" );
unless ($res->is_success) {
print "Error.\n";
next;
}
print '.';
$mech->field('domains' => $domain);
$mech->field('rbcsr' => 'scsr');
$mech->field('areaCSR' => $csr);
$res = $mech->click_button(number => 1);
unless ($res->is_success) {
print "Error.\n";
next;
}
print '.';
my $link = $mech->find_link(text => 'here');
my $url = $link->url();
if ($url =~ s/DownLoadCert/WriteCert/) {
print " OK!\nFetching new cert to ${domain}.crt ... ";
$res = $mech->request(HTTP::Request->new(GET => 'https://startssl.com' . $url), "${domain}.crt");
if ($res->is_success) {
print "OK!\n";
} else {
print "FAILED! You should be able to download it yourself from https://startssl.com\n";
}
} else {
print " Probable failure creating cert. Check startssl certificate list.\n";
}
}
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment