krnl386.exe: Fix size calculation in GetSystemDirectory16.
[wine] / dlls / msisip / main.c
1 /*
2  * Copyright 2008 Juan Lang
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wincrypt.h"
24 #include "mssip.h"
25 #define COBJMACROS
26 #include "objbase.h"
27 #include "initguid.h"
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(msisip);
31
32 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
33 {
34     TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
35
36     switch (fdwReason)
37     {
38         case DLL_WINE_PREATTACH:
39             return FALSE;    /* prefer native version */
40         case DLL_PROCESS_ATTACH:
41             DisableThreadLibraryCalls(hinstDLL);
42             break;
43         case DLL_PROCESS_DETACH:
44             break;
45         default:
46             break;
47     }
48
49     return TRUE;
50 }
51
52 static GUID mySubject = { 0x000c10f1, 0x0000, 0x0000,
53  { 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46 }};
54
55 /***********************************************************************
56  *              DllRegisterServer (MSISIP.@)
57  */
58 HRESULT WINAPI DllRegisterServer(void)
59 {
60     static WCHAR msisip[] = { 'M','S','I','S','I','P','.','D','L','L',0 };
61     static WCHAR getSignedDataMsg[] = { 'M','s','i','S','I','P','G','e','t',
62      'S','i','g','n','e','d','D','a','t','a','M','s','g',0 };
63     static WCHAR putSignedDataMsg[] = { 'M','s','i','S','I','P','P','u','t',
64      'S','i','g','n','e','d','D','a','t','a','M','s','g',0 };
65     static WCHAR createIndirectData[] = { 'M','s','i','S','I','P',
66      'C','r','e','a','t','e','I','n','d','i','r','e','c','t','D','a','t','a',
67      0 };
68     static WCHAR verifyIndirectData[] = { 'M','s','i','S','I','P',
69      'V','e','r','i','f','y','I','n','d','i','r','e','c','t','D','a','t','a',
70      0 };
71     static WCHAR removeSignedDataMsg[] = { 'M','s','i','S','I','P','R','e','m',
72      'o','v','e','S','i','g','n','e','d','D','a','t','a','M','s','g', 0 };
73     static WCHAR isMyTypeOfFile[] = { 'M','s','i','S','I','P',
74      'I','s','M','y','T','y','p','e','O','f','F','i','l','e',0 };
75
76     SIP_ADD_NEWPROVIDER prov;
77
78     memset(&prov, 0, sizeof(prov));
79     prov.cbStruct = sizeof(prov);
80     prov.pwszDLLFileName = msisip;
81     prov.pgSubject = &mySubject;
82     prov.pwszGetFuncName = getSignedDataMsg;
83     prov.pwszPutFuncName = putSignedDataMsg;
84     prov.pwszCreateFuncName = createIndirectData;
85     prov.pwszVerifyFuncName = verifyIndirectData;
86     prov.pwszRemoveFuncName = removeSignedDataMsg;
87     prov.pwszIsFunctionNameFmt2 = isMyTypeOfFile;
88     return CryptSIPAddProvider(&prov) ? S_OK : S_FALSE;
89 }
90
91 /***********************************************************************
92  *              DllUnregisterServer (MSISIP.@)
93  */
94 HRESULT WINAPI DllUnregisterServer(void)
95 {
96     CryptSIPRemoveProvider(&mySubject);
97     return S_OK;
98 }
99
100 /***********************************************************************
101  *              MsiSIPGetSignedDataMsg  (MSISIP.@)
102  */
103 BOOL WINAPI MsiSIPGetSignedDataMsg(SIP_SUBJECTINFO *pSubjectInfo,
104  DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
105  BYTE *pbSignedDataMsg)
106 {
107     static const WCHAR digitalSig[] = { 5,'D','i','g','i','t','a','l',
108      'S','i','g','n','a','t','u','r','e',0 };
109     BOOL ret = FALSE;
110     IStorage *stg = NULL;
111     HRESULT r;
112     IStream *stm = NULL;
113     BYTE hdr[2], len[sizeof(DWORD)];
114     DWORD count, lenBytes, dataBytes;
115
116     TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
117           pcbSignedDataMsg, pbSignedDataMsg);
118
119     r = StgOpenStorage(pSubjectInfo->pwsFileName, NULL,
120      STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
121     if (FAILED(r))
122     {
123         TRACE("couldn't open %s\n", debugstr_w(pSubjectInfo->pwsFileName));
124         goto end;
125     }
126
127     r = IStorage_OpenStream(stg, digitalSig, 0,
128      STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stm);
129     if (FAILED(r))
130     {
131         TRACE("couldn't find digital signature stream\n");
132         goto freestorage;
133     }
134
135     r = IStream_Read(stm, hdr, sizeof(hdr), &count);
136     if (FAILED(r) || count != sizeof(hdr))
137         goto freestream;
138     if (hdr[0] != 0x30)
139     {
140         WARN("unexpected data in digital sig: 0x%02x%02x\n", hdr[0], hdr[1]);
141         goto freestream;
142     }
143
144     /* Read the asn.1 length from the stream.  Only supports definite-length
145      * values, which DER-encoded signatures should be.
146      */
147     if (hdr[1] == 0x80)
148     {
149         WARN("indefinite-length encoding not supported!\n");
150         goto freestream;
151     }
152     else if (hdr[1] & 0x80)
153     {
154         DWORD temp;
155         LPBYTE ptr;
156
157         lenBytes = hdr[1] & 0x7f;
158         if (lenBytes > sizeof(DWORD))
159         {
160             WARN("asn.1 length too long (%d)\n", lenBytes);
161             goto freestream;
162         }
163         r = IStream_Read(stm, len, lenBytes, &count);
164         if (FAILED(r) || count != lenBytes)
165             goto freestream;
166         dataBytes = 0;
167         temp = lenBytes;
168         ptr = len;
169         while (temp--)
170         {
171             dataBytes <<= 8;
172             dataBytes |= *ptr++;
173         }
174     }
175     else
176     {
177         lenBytes = 0;
178         dataBytes = hdr[1];
179     }
180
181     if (!pbSignedDataMsg)
182     {
183         *pcbSignedDataMsg = 2 + lenBytes + dataBytes;
184         ret = TRUE;
185     }
186     else if (*pcbSignedDataMsg < 2 + lenBytes + dataBytes)
187     {
188         SetLastError(ERROR_INSUFFICIENT_BUFFER);
189         *pcbSignedDataMsg = 2 + lenBytes + dataBytes;
190     }
191     else
192     {
193         LPBYTE ptr = pbSignedDataMsg;
194
195         memcpy(ptr, hdr, sizeof(hdr));
196         ptr += sizeof(hdr);
197         if (lenBytes)
198         {
199             memcpy(ptr, len, lenBytes);
200             ptr += lenBytes;
201         }
202         r = IStream_Read(stm, ptr, dataBytes, &count);
203         if (SUCCEEDED(r) && count == dataBytes)
204         {
205             *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
206             *pcbSignedDataMsg = 2 + lenBytes + dataBytes;
207             ret = TRUE;
208         }
209     }
210
211 freestream:
212     IStream_Release(stm);
213 freestorage:
214     IStorage_Release(stg);
215 end:
216
217     TRACE("returning %d\n", ret);
218     return ret;
219 }
220
221 DEFINE_GUID(CLSID_MsiTransform, 0x000c1082,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
222 DEFINE_GUID(CLSID_MsiDatabase,  0x000c1084,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
223 DEFINE_GUID(CLSID_MsiPatch,     0x000c1086,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
224
225 /***********************************************************************
226  *              MsiSIPIsMyTypeOfFile (MSISIP.@)
227  */
228 BOOL WINAPI MsiSIPIsMyTypeOfFile(WCHAR *name, GUID *subject)
229 {
230     BOOL ret = FALSE;
231     IStorage *stg = NULL;
232     HRESULT r;
233
234     TRACE("(%s, %p)\n", debugstr_w(name), subject);
235
236     r = StgOpenStorage(name, NULL, STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE,
237      NULL, 0, &stg);
238     if (SUCCEEDED(r))
239     {
240         STATSTG stat;
241
242         r = IStorage_Stat(stg, &stat, STATFLAG_NONAME);
243         if (SUCCEEDED(r))
244         {
245             if (IsEqualGUID(&stat.clsid, &CLSID_MsiDatabase) ||
246              IsEqualGUID(&stat.clsid, &CLSID_MsiPatch) ||
247              IsEqualGUID(&stat.clsid, &CLSID_MsiTransform))
248             {
249                 ret = TRUE;
250                 *subject = mySubject;
251             }
252         }
253         IStorage_Release(stg);
254     }
255     return ret;
256 }