1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "ecm"
19
20 #include <inttypes.h>
21
22 #include "ecm.h"
23 #include "ecm_generator.h"
24 #include "protos/license_protos.pb.h"
25
26 #include <media/stagefright/foundation/ADebug.h>
27 #include <media/stagefright/MediaErrors.h>
28 #include <utils/Log.h>
29
30 namespace android {
31 namespace clearkeycas {
32
Ecm()33 Ecm::Ecm()
34 : asset_id_(0),
35 asset_id_set_(false),
36 system_id_(0),
37 system_id_set_(false) {}
38
~Ecm()39 Ecm::~Ecm() {}
40
Parse(const sp<ABuffer> & buffer_as_binary)41 status_t Ecm::Parse(const sp<ABuffer>& buffer_as_binary) {
42 if (buffer_as_binary->size() < kSizeBytes) {
43 ALOGE("Short Ecm buffer: expected %zu, received %zu.",
44 kSizeBytes, buffer_as_binary->size());
45 return BAD_VALUE;
46 }
47
48 Asset asset;
49 ecm_generator::DefaultEcmFields default_fields;
50 status_t status = ecm_generator::DecodeECMClearFields(
51 buffer_as_binary, &asset, &default_fields);
52 if (status != OK) {
53 ALOGE("DecodeECMClearFields failed with status %d", status);
54 return status;
55 }
56 set_asset_id(asset.id());
57 set_system_id(default_fields.system_id);
58
59 // Save a copy of the buffer_as_binary for a future DecryptEcm call.
60 set_buffer(buffer_as_binary);
61 return OK;
62 }
63
Decrypt(const sp<ABuffer> & buffer_as_binary,const Asset & asset_from_emm)64 status_t Ecm::Decrypt(
65 const sp<ABuffer>& buffer_as_binary,
66 const Asset& asset_from_emm) {
67 // Invariant: asset has id. These are postconditions for Emm::Decrypt().
68 CHECK(asset_from_emm.has_id());
69
70 // DecodeEcm fills in |asset|.id() with the asset_id from the encoded Ecm.
71 Asset asset(asset_from_emm);
72 ecm_generator::DefaultEcmFields default_fields;
73 sp<ABuffer> content_key;
74 status_t status = ecm_generator::DecodeECM(
75 buffer_as_binary, &asset, &content_key, &default_fields);
76 if (status != OK) {
77 ALOGE("DecodeECM failed with status %d", status);
78 return status;
79 }
80 if (asset.id() != asset_from_emm.id()) {
81 ALOGE("Asset_id from Emm (%" PRIu64 ") does not match asset_id from Ecm (%" PRIu64 ").",
82 asset_from_emm.id(), asset.id());
83 return CLEARKEY_STATUS_INVALID_PARAMETER;
84 }
85 set_asset_id(asset.id());
86 set_system_id(default_fields.system_id);
87 set_content_key(content_key);
88 return status;
89 }
90
EcmDescriptor()91 EcmDescriptor::EcmDescriptor() : ecm_set_(false), id_(0), id_set_(false) {}
92
EcmDescriptor(uint16_t id,const Ecm & ecm)93 EcmDescriptor::EcmDescriptor(uint16_t id, const Ecm& ecm)
94 : ecm_(ecm), ecm_set_(true), id_(id), id_set_(true) {}
95
~EcmDescriptor()96 EcmDescriptor::~EcmDescriptor() {}
97
Parse(const sp<ABuffer> & buffer_as_binary)98 status_t EcmDescriptor::Parse(const sp<ABuffer>& buffer_as_binary) {
99 if (buffer_as_binary->size() < kSizeBytes) {
100 ALOGE("Short EcmDescriptor buffer: expected %zu, received %zu.",
101 kSizeBytes, buffer_as_binary->size());
102 return BAD_VALUE;
103 }
104 sp<ABuffer> id_buffer = new ABuffer(buffer_as_binary->data(), kIdSizeBytes);
105 const uint8_t *id_bytes = id_buffer->data();
106 uint16_t id = (id_bytes[0] << 8) | id_bytes[1];
107 set_id(id);
108
109 // Unmarshall the contained Ecm.
110 sp<ABuffer> ecm_buffer = new ABuffer(
111 buffer_as_binary->data() + kIdSizeBytes, Ecm::kSizeBytes);
112 status_t status = mutable_ecm()->Parse(ecm_buffer);
113 if (status != OK) {
114 return status;
115 }
116 return OK;
117 }
118
EcmContainer()119 EcmContainer::EcmContainer() : count_(0), count_set_(false) {}
120
~EcmContainer()121 EcmContainer::~EcmContainer() {}
122
Add(const EcmDescriptor & descriptor)123 status_t EcmContainer::Add(const EcmDescriptor& descriptor) {
124 switch (count_) {
125 case 0:
126 descriptor_[0] = descriptor;
127 count_ = 1;
128 break;
129 case 1:
130 descriptor_[1] = descriptor;
131 count_ = 2;
132 break;
133 case 2:
134 descriptor_[0] = descriptor_[1];
135 descriptor_[1] = descriptor;
136 break;
137 default:
138 ALOGE("Bad state.");
139 return INVALID_OPERATION;
140 }
141 count_set_ = true;
142 return OK;
143 }
144
Parse(const sp<ABuffer> & buffer_as_binary)145 status_t EcmContainer::Parse(const sp<ABuffer>& buffer_as_binary) {
146 // EcmContainer can contain 1 or 2 EcmDescriptors so this is a check for
147 // minimum size.
148 if (buffer_as_binary->size() < kMinimumSizeBytes) {
149 ALOGE("Short EcmContainer buffer: expected >= %zu, received %zu.",
150 kMinimumSizeBytes, buffer_as_binary->size());
151 return BAD_VALUE;
152 }
153
154 sp<ABuffer> count_buffer = new ABuffer(
155 buffer_as_binary->data(), kCountSizeBytes);
156 const uint8_t *count_bytes = count_buffer->data();
157 size_t count = (count_bytes[0] << 8) | count_bytes[1];
158 // Check that count is a legal value.
159 if (!CountLegal(count)) {
160 ALOGE("Invalid descriptor count: expected %zu <= count <= %zu, received %zu.",
161 kMinDescriptorCount, kMaxDescriptorCount, count);
162 return ERROR_OUT_OF_RANGE;
163 }
164 // If needed, check that buffer_as_binary can hold 2 EcmDescriptors.
165 if (count > kMinDescriptorCount) {
166 size_t expected_bytes =
167 kCountSizeBytes + (count * EcmDescriptor::kSizeBytes);
168 if (buffer_as_binary->size() < expected_bytes) {
169 ALOGE("Short EcmContainer buffer: expected %zu, received %zu.",
170 expected_bytes, buffer_as_binary->size());
171 return BAD_VALUE;
172 }
173 }
174 set_count(count);
175 // Unmarshall the contained EcmDescriptors.
176 size_t offset = kCountSizeBytes;
177 for (size_t i = 0; i < count_; ++i) {
178 sp<ABuffer> descriptor_buffer = new ABuffer(
179 buffer_as_binary->data() + offset, EcmDescriptor::kSizeBytes);
180 status_t status = mutable_descriptor(i)->Parse(descriptor_buffer);
181 if (status != OK) {
182 return status;
183 }
184 offset += EcmDescriptor::kSizeBytes;
185 }
186 return OK;
187 }
188
189 } // namespace clearkeycas
190 } // namespace android
191