1791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang/* 2791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * Copyright (C) 2017 The Android Open Source Project 3791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * 4791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * Licensed under the Apache License, Version 2.0 (the "License"); 5791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * you may not use this file except in compliance with the License. 6791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * You may obtain a copy of the License at 7791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * 8791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * http://www.apache.org/licenses/LICENSE-2.0 9791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * 10791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * Unless required by applicable law or agreed to in writing, software 11791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * distributed under the License is distributed on an "AS IS" BASIS, 12791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * See the License for the specific language governing permissions and 14791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * limitations under the License. 15791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang */ 16791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang 17791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#ifndef CLEAR_KEY_ECM_GENERATOR_H_ 18791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#define CLEAR_KEY_ECM_GENERATOR_H_ 19791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang 20791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#include <string> 21791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang 22791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#include "protos/license_protos.pb.h" 23791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang 24791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#include <media/stagefright/foundation/ABuffer.h> 25791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#include <media/stagefright/MediaErrors.h> 26791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang 27791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangusing namespace std; 28791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang 29791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangnamespace android { 30791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangnamespace clearkeycas { 31791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangenum { 32791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang CLEARKEY_STATUS_BASE = ERROR_DRM_VENDOR_MAX, 33791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang CLEARKEY_STATUS_INVALIDASSETID = CLEARKEY_STATUS_BASE - 1, 34791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang CLEARKEY_STATUS_INVALIDSYSTEMID = CLEARKEY_STATUS_BASE - 2, 35791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang CLEARKEY_STATUS_INVALID_PARAMETER = CLEARKEY_STATUS_BASE - 3, 36791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang}; 37791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangclass Organization; 38791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang 39791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangnamespace ecm_generator { 40791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang 41791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// Layout of the ECM 42791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// ECM 43791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// 0 - 3 : Old ECM version (deprecated) 44791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// 4 - 7 : Clear lead (milliseconds) 45791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// 8 : ECM Version 46791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// 9 - 11 : System ID 47791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// 12 - 15 : Asset ID 48791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// 16 - 31 : Content Key (clear) 49791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// 50791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// The clear asset ID (bytes 12-15) is compared to the encrypted asset ID 51791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// (bytes 48-51) as a consistency check. 52791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang 53791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangstruct DefaultEcmFields { 54791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang uint32_t old_version; 55791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang uint32_t clear_lead; 56791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang uint32_t ecm_version; 57791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang uint32_t system_id; 58791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang}; 59791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang 60791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// Decodes a clear key ecm. 61791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// The following fields are decoded from the clear fields portion of the ecm: 62791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// asset->id 63791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// default_fields->old_version 64791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// default_fields->clear_lead 65791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// default_fields->system_id 66791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// default_fields->ecm_version 67791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// 68791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// The following fields are decoded from the content key portion of the ecm: 69791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// content_key 70791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// 71791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// |asset|, |content_key|, |default_fields| are owned by caller and must not 72791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// be NULL. 73791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// Returns failure via ecm_generator::DecodeECMClearFields. 74791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// 75791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// Example usage: 76791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// Asset asset; 77791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// string content_key; 78791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// DefaultEcmFields default_fields; 79791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// // Get a clear key |ecm|. 80791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// status_t status = ecm_generator::DecodeECM(ecm, &asset, &content_key, &default_fields); 81791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangstatus_t DecodeECM(const sp<ABuffer>& ecm, Asset* asset, 82791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang sp<ABuffer> *content_key, DefaultEcmFields* default_fields); 83791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang 84791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// Decodes the following fields from the clear fields portion of the ecm: 85791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// asset->id 86791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// default_fields->old_version 87791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// default_fields->clear_lead 88791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// default_fields->system_id 89791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// default_fields->ecm_version 90791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// 91791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// offset, asset and default_fields are owned by caller and must not be NULL. 92791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// offset is updated to show the number of bytes consumed. 93791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// Returns: 94791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// - BAD_VALUE on short ECM, or 95791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// - CLEARKEY_STATUS_INVALIDASSETID via ecm_generator::DecodeEcmClearFields if 96791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// asset_id is 0, or 97791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// - CLEARKEY_STATUS_INVALIDSYSTEMID via ecm_generator::DecodeEcmClearFields if 98791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// system_id is 0. 99791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// 100791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// Example usage: 101791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// Asset asset; 102791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// DefaultEcmFields default_fields; 103791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// // Get a clear key ecm. 104791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang// status_t status = ecm_generator::DecodeECMClearFields(ecm, &asset, &default_fields); 105791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangstatus_t DecodeECMClearFields(const sp<ABuffer>& ecm, Asset* asset, 106791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang DefaultEcmFields* default_fields); 107791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang 108791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang} // namespace ecm_generator 109791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang} // namespace clearkeycas 110791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang} // namespace android 111791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#endif // CLEAR_KEY_ECM_GENERATOR_H_ 112