@Echo off
set temp_path=%path%
path %path%;C:\Progra~1\OpenSSL

Echo.
Echo  Simple CA utility for NetKeeper
Echo  Written by yanghoo (yanghoo@nari-china.com)
Echo.

if %1.==genca. goto genca
if %1.==sign.  goto sign
if %1.==client.  goto client
:help
Echo  Usage:
Echo    ca genca    - generate CA certificate
Echo    ca sign     - sign user certificate request
Echo    ca client   - generate and sign client certificate
Echo.
goto exit

:genca
if not exist C:\progra~1\OpenSSL\openssl.exe goto ssldoesntexist
if not exist C:\progra~1\OpenSSL\CA goto genca2
Echo Warning!
Echo The content of the C:\CA directory will be removed.
Echo Press CTRL-C to break, or ENTER to continue...
Echo.
pause > nul
Echo Removing C:\progra~1\OpenSSL\CA...
rmdir /S /Q C:\progra~1\OpenSSL\CA > nul
rem deltree /Y C:\progra~1\OpenSSL\CA > nul
goto genca3

:genca2
Echo The script will create the C:\CA directory structure needed by
Echo OpenSSL, and generate CA Certificate.
Echo.
Echo Press CTRL-C to break, or ENTER to continue...
Echo.
pause > nul

:genca3
Echo Creating C:\progra~1\OpenSSL\CA...
mkdir C:\progra~1\OpenSSL\CA
Echo Creating C:\progra~1\OpenSSL\CA\certs...
mkdir C:\progra~1\OpenSSL\CA\certs
Echo Creating C:\progra~1\OpenSSL\CA\crl...
mkdir C:\progra~1\OpenSSL\CA\crl
Echo Creating C:\progra~1\OpenSSL\CA\newcerts...
mkdir C:\progra~1\OpenSSL\CA\newcerts
Echo Creating C:\progra~1\OpenSSL\CA\private...
mkdir C:\progra~1\OpenSSL\CA\private
Echo Creating C:\progra~1\OpenSSL\CA\temp...
mkdir C:\progra~1\OpenSSL\CA\temp
Echo Creating CA index file...
copy nul C:\progra~1\OpenSSL\CA\index.txt > nul
Echo Creating CA serial file...
echo 01> C:\progra~1\OpenSSL\CA\serial
Echo.
Echo --------------------------------------------------------------------
Echo  Generating the CA's certificate...
Echo --------------------------------------------------------------------
Echo.
openssl req -new -x509 -keyout C:\progra~1\OpenSSL\CA\private\CAkey.pem -out C:\progra~1\OpenSSL\CA\CAcert.pem -config c:\progra~1\OpenSSL\openssl.conf
goto exit

:sign
if not exist C:\progra~1\OpenSSL\openssl.exe goto ssldoesntexist
if not exist C:\progra~1\OpenSSL\CA\CAcert.pem goto cadoesntexist
if not exist C:\progra~1\OpenSSL\CA\temp\newreq.pem goto reqdoesntexist
Echo  Warning!
Echo  The content of the C:\progra~1\OpenSSL\CA\temp\newcert.pem will be removed.
Echo  Press CTRL-C to break, or ENTER to continue...
pause > nul

Echo --------------------------------------------------------------------
Echo        Sign the certificate
Echo --------------------------------------------------------------------
Echo.
openssl ca -config C:\Progra~1\OpenSSL\openssl.conf -policy policy_anything -notext -in C:\progra~1\OpenSSL\CA\temp\newreq.pem -out C:\progra~1\OpenSSL\CA\temp\newcert.pem
goto exit

:client
if not exist C:\progra~1\OpenSSL\openssl.exe goto ssldoesntexist
if not exist C:\progra~1\OpenSSL\CA\CAcert.pem goto cadoesntexist
if not exist C:\progra~1\OpenSSL\CA\temp\client goto client2
Echo  Warning!
Echo  The content of the C:\progra~1\OpenSSL\CA\temp\client directory will be removed.
Echo  Press CTRL-C to break, or ENTER to continue...
pause > nul
rmdir /s /q C:\progra~1\OpenSSL\CA\temp\client > nul
rem deltree /Y C:\progra~1\OpenSSL\CA\temp\client > nul

:client2
mkdir C:\progra~1\OpenSSL\CA\temp\client
Echo --------------------------------------------------------------------
Echo  Step 1: Generate the keys and the certificate request
Echo --------------------------------------------------------------------
Echo.
openssl req -nodes -config C:\Progra~1\OpenSSL\openssl.conf -new -newkey rsa:1024 -keyout C:\progra~1\OpenSSL\CA\temp\client\client.key -out C:\progra~1\OpenSSL\CA\temp\client\client.req
Echo.
Echo --------------------------------------------------------------------
Echo  Step 2: Sign the certificate
Echo --------------------------------------------------------------------
Echo.
openssl ca -config C:\Progra~1\OpenSSL\openssl.conf -policy policy_anything -notext -in C:\progra~1\OpenSSL\CA\temp\client\client.req -out C:\progra~1\OpenSSL\CA\temp\client\client.crt
copy C:\progra~1\OpenSSL\CA\temp\client\client.key + C:\progra~1\OpenSSL\CA\temp\client\client.crt C:\progra~1\OpenSSL\CA\temp\client\client.pem > nul
del /Q C:\progra~1\OpenSSL\CA\temp\client\client.req
goto exit
:ssldoesntexist
Echo Error while accessing C:\Progra~1\OpenSSL\openssl.exe!
goto exit

:cadoesntexist
Echo  CA certificate doesn't exist! Run "ca genca" first.
Echo.
goto exit

:reqdoesntexist
Echo  Certificate  Request  C:\progra~1\OpenSSL\CA\temp\newreq.pem  doesn't exist!
Echo.
goto exit

:exit
path %temp_path%
