mf/session: Implement support for sinks that provide sample allocators.
[wine.git] / dlls / msisip / main.c
blob11893f149c364735cc9ba93ce8f70dba818365cd
1 /*
2 * Copyright 2008 Juan Lang
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.
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.
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
19 #include <stdarg.h>
20 #include "windef.h"
21 #include "winbase.h"
22 #include "wincrypt.h"
23 #include "mssip.h"
24 #define COBJMACROS
25 #include "objbase.h"
26 #include "initguid.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(msisip);
31 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
33 TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
35 switch (fdwReason)
37 case DLL_WINE_PREATTACH:
38 return FALSE; /* prefer native version */
39 case DLL_PROCESS_ATTACH:
40 DisableThreadLibraryCalls(hinstDLL);
41 break;
44 return TRUE;
47 static GUID mySubject = { 0x000c10f1, 0x0000, 0x0000,
48 { 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46 }};
50 /***********************************************************************
51 * DllRegisterServer (MSISIP.@)
53 HRESULT WINAPI DllRegisterServer(void)
55 static WCHAR msisip[] = L"MSISIP.DLL";
56 static WCHAR getSignedDataMsg[] = L"MsiSIPGetSignedDataMsg";
57 static WCHAR putSignedDataMsg[] = L"MsiSIPPutSignedDataMsg";
58 static WCHAR createIndirectData[] = L"MsiSIPCreateIndirectData";
59 static WCHAR verifyIndirectData[] = L"MsiSIPVerifyIndirectData";
60 static WCHAR removeSignedDataMsg[] = L"MsiSIPRemoveSignedDataMsg";
61 static WCHAR isMyTypeOfFile[] = L"MsiSIPIsMyTypeOfFile";
63 SIP_ADD_NEWPROVIDER prov;
65 memset(&prov, 0, sizeof(prov));
66 prov.cbStruct = sizeof(prov);
67 prov.pwszDLLFileName = msisip;
68 prov.pgSubject = &mySubject;
69 prov.pwszGetFuncName = getSignedDataMsg;
70 prov.pwszPutFuncName = putSignedDataMsg;
71 prov.pwszCreateFuncName = createIndirectData;
72 prov.pwszVerifyFuncName = verifyIndirectData;
73 prov.pwszRemoveFuncName = removeSignedDataMsg;
74 prov.pwszIsFunctionNameFmt2 = isMyTypeOfFile;
75 prov.pwszGetCapFuncName = NULL;
76 return CryptSIPAddProvider(&prov) ? S_OK : S_FALSE;
79 /***********************************************************************
80 * DllUnregisterServer (MSISIP.@)
82 HRESULT WINAPI DllUnregisterServer(void)
84 CryptSIPRemoveProvider(&mySubject);
85 return S_OK;
88 /***********************************************************************
89 * MsiSIPGetSignedDataMsg (MSISIP.@)
91 BOOL WINAPI MsiSIPGetSignedDataMsg(SIP_SUBJECTINFO *pSubjectInfo,
92 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
93 BYTE *pbSignedDataMsg)
95 BOOL ret = FALSE;
96 IStorage *stg = NULL;
97 HRESULT r;
98 IStream *stm = NULL;
99 BYTE hdr[2], len[sizeof(DWORD)];
100 DWORD count, lenBytes, dataBytes;
102 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
103 pcbSignedDataMsg, pbSignedDataMsg);
105 r = StgOpenStorage(pSubjectInfo->pwsFileName, NULL,
106 STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
107 if (FAILED(r))
109 TRACE("couldn't open %s\n", debugstr_w(pSubjectInfo->pwsFileName));
110 goto end;
113 r = IStorage_OpenStream(stg, L"\5DigitalSignature", 0,
114 STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stm);
115 if (FAILED(r))
117 TRACE("couldn't find digital signature stream\n");
118 goto freestorage;
121 r = IStream_Read(stm, hdr, sizeof(hdr), &count);
122 if (FAILED(r) || count != sizeof(hdr))
123 goto freestream;
124 if (hdr[0] != 0x30)
126 WARN("unexpected data in digital sig: 0x%02x%02x\n", hdr[0], hdr[1]);
127 goto freestream;
130 /* Read the asn.1 length from the stream. Only supports definite-length
131 * values, which DER-encoded signatures should be.
133 if (hdr[1] == 0x80)
135 WARN("indefinite-length encoding not supported!\n");
136 goto freestream;
138 else if (hdr[1] & 0x80)
140 DWORD temp;
141 LPBYTE ptr;
143 lenBytes = hdr[1] & 0x7f;
144 if (lenBytes > sizeof(DWORD))
146 WARN("asn.1 length too long (%d)\n", lenBytes);
147 goto freestream;
149 r = IStream_Read(stm, len, lenBytes, &count);
150 if (FAILED(r) || count != lenBytes)
151 goto freestream;
152 dataBytes = 0;
153 temp = lenBytes;
154 ptr = len;
155 while (temp--)
157 dataBytes <<= 8;
158 dataBytes |= *ptr++;
161 else
163 lenBytes = 0;
164 dataBytes = hdr[1];
167 if (!pbSignedDataMsg)
169 *pcbSignedDataMsg = 2 + lenBytes + dataBytes;
170 ret = TRUE;
172 else if (*pcbSignedDataMsg < 2 + lenBytes + dataBytes)
174 SetLastError(ERROR_INSUFFICIENT_BUFFER);
175 *pcbSignedDataMsg = 2 + lenBytes + dataBytes;
177 else
179 LPBYTE ptr = pbSignedDataMsg;
181 memcpy(ptr, hdr, sizeof(hdr));
182 ptr += sizeof(hdr);
183 if (lenBytes)
185 memcpy(ptr, len, lenBytes);
186 ptr += lenBytes;
188 r = IStream_Read(stm, ptr, dataBytes, &count);
189 if (SUCCEEDED(r) && count == dataBytes)
191 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
192 *pcbSignedDataMsg = 2 + lenBytes + dataBytes;
193 ret = TRUE;
197 freestream:
198 IStream_Release(stm);
199 freestorage:
200 IStorage_Release(stg);
201 end:
203 TRACE("returning %d\n", ret);
204 return ret;
207 DEFINE_GUID(CLSID_MsiTransform, 0x000c1082,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
208 DEFINE_GUID(CLSID_MsiDatabase, 0x000c1084,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
209 DEFINE_GUID(CLSID_MsiPatch, 0x000c1086,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
211 /***********************************************************************
212 * MsiSIPIsMyTypeOfFile (MSISIP.@)
214 BOOL WINAPI MsiSIPIsMyTypeOfFile(WCHAR *name, GUID *subject)
216 BOOL ret = FALSE;
217 IStorage *stg = NULL;
218 HRESULT r;
220 TRACE("(%s, %p)\n", debugstr_w(name), subject);
222 r = StgOpenStorage(name, NULL, STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE,
223 NULL, 0, &stg);
224 if (SUCCEEDED(r))
226 STATSTG stat;
228 r = IStorage_Stat(stg, &stat, STATFLAG_NONAME);
229 if (SUCCEEDED(r))
231 if (IsEqualGUID(&stat.clsid, &CLSID_MsiDatabase) ||
232 IsEqualGUID(&stat.clsid, &CLSID_MsiPatch) ||
233 IsEqualGUID(&stat.clsid, &CLSID_MsiTransform))
235 ret = TRUE;
236 *subject = mySubject;
239 IStorage_Release(stg);
241 return ret;