• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_generator"
19 #include "ecm_generator.h"
20 
21 #include <string.h>
22 #include <algorithm>
23 #include <endian.h>
24 
25 #include "protos/license_protos.pb.h"
26 
27 #include <media/stagefright/foundation/ADebug.h>
28 #include <media/stagefright/MediaErrors.h>
29 #include <openssl/aes.h>
30 #include <utils/Log.h>
31 
32 namespace android {
33 namespace clearkeycas {
34 
35 // These constants are internal to this module.
36 const uint16_t kEcmClearFieldsSize = 16;
37 const uint32_t kContentKeyByteSize = 16;
38 const uint16_t kTotalEcmSize =
39         kEcmClearFieldsSize + kContentKeyByteSize; // clear fields + clear key
40 
41 #define UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32_t *>(_p))
42 
Load32(const void * p)43 static uint32_t Load32(const void *p) {
44     return ntohl(UNALIGNED_LOAD32(p));
45 }
46 
LoadNext32(const uint8_t ** pptr)47 static uint32_t LoadNext32(const uint8_t** pptr) {
48     CHECK(pptr);
49     CHECK(*pptr);
50     const uint8_t* p = *pptr;
51     *pptr += sizeof(uint32_t);
52     return Load32(p);
53 }
54 
55 namespace ecm_generator {
56 
DecodeECM(const sp<ABuffer> & ecm,Asset * asset,sp<ABuffer> * content_key,DefaultEcmFields * default_fields)57 status_t DecodeECM(const sp<ABuffer>& ecm, Asset* asset,
58         sp<ABuffer> *content_key, DefaultEcmFields* default_fields) {
59     CHECK(asset);
60     CHECK(content_key);
61     CHECK(default_fields);
62 
63     status_t status = DecodeECMClearFields(ecm, asset, default_fields);
64     if (status != OK) {
65         return status;
66     }
67 
68     const uint8_t* ptr = ecm->data() + kEcmClearFieldsSize;
69     *content_key = new ABuffer(kContentKeyByteSize);
70     memcpy((*content_key)->data(), ptr, kContentKeyByteSize);
71 
72     return OK;
73 }
74 
DecodeECMClearFields(const sp<ABuffer> & ecm,Asset * asset,DefaultEcmFields * default_fields)75 status_t DecodeECMClearFields(const sp<ABuffer>& ecm, Asset* asset,
76         DefaultEcmFields* default_fields) {
77     CHECK(asset);
78     CHECK(default_fields);
79 
80     if (ecm->size() < kTotalEcmSize) {
81         ALOGE("Short ECM: expected_length=%d, actual_length=%zu",
82                 kTotalEcmSize, ecm->size());
83         return BAD_VALUE;
84     }
85     const uint8_t* ptr = ecm->data();
86     default_fields->old_version = LoadNext32(&ptr);
87     default_fields->clear_lead = LoadNext32(&ptr);
88     default_fields->system_id = LoadNext32(&ptr);
89     // The real ecm version is hidden in the system id.
90     default_fields->ecm_version = (default_fields->system_id >> 24) & 0xFF;
91     default_fields->system_id &= 0x00FFFFFF;
92     if (default_fields->system_id == 0) {
93         ALOGE("Ecm has invalid system_id 0");
94         return CLEARKEY_STATUS_INVALIDSYSTEMID;
95     }
96     asset->set_id(LoadNext32(&ptr));
97     if (asset->id() == 0) {
98         ALOGE("Ecm has invalid asset_id 0");
99         return CLEARKEY_STATUS_INVALIDASSETID;
100     }
101     return OK;
102 }
103 
104 }  // namespace ecm_generator
105 }  // namespace clearkeycas
106 }  // namespace android
107