#!/usr/bin/perl -w

print "Additional Public Key demo\n";

print "http://www2.futureware.at/PostQuantum/AdditionalPublicKey/\n";

print "The ntru-pubkey.der is generated by the sample program from NTRU:\n";
print "ntru-crypto/reference-code/C/Encrypt/bin/sampleNTRUEncrypt\n";

print "Generating a NTRU key:\n";
system "sampleNTRUEncrypt";

print "Reading in the DER encoded NTRU public key\n";
open IN,"<ntru-pubkey.der";
undef $/;
my $content=<IN>;
close IN;

print "Converting it to the OpenSSL format\n";
my $der="1.3.6.1.4.1.18506.7=DER";
foreach(0 .. length($content)-1)
{
  $der.=sprintf(":%02X",unpack("C",substr($content,$_,1)));
}
print "$der\n";

print "Generating a OpenSSL compatible config file config.txt from a template\n";
system "cp config.template config.txt";
open OUT,">>config.txt";
print OUT $der;
close OUT;

print "Generating the RSA root key\n";
system "openssl genrsa -out RootCA.key.pem 1024" unless(-s "RootCA.key.pem");
print "Generating the certificate with an additional public key\n";
system "openssl req -new -x509 -days 4827 -key RootCA.key.pem -out RootCA.cert.pem -nodes -batch -config config.txt -extensions extensions";
print "Analyzing the generated certificate:\n";
system "openssl x509 -in RootCA.cert.pem -text -noout";

