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=JCSP
set STORE_TYPE=HDIMAGE
set KEY_PASS=123
set KEY_ALIAS="Crypto-Pro GOST R 34.10-2001 Cryptographic Service Provider####gost_no_cert"
set DN_NAME=CN=myGostKey,O=CryptoPro,C=RU
set REQUEST_FILE=request_gost.bin
set P7B_FILE=certnew_gost.p7b
set EXPORT_CERT=myGostKeyCert.cer
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 -J-Duse.cert.stub=true

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

pause

echo "4. Creating self-signed certificate:"
call "%KEY_TOOL%" %PARAM% -selfcert -alias %KEY_ALIAS%::::%KEY_PASS% -providername %PROVIDER% -keypass %KEY_PASS% -storetype %STORE_TYPE% -keystore NONE -storepass 1 -sigalg GOST3411withGOST3410EL -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%::::%KEY_PASS% -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%::::%KEY_PASS% -providername %PROVIDER% -storetype %STORE_TYPE% -keystore NONE -storepass 1 -file %EXPORT_CERT%
echo "Exporting certificate completed."

pause

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

pause

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

pause

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