rem @echo off
rem echo Using no random seed. Bad.
set rnd=

rem ------------------------------------------------------------------------------
rem --  Use this batch file to generate a test certificate.
rem --  There are no interactive steps in this script (no interaction with a CA).
rem --
rem --  Parameters:
rem --     %1 - password phrase (must match the value in mgreq.ini)
rem ------------------------------------------------------------------------------

mkdir tmp

certreq -alg rsa %rnd% -str 1024 -enc pem -prv tmp\rsa_0.prv -req tmp\rsa_0.req -dn "c=IE\,st=Dublin\,o=Baltimore Technologies, plc\,ou=C/SSL\,cn=C/SSL 1024-bit RSA TLCA" -alt "uniformResourceIdentifier=http://www.baltimore.com/products/cssl/" -pass "%1"
certsign %rnd% -prv tmp\rsa_0.prv -ku c -bc 1 -req tmp\rsa_0.req -enc pem -crt tmp\rsa_0.crt -pass "%1"

certreq -alg rsa %rnd% -str 1024 -enc pem -prv tmp\rsa_1.prv -req tmp\rsa_1.req -dn "c=IE\,st=Dublin\,o=Baltimore Technologies, plc\,ou=C/SSL\,cn=C/SSL 1024-bit RSA CA" -pass "%1"
certsign %rnd% -prv tmp\rsa_0.prv -ku c -bc 0 -ca tmp\rsa_0.crt -req tmp\rsa_1.req -enc pem -crt tmp\rsa_1.crt -pass "%1"

certreq -alg rsa %rnd% -str 1024 -enc pem -prv tmp\rsa_2.prv -req tmp\rsa_2.req -dn "c=IE\,st=Dublin\,o=Baltimore Technologies, plc\,ou=C/SSL\,cn=C/SSL 1024-bit RSA server" -alt "dnsName=localhost" -pass "%1"
certsign %rnd% -prv tmp\rsa_1.prv -ku S -ca tmp\rsa_1.crt -req tmp\rsa_2.req -enc pem -crt tmp\rsa_2.crt -pass "%1"

certreq -alg rsa %rnd% -str 1024 -enc pem -prv tmp\rsa_3.prv -req tmp\rsa_3.req -dn "c=IE\,st=Dublin\,o=Baltimore Technologies, plc\,ou=C/SSL\,cn=C/SSL signing-only 1024-bit RSA server" -alt "dnsName=localhost" -pass "%1"
certsign %rnd% -prv tmp\rsa_1.prv -ku sS -ca tmp\rsa_1.crt -req tmp\rsa_3.req -enc pem -crt tmp\rsa_3.crt -pass "%1"

certreq -alg rsa %rnd% -str 1024 -enc pem -prv tmp\rsa_4.prv -req tmp\rsa_4.req -dn "c=IE\,st=Dublin\,o=Baltimore Technologies, plc\,ou=C/SSL\,cn=C/SSL 1024-bit RSA client" -pass "%1"
certsign %rnd% -prv tmp\rsa_1.prv -ku C -ca tmp\rsa_1.crt -req tmp\rsa_4.req -enc pem -crt tmp\rsa_4.crt -pass "%1"

certreq -alg rsa %rnd% -str 512 -enc pem -prv tmp\rsa_5.prv -req tmp\rsa_5.req -dn "c=IE\,st=Dublin\,o=Baltimore Technologies, plc\,ou=C/SSL\,cn=C/SSL 512-bit RSA server" -alt "dnsName=localhost" -pass "%1"
certsign %rnd% -prv tmp\rsa_1.prv -ku S -ca tmp\rsa_1.crt -req tmp\rsa_5.req -enc pem -crt tmp\rsa_5.crt -pass "%1"

certreq -alg rsa %rnd% -str 512 -enc pem -prv tmp\rsa_6.prv -req tmp\rsa_6.req -dn "c=IE\,st=Dublin\,o=Baltimore Technologies, plc\,ou=C/SSL\,cn=C/SSL 512-bit RSA client" -pass "%1"
certsign %rnd% -prv tmp\rsa_1.prv -ku C -ca tmp\rsa_1.crt -req tmp\rsa_6.req -enc pem -crt tmp\rsa_6.crt -pass "%1"

