Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DeCSSInputPin.cpp « DeCSS « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 15fc5885af5885617bb1002395cb818a9524ab9b (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * (C) 2003-2006 Gabest
 * (C) 2006-2013, 2015-2016 see Authors.txt
 *
 * This file is part of MPC-HC.
 *
 * MPC-HC is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * MPC-HC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "stdafx.h"
#include <atlbase.h>
#include <algorithm>
#include "BaseClasses/streams.h"
#include <dvdmedia.h>
#include <ks.h>
#include <ksmedia.h>
#include "DeCSSInputPin.h"
#include "../DSUtil/DSUtil.h"
#include "CSSauth.h"
#include "CSSscramble.h"

#include "moreuuids.h"

//
// CDeCSSInputPin
//

CDeCSSInputPin::CDeCSSInputPin(TCHAR* pObjectName, CTransformFilter* pFilter, HRESULT* phr, LPWSTR pName)
    : CTransformInputPin(pObjectName, pFilter, phr, pName)
    , m_varient(-1)
{
    ZeroMemory(m_Challenge, sizeof(m_Challenge));
    ZeroMemory(m_KeyCheck, sizeof(m_KeyCheck));
    ZeroMemory(m_Key, sizeof(m_Key));
    ZeroMemory(m_DiscKey, sizeof(m_DiscKey));
    ZeroMemory(m_TitleKey, sizeof(m_TitleKey));
}

STDMETHODIMP CDeCSSInputPin::NonDelegatingQueryInterface(REFIID riid, void** ppv)
{
    CheckPointer(ppv, E_POINTER);

    return
        QI(IKsPropertySet)
        __super::NonDelegatingQueryInterface(riid, ppv);
}

// IMemInputPin

STDMETHODIMP CDeCSSInputPin::Receive(IMediaSample* pSample)
{
    long len = pSample->GetActualDataLength();

    BYTE* p = nullptr;
    if (SUCCEEDED(pSample->GetPointer(&p)) && len > 0) {
        if (m_mt.majortype == MEDIATYPE_DVD_ENCRYPTED_PACK && len == 2048 && (p[0x14] & 0x30)) {
            CSSdescramble(p, m_TitleKey);
            p[0x14] &= ~0x30;

            if (CComQIPtr<IMediaSample2> pMS2 = pSample) {
                AM_SAMPLE2_PROPERTIES props;
                ZeroMemory(&props, sizeof(props));
                if (SUCCEEDED(pMS2->GetProperties(sizeof(props), (BYTE*)&props))
                        && (props.dwTypeSpecificFlags & AM_UseNewCSSKey)) {
                    props.dwTypeSpecificFlags &= ~AM_UseNewCSSKey;
                    pMS2->SetProperties(sizeof(props), (BYTE*)&props);
                }
            }
        }
    }

    HRESULT hr = Transform(pSample);

    return hr == S_OK ? __super::Receive(pSample) :
           hr == S_FALSE ? S_OK : hr;
}

void CDeCSSInputPin::StripPacket(BYTE*& p, long& len)
{
    GUID majortype = m_mt.majortype;

    if (majortype == MEDIATYPE_MPEG2_PACK || majortype == MEDIATYPE_DVD_ENCRYPTED_PACK) {
        if (len > 0 && *(DWORD*)p == 0xba010000) { // MEDIATYPE_*_PACK
            len -= 14;
            p += 14;
            if (int stuffing = (p[-1] & 7)) {
                len -= stuffing;
                p += stuffing;
            }
            majortype = MEDIATYPE_MPEG2_PES;
        }
    }

    if (majortype == MEDIATYPE_MPEG2_PES) {
        if (len > 0 && *(DWORD*)p == 0xbb010000) {
            len -= 4;
            p += 4;
            int hdrlen = ((p[0] << 8) | p[1]) + 2;
            len -= hdrlen;
            p += hdrlen;
        }

        if (len > 0
                && ((*(DWORD*)p & 0xf0ffffff) == 0xe0010000
                    || (*(DWORD*)p & 0xe0ffffff) == 0xc0010000
                    || (*(DWORD*)p & 0xbdffffff) == 0xbd010000)) { // PES
            bool ps1 = (*(DWORD*)p & 0xbdffffff) == 0xbd010000;

            len -= 4;
            p += 4;
            long expected = ((p[0] << 8) | p[1]);
            len -= 2;
            p += 2;
            BYTE* p0 = p;

            for (int i = 0; i < 16 && *p == 0xff; i++, len--, p++) {
                ;
            }

            if ((*p & 0xc0) == 0x80) { // mpeg2
                len -= 2;
                p += 2;
                len -= *p + 1;
                p += *p + 1;
            } else { // mpeg1
                if ((*p & 0xc0) == 0x40) {
                    len -= 2;
                    p += 2;
                }

                if ((*p & 0x30) == 0x30 || (*p & 0x30) == 0x20) {
                    bool pts = !!(*p & 0x20), dts = !!(*p & 0x10);
                    if (pts) {
                        len -= 5;
                    }
                    p += 5;
                    if (dts) {
                        ASSERT((*p & 0xf0) == 0x10);
                        len -= 5;
                        p += 5;
                    }
                } else {
                    len--;
                    p++;
                }
            }

            if (ps1) {
                len--;
                p++;
                if (m_mt.subtype == MEDIASUBTYPE_DVD_LPCM_AUDIO) {
                    len -= 6;
                    p += 6;
                } else if (m_mt.subtype == MEDIASUBTYPE_DOLBY_AC3 || m_mt.subtype == MEDIASUBTYPE_WAVE_DOLBY_AC3
                           || m_mt.subtype == MEDIASUBTYPE_DTS || m_mt.subtype == MEDIASUBTYPE_WAVE_DTS) {
                    len -= 3;
                    p += 3;
                }
            }

            if (expected > 0) {
                expected -= (long)(p - p0);
                len = std::min(expected, len);
            }
        }
    }

    if (len < 0) {
        ASSERT(0);
        len = 0;
    }
}

// IKsPropertySet

STDMETHODIMP CDeCSSInputPin::Set(REFGUID PropSet, ULONG Id, LPVOID pInstanceData, ULONG InstanceLength, LPVOID pPropertyData, ULONG DataLength)
{
    if (PropSet != AM_KSPROPSETID_CopyProt) {
        return E_NOTIMPL;
    }

    switch (Id) {
        case AM_PROPERTY_COPY_MACROVISION:
            break;
        case AM_PROPERTY_DVDCOPY_CHLG_KEY: { // 3. auth: receive drive nonce word, also store and encrypt the buskey made up of the two nonce words
            AM_DVDCOPY_CHLGKEY* pChlgKey = (AM_DVDCOPY_CHLGKEY*)pPropertyData;
            for (int i = 0; i < 10; i++) {
                m_Challenge[i] = pChlgKey->ChlgKey[9 - i];
            }

            CSSkey2(m_varient, m_Challenge, &m_Key[5]);

            CSSbuskey(m_varient, m_Key, m_KeyCheck);
        }
        break;
        case AM_PROPERTY_DVDCOPY_DISC_KEY: { // 5. receive the disckey
            AM_DVDCOPY_DISCKEY* pDiscKey = (AM_DVDCOPY_DISCKEY*)pPropertyData; // pDiscKey->DiscKey holds the disckey encrypted with itself and the 408 disckeys encrypted with the playerkeys

            bool fSuccess = false;

            for (int j = 0; j < g_nPlayerKeys; j++) {
                for (int k = 1; k < 409; k++) {
                    BYTE DiscKey[6];
                    for (int i = 0; i < 5; i++) {
                        DiscKey[i] = pDiscKey->DiscKey[k * 5 + i] ^ m_KeyCheck[4 - i];
                    }
                    DiscKey[5] = 0;

                    CSSdisckey(DiscKey, g_PlayerKeys[j]);

                    BYTE Hash[6];
                    for (int i = 0; i < 5; i++) {
                        Hash[i] = pDiscKey->DiscKey[i] ^ m_KeyCheck[4 - i];
                    }
                    Hash[5] = 0;

                    CSSdisckey(Hash, DiscKey);

                    if (!memcmp(Hash, DiscKey, 6)) {
                        memcpy(m_DiscKey, DiscKey, 6);
                        j = g_nPlayerKeys;
                        fSuccess = true;
                        break;
                    }
                }
            }

            if (!fSuccess) {
                return E_FAIL;
            }
        }
        break;
        case AM_PROPERTY_DVDCOPY_DVD_KEY1: { // 2. auth: receive our drive-encrypted nonce word and decrypt it for verification
            AM_DVDCOPY_BUSKEY* pKey1 = (AM_DVDCOPY_BUSKEY*)pPropertyData;
            for (int i = 0; i < 5; i++) {
                m_Key[i] = pKey1->BusKey[4 - i];
            }

            m_varient = -1;

            for (int i = 31; i >= 0; i--) {
                CSSkey1(i, m_Challenge, m_KeyCheck);

                if (memcmp(m_KeyCheck, &m_Key[0], 5) == 0) {
                    m_varient = i;
                }
            }
        }
        break;
        case AM_PROPERTY_DVDCOPY_REGION:
            break;
        case AM_PROPERTY_DVDCOPY_SET_COPY_STATE:
            break;
        case AM_PROPERTY_DVDCOPY_TITLE_KEY: { // 6. receive the title key and decrypt it with the disc key
            AM_DVDCOPY_TITLEKEY* pTitleKey = (AM_DVDCOPY_TITLEKEY*)pPropertyData;
            for (int i = 0; i < 5; i++) {
                m_TitleKey[i] = pTitleKey->TitleKey[i] ^ m_KeyCheck[4 - i];
            }
            m_TitleKey[5] = 0;
            CSStitlekey(m_TitleKey, m_DiscKey);
        }
        break;
        default:
            return E_PROP_ID_UNSUPPORTED;
    }

    return S_OK;
}

STDMETHODIMP CDeCSSInputPin::Get(REFGUID PropSet, ULONG Id, LPVOID pInstanceData, ULONG InstanceLength, LPVOID pPropertyData, ULONG DataLength, ULONG* pBytesReturned)
{
    if (PropSet != AM_KSPROPSETID_CopyProt) {
        return E_NOTIMPL;
    }

    switch (Id) {
        case AM_PROPERTY_DVDCOPY_CHLG_KEY: { // 1. auth: send our nonce word
            AM_DVDCOPY_CHLGKEY* pChlgKey = (AM_DVDCOPY_CHLGKEY*)pPropertyData;
            for (BYTE i = 0; i < 10; i++) {
                pChlgKey->ChlgKey[i] = 9 - (m_Challenge[i] = i);
            }
            *pBytesReturned = sizeof(AM_DVDCOPY_CHLGKEY);
        }
        break;
        case AM_PROPERTY_DVDCOPY_DEC_KEY2: { // 4. auth: send back the encrypted drive nonce word to finish the authentication
            AM_DVDCOPY_BUSKEY* pKey2 = (AM_DVDCOPY_BUSKEY*)pPropertyData;
            for (int i = 0; i < 5; i++) {
                pKey2->BusKey[4 - i] = m_Key[5 + i];
            }
            *pBytesReturned = sizeof(AM_DVDCOPY_BUSKEY);
        }
        break;
        case AM_PROPERTY_DVDCOPY_REGION: {
            DVD_REGION* pRegion = (DVD_REGION*)pPropertyData;
            pRegion->RegionData = 0;
            pRegion->SystemRegion = 0;
            *pBytesReturned = sizeof(DVD_REGION);
        }
        break;
        case AM_PROPERTY_DVDCOPY_SET_COPY_STATE: {
            AM_DVDCOPY_SET_COPY_STATE* pState = (AM_DVDCOPY_SET_COPY_STATE*)pPropertyData;
            pState->DVDCopyState = AM_DVDCOPYSTATE_AUTHENTICATION_REQUIRED;
            *pBytesReturned = sizeof(AM_DVDCOPY_SET_COPY_STATE);
        }
        break;
        default:
            return E_PROP_ID_UNSUPPORTED;
    }

    return S_OK;
}

STDMETHODIMP CDeCSSInputPin::QuerySupported(REFGUID PropSet, ULONG Id, ULONG* pTypeSupport)
{
    if (PropSet != AM_KSPROPSETID_CopyProt) {
        return E_NOTIMPL;
    }

    switch (Id) {
        case AM_PROPERTY_COPY_MACROVISION:
            *pTypeSupport = KSPROPERTY_SUPPORT_SET;
            break;
        case AM_PROPERTY_DVDCOPY_CHLG_KEY:
            *pTypeSupport = KSPROPERTY_SUPPORT_GET | KSPROPERTY_SUPPORT_SET;
            break;
        case AM_PROPERTY_DVDCOPY_DEC_KEY2:
            *pTypeSupport = KSPROPERTY_SUPPORT_GET;
            break;
        case AM_PROPERTY_DVDCOPY_DISC_KEY:
            *pTypeSupport = KSPROPERTY_SUPPORT_SET;
            break;
        case AM_PROPERTY_DVDCOPY_DVD_KEY1:
            *pTypeSupport = KSPROPERTY_SUPPORT_SET;
            break;
        case AM_PROPERTY_DVDCOPY_REGION:
            *pTypeSupport = KSPROPERTY_SUPPORT_GET | KSPROPERTY_SUPPORT_SET;
            break;
        case AM_PROPERTY_DVDCOPY_SET_COPY_STATE:
            *pTypeSupport = KSPROPERTY_SUPPORT_GET | KSPROPERTY_SUPPORT_SET;
            break;
        case AM_PROPERTY_DVDCOPY_TITLE_KEY:
            *pTypeSupport = KSPROPERTY_SUPPORT_SET;
            break;
        default:
            return E_PROP_ID_UNSUPPORTED;
    }

    return S_OK;
}