1
'***************************************************************************
3
'* Project ___| | | | _ \| |
5
'* | (__| |_| | _ <| |___
6
'* \___|\___/|_| \_\_____|
8
'* Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18
'* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
'* KIND, either express or implied.
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
'***************************************************************************
29
Const myVersion = "0.3.5"
31
Const myUrl = "http://mxr.mozilla.org/firefox/source/security/nss/lib/ckfw/builtins/certdata.txt?raw=1"
33
Const myOpenssl = "openssl.exe"
35
Const myCdSavF = FALSE ' Flag: save downloaded data to file certdata.txt
36
Const myCaBakF = TRUE ' Flag: backup existing ca-bundle certificate
37
Const myAskLiF = TRUE ' Flag: display certdata.txt license agreement
38
Const myAskTiF = TRUE ' Flag: ask to include certificate text info
40
'******************* Nothing to configure below! *******************
41
Dim objShell, objNetwork, objFSO, objHttp
42
Dim myBase, mySelf, myFh, myTmpFh, myCdData, myCdFile, myCaFile, myTmpName, myBakNum, myOptTxt, i
43
Set objNetwork = WScript.CreateObject("WScript.Network")
44
Set objShell = WScript.CreateObject("WScript.Shell")
45
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
46
Set objHttp = WScript.CreateObject("WinHttp.WinHttpRequest.5.1")
47
If objHttp Is Nothing Then Set objHttp = WScript.CreateObject("WinHttp.WinHttpRequest")
48
myBase = Left(WScript.ScriptFullName, InstrRev(WScript.ScriptFullName, "\"))
49
mySelf = Left(WScript.ScriptName, InstrRev(WScript.ScriptName, ".") - 1) & " " & myVersion
50
myCdFile = Mid(myUrl, InstrRev(myUrl, "/") + 1, InstrRev(myUrl, "?") - InstrRev(myUrl, "/") - 1)
51
myCaFile = "ca-bundle.crt"
52
myTmpName = InputBox("Enter output filename:", mySelf, myCaFile)
53
If Not (myTmpName = "") Then
56
' Lets ignore SSL invalid cert errors
57
objHttp.Option(4) = 256 + 512 + 4096 + 8192
58
objHttp.SetTimeouts 0, 5000, 10000, 10000
59
objHttp.Open "GET", myUrl, FALSE
60
objHttp.setRequestHeader "User-Agent", WScript.ScriptName & "/" & myVersion
62
If Not (objHttp.statusText = "OK") Then
63
MsgBox("Failed to download '" & myCdFile & "': " & objHttp.statusText), vbCritical, mySelf
66
' Convert data from ResponseBody instead of using ResponseText because of UTF-8
67
myCdData = ConvertBinaryData(objHttp.ResponseBody)
69
' Write received data to file if enabled
70
If (myCdSavF = TRUE) Then
71
Set myFh = objFSO.OpenTextFile(myCdFile, 2, TRUE)
75
' Backup exitsing ca-bundle certificate file
76
If (myCaBakF = TRUE) Then
77
If objFSO.FileExists(myCaFile) Then
80
myBakFile = myCaFile & ".~" & b & "~"
81
While objFSO.FileExists(myBakFile)
83
myBakFile = myCaFile & ".~" & b & "~"
85
Set myTmpFh = objFSO.GetFile(myCaFile)
86
myTmpFh.Move myBakFile
89
If (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
98
' Process the received data
99
Dim myLines, myPattern, myInsideCert, myInsideLicense, myLicenseText, myNumCerts
100
Dim myLabel, myOctets, myData, myPem, myRev, j
102
myLines = Split(myCdData, vbLf, -1)
103
Set myFh = objFSO.OpenTextFile(myCaFile, 2, TRUE)
104
myFh.Write "##" & vbLf
105
myFh.Write "## " & myCaFile & " -- Bundle of CA Root Certificates" & vbLf
106
myFh.Write "##" & vbLf
107
myFh.Write "## Converted at: " & Now & vbLf
108
myFh.Write "##" & vbLf
109
myFh.Write "## This is a bundle of X.509 certificates of public Certificate Authorities" & vbLf
110
myFh.Write "## (CA). These were automatically extracted from Mozilla's root certificates" & vbLf
111
myFh.Write "## file (certdata.txt). This file can be found in the mozilla source tree:" & vbLf
112
myFh.Write "## '/mozilla/security/nss/lib/ckfw/builtins/certdata.txt'" & vbLf
113
myFh.Write "##" & vbLf
114
myFh.Write "## It contains the certificates in PEM format and therefore" & vbLf
115
myFh.Write "## can be directly used with curl / libcurl / php_curl, or with" & vbLf
116
myFh.Write "## an Apache+mod_ssl webserver for SSL client authentication." & vbLf
117
myFh.Write "## Just configure this file as the SSLCACertificateFile." & vbLf
118
myFh.Write "##" & vbLf
120
For 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))
125
If (myInsideCert = TRUE) Then
126
If InstrRev(myLines(i), "END") Then
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
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)
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
151
Set myTmpFh = objFSO.OpenTextFile(myTmpOut, 1)
152
myFh.Write myTmpFh.ReadAll & vbLf
154
objFSO.DeleteFile myTmpOut, TRUE
157
myNumCerts = myNumCerts + 1
159
myOctets = Split(myLines(i), "\")
160
For j = 1 To UBound(myOctets)
161
myData = myData & Chr(CByte("&o" & myOctets(j)))
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
170
If InstrRev(myLines(i), "CKA_VALUE MULTILINE_OCTAL") Then
173
If InstrRev(myLines(i), "***** BEGIN LICENSE BLOCK *****") Then
174
myInsideLicense = TRUE
176
If (myInsideLicense = TRUE) Then
177
myFh.Write myLines(i) & vbLf
178
myLicenseText = myLicenseText & Mid(myLines(i), 2) & vbLf
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
187
objFSO.DeleteFile myCaFile, TRUE
194
objShell.PopUp "Done (" & myNumCerts & " CA certs processed).", 20, mySelf, vbInformation
197
Function ConvertBinaryData(arrBytes)
199
Set objStream = CreateObject("ADODB.Stream")
202
objStream.Write arrBytes
203
objStream.Position = 0
205
objStream.Charset = "ascii"
206
ConvertBinaryData = objStream.ReadText
207
Set objStream = Nothing
210
Function 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.
222
Set objRegExp = Nothing
225
Function Base64Encode(inData)
226
Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
229
'For each group of 3 bytes
230
For I = 1 To Len(inData) Step 3
231
Dim nGroup, pOut, sGroup
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))
238
'Oct splits the long To 8 groups with 3 bits
242
nGroup = String(8 - Len(nGroup), "0") & nGroup
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)
250
'Add the part To OutPut string
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
258
Select Case Len(inData) Mod 3
260
sOut = Left(sOut, Len(sOut) - 2) & "=="
261
Case 2: '16 bit final
262
sOut = Left(sOut, Len(sOut) - 1) & "="
267
Function MyASC(OneChar)
268
If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)