Imported Upstream version 7.53.1
[platform/upstream/curl.git] / lib / mk-ca-bundle.vbs
index b0d9427..a9b983e 100755 (executable)
 '* Hacked by Guenter Knauf\r
 '***************************************************************************\r
 Option Explicit\r
-Const myVersion = "0.3.9"\r
+Const myVersion = "0.4.0"\r
 \r
-Const myUrl = "http://hg.mozilla.org/releases/mozilla-release/raw-file/default/security/nss/lib/ckfw/builtins/certdata.txt"\r
-Const myOpenssl = "openssl.exe"\r
+Const myUrl = "https://hg.mozilla.org/releases/mozilla-release/raw-file/default/security/nss/lib/ckfw/builtins/certdata.txt"\r
 \r
-Const myCdSavF = FALSE       ' Flag: save downloaded data to file certdata.txt\r
+Const myOpenSSL = "openssl.exe"\r
+Dim myUseOpenSSL\r
+myUseOpenSSL = TRUE          ' Flag: TRUE to use OpenSSL. If TRUE and is not\r
+                             ' found then a warning is shown before continuing.\r
+\r
+Const myCdSavF = TRUE        ' Flag: save downloaded data to file certdata.txt\r
 Const myCaBakF = TRUE        ' Flag: backup existing ca-bundle certificate\r
 Const myAskLiF = TRUE        ' Flag: display certdata.txt license agreement\r
-Const myAskTiF = TRUE        ' Flag: ask to include certificate text info\r
 Const myWrapLe = 76          ' Default length of base64 output lines\r
 \r
+' cert info code doesn't work properly with any recent openssl, leave disabled.\r
+' Also: we want our certificate output by default to be as similar as possible\r
+' to mk-ca-bundle.pl and setting this TRUE changes the base64 width to\r
+' OpenSSL's built-in default width, which is not the same as mk-ca-bundle.pl.\r
+Const myAskTiF = FALSE       ' Flag: ask to include certificate text info\r
+\r
+'\r
 '******************* Nothing to configure below! *******************\r
+'\r
+Const adTypeBinary = 1\r
+Const adTypeText = 2\r
+Const adSaveCreateNotExist = 1\r
+Const adSaveCreateOverWrite = 2\r
 Dim objShell, objNetwork, objFSO, objHttp\r
-Dim myBase, mySelf, myFh, myTmpFh, myCdData, myCdFile, myCaFile, myTmpName, myBakNum, myOptTxt, i\r
+Dim myBase, mySelf, myStream, myTmpFh, myCdData, myCdFile\r
+Dim myCaFile, myTmpName, myBakNum, myOptTxt, i\r
 Set objNetwork = WScript.CreateObject("WScript.Network")\r
 Set objShell = WScript.CreateObject("WScript.Shell")\r
 Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")\r
