MockDrmCryptoPlugin.h 6.8 KB

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