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