]> granicus.if.org Git - curl/commitdiff
mk-ca-bundle.vbs: Fix UTF-8 output
authorJay Satiro <raysatiro@yahoo.com>
Sun, 30 Oct 2016 05:01:29 +0000 (01:01 -0400)
committerJay Satiro <raysatiro@yahoo.com>
Sun, 30 Oct 2016 05:01:29 +0000 (01:01 -0400)
- Change initial message box to mention delay when downloading/parsing.

Since there is no progress meter it was somewhat unexpected that after
choosing a filename nothing appears to happen, when actually the cert
data is in the process of being downloaded and parsed.

- Warn if OpenSSL is not present.

- Use a UTF-8 stream to make the ca-bundle data.

- Save the UTF-8 ca-bundle stream as binary so that no BOM is added.

---

This is a follow-up to d2c6d15 which switched mk-ca-bundle.vbs output to
ANSI due to corrupt UTF-8 output, now fixed.

This change completes making the default certificate bundle output of
mk-ca-bundle.vbs as close as possible to that of mk-ca-bundle.pl, which
should make it easier to review any difference between their output.

Ref: https://github.com/curl/curl/pull/1012

lib/mk-ca-bundle.pl
lib/mk-ca-bundle.vbs

index 75278f168ac7bfe895f58ee491b8c544d16edc98..9574f1dbf95bddf5378163cdd254dcfb3d1f7d6f 100755 (executable)
@@ -30,6 +30,7 @@
 # dependency is the OpenSSL commandline tool for optional text listing.
 # Hacked by Guenter Knauf.
 #
+use Encode;
 use Getopt::Std;
 use MIME::Base64;
 use strict;
@@ -487,7 +488,7 @@ while (<TXT>) {
               . "-----END CERTIFICATE-----\n";
       print CRT "\n$caname\n";
       print CRT @precert if($opt_m);
-      my $maxStringLength = length($caname);
+      my $maxStringLength = length(decode('UTF-8', $caname, Encode::FB_CROAK));
       if ($opt_t) {
         foreach my $key (keys %trust_purposes_by_level) {
            my $string = $key . ": " . join(", ", @{$trust_purposes_by_level{$key}});
index 38eb48abad50dc5414840b0798335dca300b1b28..a9b983e9bd61f9ca4e935f34f66315fd16e3ef2f 100755 (executable)
@@ -29,20 +29,33 @@ Option Explicit
 Const myVersion = "0.4.0"\r
 \r
 Const myUrl = "https://hg.mozilla.org/releases/mozilla-release/raw-file/default/security/nss/lib/ckfw/builtins/certdata.txt"\r
-Const myOpenssl = "openssl.exe"\r
 \r
-Const myUseOpenSSL = TRUE    ' Flag: set FALSE if openssl is not present\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 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
@@ -50,16 +63,58 @@ 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
+\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
@@ -75,7 +130,7 @@ If (myCdSavF = TRUE) Then
   Call SaveBinaryData(myCdFile, objHttp.ResponseBody)\r
 End If\r
 ' Convert data from ResponseBody instead of using ResponseText because of UTF-8\r
-myCdData = ConvertBinaryData(objHttp.ResponseBody)\r
+myCdData = ConvertBinaryToUTF8(objHttp.ResponseBody)\r
 Set objHttp = Nothing\r
 ' Backup exitsing ca-bundle certificate file\r
 If (myCaBakF = TRUE) Then\r
@@ -91,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
@@ -107,30 +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 "## Bundle of CA Root Certificates" & vbLf\r
-myFh.Write "##" & vbLf\r
-myFh.Write "## Certificate data from Mozilla as of: " & _\r
-  ConvertDateToString(LocalDateToUTC(Now)) & " GMT" & 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 "## " & myUrl & 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 "## Conversion done with mk-ca-bundle.vbs version " & myVersion & "." & 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
-  myFh.Write "## SHA256: " & FileSHA256(myCdFile) & vbLf\r
+  myStream.WriteText "## SHA256: " & FileSHA256(myCdFile) & vbLf\r
 End If\r
-myFh.Write "##" & vbLf & vbLf\r
+myStream.WriteText "##" & vbLf & vbLf\r
 \r
-myFh.Write vbLf\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
@@ -148,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
@@ -162,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
@@ -172,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
@@ -188,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
@@ -199,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
@@ -208,34 +258,47 @@ 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.Type = adTypeText\r
   objStream.Charset = "utf-8"\r
-  ConvertBinaryData = objStream.ReadText\r
+  ConvertBinaryToUTF8 = objStream.ReadText\r
   Set objStream = Nothing\r
 End Function\r
 \r
 Function SaveBinaryData(filename, data)\r
-  Const adTypeBinary = 1\r
-  Const adSaveCreateOverWrite = 2\r
   Dim objStream\r
   Set objStream = CreateObject("ADODB.Stream")\r
   objStream.Type = adTypeBinary\r
@@ -351,7 +414,7 @@ Function FileSHA256(filename)
   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
+    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