echo off

set JAR_SIGNER_JDK=C:\Program Files\Java\jdk1.8.0_111
set KEY_TOOL_JRE=%JAR_SIGNER_JDK%\jre
set KEY_TOOL=%KEY_TOOL_JRE%\bin\keytool.exe
set JAR_SIGNER=%JAR_SIGNER_JDK%\bin\jarsigner.exe
set PROVIDER=JCSPRSA
set STORE_TYPE=HSMDB
set KEY_PASS=11111111
set KEY_ALIAS=myRsaKey
set DN_NAME=CN=myRsaKey,O=CryptoPro,C=RU
set COPY_KEY_ALIAS=myRsaKeyCopy
set COPY_KEY_PASS=11111111
set REQUEST_FILE=request_rsa.bin
set P7B_FILE=certnew_rsa.p7b
set EXPORT_CERT=myRsaKeyCert.cer
set NEW_PASSWORD=12345678
set JAR_FILE=test.jar
set SIGNED_JAR_FILE=test.signed.jar
set TRUST_STORE=trust_for_jar_signer
set PARAM=-J-Dkeytool.compat=true

echo "1. List containers:"
call "%KEY_TOOL%" %PARAM% -list -providername %PROVIDER% -storetype %STORE_TYPE% -keystore NONE -storepass 1
echo "Listing completed."

pause

echo "1*. Control deleting container:"
call "%KEY_TOOL%" %PARAM% -delete -alias %KEY_ALIAS% -providername %PROVIDER% -storetype %STORE_TYPE% -keystore NONE -storepass 1
echo "Control deleting completed."

pause

echo "1**. Control deleting copy-container:"
call "%KEY_TOOL%" %PARAM% -delete -alias %COPY_KEY_ALIAS% -providername %PROVIDER% -storetype %STORE_TYPE% -keystore NONE -storepass 1
echo "Control deleting completed."

pause

echo "2. Generating key pair:"
call "%KEY_TOOL%" %PARAM% -genkey -alias %KEY_ALIAS% -keysize 2048 -providername %PROVIDER% -storetype %STORE_TYPE% -dname %DN_NAME% -keystore NONE -storepass 1 -keyalg CP_RSA -sigalg CP_SHA256withRSA -keypass %KEY_PASS%
echo "Generating pair completed."

pause

echo "3. Creating certificate request:"
call "%KEY_TOOL%" %PARAM% -certreq -alias %KEY_ALIAS% -providername %PROVIDER% -keypass %KEY_PASS% -storetype %STORE_TYPE% -keystore NONE -storepass 1 -sigalg CP_SHA256withRSA -file %REQUEST_FILE%
echo "Creating request completed."

pause

echo "4. Creating self-signed certificate:"
call "%KEY_TOOL%" %PARAM% -selfcert -alias %KEY_ALIAS% -providername %PROVIDER% -keypass %KEY_PASS% -storetype %STORE_TYPE% -keystore NONE -storepass 1 -sigalg CP_SHA256withRSA -dname %DN_NAME%
echo "Creating certificate completed."

echo "5. Please, use certificate request %REQUEST_FILE%, send it to CA, retreive and save to file %P7B_FILE%"
pause

echo "6. Importing certificate chain (p7b):"
call "%KEY_TOOL%" %PARAM% -import -alias %KEY_ALIAS% -providername %PROVIDER% -keypass %KEY_PASS% -storetype %STORE_TYPE% -keystore NONE -storepass 1 -file %P7B_FILE% -noprompt
echo "Importing chain completed."

pause

echo "7. Exporting certificate:"
call "%KEY_TOOL%" %PARAM% -export -alias %KEY_ALIAS% -providername %PROVIDER% -storetype %STORE_TYPE% -keystore NONE -storepass 1 -file %EXPORT_CERT%
echo "Exporting certificate completed."

pause

echo "8. Copy container:"
call "%KEY_TOOL%" %PARAM% -importkeystore -srckeystore %TRUST_STORE% -srcstoretype %STORE_TYPE% -srcstorepass 1 -destkeystore NONE -deststoretype %STORE_TYPE% -deststorepass 1 -srcprovidername %PROVIDER% -destprovidername %PROVIDER% -srcalias %KEY_ALIAS% -destalias %COPY_KEY_ALIAS% -srckeypass %KEY_PASS% -destkeypass %COPY_KEY_PASS%
echo "Copying completed."

echo "9. Please, put jar-file %JAR_FILE% before signing and install Java CSP into JDK %JAR_SIGNER_JDK%"
pause

echo "10. Signing jar:"
call "%JAR_SIGNER%" %PARAM% -keystore NONE -storepass 1 -storetype %STORE_TYPE% -signedjar %SIGNED_JAR_FILE% -digestalg CP_SHA256 -sigalg CP_SHA256withRSA -providerName %PROVIDER% -keypass %KEY_PASS% %JAR_FILE% %KEY_ALIAS%
echo "Signing jar completed."

pause

echo "11. Verifying signed jar:"
call "%JAR_SIGNER%" %PARAM% -verify %SIGNED_JAR_FILE%
echo "Verifying completed."

pause

echo "12. Deleting container:"
call "%KEY_TOOL%" %PARAM% -delete -alias %KEY_ALIAS% -providername %PROVIDER% -storetype %STORE_TYPE% -keystore NONE -storepass 1
echo "Deleting container completed."

pause

echo "13. Deleting copy-container:"
call "%KEY_TOOL%" %PARAM% -delete -alias %COPY_KEY_ALIAS% -providername %PROVIDER% -storetype %STORE_TYPE% -keystore NONE -storepass 1
echo "Deleting copy-container completed."