summaryrefslogtreecommitdiff
path: root/src/utilcode/assemblyfilehash.cpp
blob: c19b7153222f8229ced74e5f66a18542c7db79ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


#include "stdafx.h"
#include <stdlib.h>
#include "utilcode.h"
#include "strongname.h"
#include "assemblyfilehash.h"
#include "ex.h"
#include "corperm.h"

#include <wincrypt.h>

HRESULT AssemblyFileHash::ReadData()
{
    CONTRACTL
    {
        INSTANCE_CHECK;
        NOTHROW;
        GC_NOTRIGGER;
        INJECT_FAULT(return E_OUTOFMEMORY;);
#ifdef MODE_PREEMPTIVE
        MODE_PREEMPTIVE;
#endif          
    }
    CONTRACTL_END;
    NewArrayHolder<BYTE> pBuffer;
    
    DWORD dwFileSize = SafeGetFileSize( m_hFile, NULL );

    if (dwFileSize == 0xffffffff)
        return HRESULT_FROM_GetLastError();

    pBuffer = new (nothrow)BYTE[dwFileSize];
    if(pBuffer==NULL)
        return E_OUTOFMEMORY;
    
    DWORD cbBuffer = dwFileSize;
    DWORD cbRead;

    if (!ReadFile( m_hFile, pBuffer, cbBuffer, &cbRead, NULL ) ||
        cbRead != cbBuffer)
        return HRESULT_FROM_GetLastError();

    pBuffer.SuppressRelease();
    this->m_pbData = pBuffer;
    this->m_cbData = cbBuffer;
    this->m_bDataOwner = TRUE;
    this->m_NeedToReadData=FALSE;

    return S_OK;
    
}

HRESULT AssemblyFileHash::SetFileName(LPCWSTR wszFileName)
{
    CONTRACTL
    {
        INSTANCE_CHECK;
        NOTHROW;
        GC_NOTRIGGER;
        INJECT_FAULT(return E_OUTOFMEMORY;);
#ifdef MODE_PREEMPTIVE
        MODE_PREEMPTIVE;
#endif          
    }
    CONTRACTL_END;

    HRESULT hr = S_OK;
    HandleHolder hFile;

    hFile = WszCreateFile(wszFileName,
                          GENERIC_READ,
                          FILE_SHARE_READ,
                          NULL,
                          OPEN_EXISTING,
                          0,
                          NULL);

    if (hFile == INVALID_HANDLE_VALUE)
        return HRESULT_FROM_GetLastError();

    IfFailRet(SetFileHandle(hFile));
    hFile.SuppressRelease();
    return S_OK;
}


HRESULT AssemblyFileHash::HashData(HCRYPTHASH hHash)
{
    WRAPPER_NO_CONTRACT;
    if(!CryptHashData(hHash, m_pbData, m_cbData, 0))
        return HRESULT_FROM_GetLastError();
    return S_OK;
}

HRESULT AssemblyFileHash::CalculateHash(DWORD algid)
{
    CONTRACTL
    {
        INSTANCE_CHECK;
        NOTHROW;
        INJECT_FAULT(return E_OUTOFMEMORY;);
    }
    CONTRACTL_END;

    HRESULT hr = S_OK;
    if(m_NeedToReadData)
        IfFailRet(ReadData());

    _ASSERTE(!m_NeedToReadData);
    
    HCRYPTPROV pProvider = NULL;
    HCRYPTHASH hHash = NULL;
    DWORD count;

    if(!WszCryptAcquireContext(&pProvider,
                               NULL,
                               NULL,
                               //PROV_RSA_SIG,
                               PROV_RSA_FULL,
                               CRYPT_VERIFYCONTEXT))
        IfFailGo(HRESULT_FROM_GetLastError());

    
    if(!CryptCreateHash(pProvider,
                        algid,
                        0,
                        0,
                        &hHash))
        IfFailGo(HRESULT_FROM_GetLastError());

    IfFailGo(HashData(hHash));

    count = sizeof(m_cbHash);
    if(!CryptGetHashParam(hHash, 
                          HP_HASHSIZE,
                          (PBYTE) &m_cbHash,
                          &count,
                          0))
        IfFailGo(HRESULT_FROM_GetLastError());
        
    if(m_cbHash > 0) {
        m_pbHash = new (nothrow) BYTE[m_cbHash];
        if (!m_pbHash)
            IfFailGo(E_OUTOFMEMORY);

        if(!CryptGetHashParam(hHash, 
                              HP_HASHVAL,
                              m_pbHash,
                              &m_cbHash,
                              0))
            IfFailGo(HRESULT_FROM_GetLastError());
    }

 ErrExit:

    if(hHash) 
        CryptDestroyHash(hHash);
    if(pProvider)
        CryptReleaseContext(pProvider, 0);
    return hr;
}