• 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 "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 releaseSecureStops(Vector<uint8_t> const &ssRelease);
92 
93         status_t getPropertyString(String8 const &name, String8 &value ) const;
94         status_t getPropertyByteArray(String8 const &name,
95                                               Vector<uint8_t> &value ) const;
96 
97         status_t setPropertyString(String8 const &name,
98                                    String8 const &value );
99         status_t setPropertyByteArray(String8 const &name,
100                                       Vector<uint8_t> const &value );
101 
102         status_t setCipherAlgorithm(Vector<uint8_t> const &sessionId,
103                                     String8 const &algorithm);
104 
105         status_t setMacAlgorithm(Vector<uint8_t> const &sessionId,
106                                  String8 const &algorithm);
107 
108         status_t encrypt(Vector<uint8_t> const &sessionId,
109                          Vector<uint8_t> const &keyId,
110                          Vector<uint8_t> const &input,
111                          Vector<uint8_t> const &iv,
112                          Vector<uint8_t> &output);
113 
114         status_t decrypt(Vector<uint8_t> const &sessionId,
115                          Vector<uint8_t> const &keyId,
116                          Vector<uint8_t> const &input,
117                          Vector<uint8_t> const &iv,
118                          Vector<uint8_t> &output);
119 
120         status_t sign(Vector<uint8_t> const &sessionId,
121                       Vector<uint8_t> const &keyId,
122                       Vector<uint8_t> const &message,
123                       Vector<uint8_t> &signature);
124 
125         status_t verify(Vector<uint8_t> const &sessionId,
126                         Vector<uint8_t> const &keyId,
127                         Vector<uint8_t> const &message,
128                         Vector<uint8_t> const &signature,
129                         bool &match);
130 
131         status_t signRSA(Vector<uint8_t> const &sessionId,
132                          String8 const &algorithm,
133                          Vector<uint8_t> const &message,
134                          Vector<uint8_t> const &wrappedKey,
135                          Vector<uint8_t> &signature);
136 
137     private:
138         String8 vectorToString(Vector<uint8_t> const &vector) const;
139         String8 arrayToString(uint8_t const *array, size_t len) const;
140         String8 stringMapToString(KeyedVector<String8, String8> map) const;
141 
142         SortedVector<Vector<uint8_t> > mSessions;
143         SortedVector<Vector<uint8_t> > mKeySets;
144 
145         static const ssize_t kNotFound = -1;
146         ssize_t findSession(Vector<uint8_t> const &sessionId) const;
147         ssize_t findKeySet(Vector<uint8_t> const &keySetId) const;
148 
149         Mutex mLock;
150         KeyedVector<String8, String8> mStringProperties;
151         KeyedVector<String8, Vector<uint8_t> > mByteArrayProperties;
152     };
153 
154 
155     class MockCryptoPlugin : public CryptoPlugin {
156 
157         bool requiresSecureDecoderComponent(const char *mime) const;
158 
159         ssize_t decrypt(bool secure,
160             const uint8_t key[16], const uint8_t iv[16],
161             Mode mode, const void *srcPtr,
162             const SubSample *subSamples, size_t numSubSamples,
163             void *dstPtr, AString *errorDetailMsg);
164     private:
165         String8 subSamplesToString(CryptoPlugin::SubSample const *subSamples, size_t numSubSamples) const;
166         String8 arrayToString(uint8_t const *array, size_t len) const;
167     };
168 };
169