summaryrefslogtreecommitdiffstats
path: root/media/libmediaplayerservice/Crypto.cpp
blob: 574ae7175ba007547858895322aa83e1977f876a (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
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "Crypto"
#include <utils/Log.h>

#include "Crypto.h"

#include <media/hardware/CryptoAPI.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/hexdump.h>
#include <media/stagefright/MediaErrors.h>

#include <dlfcn.h>

namespace android {

Crypto::Crypto()
    : mInitCheck(NO_INIT),
      mLibHandle(NULL),
      mFactory(NULL),
      mPlugin(NULL) {
    mInitCheck = init();
}

Crypto::~Crypto() {
    delete mPlugin;
    mPlugin = NULL;

    delete mFactory;
    mFactory = NULL;

    if (mLibHandle != NULL) {
        dlclose(mLibHandle);
        mLibHandle = NULL;
    }
}

status_t Crypto::initCheck() const {
    return mInitCheck;
}

status_t Crypto::init() {
    mLibHandle = dlopen("libdrmdecrypt.so", RTLD_NOW);

    if (mLibHandle == NULL) {
        ALOGE("Unable to locate libdrmdecrypt.so");

        return ERROR_UNSUPPORTED;
    }

    typedef CryptoFactory *(*CreateCryptoFactoryFunc)();
    CreateCryptoFactoryFunc createCryptoFactory =
        (CreateCryptoFactoryFunc)dlsym(mLibHandle, "createCryptoFactory");

    if (createCryptoFactory == NULL
            || ((mFactory = createCryptoFactory()) == NULL)) {
        if (createCryptoFactory == NULL) {
            ALOGE("Unable to find symbol 'createCryptoFactory'.");
        } else {
            ALOGE("createCryptoFactory() failed.");
        }

        dlclose(mLibHandle);
        mLibHandle = NULL;

        return ERROR_UNSUPPORTED;
    }

    return OK;
}

bool Crypto::isCryptoSchemeSupported(const uint8_t uuid[16]) const {
    Mutex::Autolock autoLock(mLock);

    if (mInitCheck != OK) {
        return false;
    }

    return mFactory->isCryptoSchemeSupported(uuid);
}

status_t Crypto::createPlugin(
        const uint8_t uuid[16], const void *data, size_t size) {
    Mutex::Autolock autoLock(mLock);

    if (mInitCheck != OK) {
        return mInitCheck;
    }

    if (mPlugin != NULL) {
        return -EINVAL;
    }

    return mFactory->createPlugin(uuid, data, size, &mPlugin);
}

status_t Crypto::destroyPlugin() {
    Mutex::Autolock autoLock(mLock);

    if (mInitCheck != OK) {
        return mInitCheck;
    }

    if (mPlugin == NULL) {
        return -EINVAL;
    }

    delete mPlugin;
    mPlugin = NULL;

    return OK;
}

bool Crypto::requiresSecureDecoderComponent(const char *mime) const {
    Mutex::Autolock autoLock(mLock);

    if (mInitCheck != OK) {
        return mInitCheck;
    }

    if (mPlugin == NULL) {
        return -EINVAL;
    }

    return mPlugin->requiresSecureDecoderComponent(mime);
}

status_t Crypto::decrypt(
        bool secure,
        const uint8_t key[16],
        const uint8_t iv[16],
        CryptoPlugin::Mode mode,
        const void *srcPtr,
        const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
        void *dstPtr) {
    Mutex::Autolock autoLock(mLock);

    if (mInitCheck != OK) {
        return mInitCheck;
    }

    if (mPlugin == NULL) {
        return -EINVAL;
    }

    return mPlugin->decrypt(
            secure, key, iv, mode, srcPtr, subSamples, numSubSamples, dstPtr);
}

}  // namespace android