1 '***************************************************************************
\r
3 '* Project ___| | | | _ \| |
\r
4 '* / __| | | | |_) | |
\r
5 '* | (__| |_| | _ <| |___
\r
6 '* \___|\___/|_| \_\_____|
\r
8 '* Copyright (C) 1998 - 2010, 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.5"
\r
31 Const myUrl = "http://mxr.mozilla.org/firefox/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1"
\r
33 Const myOpenssl = "openssl.exe"
\r
35 Const myCdSavF = FALSE ' Flag: save downloaded data to file certdata.txt
\r
36 Const myCaBakF = TRUE ' Flag: backup existing ca-bundle certificate
\r
37 Const myAskLiF = TRUE ' Flag: display certdata.txt license agreement
\r
38 Const myAskTiF = TRUE ' Flag: ask to include certificate text info
\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, InstrRev(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.statusText = "OK") Then
\r
63 MsgBox("Failed to download '" & myCdFile & "': " & 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
\r
100 Dim myLabel, myOctets, myData, myPem, myRev, j
\r
102 myLines = Split(myCdData, vbLf, -1)
\r
103 Set myFh = objFSO.OpenTextFile(myCaFile, 2, TRUE)
\r
104 myFh.Write "##" & vbLf
\r
105 myFh.Write "## " & myCaFile & " -- Bundle of CA Root Certificates" & vbLf
\r
106 myFh.Write "##" & vbLf
\r
107 myFh.Write "## Converted at: " & Now & vbLf
\r
108 myFh.Write "##" & vbLf
\r
109 myFh.Write "## This is a bundle of X.509 certificates of public Certificate Authorities" & vbLf
\r
110 myFh.Write "## (CA). These were automatically extracted from Mozilla's root certificates" & vbLf
\r
111 myFh.Write "## file (certdata.txt). This file can be found in the mozilla source tree:" & vbLf
\r
112 myFh.Write "## '/mozilla/security/nss/lib/ckfw/builtins/certdata.txt'" & vbLf
\r
113 myFh.Write "##" & vbLf
\r
114 myFh.Write "## It contains the certificates in PEM format and therefore" & vbLf
\r
115 myFh.Write "## can be directly used with curl / libcurl / php_curl, or with" & vbLf
\r
116 myFh.Write "## an Apache+mod_ssl webserver for SSL client authentication." & vbLf
\r
117 myFh.Write "## Just configure this file as the SSLCACertificateFile." & vbLf
\r
118 myFh.Write "##" & vbLf
\r
120 For i = 0 To UBound(myLines)
\r
121 If InstrRev(myLines(i), "CKA_LABEL ") Then
\r
122 myPattern = "^CKA_LABEL\s+[A-Z0-9]+\s+""(.+?)"""
\r
123 myLabel = RegExprFirst(myPattern, myLines(i))
\r
125 If (myInsideCert = TRUE) Then
\r
126 If InstrRev(myLines(i), "END") Then
\r
127 myInsideCert = FALSE
\r
128 myFh.Write myLabel & vbLf
\r
129 myFh.Write String(Len(myLabel), "=") & vbLf
\r
130 myPem = "-----BEGIN CERTIFICATE-----" & vbLf & _
\r
131 Base64Encode(myData) & vbLf & _
\r
132 "-----END CERTIFICATE-----" & vbLf
\r
133 If (myOptTxt = FALSE) Then
\r
134 myFh.Write myPem & vbLf
\r
136 Dim myCmd, myRval, myTmpIn, myTmpOut
\r
137 myTmpIn = objFSO.GetSpecialFolder(2).Path & "\" & objFSO.GetTempName
\r
138 myTmpOut = objFSO.GetSpecialFolder(2).Path & "\" & objFSO.GetTempName
\r
139 Set myTmpFh = objFSO.OpenTextFile(myTmpIn, 2, TRUE)
\r
140 myTmpFh.Write myPem
\r
142 myCmd = myOpenssl & " x509 -md5 -fingerprint -text -inform PEM" & _
\r
143 " -in " & myTmpIn & " -out " & myTmpOut
\r
144 myRval = objShell.Run (myCmd, 0, TRUE)
\r
145 objFSO.DeleteFile myTmpIn, TRUE
\r
146 If Not (myRval = 0) Then
\r
147 MsgBox("Failed to process PEM cert with OpenSSL commandline!"), vbCritical, mySelf
\r
148 objFSO.DeleteFile myTmpOut, TRUE
\r
151 Set myTmpFh = objFSO.OpenTextFile(myTmpOut, 1)
\r
152 myFh.Write myTmpFh.ReadAll & vbLf
\r
154 objFSO.DeleteFile myTmpOut, TRUE
\r
157 myNumCerts = myNumCerts + 1
\r
159 myOctets = Split(myLines(i), "\")
\r
160 For j = 1 To UBound(myOctets)
\r
161 myData = myData & Chr(CByte("&o" & myOctets(j)))
\r
165 If InstrRev(myLines(i), "CVS_ID ") Then
\r
166 myPattern = "^CVS_ID\s+""(.+?)"""
\r
167 myRev = RegExprFirst(myPattern, myLines(i))
\r
168 myFh.Write "# " & myRev & vbLf & vbLf
\r
170 If InstrRev(myLines(i), "CKA_VALUE MULTILINE_OCTAL") Then
\r
171 myInsideCert = TRUE
\r
173 If InstrRev(myLines(i), "***** BEGIN LICENSE BLOCK *****") Then
\r
174 myInsideLicense = TRUE
\r
176 If (myInsideLicense = TRUE) Then
\r
177 myFh.Write myLines(i) & vbLf
\r
178 myLicenseText = myLicenseText & Mid(myLines(i), 2) & vbLf
\r
180 If InstrRev(myLines(i), "***** END LICENSE BLOCK *****") Then
\r
181 myInsideLicense = FALSE
\r
182 If (myAskLiF = TRUE) Then
\r
183 If Not (6 = objShell.PopUp(myLicenseText & vbLf & _
\r
184 "Do you agree to the license shown above (required to proceed) ?",, _
\r
185 mySelf, vbQuestion + vbYesNo + vbDefaultButton1)) Then
\r
187 objFSO.DeleteFile myCaFile, TRUE
\r
194 objShell.PopUp "Done (" & myNumCerts & " CA certs processed).", 20, mySelf, vbInformation
\r
197 Function ConvertBinaryData(arrBytes)
\r
199 Set objStream = CreateObject("ADODB.Stream")
\r
202 objStream.Write arrBytes
\r
203 objStream.Position = 0
\r
205 objStream.Charset = "ascii"
\r
206 ConvertBinaryData = objStream.ReadText
\r
207 Set objStream = Nothing
\r
210 Function RegExprFirst(SearchPattern, TheString)
\r
211 Dim objRegExp, Matches ' create variables.
\r
212 Set objRegExp = New RegExp ' create a regular expression.
\r
213 objRegExp.Pattern = SearchPattern ' sets the search pattern.
\r
214 objRegExp.IgnoreCase = TRUE ' set to ignores case.
\r
215 objRegExp.Global = TRUE ' set to gloabal search.
\r
216 Set Matches = objRegExp.Execute(TheString) ' do the search.
\r
217 If (Matches.Count) Then
\r
218 RegExprFirst = Matches(0).SubMatches(0) ' return first match.
\r
222 Set objRegExp = Nothing
\r
225 Function Base64Encode(inData)
\r
226 Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
\r
229 'For each group of 3 bytes
\r
230 For I = 1 To Len(inData) Step 3
\r
231 Dim nGroup, pOut, sGroup
\r
233 'Create one long from this 3 bytes.
\r
234 nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _
\r
235 &H100 * MyASC(Mid(inData, I + 1, 1)) + _
\r
236 MyASC(Mid(inData, I + 2, 1))
\r
238 'Oct splits the long To 8 groups with 3 bits
\r
239 nGroup = Oct(nGroup)
\r
242 nGroup = String(8 - Len(nGroup), "0") & nGroup
\r
245 pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) & _
\r
246 Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) & _
\r
247 Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) & _
\r
248 Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1)
\r
250 'Add the part To OutPut string
\r
253 'Add a new line For Each 76 chars In dest (76*3/4 = 57)
\r
254 If (I < Len(inData) - 2) Then
\r
255 If (I + 2) Mod 57 = 0 Then sOut = sOut & vbLf
\r
258 Select Case Len(inData) Mod 3
\r
259 Case 1: '8 bit final
\r
260 sOut = Left(sOut, Len(sOut) - 2) & "=="
\r
261 Case 2: '16 bit final
\r
262 sOut = Left(sOut, Len(sOut) - 1) & "="
\r
264 Base64Encode = sOut
\r
267 Function MyASC(OneChar)
\r
268 If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)
\r