1 /*
2  * Copyright (C) 2013 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 DRM_H_
18 
19 #define DRM_H_
20 
21 #include "SharedLibrary.h"
22 
23 #include <media/IDrm.h>
24 #include <media/IDrmClient.h>
25 #include <utils/threads.h>
26 
27 namespace android {
28 
29 class DrmFactory;
30 class DrmPlugin;
31 struct DrmSessionClientInterface;
32 
33 struct Drm : public BnDrm,
34              public IBinder::DeathRecipient,
35              public DrmPluginListener {
36     Drm();
37     virtual ~Drm();
38 
39     virtual status_t initCheck() const;
40 
41     virtual bool isCryptoSchemeSupported(const uint8_t uuid[16], const String8 &mimeType);
42 
43     virtual status_t createPlugin(const uint8_t uuid[16]);
44 
45     virtual status_t destroyPlugin();
46 
47     virtual status_t openSession(Vector<uint8_t> &sessionId);
48 
49     virtual status_t closeSession(Vector<uint8_t> const &sessionId);
50 
51     virtual status_t
52         getKeyRequest(Vector<uint8_t> const &sessionId,
53                       Vector<uint8_t> const &initData,
54                       String8 const &mimeType, DrmPlugin::KeyType keyType,
55                       KeyedVector<String8, String8> const &optionalParameters,
56                       Vector<uint8_t> &request, String8 &defaultUrl,
57                       DrmPlugin::KeyRequestType *keyRequestType);
58 
59     virtual status_t provideKeyResponse(Vector<uint8_t> const &sessionId,
60                                         Vector<uint8_t> const &response,
61                                         Vector<uint8_t> &keySetId);
62 
63     virtual status_t removeKeys(Vector<uint8_t> const &keySetId);
64 
65     virtual status_t restoreKeys(Vector<uint8_t> const &sessionId,
66                                  Vector<uint8_t> const &keySetId);
67 
68     virtual status_t queryKeyStatus(Vector<uint8_t> const &sessionId,
69                                     KeyedVector<String8, String8> &infoMap) const;
70 
71     virtual status_t getProvisionRequest(String8 const &certType,
72                                          String8 const &certAuthority,
73                                          Vector<uint8_t> &request,
74                                          String8 &defaulUrl);
75 
76     virtual status_t provideProvisionResponse(Vector<uint8_t> const &response,
77                                               Vector<uint8_t> &certificate,
78                                               Vector<uint8_t> &wrappedKey);
79 
80     virtual status_t getSecureStops(List<Vector<uint8_t> > &secureStops);
81     virtual status_t getSecureStop(Vector<uint8_t> const &ssid, Vector<uint8_t> &secureStop);
82 
83     virtual status_t releaseSecureStops(Vector<uint8_t> const &ssRelease);
84     virtual status_t releaseAllSecureStops();
85 
86     virtual status_t getPropertyString(String8 const &name, String8 &value ) const;
87     virtual status_t getPropertyByteArray(String8 const &name,
88                                           Vector<uint8_t> &value ) const;
89     virtual status_t setPropertyString(String8 const &name, String8 const &value ) const;
90     virtual status_t setPropertyByteArray(String8 const &name,
91                                           Vector<uint8_t> const &value ) const;
92 
93     virtual status_t setCipherAlgorithm(Vector<uint8_t> const &sessionId,
94                                         String8 const &algorithm);
95 
96     virtual status_t setMacAlgorithm(Vector<uint8_t> const &sessionId,
97                                      String8 const &algorithm);
98 
99     virtual status_t encrypt(Vector<uint8_t> const &sessionId,
100                              Vector<uint8_t> const &keyId,
101                              Vector<uint8_t> const &input,
102                              Vector<uint8_t> const &iv,
103                              Vector<uint8_t> &output);
104 
105     virtual status_t decrypt(Vector<uint8_t> const &sessionId,
106                              Vector<uint8_t> const &keyId,
107                              Vector<uint8_t> const &input,
108                              Vector<uint8_t> const &iv,
109                              Vector<uint8_t> &output);
110 
111     virtual status_t sign(Vector<uint8_t> const &sessionId,
112                           Vector<uint8_t> const &keyId,
113                           Vector<uint8_t> const &message,
114                           Vector<uint8_t> &signature);
115 
116     virtual status_t verify(Vector<uint8_t> const &sessionId,
117                             Vector<uint8_t> const &keyId,
118                             Vector<uint8_t> const &message,
119                             Vector<uint8_t> const &signature,
120                             bool &match);
121 
122     virtual status_t signRSA(Vector<uint8_t> const &sessionId,
123                              String8 const &algorithm,
124                              Vector<uint8_t> const &message,
125                              Vector<uint8_t> const &wrappedKey,
126                              Vector<uint8_t> &signature);
127 
128     virtual status_t setListener(const sp<IDrmClient>& listener);
129 
130     virtual void sendEvent(DrmPlugin::EventType eventType, int extra,
131                            Vector<uint8_t> const *sessionId,
132                            Vector<uint8_t> const *data);
133 
134     virtual void sendExpirationUpdate(Vector<uint8_t> const *sessionId,
135                                       int64_t expiryTimeInMS);
136 
137     virtual void sendKeysChange(Vector<uint8_t> const *sessionId,
138                                 Vector<DrmPlugin::KeyStatus> const *keyStatusList,
139                                 bool hasNewUsableKey);
140 
141     virtual void binderDied(const wp<IBinder> &the_late_who);
142 
143 private:
144     static Mutex mLock;
145 
146     status_t mInitCheck;
147 
148     sp<DrmSessionClientInterface> mDrmSessionClient;
149 
150     sp<IDrmClient> mListener;
151     mutable Mutex mEventLock;
152     mutable Mutex mNotifyLock;
153 
154     sp<SharedLibrary> mLibrary;
155     DrmFactory *mFactory;
156     DrmPlugin *mPlugin;
157 
158     static KeyedVector<Vector<uint8_t>, String8> mUUIDToLibraryPathMap;
159     static KeyedVector<String8, wp<SharedLibrary> > mLibraryPathToOpenLibraryMap;
160     static Mutex mMapLock;
161 
162     void findFactoryForScheme(const uint8_t uuid[16]);
163     bool loadLibraryForScheme(const String8 &path, const uint8_t uuid[16]);
164     void closeFactory();
165     void writeByteArray(Parcel &obj, Vector<uint8_t> const *array);
166 
167     DISALLOW_EVIL_CONSTRUCTORS(Drm);
168 };
169 
170 }  // namespace android
171 
172 #endif  // CRYPTO_H_
173