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 #ifndef CLEAR_KEY_ECM_GENERATOR_H_
18 #define CLEAR_KEY_ECM_GENERATOR_H_
19 
20 #include <string>
21 
22 #include "protos/license_protos.pb.h"
23 
24 #include <media/stagefright/foundation/ABuffer.h>
25 #include <media/stagefright/MediaErrors.h>
26 
27 using namespace std;
28 
29 namespace android {
30 namespace clearkeycas {
31 enum {
32     CLEARKEY_STATUS_BASE = ERROR_CAS_VENDOR_MAX,
33     CLEARKEY_STATUS_INVALIDASSETID = CLEARKEY_STATUS_BASE - 1,
34     CLEARKEY_STATUS_INVALIDSYSTEMID = CLEARKEY_STATUS_BASE - 2,
35     CLEARKEY_STATUS_INVALID_PARAMETER = CLEARKEY_STATUS_BASE - 3,
36 };
37 class Organization;
38 
39 namespace ecm_generator {
40 
41 // Layout of the ECM
42 // ECM
43 //  0 -  3 : Old ECM version (deprecated)
44 //  4 -  7 : Clear lead (milliseconds)
45 //       8 : ECM Version
46 //  9 - 11 : System ID
47 // 12 - 15 : Asset ID
48 // 16 - 31 : Content Key (clear)
49 //
50 // The clear asset ID (bytes 12-15) is compared to the encrypted asset ID
51 // (bytes 48-51) as a consistency check.
52 
53 struct DefaultEcmFields {
54     uint32_t old_version;
55     uint32_t clear_lead;
56     uint32_t ecm_version;
57     uint32_t system_id;
58 };
59 
60 // Decodes a clear key ecm.
61 // The following fields are decoded from the clear fields portion of the ecm:
62 //   asset->id
63 //   default_fields->old_version
64 //   default_fields->clear_lead
65 //   default_fields->system_id
66 //   default_fields->ecm_version
67 //
68 // The following fields are decoded from the content key portion of the ecm:
69 //   content_key
70 //
71 // |asset|, |content_key|, |default_fields| are owned by caller and must not
72 // be NULL.
73 // Returns failure via ecm_generator::DecodeECMClearFields.
74 //
75 // Example usage:
76 //   Asset asset;
77 //   string content_key;
78 //   DefaultEcmFields default_fields;
79 //   // Get a clear key |ecm|.
80 //   status_t status = ecm_generator::DecodeECM(ecm, &asset, &content_key, &default_fields);
81 status_t DecodeECM(const sp<ABuffer>& ecm, Asset* asset,
82         sp<ABuffer> *content_key, DefaultEcmFields* default_fields);
83 
84 // Decodes the following fields from the clear fields portion of the ecm:
85 //   asset->id
86 //   default_fields->old_version
87 //   default_fields->clear_lead
88 //   default_fields->system_id
89 //   default_fields->ecm_version
90 //
91 // offset, asset and default_fields are owned by caller and must not be NULL.
92 // offset is updated to show the number of bytes consumed.
93 // Returns:
94 // - BAD_VALUE on short ECM, or
95 // - CLEARKEY_STATUS_INVALIDASSETID via ecm_generator::DecodeEcmClearFields if
96 //   asset_id is 0, or
97 // - CLEARKEY_STATUS_INVALIDSYSTEMID via ecm_generator::DecodeEcmClearFields if
98 //   system_id is 0.
99 //
100 // Example usage:
101 //   Asset asset;
102 //   DefaultEcmFields default_fields;
103 //   // Get a clear key ecm.
104 //   status_t status = ecm_generator::DecodeECMClearFields(ecm, &asset, &default_fields);
105 status_t DecodeECMClearFields(const sp<ABuffer>& ecm, Asset* asset,
106         DefaultEcmFields* default_fields);
107 
108 }  // namespace ecm_generator
109 }  // namespace clearkeycas
110 }  // namespace android
111 #endif  // CLEAR_KEY_ECM_GENERATOR_H_
112