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 #include <utils/Mutex.h>
18 
19 #include "drm/DrmAPI.h"
20 #include "hardware/CryptoAPI.h"
21 
22 extern "C" {
23       android::DrmFactory *createDrmFactory();
24       android::CryptoFactory *createCryptoFactory();
25 }
26 
27 namespace android {
28 
29     class MockDrmFactory : public DrmFactory {
30     public:
MockDrmFactory()31         MockDrmFactory() {}
~MockDrmFactory()32         virtual ~MockDrmFactory() {}
33 
34         bool isCryptoSchemeSupported(const uint8_t uuid[16]);
35         bool isContentTypeSupported(const String8 &mimeType);
36         status_t createDrmPlugin(const uint8_t uuid[16], DrmPlugin **plugin);
37     };
38 
39     class MockCryptoFactory : public CryptoFactory {
40     public:
MockCryptoFactory()41         MockCryptoFactory() {}
~MockCryptoFactory()42         virtual ~MockCryptoFactory() {}
43 
44         bool isCryptoSchemeSupported(const uint8_t uuid[16]) const;
45         status_t createPlugin(
46             const uint8_t uuid[16], const void *data, size_t size,
47             CryptoPlugin **plugin);
48     };
49 
50 
51 
52     class MockDrmPlugin : public DrmPlugin {
53     public:
MockDrmPlugin()54         MockDrmPlugin() {}
~MockDrmPlugin()55         virtual ~MockDrmPlugin() {}
56 
57         // from DrmPlugin
58         status_t openSession(Vector<uint8_t> &sessionId);
59         status_t closeSession(Vector<uint8_t> const &sessionId);
60 
61         status_t getKeyRequest(Vector<uint8_t> const &sessionId,
62                                Vector<uint8_t> const &initData,
63                                String8 const &mimeType, KeyType keyType,
64                                KeyedVector<String8, String8> const &optionalParameters,
65                                Vector<uint8_t> &request, String8 &defaultUrl);
66 
67         status_t provideKeyResponse(Vector<uint8_t> const &sessionId,
68                                     Vector<uint8_t> const &response,
69                                     Vector<uint8_t> &keySetId);
70 
71         status_t removeKeys(Vector<uint8_t> const &keySetId);
72 
73         status_t restoreKeys(Vector<uint8_t> const &sessionId,
74                              Vector<uint8_t> const &keySetId);
75 
76         status_t queryKeyStatus(Vector<uint8_t> const &sessionId,
77                                 KeyedVector<String8, String8> &infoMap) const;
78 
79         status_t getProvisionRequest(String8 const &certType,
80                                      String8 const &certAuthority,
81                                      Vector<uint8_t> &request,
82                                      String8 &defaultUrl);
83 
84         status_t provideProvisionResponse(Vector<uint8_t> const &response,
85                                           Vector<uint8_t> &certificate,
86                                           Vector<uint8_t> &wrappedKey);
87 
88         status_t unprovisionDevice();
89 
90         status_t getSecureStops(List<Vector<uint8_t> > &secureStops);
91         status_t getSecureStop(Vector<uint8_t> const &ssid, Vector<uint8_t> &secureStop);
92         status_t releaseSecureStops(Vector<uint8_t> const &ssRelease);
93         status_t releaseAllSecureStops();
94 
95         status_t getPropertyString(String8 const &name, String8 &value ) const;
96         status_t getPropertyByteArray(String8 const &name,
97                                               Vector<uint8_t> &value ) const;
98 
99         status_t setPropertyString(String8 const &name,
100                                    String8 const &value );
101         status_t setPropertyByteArray(String8 const &name,
102                                       Vector<uint8_t> const &value );
103 
104         status_t setCipherAlgorithm(Vector<uint8_t> const &sessionId,
105                                     String8 const &algorithm);
106 
107         status_t setMacAlgorithm(Vector<uint8_t> const &sessionId,
108                                  String8 const &algorithm);
109 
110         status_t encrypt(Vector<uint8_t> const &sessionId,
111                          Vector<uint8_t> const &keyId,
112                          Vector<uint8_t> const &input,
113                          Vector<uint8_t> const &iv,
114                          Vector<uint8_t> &output);
115 
116         status_t decrypt(Vector<uint8_t> const &sessionId,
117                          Vector<uint8_t> const &keyId,
118                          Vector<uint8_t> const &input,
119                          Vector<uint8_t> const &iv,
120                          Vector<uint8_t> &output);
121 
122         status_t sign(Vector<uint8_t> const &sessionId,
123                       Vector<uint8_t> const &keyId,
124                       Vector<uint8_t> const &message,
125                       Vector<uint8_t> &signature);
126 
127         status_t verify(Vector<uint8_t> const &sessionId,
128                         Vector<uint8_t> const &keyId,
129                         Vector<uint8_t> const &message,
130                         Vector<uint8_t> const &signature,
131                         bool &match);
132 
133         status_t signRSA(Vector<uint8_t> const &sessionId,
134                          String8 const &algorithm,
135                          Vector<uint8_t> const &message,
136                          Vector<uint8_t> const &wrappedKey,
137                          Vector<uint8_t> &signature);
138 
139     private:
140         String8 vectorToString(Vector<uint8_t> const &vector) const;
141         String8 arrayToString(uint8_t const *array, size_t len) const;
142         String8 stringMapToString(KeyedVector<String8, String8> map) const;
143 
144         SortedVector<Vector<uint8_t> > mSessions;
145         SortedVector<Vector<uint8_t> > mKeySets;
146 
147         static const ssize_t kNotFound = -1;
148         ssize_t findSession(Vector<uint8_t> const &sessionId) const;
149         ssize_t findKeySet(Vector<uint8_t> const &keySetId) const;
150 
151         Mutex mLock;
152         KeyedVector<String8, String8> mStringProperties;
153         KeyedVector<String8, Vector<uint8_t> > mByteArrayProperties;
154     };
155 
156 
157     class MockCryptoPlugin : public CryptoPlugin {
158 
159         bool requiresSecureDecoderComponent(const char *mime) const;
160 
161         ssize_t decrypt(bool secure,
162             const uint8_t key[16], const uint8_t iv[16],
163             Mode mode, const void *srcPtr,
164             const SubSample *subSamples, size_t numSubSamples,
165             void *dstPtr, AString *errorDetailMsg);
166     private:
167         String8 subSamplesToString(CryptoPlugin::SubSample const *subSamples, size_t numSubSamples) const;
168         String8 arrayToString(uint8_t const *array, size_t len) const;
169     };
170 };
171