summaryrefslogtreecommitdiffstats
path: root/drm/mediacas/plugins/clearkey/ecm.cpp
blob: b3b521807ea32cf2b7ef68fcbc41c997470aedcb (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
/*
 * Copyright (C) 2017 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 "ecm"

#include <inttypes.h>

#include "ecm.h"
#include "ecm_generator.h"
#include "protos/license_protos.pb.h"

#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/MediaErrors.h>
#include <utils/Log.h>

namespace android {
namespace clearkeycas {

Ecm::Ecm()
    : asset_id_(0),
      asset_id_set_(false),
      system_id_(0),
      system_id_set_(false) {}

Ecm::~Ecm() {}

status_t Ecm::Parse(const sp<ABuffer>& buffer_as_binary) {
    if (buffer_as_binary->size() < kSizeBytes) {
        ALOGE("Short Ecm buffer: expected %zu, received %zu.",
                kSizeBytes, buffer_as_binary->size());
        return BAD_VALUE;
    }

    Asset asset;
    ecm_generator::DefaultEcmFields default_fields;
    status_t status = ecm_generator::DecodeECMClearFields(
            buffer_as_binary, &asset, &default_fields);
    if (status != OK) {
        ALOGE("DecodeECMClearFields failed with status %d", status);
        return status;
    }
    set_asset_id(asset.id());
    set_system_id(default_fields.system_id);

    // Save a copy of the buffer_as_binary for a future DecryptEcm call.
    set_buffer(buffer_as_binary);
    return OK;
}

status_t Ecm::Decrypt(
        const sp<ABuffer>& buffer_as_binary,
        const Asset& asset_from_emm) {
    // Invariant: asset has id. These are postconditions for Emm::Decrypt().
    CHECK(asset_from_emm.has_id());

    // DecodeEcm fills in |asset|.id() with the asset_id from the encoded Ecm.
    Asset asset(asset_from_emm);
    ecm_generator::DefaultEcmFields default_fields;
    sp<ABuffer> content_key;
    status_t status = ecm_generator::DecodeECM(
            buffer_as_binary, &asset, &content_key, &default_fields);
    if (status != OK) {
        ALOGE("DecodeECM failed with status %d", status);
        return status;
    }
    if (asset.id() != asset_from_emm.id()) {
        ALOGE("Asset_id from Emm (%" PRIu64 ") does not match asset_id from Ecm (%" PRIu64 ").",
                asset_from_emm.id(), asset.id());
        return CLEARKEY_STATUS_INVALID_PARAMETER;
    }
    set_asset_id(asset.id());
    set_system_id(default_fields.system_id);
    set_content_key(content_key);
    return status;
}

EcmDescriptor::EcmDescriptor() : ecm_set_(false), id_(0), id_set_(false) {}

EcmDescriptor::EcmDescriptor(uint16_t id, const Ecm& ecm)
: ecm_(ecm), ecm_set_(true), id_(id), id_set_(true) {}

EcmDescriptor::~EcmDescriptor() {}

status_t EcmDescriptor::Parse(const sp<ABuffer>& buffer_as_binary) {
    if (buffer_as_binary->size() < kSizeBytes) {
        ALOGE("Short EcmDescriptor buffer: expected %zu, received %zu.",
                kSizeBytes, buffer_as_binary->size());
        return BAD_VALUE;
    }
    sp<ABuffer> id_buffer = new ABuffer(buffer_as_binary->data(), kIdSizeBytes);
    const uint8_t *id_bytes = id_buffer->data();
    uint16_t id = (id_bytes[0] << 8) | id_bytes[1];
    set_id(id);

    // Unmarshall the contained Ecm.
    sp<ABuffer> ecm_buffer = new ABuffer(
            buffer_as_binary->data() + kIdSizeBytes, Ecm::kSizeBytes);
    status_t status = mutable_ecm()->Parse(ecm_buffer);
    if (status != OK) {
        return status;
    }
    return OK;
}

EcmContainer::EcmContainer() : count_(0), count_set_(false) {}

EcmContainer::~EcmContainer() {}

status_t EcmContainer::Add(const EcmDescriptor& descriptor) {
    switch (count_) {
    case 0:
        descriptor_[0] = descriptor;
        count_ = 1;
        break;
    case 1:
        descriptor_[1] = descriptor;
        count_ = 2;
        break;
    case 2:
        descriptor_[0] = descriptor_[1];
        descriptor_[1] = descriptor;
        break;
    default:
        ALOGE("Bad state.");
        return INVALID_OPERATION;
    }
    count_set_ = true;
    return OK;
}

status_t EcmContainer::Parse(const sp<ABuffer>& buffer_as_binary) {
    // EcmContainer can contain 1 or 2 EcmDescriptors so this is a check for
    // minimum size.
    if (buffer_as_binary->size() < kMinimumSizeBytes) {
        ALOGE("Short EcmContainer buffer: expected >= %zu, received %zu.",
                kMinimumSizeBytes, buffer_as_binary->size());
        return BAD_VALUE;
    }

    sp<ABuffer> count_buffer = new ABuffer(
            buffer_as_binary->data(), kCountSizeBytes);
    const uint8_t *count_bytes = count_buffer->data();
    size_t count = (count_bytes[0] << 8) | count_bytes[1];
    // Check that count is a legal value.
    if (!CountLegal(count)) {
        ALOGE("Invalid descriptor count: expected %zu <= count <= %zu, received %zu.",
                kMinDescriptorCount, kMaxDescriptorCount, count);
        return ERROR_OUT_OF_RANGE;
    }
    // If needed, check that buffer_as_binary can hold 2 EcmDescriptors.
    if (count > kMinDescriptorCount) {
        size_t expected_bytes =
                kCountSizeBytes + (count * EcmDescriptor::kSizeBytes);
        if (buffer_as_binary->size() < expected_bytes) {
            ALOGE("Short EcmContainer buffer: expected %zu, received %zu.",
                    expected_bytes, buffer_as_binary->size());
            return BAD_VALUE;
        }
    }
    set_count(count);
    // Unmarshall the contained EcmDescriptors.
    size_t offset = kCountSizeBytes;
    for (size_t i = 0; i < count_; ++i) {
        sp<ABuffer> descriptor_buffer = new ABuffer(
                buffer_as_binary->data() + offset, EcmDescriptor::kSizeBytes);
        status_t status = mutable_descriptor(i)->Parse(descriptor_buffer);
        if (status != OK) {
            return status;
        }
        offset += EcmDescriptor::kSizeBytes;
    }
    return OK;
}

}  // namespace clearkeycas
}  // namespace android