generate_ssl_cert.bat 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. @echo off
  2. chcp 65001 >nul 2>&1
  3. REM Generate self-signed SSL certificate script (Windows)
  4. REM Usage: scripts\generate_ssl_cert.bat
  5. REM Enable error handling
  6. setlocal enabledelayedexpansion
  7. REM Change to script directory and then go up to project root
  8. cd /d "%~dp0"
  9. cd /d ".."
  10. set "PROJECT_ROOT=%CD%"
  11. REM Set certificate directory (relative to project root)
  12. set "CERT_DIR=%PROJECT_ROOT%\certs"
  13. if not exist "%CERT_DIR%" mkdir "%CERT_DIR%"
  14. REM Certificate configuration
  15. set DOMAIN=localhost
  16. set DAYS=365
  17. set KEY_SIZE=2048
  18. echo Generating self-signed SSL certificate...
  19. echo Domain: %DOMAIN%
  20. echo Validity: %DAYS% days
  21. echo Key size: %KEY_SIZE% bits
  22. echo.
  23. REM Check if OpenSSL is installed
  24. set "OPENSSL_CMD="
  25. where openssl >nul 2>&1
  26. if !ERRORLEVEL! EQU 0 (
  27. REM OpenSSL found in PATH - capture output to temp file to avoid parsing issues with parentheses
  28. where openssl > "%TEMP%\openssl_path.txt" 2>nul
  29. if exist "%TEMP%\openssl_path.txt" (
  30. for /f "usebackq delims=" %%i in ("%TEMP%\openssl_path.txt") do (
  31. set "OPENSSL_CMD=%%i"
  32. goto :openssl_found
  33. )
  34. :openssl_found
  35. del /f /q "%TEMP%\openssl_path.txt" 2>nul
  36. REM Remove any trailing spaces and newlines
  37. set "OPENSSL_CMD=!OPENSSL_CMD: =!"
  38. if not "!OPENSSL_CMD!"=="" (
  39. echo [INFO] Found OpenSSL in PATH: !OPENSSL_CMD!
  40. goto :openssl_ready
  41. )
  42. )
  43. )
  44. REM Try common installation paths if not found in PATH
  45. if "!OPENSSL_CMD!"=="" (
  46. echo [DEBUG] Checking common installation paths...
  47. if exist "C:\Program Files\OpenSSL-Win64\bin\openssl.exe" (
  48. set "OPENSSL_CMD=C:\Program Files\OpenSSL-Win64\bin\openssl.exe"
  49. echo [INFO] Found OpenSSL: !OPENSSL_CMD!
  50. ) else if exist "C:\Program Files (x86)\OpenSSL-Win32\bin\openssl.exe" (
  51. set "OPENSSL_CMD=C:\Program Files (x86)\OpenSSL-Win32\bin\openssl.exe"
  52. echo [INFO] Found OpenSSL: !OPENSSL_CMD!
  53. ) else if exist "C:\OpenSSL-Win64\bin\openssl.exe" (
  54. set "OPENSSL_CMD=C:\OpenSSL-Win64\bin\openssl.exe"
  55. echo [INFO] Found OpenSSL: !OPENSSL_CMD!
  56. ) else if exist "D:\OpenSSL-Win64\bin\openssl.exe" (
  57. set "OPENSSL_CMD=D:\OpenSSL-Win64\bin\openssl.exe"
  58. echo [INFO] Found OpenSSL: !OPENSSL_CMD!
  59. goto :openssl_ready
  60. ) else (
  61. echo [ERROR] OpenSSL is not installed or not in PATH.
  62. echo.
  63. echo ========================================
  64. echo QUICK INSTALL GUIDE
  65. echo ========================================
  66. echo.
  67. echo Method 1: Install Git for Windows (EASIEST)
  68. echo 1. Download: https://git-scm.com/download/win
  69. echo 2. Install with default options
  70. echo 3. OpenSSL will be automatically available
  71. echo.
  72. echo Method 2: Install OpenSSL manually
  73. echo 1. Download: https://slproweb.com/products/Win32OpenSSL.html
  74. echo 2. Install to: C:\Program Files\OpenSSL-Win64
  75. echo 3. Check: "Copy OpenSSL DLLs to The Windows system directory"
  76. echo 4. Add to PATH: C:\Program Files\OpenSSL-Win64\bin
  77. echo.
  78. echo Method 3: Use Chocolatey (if installed)
  79. echo choco install openssl
  80. echo.
  81. echo ========================================
  82. echo After installation:
  83. echo 1. CLOSE this window
  84. echo 2. Open a NEW command prompt
  85. echo 3. Run: openssl version (to verify)
  86. echo 4. Run this script again
  87. echo ========================================
  88. echo.
  89. echo For detailed guide, see: docs/INSTALL_OPENSSL_WINDOWS.md
  90. echo.
  91. pause
  92. exit /b 1
  93. )
  94. )
  95. :openssl_ready
  96. REM Generate private key
  97. echo 1. Generating private key...
  98. call "!OPENSSL_CMD!" genrsa -out %CERT_DIR%\server.key %KEY_SIZE%
  99. if !ERRORLEVEL! NEQ 0 (
  100. echo [ERROR] Failed to generate private key. Error code: !ERRORLEVEL!
  101. echo [DEBUG] OpenSSL path: !OPENSSL_CMD!
  102. pause
  103. exit /b 1
  104. )
  105. REM Generate certificate signing request (CSR)
  106. echo 2. Generating certificate signing request...
  107. call "!OPENSSL_CMD!" req -new -key "%CERT_DIR%\server.key" -out "%CERT_DIR%\server.csr" -subj "/C=CN/ST=State/L=City/O=Organization/CN=%DOMAIN%"
  108. if !ERRORLEVEL! NEQ 0 (
  109. echo [ERROR] Failed to generate CSR. Error code: !ERRORLEVEL!
  110. pause
  111. exit /b 1
  112. )
  113. REM Create configuration file
  114. echo 3. Creating certificate configuration file...
  115. (
  116. echo [req]
  117. echo distinguished_name = req_distinguished_name
  118. echo req_extensions = v3_req
  119. echo.
  120. echo [req_distinguished_name]
  121. echo.
  122. echo [v3_req]
  123. echo basicConstraints = CA:FALSE
  124. echo keyUsage = nonRepudiation, digitalSignature, keyEncipherment
  125. echo subjectAltName = @alt_names
  126. echo.
  127. echo [alt_names]
  128. echo DNS.1 = %DOMAIN%
  129. echo DNS.2 = *.localhost
  130. echo IP.1 = 127.0.0.1
  131. echo IP.2 = ::1
  132. ) > "%CERT_DIR%\server.conf"
  133. REM Generate self-signed certificate
  134. echo 4. Generating self-signed certificate...
  135. call "!OPENSSL_CMD!" x509 -req -days %DAYS% -in "%CERT_DIR%\server.csr" -signkey "%CERT_DIR%\server.key" -out "%CERT_DIR%\server.crt" -extensions v3_req -extfile "%CERT_DIR%\server.conf"
  136. if !ERRORLEVEL! NEQ 0 (
  137. echo [ERROR] Failed to generate certificate. Error code: !ERRORLEVEL!
  138. echo [DEBUG] Check if config file exists: %CERT_DIR%\server.conf
  139. pause
  140. exit /b 1
  141. )
  142. REM Clean up temporary files
  143. del /f /q "%CERT_DIR%\server.csr" "%CERT_DIR%\server.conf" 2>nul
  144. echo.
  145. echo [SUCCESS] Certificate generation completed!
  146. echo Certificate files location:
  147. echo - Private key: "%CERT_DIR%\server.key"
  148. echo - Certificate: "%CERT_DIR%\server.crt"
  149. echo.
  150. echo [WARNING] This is a self-signed certificate. Browsers will show security warnings, this is normal.
  151. echo For production environments, please use a certificate issued by a trusted CA.
  152. echo.
  153. pause