• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "media/drm/DrmAPI.h"
20 #include "media/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],
37                                  DrmPlugin **plugin);
38     };
39 
40     class MockCryptoFactory : public CryptoFactory {
41     public:
MockCryptoFactory()42         MockCryptoFactory() {}
~MockCryptoFactory()43         virtual ~MockCryptoFactory() {}
44 
45         bool isCryptoSchemeSupported(const uint8_t uuid[16]) const;
46         status_t createPlugin(
47             const uint8_t uuid[16], const void *data, size_t size,
48             CryptoPlugin **plugin);
49     };
50 
51 
52 
53     class MockDrmPlugin : public DrmPlugin {
54     public:
MockDrmPlugin()55         MockDrmPlugin() {}
~MockDrmPlugin()56         virtual ~MockDrmPlugin() {}
57 
58         // from DrmPlugin
59         status_t openSession(Vector<uint8_t> &sessionId);
60         status_t closeSession(Vector<uint8_t> const &sessionId);
61 
62         status_t getKeyRequest(Vector<uint8_t> const &sessionId,
63                                Vector<uint8_t> const &initData,
64                                String8 const &mimeType, KeyType keyType,
65                                KeyedVector<String8, String8> const &optionalParameters,
66                                Vector<uint8_t> &request, String8 &defaultUrl,
67                                KeyRequestType *keyRequestType);
68 
69         status_t provideKeyResponse(Vector<uint8_t> const &sessionId,
70                                     Vector<uint8_t> const &response,
71                                     Vector<uint8_t> &keySetId);
72 
73         status_t removeKeys(Vector<uint8_t> const &keySetId);
74 
75         status_t restoreKeys(Vector<uint8_t> const &sessionId,
76                              Vector<uint8_t> const &keySetId);
77 
78         status_t queryKeyStatus(Vector<uint8_t> const &sessionId,
79                                 KeyedVector<String8, String8> &infoMap) const;
80 
81         status_t getProvisionRequest(String8 const &certType,
82                                      String8 const &certAuthority,
83                                      Vector<uint8_t> &request,
84                                      String8 &defaultUrl);
85 
86         status_t provideProvisionResponse(Vector<uint8_t> const &response,
87                                           Vector<uint8_t> &certificate,
88                                           Vector<uint8_t> &wrappedKey);
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(const 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         static constexpr size_t DECRYPT_KEY_SIZE = 16;
162 
163         ssize_t decrypt(bool secure,
164             const uint8_t key[DECRYPT_KEY_SIZE],
165             const uint8_t iv[DECRYPT_KEY_SIZE], Mode mode,
166             const Pattern &pattern, const void *srcPtr,
167             const SubSample *subSamples, size_t numSubSamples,
168             void *dstPtr, AString *errorDetailMsg);
169     private:
170         String8 subSamplesToString(CryptoPlugin::SubSample const *subSamples, size_t numSubSamples) const;
171         String8 arrayToString(uint8_t const *array, size_t len) const;
172     };
173 };
174