mfplat: Remove duplicated GUID entry from attribute tracing.
[wine/zf.git] / dlls / msisip / main.c
blob6324542428af60916ec011eb5f1158fd5a272494
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 static GUID mySubject = { 0x000c10f1, 0x0000, 0x0000,
32 { 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46 }};
34 /***********************************************************************
35 * DllRegisterServer (MSISIP.@)
37 HRESULT WINAPI DllRegisterServer(void)
39 static WCHAR msisip[] = L"MSISIP.DLL";
40 static WCHAR getSignedDataMsg[] = L"MsiSIPGetSignedDataMsg";
41 static WCHAR putSignedDataMsg[] = L"MsiSIPPutSignedDataMsg";
42 static WCHAR createIndirectData[] = L"MsiSIPCreateIndirectData";
43 static WCHAR verifyIndirectData[] = L"MsiSIPVerifyIndirectData";
44 static WCHAR removeSignedDataMsg[] = L"MsiSIPRemoveSignedDataMsg";
45 static WCHAR isMyTypeOfFile[] = L"MsiSIPIsMyTypeOfFile";
47 SIP_ADD_NEWPROVIDER prov;
49 memset(&prov, 0, sizeof(prov));
50 prov.cbStruct = sizeof(prov);
51 prov.pwszDLLFileName = msisip;
52 prov.pgSubject = &mySubject;
53 prov.pwszGetFuncName = getSignedDataMsg;
54 prov.pwszPutFuncName = putSignedDataMsg;
55 prov.pwszCreateFuncName = createIndirectData;
56 prov.pwszVerifyFuncName = verifyIndirectData;
57 prov.pwszRemoveFuncName = removeSignedDataMsg;
58 prov.pwszIsFunctionNameFmt2 = isMyTypeOfFile;
59 prov.pwszGetCapFuncName = NULL;
60 return CryptSIPAddProvider(&prov) ? S_OK : S_FALSE;
63 /***********************************************************************
64 * DllUnregisterServer (MSISIP.@)
66 HRESULT WINAPI DllUnregisterServer(void)
68 CryptSIPRemoveProvider(&mySubject);
69 return S_OK;
72 /***********************************************************************
73 * MsiSIPGetSignedDataMsg (MSISIP.@)
75 BOOL WINAPI MsiSIPGetSignedDataMsg(SIP_SUBJECTINFO *pSubjectInfo,
76 DWORD *pdwEncodingType, DWORD dwIndex, DWORD *pcbSignedDataMsg,
77 BYTE *pbSignedDataMsg)
79 BOOL ret = FALSE;
80 IStorage *stg = NULL;
81 HRESULT r;
82 IStream *stm = NULL;
83 BYTE hdr[2], len[sizeof(DWORD)];
84 DWORD count, lenBytes, dataBytes;
86 TRACE("(%p %p %d %p %p)\n", pSubjectInfo, pdwEncodingType, dwIndex,
87 pcbSignedDataMsg, pbSignedDataMsg);
89 r = StgOpenStorage(pSubjectInfo->pwsFileName, NULL,
90 STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE, NULL, 0, &stg);
91 if (FAILED(r))
93 TRACE("couldn't open %s\n", debugstr_w(pSubjectInfo->pwsFileName));
94 goto end;
97 r = IStorage_OpenStream(stg, L"\5DigitalSignature", 0,
98 STGM_READ|STGM_SHARE_EXCLUSIVE, 0, &stm);
99 if (FAILED(r))
101 TRACE("couldn't find digital signature stream\n");
102 goto freestorage;
105 r = IStream_Read(stm, hdr, sizeof(hdr), &count);
106 if (FAILED(r) || count != sizeof(hdr))
107 goto freestream;
108 if (hdr[0] != 0x30)
110 WARN("unexpected data in digital sig: 0x%02x%02x\n", hdr[0], hdr[1]);
111 goto freestream;
114 /* Read the asn.1 length from the stream. Only supports definite-length
115 * values, which DER-encoded signatures should be.
117 if (hdr[1] == 0x80)
119 WARN("indefinite-length encoding not supported!\n");
120 goto freestream;
122 else if (hdr[1] & 0x80)
124 DWORD temp;
125 LPBYTE ptr;
127 lenBytes = hdr[1] & 0x7f;
128 if (lenBytes > sizeof(DWORD))
130 WARN("asn.1 length too long (%d)\n", lenBytes);
131 goto freestream;
133 r = IStream_Read(stm, len, lenBytes, &count);
134 if (FAILED(r) || count != lenBytes)
135 goto freestream;
136 dataBytes = 0;
137 temp = lenBytes;
138 ptr = len;
139 while (temp--)
141 dataBytes <<= 8;
142 dataBytes |= *ptr++;
145 else
147 lenBytes = 0;
148 dataBytes = hdr[1];
151 if (!pbSignedDataMsg)
153 *pcbSignedDataMsg = 2 + lenBytes + dataBytes;
154 ret = TRUE;
156 else if (*pcbSignedDataMsg < 2 + lenBytes + dataBytes)
158 SetLastError(ERROR_INSUFFICIENT_BUFFER);
159 *pcbSignedDataMsg = 2 + lenBytes + dataBytes;
161 else
163 LPBYTE ptr = pbSignedDataMsg;
165 memcpy(ptr, hdr, sizeof(hdr));
166 ptr += sizeof(hdr);
167 if (lenBytes)
169 memcpy(ptr, len, lenBytes);
170 ptr += lenBytes;
172 r = IStream_Read(stm, ptr, dataBytes, &count);
173 if (SUCCEEDED(r) && count == dataBytes)
175 *pdwEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
176 *pcbSignedDataMsg = 2 + lenBytes + dataBytes;
177 ret = TRUE;
181 freestream:
182 IStream_Release(stm);
183 freestorage:
184 IStorage_Release(stg);
185 end:
187 TRACE("returning %d\n", ret);
188 return ret;
191 DEFINE_GUID(CLSID_MsiTransform, 0x000c1082,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
192 DEFINE_GUID(CLSID_MsiDatabase, 0x000c1084,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
193 DEFINE_GUID(CLSID_MsiPatch, 0x000c1086,0x0000,0x0000,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46);
195 /***********************************************************************
196 * MsiSIPIsMyTypeOfFile (MSISIP.@)
198 BOOL WINAPI MsiSIPIsMyTypeOfFile(WCHAR *name, GUID *subject)
200 BOOL ret = FALSE;
201 IStorage *stg = NULL;
202 HRESULT r;
204 TRACE("(%s, %p)\n", debugstr_w(name), subject);
206 r = StgOpenStorage(name, NULL, STGM_DIRECT|STGM_READ|STGM_SHARE_DENY_WRITE,
207 NULL, 0, &stg);
208 if (SUCCEEDED(r))
210 STATSTG stat;
212 r = IStorage_Stat(stg, &stat, STATFLAG_NONAME);
213 if (SUCCEEDED(r))
215 if (IsEqualGUID(&stat.clsid, &CLSID_MsiDatabase) ||
216 IsEqualGUID(&stat.clsid, &CLSID_MsiPatch) ||
217 IsEqualGUID(&stat.clsid, &CLSID_MsiTransform))
219 ret = TRUE;
220 *subject = mySubject;
223 IStorage_Release(stg);
225 return ret;