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
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
);
37 case DLL_WINE_PREATTACH
:
38 return FALSE
; /* prefer native version */
39 case DLL_PROCESS_ATTACH
:
40 DisableThreadLibraryCalls(hinstDLL
);
42 case DLL_PROCESS_DETACH
:
51 static GUID mySubject
= { 0x000c10f1, 0x0000, 0x0000,
52 { 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46 }};
54 /***********************************************************************
55 * DllRegisterServer (MSISIP.@)
57 HRESULT WINAPI
DllRegisterServer(void)
59 static WCHAR msisip
[] = { 'M','S','I','S','I','P','.','D','L','L',0 };
60 static WCHAR getSignedDataMsg
[] = { 'M','s','i','S','I','P','G','e','t',
61 'S','i','g','n','e','d','D','a','t','a','M','s','g',0 };
62 static WCHAR putSignedDataMsg
[] = { 'M','s','i','S','I','P','P','u','t',
63 'S','i','g','n','e','d','D','a','t','a','M','s','g',0 };
64 static WCHAR createIndirectData
[] = { 'M','s','i','S','I','P',
65 'C','r','e','a','t','e','I','n','d','i','r','e','c','t','D','a','t','a',
67 static WCHAR verifyIndirectData
[] = { 'M','s','i','S','I','P',
68 'V','e','r','i','f','y','I','n','d','i','r','e','c','t','D','a','t','a',
70 static WCHAR removeSignedDataMsg
[] = { 'M','s','i','S','I','P','R','e','m',
71 'o','v','e','S','i','g','n','e','d','D','a','t','a','M','s','g', 0 };
72 static WCHAR isMyTypeOfFile
[] = { 'M','s','i','S','I','P',
73 'I','s','M','y','T','y','p','e','O','f','F','i','l','e',0 };
75 SIP_ADD_NEWPROVIDER prov
;
77 memset(&prov
, 0, sizeof(prov
));
78 prov
.cbStruct
= sizeof(prov
);
79 prov
.pwszDLLFileName
= msisip
;
80 prov
.pgSubject
= &mySubject
;
81 prov
.pwszGetFuncName
= getSignedDataMsg
;
82 prov
.pwszPutFuncName
= putSignedDataMsg
;
83 prov
.pwszCreateFuncName
= createIndirectData
;
84 prov
.pwszVerifyFuncName
= verifyIndirectData
;
85 prov
.pwszRemoveFuncName
= removeSignedDataMsg
;
86 prov
.pwszIsFunctionNameFmt2
= isMyTypeOfFile
;
87 return CryptSIPAddProvider(&prov
) ? S_OK
: S_FALSE
;
90 /***********************************************************************
91 * DllUnregisterServer (MSISIP.@)
93 HRESULT WINAPI
DllUnregisterServer(void)
95 CryptSIPRemoveProvider(&mySubject
);
99 /***********************************************************************
100 * MsiSIPGetSignedDataMsg (MSISIP.@)
102 BOOL WINAPI
MsiSIPGetSignedDataMsg(SIP_SUBJECTINFO
*pSubjectInfo
,
103 DWORD
*pdwEncodingType
, DWORD dwIndex
, DWORD
*pcbSignedDataMsg
,
104 BYTE
*pbSignedDataMsg
)
106 static const WCHAR digitalSig
[] = { 5,'D','i','g','i','t','a','l',
107 'S','i','g','n','a','t','u','r','e',0 };
109 IStorage
*stg
= NULL
;
112 BYTE hdr
[2], len
[sizeof(DWORD
)];
113 DWORD count
, lenBytes
, dataBytes
;
115 TRACE("(%p %p %d %p %p)\n", pSubjectInfo
, pdwEncodingType
, dwIndex
,
116 pcbSignedDataMsg
, pbSignedDataMsg
);
118 r
= StgOpenStorage(pSubjectInfo
->pwsFileName
, NULL
,
119 STGM_DIRECT
|STGM_READ
|STGM_SHARE_DENY_WRITE
, NULL
, 0, &stg
);
122 TRACE("couldn't open %s\n", debugstr_w(pSubjectInfo
->pwsFileName
));
126 r
= IStorage_OpenStream(stg
, digitalSig
, 0,
127 STGM_READ
|STGM_SHARE_EXCLUSIVE
, 0, &stm
);
130 TRACE("couldn't find digital signature stream\n");
134 r
= IStream_Read(stm
, hdr
, sizeof(hdr
), &count
);
135 if (FAILED(r
) || count
!= sizeof(hdr
))
139 WARN("unexpected data in digital sig: 0x%02x%02x\n", hdr
[0], hdr
[1]);
143 /* Read the asn.1 length from the stream. Only supports definite-length
144 * values, which DER-encoded signatures should be.
148 WARN("indefinite-length encoding not supported!\n");
151 else if (hdr
[1] & 0x80)
156 lenBytes
= hdr
[1] & 0x7f;
157 if (lenBytes
> sizeof(DWORD
))
159 WARN("asn.1 length too long (%d)\n", lenBytes
);
162 r
= IStream_Read(stm
, len
, lenBytes
, &count
);
163 if (FAILED(r
) || count
!= lenBytes
)
180 if (!pbSignedDataMsg
)
182 *pcbSignedDataMsg
= 2 + lenBytes
+ dataBytes
;
185 else if (*pcbSignedDataMsg
< 2 + lenBytes
+ dataBytes
)
187 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
188 *pcbSignedDataMsg
= 2 + lenBytes
+ dataBytes
;
192 LPBYTE ptr
= pbSignedDataMsg
;
194 memcpy(ptr
, hdr
, sizeof(hdr
));
198 memcpy(ptr
, len
, lenBytes
);
201 r
= IStream_Read(stm
, ptr
, dataBytes
, &count
);
202 if (SUCCEEDED(r
) && count
== dataBytes
)
204 *pdwEncodingType
= X509_ASN_ENCODING
| PKCS_7_ASN_ENCODING
;
205 *pcbSignedDataMsg
= 2 + lenBytes
+ dataBytes
;
211 IStream_Release(stm
);
213 IStorage_Release(stg
);
216 TRACE("returning %d\n", ret
);
220 /***********************************************************************
221 * MsiSIPIsMyTypeOfFile (MSISIP.@)
223 BOOL WINAPI
MsiSIPIsMyTypeOfFile(WCHAR
*name
, GUID
*subject
)
225 static const WCHAR msi
[] = { '.','m','s','i',0 };
226 static const WCHAR msp
[] = { '.','m','s','p',0 };
229 TRACE("(%s, %p)\n", debugstr_w(name
), subject
);
231 if (lstrlenW(name
) < lstrlenW(msi
))
233 else if (lstrcmpiW(name
+ lstrlenW(name
) - lstrlenW(msi
), msi
) &&
234 lstrcmpiW(name
+ lstrlenW(name
) - lstrlenW(msp
), msp
))
238 IStorage
*stg
= NULL
;
239 HRESULT r
= StgOpenStorage(name
, NULL
,
240 STGM_DIRECT
|STGM_READ
|STGM_SHARE_DENY_WRITE
, NULL
, 0, &stg
);
244 IStorage_Release(stg
);
245 *subject
= mySubject
;