1*cdf0e10cSrcweirAttribute VB_Name = "CommonPreparation"
2*cdf0e10cSrcweir'/*************************************************************************
3*cdf0e10cSrcweir' *
4*cdf0e10cSrcweir' DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5*cdf0e10cSrcweir'
6*cdf0e10cSrcweir' Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir'
8*cdf0e10cSrcweir' OpenOffice.org - a multi-platform office productivity suite
9*cdf0e10cSrcweir'
10*cdf0e10cSrcweir' This file is part of OpenOffice.org.
11*cdf0e10cSrcweir'
12*cdf0e10cSrcweir' OpenOffice.org is free software: you can redistribute it and/or modify
13*cdf0e10cSrcweir' it under the terms of the GNU Lesser General Public License version 3
14*cdf0e10cSrcweir' only, as published by the Free Software Foundation.
15*cdf0e10cSrcweir'
16*cdf0e10cSrcweir' OpenOffice.org is distributed in the hope that it will be useful,
17*cdf0e10cSrcweir' but WITHOUT ANY WARRANTY; without even the implied warranty of
18*cdf0e10cSrcweir' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19*cdf0e10cSrcweir' GNU Lesser General Public License version 3 for more details
20*cdf0e10cSrcweir' (a copy is included in the LICENSE file that accompanied this code).
21*cdf0e10cSrcweir'
22*cdf0e10cSrcweir' You should have received a copy of the GNU Lesser General Public License
23*cdf0e10cSrcweir' version 3 along with OpenOffice.org.  If not, see
24*cdf0e10cSrcweir' <http://www.openoffice.org/license.html>
25*cdf0e10cSrcweir' for a copy of the LGPLv3 License.
26*cdf0e10cSrcweir'
27*cdf0e10cSrcweir' ************************************************************************/
28*cdf0e10cSrcweir
29*cdf0e10cSrcweirOption Explicit
30*cdf0e10cSrcweirPrivate Declare Function CryptAcquireContext Lib "advapi32.dll" _
31*cdf0e10cSrcweir   Alias "CryptAcquireContextA" (ByRef phProv As Long, _
32*cdf0e10cSrcweir   ByVal pszContainer As String, ByVal pszProvider As String, _
33*cdf0e10cSrcweir   ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
34*cdf0e10cSrcweir
35*cdf0e10cSrcweirPrivate Declare Function CryptReleaseContext Lib "advapi32.dll" ( _
36*cdf0e10cSrcweir   ByVal hProv As Long, ByVal dwFlags As Long) As Long
37*cdf0e10cSrcweir
38*cdf0e10cSrcweirPrivate Declare Function CryptCreateHash Lib "advapi32.dll" ( _
39*cdf0e10cSrcweir   ByVal hProv As Long, ByVal Algid As Long, ByVal hKey As Long, _
40*cdf0e10cSrcweir   ByVal dwFlags As Long, ByRef phHash As Long) As Long
41*cdf0e10cSrcweir
42*cdf0e10cSrcweirPrivate Declare Function CryptDestroyHash Lib "advapi32.dll" (ByVal hHash As Long) As Long
43*cdf0e10cSrcweir
44*cdf0e10cSrcweirPrivate Declare Function CryptHashData Lib "advapi32.dll" (ByVal hHash As Long, _
45*cdf0e10cSrcweir    pbData As Any, ByVal dwDataLen As Long, ByVal dwFlags As Long) As Long
46*cdf0e10cSrcweir
47*cdf0e10cSrcweirPrivate Declare Function CryptGetHashParam Lib "advapi32.dll" ( _
48*cdf0e10cSrcweir   ByVal hHash As Long, ByVal dwParam As Long, pbData As Any, _
49*cdf0e10cSrcweir   pdwDataLen As Long, ByVal dwFlags As Long) As Long
50*cdf0e10cSrcweir
51*cdf0e10cSrcweirPrivate Const ALG_CLASS_ANY     As Long = 0
52*cdf0e10cSrcweirPrivate Const ALG_TYPE_ANY      As Long = 0
53*cdf0e10cSrcweirPrivate Const ALG_CLASS_HASH    As Long = 32768
54*cdf0e10cSrcweirPrivate Const ALG_SID_MD5       As Long = 3
55*cdf0e10cSrcweir' Hash algorithms
56*cdf0e10cSrcweirPrivate Const MD5_ALGORITHM As Long = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5
57*cdf0e10cSrcweir' CryptSetProvParam
58*cdf0e10cSrcweirPrivate Const PROV_RSA_FULL        As Long = 1
59*cdf0e10cSrcweir' used when acquiring the provider
60*cdf0e10cSrcweirPrivate Const CRYPT_VERIFYCONTEXT  As Long = &HF0000000
61*cdf0e10cSrcweir' Microsoft provider data
62*cdf0e10cSrcweirPrivate Const MS_DEFAULT_PROVIDER  As String = _
63*cdf0e10cSrcweir              "Microsoft Base Cryptographic Provider v1.0"
64*cdf0e10cSrcweir
65*cdf0e10cSrcweirFunction DoPreparation(docAnalysis As DocumentAnalysis, myIssue As IssueInfo, preparationNote As String, _
66*cdf0e10cSrcweir                       var As Variant, currDoc As Object) As Boolean
67*cdf0e10cSrcweir    On Error GoTo HandleErrors
68*cdf0e10cSrcweir    Dim currentFunctionName As String
69*cdf0e10cSrcweir    currentFunctionName = "DoPreparation"
70*cdf0e10cSrcweir
71*cdf0e10cSrcweir    DoPreparation = False
72*cdf0e10cSrcweir
73*cdf0e10cSrcweir    'Log as Preparable
74*cdf0e10cSrcweir    AddIssueDetailsNote myIssue, 0, preparationNote, RID_STR_COMMON_PREPARATION_NOTE
75*cdf0e10cSrcweir    myIssue.Preparable = True
76*cdf0e10cSrcweir    docAnalysis.PreparableIssuesCount = docAnalysis.PreparableIssuesCount + 1
77*cdf0e10cSrcweir
78*cdf0e10cSrcweir    If Not CheckDoPrepare Then Exit Function
79*cdf0e10cSrcweir
80*cdf0e10cSrcweir    'Do Prepare
81*cdf0e10cSrcweir
82*cdf0e10cSrcweir    If myIssue.IssueTypeXML = CSTR_ISSUE_OBJECTS_GRAPHICS_AND_FRAMES And _
83*cdf0e10cSrcweir        myIssue.SubTypeXML = CSTR_SUBISSUE_OBJECT_IN_HEADER_FOOTER Then
84*cdf0e10cSrcweir        DoPreparation = Prepare_HeaderFooter_GraphicFrames(docAnalysis, myIssue, var, currDoc)
85*cdf0e10cSrcweir
86*cdf0e10cSrcweir    ElseIf myIssue.IssueTypeXML = CSTR_ISSUE_CONTENT_DOCUMENT_PROPERTIES And _
87*cdf0e10cSrcweir        myIssue.SubTypeXML = CSTR_SUBISSUE_OLD_WORKBOOK_VERSION Then
88*cdf0e10cSrcweir        DoPreparation = Prepare_WorkbookVersion()
89*cdf0e10cSrcweir
90*cdf0e10cSrcweir    End If
91*cdf0e10cSrcweir
92*cdf0e10cSrcweirFinalExit:
93*cdf0e10cSrcweir    Exit Function
94*cdf0e10cSrcweir
95*cdf0e10cSrcweirHandleErrors:
96*cdf0e10cSrcweir    WriteDebug currentFunctionName & _
97*cdf0e10cSrcweir    " : path " & docAnalysis.name & ": " & _
98*cdf0e10cSrcweir    " : myIssue " & myIssue.IssueTypeXML & "_" & myIssue.SubTypeXML & ": " & _
99*cdf0e10cSrcweir    Err.Number & " " & Err.Description & " " & Err.Source
100*cdf0e10cSrcweir    Resume FinalExit
101*cdf0e10cSrcweirEnd Function
102*cdf0e10cSrcweir
103*cdf0e10cSrcweirFunction InDocPreparation() As Boolean
104*cdf0e10cSrcweir    InDocPreparation = True
105*cdf0e10cSrcweirEnd Function
106*cdf0e10cSrcweir
107*cdf0e10cSrcweirFunction Prepare_DocumentCustomProperties(docAnalysis As DocumentAnalysis, myIssue As IssueInfo, _
108*cdf0e10cSrcweir                                          var As Variant, currDoc As Object) As Boolean
109*cdf0e10cSrcweir    On Error GoTo HandleErrors
110*cdf0e10cSrcweir    Dim currentFunctionName As String
111*cdf0e10cSrcweir    currentFunctionName = "Prepare_DocumentCustomProperties"
112*cdf0e10cSrcweir
113*cdf0e10cSrcweir    Dim aProp As DocumentProperty
114*cdf0e10cSrcweir    Dim myCustomDocumentProperties As DocumentProperties
115*cdf0e10cSrcweir    Dim commentProp As DocumentProperty
116*cdf0e10cSrcweir    Prepare_DocumentCustomProperties = False
117*cdf0e10cSrcweir
118*cdf0e10cSrcweir    Set myCustomDocumentProperties = getAppSpecificCustomDocProperties(currDoc)
119*cdf0e10cSrcweir    Set commentProp = getAppSpecificCommentBuiltInDocProperty(currDoc)
120*cdf0e10cSrcweir    Set aProp = var 'Safe as we know that a DocumentProperty is being passed in
121*cdf0e10cSrcweir
122*cdf0e10cSrcweir    If commentProp.value <> "" Then commentProp.value = commentProp.value & vbLf
123*cdf0e10cSrcweir
124*cdf0e10cSrcweir    commentProp.value = commentProp.value & _
125*cdf0e10cSrcweir                RID_STR_COMMON_SUBISSUE_DOCUMENT_CUSTOM_PROPERTY & ": " & vbLf
126*cdf0e10cSrcweir
127*cdf0e10cSrcweir    commentProp.value = commentProp.value & _
128*cdf0e10cSrcweir        RID_STR_COMMON_ATTRIBUTE_NAME & " - " & aProp.name & ", " & _
129*cdf0e10cSrcweir        RID_STR_COMMON_ATTRIBUTE_TYPE & " - " & getCustomDocPropTypeAsString(aProp.Type) & ", " & _
130*cdf0e10cSrcweir        RID_STR_COMMON_ATTRIBUTE_VALUE & " - " & aProp.value
131*cdf0e10cSrcweir
132*cdf0e10cSrcweir    myCustomDocumentProperties.item(aProp.name).Delete
133*cdf0e10cSrcweir
134*cdf0e10cSrcweir    Prepare_DocumentCustomProperties = True
135*cdf0e10cSrcweir
136*cdf0e10cSrcweirFinalExit:
137*cdf0e10cSrcweir    Exit Function
138*cdf0e10cSrcweir
139*cdf0e10cSrcweirHandleErrors:
140*cdf0e10cSrcweir    WriteDebug currentFunctionName & " : " & docAnalysis.name & ": " & Err.Number & " " & Err.Description & " " & Err.Source
141*cdf0e10cSrcweir    Resume FinalExit
142*cdf0e10cSrcweirEnd Function
143*cdf0e10cSrcweir
144*cdf0e10cSrcweirPrivate Function GetProvider(hCtx As Long) As Boolean
145*cdf0e10cSrcweir    Const NTE_BAD_KEYSET = &H80090016
146*cdf0e10cSrcweir    Const NTE_EXISTS = &H8009000F
147*cdf0e10cSrcweir    Const NTE_KEYSET_NOT_DEF = &H80090019
148*cdf0e10cSrcweir    Dim currentFunctionName As String
149*cdf0e10cSrcweir    currentFunctionName = "GetProvider"
150*cdf0e10cSrcweir
151*cdf0e10cSrcweir    Dim strTemp       As String
152*cdf0e10cSrcweir    Dim strProvider  As String
153*cdf0e10cSrcweir    Dim strErrorMsg   As String
154*cdf0e10cSrcweir    Dim errStr As String
155*cdf0e10cSrcweir
156*cdf0e10cSrcweir    GetProvider = False
157*cdf0e10cSrcweir
158*cdf0e10cSrcweir    On Error Resume Next
159*cdf0e10cSrcweir    strTemp = vbNullChar
160*cdf0e10cSrcweir    strProvider = MS_DEFAULT_PROVIDER & vbNullChar
161*cdf0e10cSrcweir    If CBool(CryptAcquireContext(hCtx, ByVal strTemp, _
162*cdf0e10cSrcweir             ByVal strProvider, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) Then
163*cdf0e10cSrcweir        GetProvider = True
164*cdf0e10cSrcweir        Exit Function
165*cdf0e10cSrcweir    End If
166*cdf0e10cSrcweir
167*cdf0e10cSrcweir    Select Case Err.LastDllError
168*cdf0e10cSrcweir        Case NTE_BAD_KEYSET
169*cdf0e10cSrcweir            errStr = "Key container does not exist or You do not have access to the key container."
170*cdf0e10cSrcweir        Case NTE_EXISTS
171*cdf0e10cSrcweir            errStr = "The key container already exists, but you are attempting to create it"
172*cdf0e10cSrcweir        Case NTE_KEYSET_NOT_DEF
173*cdf0e10cSrcweir            errStr = "The Crypto Service Provider (CSP) may not be set up correctly"
174*cdf0e10cSrcweir    End Select
175*cdf0e10cSrcweir    WriteDebug currentFunctionName & "Problems acquiring Crypto Provider: " & MS_DEFAULT_PROVIDER & ": " & errStr
176*cdf0e10cSrcweirEnd Function
177*cdf0e10cSrcweir
178*cdf0e10cSrcweir
179*cdf0e10cSrcweir
180*cdf0e10cSrcweirFunction MD5HashString(ByVal Str As String) As String
181*cdf0e10cSrcweir    Const HP_HASHVAL = 2
182*cdf0e10cSrcweir    Const HP_HASHSIZE = 4
183*cdf0e10cSrcweir    On Error GoTo HandleErrors
184*cdf0e10cSrcweir    Dim currentFunctionName As String
185*cdf0e10cSrcweir    currentFunctionName = "MD5HashString"
186*cdf0e10cSrcweir
187*cdf0e10cSrcweir    Dim hCtx As Long
188*cdf0e10cSrcweir    Dim hHash As Long
189*cdf0e10cSrcweir    Dim ret As Long
190*cdf0e10cSrcweir    Dim lLen As Long
191*cdf0e10cSrcweir    Dim lIdx As Long
192*cdf0e10cSrcweir    Dim abData() As Byte
193*cdf0e10cSrcweir
194*cdf0e10cSrcweir    If Not GetProvider(hCtx) Then Err.Raise Err.LastDllError
195*cdf0e10cSrcweir
196*cdf0e10cSrcweir    ret = CryptCreateHash(hCtx, MD5_ALGORITHM, 0, 0, hHash)
197*cdf0e10cSrcweir    If ret = 0 Then Err.Raise Err.LastDllError
198*cdf0e10cSrcweir
199*cdf0e10cSrcweir    ret = CryptHashData(hHash, ByVal Str, Len(Str), 0)
200*cdf0e10cSrcweir    If ret = 0 Then Err.Raise Err.LastDllError
201*cdf0e10cSrcweir
202*cdf0e10cSrcweir    ret = CryptGetHashParam(hHash, HP_HASHSIZE, lLen, 4, 0)
203*cdf0e10cSrcweir    If ret = 0 Then Err.Raise Err.LastDllError
204*cdf0e10cSrcweir
205*cdf0e10cSrcweir
206*cdf0e10cSrcweir    ReDim abData(0 To lLen - 1)
207*cdf0e10cSrcweir    ret = CryptGetHashParam(hHash, HP_HASHVAL, abData(0), lLen, 0)
208*cdf0e10cSrcweir    If ret = 0 Then Err.Raise Err.LastDllError
209*cdf0e10cSrcweir
210*cdf0e10cSrcweir    For lIdx = 0 To UBound(abData)
211*cdf0e10cSrcweir        MD5HashString = MD5HashString & Right$("0" & Hex$(abData(lIdx)), 2)
212*cdf0e10cSrcweir    Next
213*cdf0e10cSrcweir    CryptDestroyHash hHash
214*cdf0e10cSrcweir
215*cdf0e10cSrcweir    CryptReleaseContext hCtx, 0
216*cdf0e10cSrcweir
217*cdf0e10cSrcweirFinalExit:
218*cdf0e10cSrcweir    Exit Function
219*cdf0e10cSrcweir
220*cdf0e10cSrcweirHandleErrors:
221*cdf0e10cSrcweir    MD5HashString = ""
222*cdf0e10cSrcweir    WriteDebug currentFunctionName & _
223*cdf0e10cSrcweir    Err.Number & " " & Err.Description & " " & Err.Source
224*cdf0e10cSrcweir    Resume FinalExit
225*cdf0e10cSrcweirEnd Function
226*cdf0e10cSrcweir
227