certreq -alg dsa %rnd% -str 1024 -enc pem -prv tmp\dsa_0.prv -req tmp\dsa_0.req -dn "c=IE\,st=Dublin\,o=Baltimore Technologies, plc\,ou=C/SSL\,cn=C/SSL 1024-bit DSA TLCA" -alt "uniformResourceIdentifier=http://www.baltimore.com/products/cssl/" -pass "%1"
certsign %rnd% -prv tmp\dsa_0.prv -ku c -bc 1 -req tmp\dsa_0.req -enc pem -crt tmp\dsa_0.crt -pass "%1"

certreq -alg dsa %rnd% -param tmp\dsa_0.crt -enc pem -prv tmp\dsa_1.prv -req tmp\dsa_1.req -dn "c=IE\,st=Dublin\,o=Baltimore Technologies, plc\,ou=C/SSL\,cn=C/SSL 1024-bit DSA CA" -pass "%1"
certsign %rnd% -prv tmp\dsa_0.prv -ku c -bc 0 -ca tmp\dsa_0.crt -req tmp\dsa_1.req -enc pem -crt tmp\dsa_1.crt -pass "%1"

certreq -alg dsa %rnd% -param tmp\dsa_1.crt -enc pem -prv tmp\dsa_2.prv -req tmp\dsa_2.req -dn "c=IE\,st=Dublin\,o=Baltimore Technologies, plc\,ou=C/SSL\,cn=C/SSL 1024-bit DSA server" -alt "dnsName=localhost" -pass "%1"
certsign %rnd% -prv tmp\dsa_1.prv -ku S -ca tmp\dsa_1.crt -req tmp\dsa_2.req -enc pem -crt tmp\dsa_2.crt -pass "%1"

certreq -alg dsa %rnd% -param tmp\dsa_1.crt -enc pem -prv tmp\dsa_3.prv -req tmp\dsa_3.req -dn "c=IE\,st=Dublin\,o=Baltimore Technologies, plc\,ou=C/SSL\,cn=C/SSL 1024-bit DSA client" -pass "%1"
certsign %rnd% -prv tmp\dsa_1.prv -ku C -ca tmp\dsa_1.crt -req tmp\dsa_3.req -enc pem -crt tmp\dsa_3.crt -pass "%1"

convert -certificate tmp\rsa_2.crt -certificate tmp\rsa_1.crt -certificate tmp\rsa_0.crt -pkcs8 tmp\rsa_2.prv "%1" toPEM -pkcs12 ..\personal\baltimore\strongServerRSA.pem "%1"
convert -certificate tmp\rsa_4.crt -certificate tmp\rsa_1.crt -certificate tmp\rsa_0.crt -pkcs8 tmp\rsa_4.prv "%1" toPEM -pkcs12 ..\personal\baltimore\strongClientRSA.pem "%1"

rem convert -certificate tmp\rsa_3.crt -certificate tmp\rsa_1.crt -certificate tmp\rsa_0.crt -pkcs8 tmp\rsa_3.prv "%1" toPEM -pkcs12 ..\personal\baltimore\signingServerRSA.pem "%1"
rem convert -certificate tmp\rsa_5.crt -certificate tmp\rsa_1.crt -certificate tmp\rsa_0.crt -pkcs8 tmp\rsa_5.prv "%1" toPEM -pkcs12 ..\personal\baltimore\weakServerRSA.pem "%1"
rem convert -certificate tmp\rsa_6.crt -certificate tmp\rsa_1.crt -certificate tmp\rsa_0.crt -pkcs8 tmp\rsa_6.prv "%1" toDER -pkcs12 ..\personal\baltimore\clientCert.p12 ""

rem convert -certificate tmp\dsa_2.crt -certificate tmp\dsa_1.crt -certificate tmp\dsa_0.crt -pkcs8 tmp\dsa_2.prv "%1" toPEM -pkcs12 ..\strongServerDSA.pem "%1"
rem convert -certificate tmp\dsa_3.crt -certificate tmp\dsa_1.crt -certificate tmp\dsa_0.crt -pkcs8 tmp\dsa_3.prv "%1" toPEM -pkcs12 ..\strongClientDSA.pem "%1"

convert -certificate tmp\rsa_0.crt toDER -certificate ..\caCert.crt
convert -certificate tmp\rsa_0.crt -certificate tmp\dsa_0.crt toPEM -sequence ..\root\Baltimore.pem

del tmp\*.prv
del tmp\*.req
del tmp\*.crt
rd tmp

del .\..\*.dat
del .\..\*.crt
del .\..\*.p12

