summaryrefslogtreecommitdiffstats
path: root/drm/mediadrm/plugins/clearkey/hidl/DeviceFiles.cpp
blob: 8fafce21c2cd99b764ec4fe05c169df96e04b47d (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
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine Master
// License Agreement.

#include <utils/Log.h>

#include <string>
#include <sys/stat.h>

#include "DeviceFiles.h"
#include "Utils.h"

#include <openssl/sha.h>

// Protobuf generated classes.
using android::hardware::drm::V1_1::clearkey::OfflineFile;
using android::hardware::drm::V1_1::clearkey::HashedFile;
using android::hardware::drm::V1_1::clearkey::License;
using android::hardware::drm::V1_1::clearkey::License_LicenseState_ACTIVE;
using android::hardware::drm::V1_1::clearkey::License_LicenseState_RELEASING;

namespace {
const char kLicenseFileNameExt[] = ".lic";

bool Hash(const std::string& data, std::string* hash) {
    if (!hash) return false;

    hash->resize(SHA256_DIGEST_LENGTH);

    const unsigned char* input = reinterpret_cast<const unsigned char*>(data.data());
    unsigned char* output = reinterpret_cast<unsigned char*>(&(*hash)[0]);
    SHA256(input, data.size(), output);
    return true;
}

}  // namespace

namespace android {
namespace hardware {
namespace drm {
namespace V1_1 {
namespace clearkey {

bool DeviceFiles::StoreLicense(
        const std::string& keySetId, LicenseState state,
        const std::string& licenseResponse) {

    OfflineFile file;
    file.set_type(OfflineFile::LICENSE);
    file.set_version(OfflineFile::VERSION_1);

    License* license = file.mutable_license();
    switch (state) {
        case kLicenseStateActive:
            license->set_state(License_LicenseState_ACTIVE);
            license->set_license(licenseResponse);
            break;
        case kLicenseStateReleasing:
            license->set_state(License_LicenseState_RELEASING);
            break;
        default:
            ALOGW("StoreLicense: Unknown license state: %u", state);
            return false;
    }

    std::string serializedFile;
    file.SerializeToString(&serializedFile);

    return StoreFileWithHash(keySetId + kLicenseFileNameExt, serializedFile);
}

bool DeviceFiles::StoreFileWithHash(const std::string& fileName,
        const std::string& serializedFile) {
    std::string hash;
    if (!Hash(serializedFile, &hash)) {
        ALOGE("StoreFileWithHash: Failed to compute hash");
        return false;
    }

    HashedFile hashFile;
    hashFile.set_file(serializedFile);
    hashFile.set_hash(hash);

    std::string serializedHashFile;
    hashFile.SerializeToString(&serializedHashFile);

    return StoreFileRaw(fileName, serializedHashFile);
}

bool DeviceFiles::StoreFileRaw(const std::string& fileName, const std::string& serializedHashFile) {
    MemoryFileSystem::MemoryFile memFile;
    memFile.setFileName(fileName);
    memFile.setContent(serializedHashFile);
    memFile.setFileSize(serializedHashFile.size());
    size_t len = mFileHandle.Write(fileName, memFile);

    if (len != static_cast<size_t>(serializedHashFile.size())) {
        ALOGE("StoreFileRaw: Failed to write %s", fileName.c_str());
        ALOGD("StoreFileRaw: expected=%zd, actual=%zu", serializedHashFile.size(), len);
        return false;
    }

    ALOGD("StoreFileRaw: wrote %zu bytes to %s", serializedHashFile.size(), fileName.c_str());
    return true;
}

bool DeviceFiles::RetrieveLicense(
    const std::string& keySetId, LicenseState* state, std::string* offlineLicense) {
    OfflineFile file;

    if (!RetrieveHashedFile(keySetId + kLicenseFileNameExt, &file)) {
        return false;
    }

    if (file.type() != OfflineFile::LICENSE) {
        ALOGE("RetrieveLicense: Invalid file type");
        return false;
    }

    if (file.version() != OfflineFile::VERSION_1) {
        ALOGE("RetrieveLicense: Invalid file version");
        return false;
    }

    if (!file.has_license()) {
        ALOGE("RetrieveLicense: License not present");
        return false;
    }

    License license = file.license();

    switch (license.state()) {
        case License_LicenseState_ACTIVE:
            *state = kLicenseStateActive;
            break;
        case License_LicenseState_RELEASING:
            *state = kLicenseStateReleasing;
            break;
        default:
            ALOGW("RetrieveLicense: Unrecognized license state: %u",
                    kLicenseStateUnknown);
            *state = kLicenseStateUnknown;
            break;
    }

    *offlineLicense = license.license();
    return true;
}

bool DeviceFiles::DeleteAllLicenses() {
    return mFileHandle.RemoveAllFiles();
}

bool DeviceFiles::LicenseExists(const std::string& keySetId) {
    return mFileHandle.FileExists(keySetId + kLicenseFileNameExt);
}

bool DeviceFiles::RetrieveHashedFile(const std::string& fileName, OfflineFile* deSerializedFile) {
    if (!deSerializedFile) {
        ALOGE("RetrieveHashedFile: invalid file parameter");
        return false;
    }

    if (!FileExists(fileName)) {
        ALOGE("RetrieveHashedFile: %s does not exist", fileName.c_str());
        return false;
    }

    ssize_t bytes = GetFileSize(fileName);
    if (bytes <= 0) {
        ALOGE("RetrieveHashedFile: invalid file size: %s", fileName.c_str());
        // Remove the corrupted file so the caller will not get the same error
        // when trying to access the file repeatedly, causing the system to stall.
        RemoveFile(fileName);
        return false;
    }

    std::string serializedHashFile;
    serializedHashFile.resize(bytes);
    bytes = mFileHandle.Read(fileName, &serializedHashFile);

    if (bytes != static_cast<ssize_t>(serializedHashFile.size())) {
        ALOGE("RetrieveHashedFile: Failed to read from %s", fileName.c_str());
        ALOGV("RetrieveHashedFile: expected: %zd, actual: %zd", serializedHashFile.size(), bytes);
        // Remove the corrupted file so the caller will not get the same error
        // when trying to access the file repeatedly, causing the system to stall.
        RemoveFile(fileName);
        return false;
    }

    ALOGV("RetrieveHashedFile: read %zd from %s", bytes, fileName.c_str());

    HashedFile hashFile;
    if (!hashFile.ParseFromString(serializedHashFile)) {
        ALOGE("RetrieveHashedFile: Unable to parse hash file");
        // Remove corrupt file.
        RemoveFile(fileName);
        return false;
    }

    std::string hash;
    if (!Hash(hashFile.file(), &hash)) {
        ALOGE("RetrieveHashedFile: Hash computation failed");
        return false;
    }

    if (hash != hashFile.hash()) {
        ALOGE("RetrieveHashedFile: Hash mismatch");
        // Remove corrupt file.
        RemoveFile(fileName);
        return false;
    }

    if (!deSerializedFile->ParseFromString(hashFile.file())) {
        ALOGE("RetrieveHashedFile: Unable to parse file");
        // Remove corrupt file.
        RemoveFile(fileName);
        return false;
    }

    return true;
}

bool DeviceFiles::FileExists(const std::string& fileName) const {
    return mFileHandle.FileExists(fileName);
}

bool DeviceFiles::RemoveFile(const std::string& fileName) {
    return mFileHandle.RemoveFile(fileName);
}

ssize_t DeviceFiles::GetFileSize(const std::string& fileName) const {
    return mFileHandle.GetFileSize(fileName);
}

} // namespace clearkey
} // namespace V1_1
} // namespace drm
} // namespace hardware
} // namespace android