1'*************************************************************************** 2'* _ _ ____ _ 3'* Project ___| | | | _ \| | 4'* / __| | | | |_) | | 5'* | (__| |_| | _ <| |___ 6'* \___|\___/|_| \_\_____| 7'* 8'* Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al. 9'* 10'* This software is licensed as described in the file COPYING, which 11'* you should have received as part of this distribution. The terms 12'* are also available at http://curl.haxx.se/docs/copyright.html. 13'* 14'* You may opt to use, copy, modify, merge, publish, distribute and/or sell 15'* copies of the Software, and permit persons to whom the Software is 16'* furnished to do so, under the terms of the COPYING file. 17'* 18'* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19'* KIND, either express or implied. 20'* 21'*************************************************************************** 22'* Script to fetch certdata.txt from Mozilla.org site and create a 23'* ca-bundle.crt for use with OpenSSL / libcurl / libcurl bindings 24'* Requires WinHttp.WinHttpRequest.5.1 and ADODB.Stream which are part of 25'* W2000 SP3 or later, WXP SP1 or later, W2003 Server SP1 or later. 26'* Hacked by Guenter Knauf 27'*************************************************************************** 28Option Explicit 29Const myVersion = "0.3.5" 30 31Const myUrl = "http://mxr.mozilla.org/mozilla/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1" 32 33Const myOpenssl = "openssl.exe" 34 35Const myCdSavF = FALSE ' Flag: save downloaded data to file certdata.txt 36Const myCaBakF = TRUE ' Flag: backup existing ca-bundle certificate 37Const myAskLiF = TRUE ' Flag: display certdata.txt license agreement 38Const myAskTiF = TRUE ' Flag: ask to include certificate text info 39 40'******************* Nothing to configure below! ******************* 41Dim objShell, objNetwork, objFSO, objHttp 42Dim myBase, mySelf, myFh, myTmpFh, myCdData, myCdFile, myCaFile, myTmpName, myBakNum, myOptTxt, i 43Set objNetwork = WScript.CreateObject("WScript.Network") 44Set objShell = WScript.CreateObject("WScript.Shell") 45Set objFSO = WScript.CreateObject("Scripting.FileSystemObject") 46Set objHttp = WScript.CreateObject("WinHttp.WinHttpRequest.5.1") 47If objHttp Is Nothing Then Set objHttp = WScript.CreateObject("WinHttp.WinHttpRequest") 48myBase = Left(WScript.ScriptFullName, InstrRev(WScript.ScriptFullName, "\")) 49mySelf = Left(WScript.ScriptName, InstrRev(WScript.ScriptName, ".") - 1) & " " & myVersion 50myCdFile = Mid(myUrl, InstrRev(myUrl, "/") + 1, InstrRev(myUrl, "?") - InstrRev(myUrl, "/") - 1) 51myCaFile = "ca-bundle.crt" 52myTmpName = InputBox("Enter output filename:", mySelf, myCaFile) 53If Not (myTmpName = "") Then 54 myCaFile = myTmpName 55End If 56' Lets ignore SSL invalid cert errors 57objHttp.Option(4) = 256 + 512 + 4096 + 8192 58objHttp.SetTimeouts 0, 5000, 10000, 10000 59objHttp.Open "GET", myUrl, FALSE 60objHttp.setRequestHeader "User-Agent", WScript.ScriptName & "/" & myVersion 61objHttp.Send "" 62If Not (objHttp.statusText = "OK") Then 63 MsgBox("Failed to download '" & myCdFile & "': " & objHttp.statusText), vbCritical, mySelf 64 WScript.Quit 1 65End If 66' Convert data from ResponseBody instead of using ResponseText because of UTF-8 67myCdData = ConvertBinaryData(objHttp.ResponseBody) 68Set objHttp = Nothing 69' Write received data to file if enabled 70If (myCdSavF = TRUE) Then 71 Set myFh = objFSO.OpenTextFile(myCdFile, 2, TRUE) 72 myFh.Write myCdData 73 myFh.Close 74End If 75' Backup exitsing ca-bundle certificate file 76If (myCaBakF = TRUE) Then 77 If objFSO.FileExists(myCaFile) Then 78 Dim myBakFile, b 79 b = 1 80 myBakFile = myCaFile & ".~" & b & "~" 81 While objFSO.FileExists(myBakFile) 82 b = b + 1 83 myBakFile = myCaFile & ".~" & b & "~" 84 Wend 85 Set myTmpFh = objFSO.GetFile(myCaFile) 86 myTmpFh.Move myBakFile 87 End If 88End If 89If (myAskTiF = TRUE) Then 90 If (6 = objShell.PopUp("Do you want to include text information about each certificate?" & vbLf & _ 91 "(requires OpenSSL commandline in current directory or in search path)",, _ 92 mySelf, vbQuestion + vbYesNo + vbDefaultButton2)) Then 93 myOptTxt = TRUE 94 Else 95 myOptTxt = FALSE 96 End If 97End If 98' Process the received data 99Dim myLines, myPattern, myInsideCert, myInsideLicense, myLicenseText, myNumCerts 100Dim myLabel, myOctets, myData, myPem, myRev, j 101myData = "" 102myLines = Split(myCdData, vbLf, -1) 103Set myFh = objFSO.OpenTextFile(myCaFile, 2, TRUE) 104myFh.Write "##" & vbLf 105myFh.Write "## " & myCaFile & " -- Bundle of CA Root Certificates" & vbLf 106myFh.Write "##" & vbLf 107myFh.Write "## Converted at: " & Now & vbLf 108myFh.Write "##" & vbLf 109myFh.Write "## This is a bundle of X.509 certificates of public Certificate Authorities" & vbLf 110myFh.Write "## (CA). These were automatically extracted from Mozilla's root certificates" & vbLf 111myFh.Write "## file (certdata.txt). This file can be found in the mozilla source tree:" & vbLf 112myFh.Write "## '/mozilla/security/nss/lib/ckfw/builtins/certdata.txt'" & vbLf 113myFh.Write "##" & vbLf 114myFh.Write "## It contains the certificates in PEM format and therefore" & vbLf 115myFh.Write "## can be directly used with curl / libcurl / php_curl, or with" & vbLf 116myFh.Write "## an Apache+mod_ssl webserver for SSL client authentication." & vbLf 117myFh.Write "## Just configure this file as the SSLCACertificateFile." & vbLf 118myFh.Write "##" & vbLf 119myFh.Write vbLf 120For i = 0 To UBound(myLines) 121 If InstrRev(myLines(i), "CKA_LABEL ") Then 122 myPattern = "^CKA_LABEL\s+[A-Z0-9]+\s+""(.+?)""" 123 myLabel = RegExprFirst(myPattern, myLines(i)) 124 End If 125 If (myInsideCert = TRUE) Then 126 If InstrRev(myLines(i), "END") Then 127 myInsideCert = FALSE 128 myFh.Write myLabel & vbLf 129 myFh.Write String(Len(myLabel), "=") & vbLf 130 myPem = "-----BEGIN CERTIFICATE-----" & vbLf & _ 131 Base64Encode(myData) & vbLf & _ 132 "-----END CERTIFICATE-----" & vbLf 133 If (myOptTxt = FALSE) Then 134 myFh.Write myPem & vbLf 135 Else 136 Dim myCmd, myRval, myTmpIn, myTmpOut 137 myTmpIn = objFSO.GetSpecialFolder(2).Path & "\" & objFSO.GetTempName 138 myTmpOut = objFSO.GetSpecialFolder(2).Path & "\" & objFSO.GetTempName 139 Set myTmpFh = objFSO.OpenTextFile(myTmpIn, 2, TRUE) 140 myTmpFh.Write myPem 141 myTmpFh.Close 142 myCmd = myOpenssl & " x509 -md5 -fingerprint -text -inform PEM" & _ 143 " -in " & myTmpIn & " -out " & myTmpOut 144 myRval = objShell.Run (myCmd, 0, TRUE) 145 objFSO.DeleteFile myTmpIn, TRUE 146 If Not (myRval = 0) Then 147 MsgBox("Failed to process PEM cert with OpenSSL commandline!"), vbCritical, mySelf 148 objFSO.DeleteFile myTmpOut, TRUE 149 WScript.Quit 3 150 End If 151 Set myTmpFh = objFSO.OpenTextFile(myTmpOut, 1) 152 myFh.Write myTmpFh.ReadAll & vbLf 153 myTmpFh.Close 154 objFSO.DeleteFile myTmpOut, TRUE 155 End If 156 myData = "" 157 myNumCerts = myNumCerts + 1 158 Else 159 myOctets = Split(myLines(i), "\") 160 For j = 1 To UBound(myOctets) 161 myData = myData & Chr(CByte("&o" & myOctets(j))) 162 Next 163 End If 164 End If 165 If InstrRev(myLines(i), "CVS_ID ") Then 166 myPattern = "^CVS_ID\s+""(.+?)""" 167 myRev = RegExprFirst(myPattern, myLines(i)) 168 myFh.Write "# " & myRev & vbLf & vbLf 169 End If 170 If InstrRev(myLines(i), "CKA_VALUE MULTILINE_OCTAL") Then 171 myInsideCert = TRUE 172 End If 173 If InstrRev(myLines(i), "***** BEGIN LICENSE BLOCK *****") Then 174 myInsideLicense = TRUE 175 End If 176 If (myInsideLicense = TRUE) Then 177 myFh.Write myLines(i) & vbLf 178 myLicenseText = myLicenseText & Mid(myLines(i), 2) & vbLf 179 End If 180 If InstrRev(myLines(i), "***** END LICENSE BLOCK *****") Then 181 myInsideLicense = FALSE 182 If (myAskLiF = TRUE) Then 183 If Not (6 = objShell.PopUp(myLicenseText & vbLf & _ 184 "Do you agree to the license shown above (required to proceed) ?",, _ 185 mySelf, vbQuestion + vbYesNo + vbDefaultButton1)) Then 186 myFh.Close 187 objFSO.DeleteFile myCaFile, TRUE 188 WScript.Quit 2 189 End If 190 End If 191 End If 192Next 193myFh.Close 194objShell.PopUp "Done (" & myNumCerts & " CA certs processed).", 20, mySelf, vbInformation 195WScript.Quit 0 196 197Function ConvertBinaryData(arrBytes) 198 Dim objStream 199 Set objStream = CreateObject("ADODB.Stream") 200 objStream.Open 201 objStream.Type = 1 202 objStream.Write arrBytes 203 objStream.Position = 0 204 objStream.Type = 2 205 objStream.Charset = "ascii" 206 ConvertBinaryData = objStream.ReadText 207 Set objStream = Nothing 208End Function 209 210Function RegExprFirst(SearchPattern, TheString) 211 Dim objRegExp, Matches ' create variables. 212 Set objRegExp = New RegExp ' create a regular expression. 213 objRegExp.Pattern = SearchPattern ' sets the search pattern. 214 objRegExp.IgnoreCase = TRUE ' set to ignores case. 215 objRegExp.Global = TRUE ' set to gloabal search. 216 Set Matches = objRegExp.Execute(TheString) ' do the search. 217 If (Matches.Count) Then 218 RegExprFirst = Matches(0).SubMatches(0) ' return first match. 219 Else 220 RegExprFirst = "" 221 End If 222 Set objRegExp = Nothing 223End Function 224 225Function Base64Encode(inData) 226 Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 227 Dim cOut, sOut, I 228 229 'For each group of 3 bytes 230 For I = 1 To Len(inData) Step 3 231 Dim nGroup, pOut, sGroup 232 233 'Create one long from this 3 bytes. 234 nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _ 235 &H100 * MyASC(Mid(inData, I + 1, 1)) + _ 236 MyASC(Mid(inData, I + 2, 1)) 237 238 'Oct splits the long To 8 groups with 3 bits 239 nGroup = Oct(nGroup) 240 241 'Add leading zeros 242 nGroup = String(8 - Len(nGroup), "0") & nGroup 243 244 'Convert To base64 245 pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) & _ 246 Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) & _ 247 Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) & _ 248 Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1) 249 250 'Add the part To OutPut string 251 sOut = sOut + pOut 252 253 'Add a new line For Each 76 chars In dest (76*3/4 = 57) 254 If (I < Len(inData) - 2) Then 255 If (I + 2) Mod 57 = 0 Then sOut = sOut & vbLf 256 End If 257 Next 258 Select Case Len(inData) Mod 3 259 Case 1: '8 bit final 260 sOut = Left(sOut, Len(sOut) - 2) & "==" 261 Case 2: '16 bit final 262 sOut = Left(sOut, Len(sOut) - 1) & "=" 263 End Select 264 Base64Encode = sOut 265End Function 266 267Function MyASC(OneChar) 268 If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar) 269End Function 270 271 272