@@ -47,14 +63,60 @@ Set objHttp = WScript.CreateObject("WinHttp.WinHttpRequest.5.1")
 If objHttp Is Nothing Then Set objHttp = WScript.CreateObject("WinHttp.WinHttpRequest")\r
 myBase = Left(WScript.ScriptFullName, InstrRev(WScript.ScriptFullName, "\"))\r
 mySelf = Left(WScript.ScriptName, InstrRev(WScript.ScriptName, ".") - 1) & " " & myVersion\r
+\r
 myCdFile = Mid(myUrl, InstrRev(myUrl, "/") + 1)\r
 myCaFile = "ca-bundle.crt"\r
-myTmpName = InputBox("Enter output filename:", mySelf, myCaFile)\r
-If Not (myTmpName = "") Then\r
-  myCaFile = myTmpName\r
+myTmpName = InputBox("It will take a minute to download and parse the " & _\r
+                     "certificate data." & _\r
+                     vbLf & vbLf & _\r
+                     "Please enter the output filename:", mySelf, myCaFile)\r
+If (myTmpName = "") Then\r
+  WScript.Quit 1\r
+End If\r
+myCaFile = myTmpName\r
+If (myCdFile = "") Then\r
+  MsgBox("URL does not contain filename!"), vbCritical, mySelf\r
+  WScript.Quit 1\r
+End If\r
+\r
+' Don't use OpenSSL if it's not present.\r
+If (myUseOpenSSL = TRUE) Then\r
+  Dim errnum\r
+\r
+  On Error Resume Next\r
+  Call objShell.Run("""" & myOpenSSL & """ version", 0, TRUE)\r
+  errnum = Err.Number\r
+  On Error GoTo 0\r
+\r
+  If Not (errnum = 0) Then\r
+    myUseOpenSSL = FALSE\r
+    MsgBox("OpenSSL was not found so the certificate bundle will not " & _\r
+           "include the SHA256 hash of the raw certificate data file " & _\r
+           "that was used to generate the certificates in the bundle. " & _\r
+           vbLf & vbLf & _\r
+           "This does not have any effect on the certificate output, " & _\r
+           "so this script will continue." & _\r
+           vbLf & vbLf & _\r
+           "If you want to set a custom location for OpenSSL or disable " & _\r
+           "this message then edit the variables at the start of the " & _\r
+           "script."), vbInformation, mySelf\r
+  End If\r
+End If\r
+\r
+If (myAskTiF = TRUE) And (myUseOpenSSL = TRUE) Then\r
+  If (6 = objShell.PopUp("Do you want to include text information about " & _\r
+                         "each certificate?" & vbLf & _\r
+                         "(Requires OpenSSL.exe in the current directory " & _\r
+                         "or search path)",, _\r
+          mySelf, vbQuestion + vbYesNo + vbDefaultButton2)) Then\r
+    myOptTxt = TRUE\r
+  Else\r
+    myOptTxt = FALSE\r
+  End If\r
 End If\r
-' Lets ignore SSL invalid cert errors\r
-objHttp.Option(4) = 256 + 512 + 4096 + 8192\r
+\r
+' Uncomment the line below to ignore SSL invalid cert errors\r
+' objHttp.Option(4) = 256 + 512 + 4096 + 8192\r
 objHttp.SetTimeouts 0, 5000, 10000, 10000\r
 objHttp.Open "GET", myUrl, FALSE\r
 objHttp.setRequestHeader "User-Agent", WScript.ScriptName & "/" & myVersion\r
@@ -63,15 +125,13 @@ If Not (objHttp.Status = 200) Then
   MsgBox("Failed to download '" & myCdFile & "': " & objHttp.Status & " - " & objHttp.StatusText), vbCritical, mySelf\r
   WScript.Quit 1\r
 End If\r
-' Convert data from ResponseBody instead of using ResponseText because of UTF-8\r
-myCdData = ConvertBinaryData(objHttp.ResponseBody)\r
-Set objHttp = Nothing\r
 ' Write received data to file if enabled\r
 If (myCdSavF = TRUE) Then\r
-  Set myFh = objFSO.OpenTextFile(myCdFile, 2, TRUE)\r
-  myFh.Write myCdData\r
-  myFh.Close\r
+  Call SaveBinaryData(myCdFile, objHttp.ResponseBody)\r
 End If\r
+' Convert data from ResponseBody instead of using ResponseText because of UTF-8\r
+myCdData = ConvertBinaryToUTF8(objHttp.ResponseBody)\r
+Set objHttp = Nothing\r
 ' Backup exitsing ca-bundle certificate file\r
 If (myCaBakF = TRUE) Then\r
   If objFSO.FileExists(myCaFile) Then\r
@@ -86,15 +146,7 @@ If (myCaBakF = TRUE) Then
     myTmpFh.Move myBakFile\r
   End If\r
 End If\r
-If (myAskTiF = TRUE) Then\r
-  If (6 = objShell.PopUp("Do you want to include text information about each certificate?" & vbLf & _\r
-          "(requires OpenSSL commandline in current directory or in search path)",, _\r
-          mySelf, vbQuestion + vbYesNo + vbDefaultButton2)) Then\r
-    myOptTxt = TRUE\r
-  Else\r
-    myOptTxt = FALSE\r
-  End If\r
-End If\r
+\r
 ' Process the received data\r
 Dim myLines, myPattern, myInsideCert, myInsideLicense, myLicenseText, myNumCerts, myNumSkipped\r
 Dim myLabel, myOctets, myData, myPem, myRev, myUntrusted, j\r
@@ -102,23 +154,33 @@ myNumSkipped = 0
 myNumCerts = 0\r
 myData = ""\r
 myLines = Split(myCdData, vbLf, -1)\r
-Set myFh = objFSO.OpenTextFile(myCaFile, 2, TRUE)\r
-myFh.Write "##" & vbLf\r
-myFh.Write "## " & myCaFile & " -- Bundle of CA Root Certificates" & vbLf\r
-myFh.Write "##" & vbLf\r
-myFh.Write "## Converted at: " & Now & vbLf\r
-myFh.Write "##" & vbLf\r
-myFh.Write "## This is a bundle of X.509 certificates of public Certificate Authorities" & vbLf\r
-myFh.Write "## (CA). These were automatically extracted from Mozilla's root certificates" & vbLf\r
-myFh.Write "## file (certdata.txt).  This file can be found in the mozilla source tree:" & vbLf\r
-myFh.Write "## '/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt'" & vbLf\r
-myFh.Write "##" & vbLf\r
-myFh.Write "## It contains the certificates in PEM format and therefore" & vbLf\r
-myFh.Write "## can be directly used with curl / libcurl / php_curl, or with" & vbLf\r
-myFh.Write "## an Apache+mod_ssl webserver for SSL client authentication." & vbLf\r
-myFh.Write "## Just configure this file as the SSLCACertificateFile." & vbLf\r
-myFh.Write "##" & vbLf\r
-myFh.Write vbLf\r
+Set myStream = CreateObject("ADODB.Stream")\r
+myStream.Open\r
+myStream.Type = adTypeText\r
+myStream.Charset = "utf-8"\r
+myStream.WriteText "##" & vbLf & _\r
+  "## Bundle of CA Root Certificates" & vbLf & _\r
+  "##" & vbLf & _\r
+  "## Certificate data from Mozilla as of: " & _\r
+    ConvertDateToString(LocalDateToUTC(Now)) & " GMT" & vbLf & _\r
+  "##" & vbLf & _\r
+  "## This is a bundle of X.509 certificates of public Certificate Authorities" & vbLf & _\r
+  "## (CA). These were automatically extracted from Mozilla's root certificates" & vbLf & _\r
+  "## file (certdata.txt).  This file can be found in the mozilla source tree:" & vbLf & _\r
+  "## " & myUrl & vbLf & _\r
+  "##" & vbLf & _\r
+  "## It contains the certificates in PEM format and therefore" & vbLf & _\r
+  "## can be directly used with curl / libcurl / php_curl, or with" & vbLf & _\r
+  "## an Apache+mod_ssl webserver for SSL client authentication." & vbLf & _\r
+  "## Just configure this file as the SSLCACertificateFile." & vbLf & _\r
+  "##" & vbLf & _\r
+  "## Conversion done with mk-ca-bundle.vbs version " & myVersion & "." & vbLf\r
+If (myCdSavF = TRUE) And (myUseOpenSSL = TRUE) Then\r
+  myStream.WriteText "## SHA256: " & FileSHA256(myCdFile) & vbLf\r
+End If\r
+myStream.WriteText "##" & vbLf & vbLf\r
+\r
+myStream.WriteText vbLf\r
 For i = 0 To UBound(myLines)\r
   If InstrRev(myLines(i), "CKA_LABEL ") Then\r
     myPattern = "^CKA_LABEL\s+[A-Z0-9]+\s+""(.+?)"""\r
@@ -136,13 +198,13 @@ For i = 0 To UBound(myLines)
       If (myUntrusted = TRUE) Then\r
         myNumSkipped = myNumSkipped + 1\r
       Else\r
-        myFh.Write myLabel & vbLf\r
-        myFh.Write String(Len(myLabel), "=") & vbLf\r
+        myStream.WriteText myLabel & vbLf\r
+        myStream.WriteText String(Len(myLabel), "=") & vbLf\r
         myPem = "-----BEGIN CERTIFICATE-----" & vbLf & _\r
                 Base64Encode(myData) & vbLf & _\r
                 "-----END CERTIFICATE-----" & vbLf\r
         If (myOptTxt = FALSE) Then\r
-          myFh.Write myPem & vbLf\r
+          myStream.WriteText myPem & vbLf\r
         Else\r
           Dim myCmd, myRval, myTmpIn, myTmpOut\r
           myTmpIn = objFSO.GetSpecialFolder(2).Path & "\" & objFSO.GetTempName\r
@@ -150,8 +212,8 @@ For i = 0 To UBound(myLines)
           Set myTmpFh = objFSO.OpenTextFile(myTmpIn, 2, TRUE)\r
           myTmpFh.Write myPem\r
           myTmpFh.Close\r
-          myCmd = myOpenssl & " x509 -md5 -fingerprint -text -inform PEM" & _\r
-                  " -in " & myTmpIn & " -out " & myTmpOut\r
+          myCmd = """" & myOpenSSL & """ x509 -md5 -fingerprint -text " & _\r
+                  "-inform PEM -in " & myTmpIn & " -out " & myTmpOut\r
           myRval = objShell.Run (myCmd, 0, TRUE)\r
           objFSO.DeleteFile myTmpIn, TRUE\r
           If Not (myRval = 0) Then\r
@@ -160,7 +222,7 @@ For i = 0 To UBound(myLines)
             WScript.Quit 3\r
           End If\r
           Set myTmpFh = objFSO.OpenTextFile(myTmpOut, 1)\r
-          myFh.Write myTmpFh.ReadAll & vbLf\r
+          myStream.WriteText myTmpFh.ReadAll & vbLf\r
           myTmpFh.Close\r
           objFSO.DeleteFile myTmpOut, TRUE\r
         End If\r
@@ -176,7 +238,7 @@ For i = 0 To UBound(myLines)
   If InstrRev(myLines(i), "CVS_ID ") Then\r
     myPattern = "^CVS_ID\s+""(.+?)"""\r
     myRev = RegExprFirst(myPattern, myLines(i))\r
-    myFh.Write "# " & myRev & vbLf & vbLf\r
+    myStream.WriteText "# " & myRev & vbLf & vbLf\r
   End If\r
   If InstrRev(myLines(i), "CKA_VALUE MULTILINE_OCTAL") Then\r
     myInsideCert = TRUE\r
@@ -187,7 +249,7 @@ For i = 0 To UBound(myLines)
     myInsideLicense = TRUE\r
   End If\r
   If (myInsideLicense = TRUE) Then\r
-    myFh.Write myLines(i) & vbLf\r
+    myStream.WriteText myLines(i) & vbLf\r
     myLicenseText = myLicenseText & Mid(myLines(i), 2) & vbLf\r
   End If\r
   If InstrRev(myLines(i), "***** END LICENSE BLOCK *****") Then\r
@@ -196,28 +258,54 @@ For i = 0 To UBound(myLines)
       If Not (6 = objShell.PopUp(myLicenseText & vbLf & _\r
               "Do you agree to the license shown above (required to proceed) ?",, _\r
               mySelf, vbQuestion + vbYesNo + vbDefaultButton1)) Then\r
-        myFh.Close\r
+        myStream.Close\r
         objFSO.DeleteFile myCaFile, TRUE\r
         WScript.Quit 2\r
       End If\r
     End If\r
   End If\r
 Next\r
-myFh.Close\r
+\r
+' To stop the UTF-8 BOM from being written the stream has to be copied and\r
+' then saved as binary.\r
+Dim myCopy\r
+Set myCopy = CreateObject("ADODB.Stream")\r
+myCopy.Type = adTypeBinary\r
+myCopy.Open\r
+myStream.Position = 3 ' Skip UTF-8 BOM\r
+myStream.CopyTo myCopy\r
+myCopy.SaveToFile myCaFile, adSaveCreateOverWrite\r
+myCopy.Close\r
+myStream.Close\r
+Set myCopy = Nothing\r
+Set myStream = Nothing\r
+\r
+' Done\r
 objShell.PopUp "Done (" & myNumCerts & " CA certs processed, " & myNumSkipped & _\r
                " untrusted skipped).", 20, mySelf, vbInformation\r
 WScript.Quit 0\r
 \r
-Function ConvertBinaryData(arrBytes)\r
+Function ConvertBinaryToUTF8(arrBytes)\r
   Dim objStream\r
   Set objStream = CreateObject("ADODB.Stream")\r
   objStream.Open\r
-  objStream.Type = 1\r
+  objStream.Type = adTypeBinary\r
   objStream.Write arrBytes\r
   objStream.Position = 0\r
-  objStream.Type = 2\r
-  objStream.Charset = "ascii"\r
-  ConvertBinaryData = objStream.ReadText\r
+  objStream.Type = adTypeText\r
+  objStream.Charset = "utf-8"\r
+  ConvertBinaryToUTF8 = objStream.ReadText\r
+  Set objStream = Nothing\r
+End Function\r
+\r
+Function SaveBinaryData(filename, data)\r
+  Dim objStream\r
+  Set objStream = CreateObject("ADODB.Stream")\r
+  objStream.Type = adTypeBinary\r
+  objStream.Open\r
+  objStream.Write data\r
+  objStream.SaveToFile filename, adSaveCreateOverWrite\r
+  objStream.Close\r
   Set objStream = Nothing\r
 End Function\r
 \r
@@ -283,4 +371,61 @@ Function MyASC(OneChar)
   If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)\r
 End Function\r
 \r
+' Return the date in the same format as perl to match mk-ca-bundle.pl output:\r
+' Wed Sep  7 03:12:05 2016\r
+Function ConvertDateToString(input)\r
+  Dim output\r
+  output = WeekDayName(WeekDay(input), TRUE) & " " & _\r
+           MonthName(Month(input), TRUE) & " "\r
+  If (Len(Day(input)) = 1) Then\r
+    output = output & " "\r
+  End If\r
+  output = output & _\r
+           Day(input) & " " & _\r
+           FormatDateTime(input, vbShortTime) & ":"\r
+  If (Len(Second(input)) = 1) Then\r
+    output = output & "0"\r
+  End If\r
+  output = output & _\r
+           Second(input) & " " & _\r
+           Year(input)\r
+  ConvertDateToString = output\r
+End Function\r
 \r
+' Convert local Date to UTC. Microsoft says:\r
+' Use Win32_ComputerSystem CurrentTimeZone property, because it automatically\r
+' adjusts the Time Zone bias for daylight saving time; Win32_Time Zone Bias\r
+' property does not.\r
+' https://msdn.microsoft.com/en-us/library/windows/desktop/ms696015.aspx\r
+Function LocalDateToUTC(localdate)\r
+  Dim item, offset\r
+  For Each item In GetObject("winmgmts:").InstancesOf("Win32_ComputerSystem")\r
+    offset = item.CurrentTimeZone ' the offset in minutes\r
+  Next\r
+  If (offset < 0) Then\r
+    LocalDateToUTC = DateAdd("n",  ABS(offset), localdate)\r
+  Else\r
+    LocalDateToUTC = DateAdd("n", -ABS(offset), localdate)\r
+  End If\r
+  'objShell.PopUp LocalDateToUTC\r
+End Function\r
+\r
+Function FileSHA256(filename)\r
+  Dim cmd, rval, tmpOut, tmpFh\r
+  if (myUseOpenSSL = TRUE) Then\r
+    tmpOut = objFSO.GetSpecialFolder(2).Path & "\" & objFSO.GetTempName\r
+    cmd = """" & myOpenSSL & """ dgst -r -sha256 -out """ & tmpOut & """ """ & filename & """"\r
+    rval = objShell.Run(cmd, 0, TRUE)\r
+    If Not (rval = 0) Then\r
+      MsgBox("Failed to get sha256 of """ & filename & """ with OpenSSL commandline!"), vbCritical, mySelf\r
+      objFSO.DeleteFile tmpOut, TRUE\r
+      WScript.Quit 3\r
+    End If\r
+    Set tmpFh = objFSO.OpenTextFile(tmpOut, 1)\r
+    FileSHA256 = RegExprFirst("^([0-9a-f]{64}) .+", tmpFh.ReadAll)\r
+    tmpFh.Close\r
+    objFSO.DeleteFile tmpOut, TRUE\r
+  Else\r
+    FileSHA256 = ""\r
+  End If\r
+End Function\r