1 /*
2  * Copyright (C) 2022 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 #include <mediadrm/DrmStatus.h>
18 #include <mediadrm/IDrm.h>
19 #include <sys/random.h>
20 #include <map>
21 #include <mutex>
22 
23 #ifndef DRM_METRICS_LOGGER_H
24 #define DRM_METRICS_LOGGER_H
25 
26 namespace android {
27 
28 // Keep enums in sync with frameworks/proto_logging/stats/enums/media/drm/enums.proto
29 
30 enum {
31     ENUM_DRM_UNKNOWN = 0,
32     ENUM_DRM_NO_LICENSE = 1,
33     ENUM_DRM_LICENSE_EXPIRED = 2,
34     ENUM_DRM_RESOURCE_BUSY = 3,
35     ENUM_DRM_INSUFFICIENT_OUTPUT_PROTECTION = 4,
36     ENUM_DRM_SESSION_NOT_OPENED = 5,
37     ENUM_DRM_CANNOT_HANDLE = 6,
38     ENUM_DRM_INSUFFICIENT_SECURITY = 7,
39     ENUM_DRM_FRAME_TOO_LARGE = 8,
40     ENUM_DRM_SESSION_LOST_STATE = 9,
41     ENUM_DRM_CERTIFICATE_MALFORMED = 10,
42     ENUM_DRM_CERTIFICATE_MISSING = 11,
43     ENUM_DRM_CRYPTO_LIBRARY = 12,
44     ENUM_DRM_GENERIC_OEM = 13,
45     ENUM_DRM_GENERIC_PLUGIN = 14,
46     ENUM_DRM_INIT_DATA = 15,
47     ENUM_DRM_KEY_NOT_LOADED = 16,
48     ENUM_DRM_LICENSE_PARSE = 17,
49     ENUM_DRM_LICENSE_POLICY = 18,
50     ENUM_DRM_LICENSE_RELEASE = 19,
51     ENUM_DRM_LICENSE_REQUEST_REJECTED = 20,
52     ENUM_DRM_LICENSE_RESTORE = 21,
53     ENUM_DRM_LICENSE_STATE = 22,
54     ENUM_DRM_MEDIA_FRAMEWORK = 23,
55     ENUM_DRM_PROVISIONING_CERTIFICATE = 24,
56     ENUM_DRM_PROVISIONING_CONFIG = 25,
57     ENUM_DRM_PROVISIONING_PARSE = 26,
58     ENUM_DRM_PROVISIONING_REQUEST_REJECTED = 27,
59     ENUM_DRM_PROVISIONING_RETRY = 28,
60     ENUM_DRM_RESOURCE_CONTENTION = 29,
61     ENUM_DRM_SECURE_STOP_RELEASE = 30,
62     ENUM_DRM_STORAGE_READ = 31,
63     ENUM_DRM_STORAGE_WRITE = 32,
64     ENUM_DRM_ZERO_SUBSAMPLES = 33,
65     ENUM_DRM_INVALID_STATE = 34,
66     ENUM_BAD_VALUE = 35,
67     ENUM_DRM_NOT_PROVISIONED = 36,
68     ENUM_DRM_DEVICE_REVOKED = 37,
69     ENUM_DRM_DECRYPT = 38,
70     ENUM_DEAD_OBJECT = 39,
71 };
72 
73 enum {
74     JSecurityLevelUnknown = 0,
75     JSecurityLevelSwSecureCrypto = 1,
76     JSecurityLevelSwSecureDecode = 2,
77     JSecurityLevelHwSecureCrypto = 3,
78     JSecurityLevelHwSecureDecode = 4,
79     JSecurityLevelHwSecureAll = 5,
80     JSecurityLevelMax = 6,
81 };
82 
83 struct SessionContext {
84     std::string mNonce;
85     DrmPlugin::SecurityLevel mTargetSecurityLevel;
86     DrmPlugin::SecurityLevel mActualSecurityLevel;
87     std::string mVersion;
88 };
89 
90 class DrmMetricsLogger : public IDrm {
91   public:
92     DrmMetricsLogger(IDrmFrontend);
93 
94     virtual ~DrmMetricsLogger();
95 
96     virtual DrmStatus initCheck() const;
97 
98     virtual DrmStatus isCryptoSchemeSupported(const uint8_t uuid[IDRM_UUID_SIZE],
99                                               const String8& mimeType,
100                                               DrmPlugin::SecurityLevel securityLevel,
101                                               bool* result);
102 
103     virtual DrmStatus createPlugin(const uint8_t uuid[IDRM_UUID_SIZE],
104                                    const String8& appPackageName);
105 
106     virtual DrmStatus destroyPlugin();
107 
108     virtual DrmStatus openSession(DrmPlugin::SecurityLevel securityLevel,
109                                   Vector<uint8_t>& sessionId);
110 
111     virtual DrmStatus closeSession(Vector<uint8_t> const& sessionId);
112 
113     virtual DrmStatus getKeyRequest(Vector<uint8_t> const& sessionId,
114                                     Vector<uint8_t> const& initData, String8 const& mimeType,
115                                     DrmPlugin::KeyType keyType,
116                                     KeyedVector<String8, String8> const& optionalParameters,
117                                     Vector<uint8_t>& request, String8& defaultUrl,
118                                     DrmPlugin::KeyRequestType* keyRequestType);
119 
120     virtual DrmStatus provideKeyResponse(Vector<uint8_t> const& sessionId,
121                                          Vector<uint8_t> const& response,
122                                          Vector<uint8_t>& keySetId);
123 
124     virtual DrmStatus removeKeys(Vector<uint8_t> const& keySetId);
125 
126     virtual DrmStatus restoreKeys(Vector<uint8_t> const& sessionId,
127                                   Vector<uint8_t> const& keySetId);
128 
129     virtual DrmStatus queryKeyStatus(Vector<uint8_t> const& sessionId,
130                                      KeyedVector<String8, String8>& infoMap) const;
131 
132     virtual DrmStatus getProvisionRequest(String8 const& certType, String8 const& certAuthority,
133                                           Vector<uint8_t>& request, String8& defaultUrl);
134 
135     virtual DrmStatus provideProvisionResponse(Vector<uint8_t> const& response,
136                                                Vector<uint8_t>& certificate,
137                                                Vector<uint8_t>& wrappedKey);
138 
139     virtual DrmStatus getSecureStops(List<Vector<uint8_t>>& secureStops);
140     virtual DrmStatus getSecureStopIds(List<Vector<uint8_t>>& secureStopIds);
141     virtual DrmStatus getSecureStop(Vector<uint8_t> const& ssid, Vector<uint8_t>& secureStop);
142 
143     virtual DrmStatus releaseSecureStops(Vector<uint8_t> const& ssRelease);
144     virtual DrmStatus removeSecureStop(Vector<uint8_t> const& ssid);
145     virtual DrmStatus removeAllSecureStops();
146 
147     virtual DrmStatus getHdcpLevels(DrmPlugin::HdcpLevel* connectedLevel,
148                                     DrmPlugin::HdcpLevel* maxLevel) const;
149     virtual DrmStatus getNumberOfSessions(uint32_t* currentSessions, uint32_t* maxSessions) const;
150     virtual DrmStatus getSecurityLevel(Vector<uint8_t> const& sessionId,
151                                        DrmPlugin::SecurityLevel* level) const;
152 
153     virtual DrmStatus getOfflineLicenseKeySetIds(List<Vector<uint8_t>>& keySetIds) const;
154     virtual DrmStatus removeOfflineLicense(Vector<uint8_t> const& keySetId);
155     virtual DrmStatus getOfflineLicenseState(Vector<uint8_t> const& keySetId,
156                                              DrmPlugin::OfflineLicenseState* licenseState) const;
157 
158     virtual DrmStatus getPropertyString(String8 const& name, String8& value) const;
159     virtual DrmStatus getPropertyByteArray(String8 const& name, Vector<uint8_t>& value) const;
160     virtual DrmStatus setPropertyString(String8 const& name, String8 const& value) const;
161     virtual DrmStatus setPropertyByteArray(String8 const& name, Vector<uint8_t> const& value) const;
162 
163     virtual DrmStatus getMetrics(const sp<IDrmMetricsConsumer>& consumer);
164 
165     virtual DrmStatus setCipherAlgorithm(Vector<uint8_t> const& sessionId,
166                                          String8 const& algorithm);
167 
168     virtual DrmStatus setMacAlgorithm(Vector<uint8_t> const& sessionId, String8 const& algorithm);
169 
170     virtual DrmStatus encrypt(Vector<uint8_t> const& sessionId, Vector<uint8_t> const& keyId,
171                               Vector<uint8_t> const& input, Vector<uint8_t> const& iv,
172                               Vector<uint8_t>& output);
173 
174     virtual DrmStatus decrypt(Vector<uint8_t> const& sessionId, Vector<uint8_t> const& keyId,
175                               Vector<uint8_t> const& input, Vector<uint8_t> const& iv,
176                               Vector<uint8_t>& output);
177 
178     virtual DrmStatus sign(Vector<uint8_t> const& sessionId, Vector<uint8_t> const& keyId,
179                            Vector<uint8_t> const& message, Vector<uint8_t>& signature);
180 
181     virtual DrmStatus verify(Vector<uint8_t> const& sessionId, Vector<uint8_t> const& keyId,
182                              Vector<uint8_t> const& message, Vector<uint8_t> const& signature,
183                              bool& match);
184 
185     virtual DrmStatus signRSA(Vector<uint8_t> const& sessionId, String8 const& algorithm,
186                               Vector<uint8_t> const& message, Vector<uint8_t> const& wrappedKey,
187                               Vector<uint8_t>& signature);
188 
189     virtual DrmStatus setListener(const sp<IDrmClient>& listener);
190 
191     virtual DrmStatus requiresSecureDecoder(const char* mime, bool* required) const;
192 
193     virtual DrmStatus requiresSecureDecoder(const char* mime,
194                                             DrmPlugin::SecurityLevel securityLevel,
195                                             bool* required) const;
196 
197     virtual DrmStatus setPlaybackId(Vector<uint8_t> const& sessionId, const char* playbackId);
198 
199     virtual DrmStatus getLogMessages(Vector<drm::V1_4::LogMessage>& logs) const;
200 
201     virtual DrmStatus getSupportedSchemes(std::vector<uint8_t>& schemes) const;
202 
203     void reportMediaDrmCreated() const;
204 
205     void reportMediaDrmSessionOpened(const std::vector<uint8_t>& sessionId) const;
206 
207     void reportMediaDrmErrored(
208             const DrmStatus& error_code, const char* api,
209             const std::vector<uint8_t>& sessionId = std::vector<uint8_t>()) const;
210 
211     DrmStatus generateNonce(std::string* out, size_t size, const char* api);
212 
213   private:
214     static const size_t kNonceSize = 16;
215     static const std::map<std::array<int64_t, 2>, std::string> kUuidSchemeMap;
216     sp<IDrm> mImpl;
217     std::array<int64_t, 2> mUuid;
218     std::string mObjNonce;
219     std::string mScheme;
220     std::string mVersion;
221     std::map<std::vector<uint8_t>, SessionContext> mSessionMap;
222     mutable std::mutex mSessionMapMutex;
223     IDrmFrontend mFrontend;
224     DISALLOW_EVIL_CONSTRUCTORS(DrmMetricsLogger);
225 };
226 
227 }  // namespace android
228 
229 #endif  // DRM_METRICS_LOGGER_H