1 | /*␊ |
2 | Copyright (c) 2021, Grégory Soutadé␊ |
3 | ␊ |
4 | All rights reserved.␊ |
5 | Redistribution and use in source and binary forms, with or without␊ |
6 | modification, are permitted provided that the following conditions are met:␊ |
7 | ␊ |
8 | * Redistributions of source code must retain the above copyright␊ |
9 | notice, this list of conditions and the following disclaimer.␊ |
10 | * Redistributions in binary form must reproduce the above copyright␊ |
11 | notice, this list of conditions and the following disclaimer in the␊ |
12 | documentation and/or other materials provided with the distribution.␊ |
13 | * Neither the name of the copyright holder nor the␊ |
14 | names of its contributors may be used to endorse or promote products␊ |
15 | derived from this software without specific prior written permission.␊ |
16 | ␊ |
17 | THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY␊ |
18 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED␊ |
19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE␊ |
20 | DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY␊ |
21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES␊ |
22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;␊ |
23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND␊ |
24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT␊ |
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS␊ |
26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.␊ |
27 | */␊ |
28 | ␊ |
29 | #ifndef _DRMPROCESSORCLIENTIMPL_H_␊ |
30 | #define _DRMPROCESSORCLIENTIMPL_H_␊ |
31 | ␊ |
32 | #include <string>␊ |
33 | ␊ |
34 | #if OPENSSL_VERSION_MAJOR >= 3␊ |
35 | #include <openssl/provider.h>␊ |
36 | #endif␊ |
37 | ␊ |
38 | #include <drmprocessorclient.h>␊ |
39 | ␊ |
40 | class DRMProcessorClientImpl : public gourou::DRMProcessorClient␊ |
41 | {␊ |
42 | public:␊ |
43 | DRMProcessorClientImpl();␊ |
44 | ~DRMProcessorClientImpl();␊ |
45 | ␊ |
46 | /* Digest interface */␊ |
47 | virtual void* createDigest(const std::string& digestName);␊ |
48 | virtual void digestUpdate(void* handler, unsigned char* data, unsigned int length);␊ |
49 | virtual void digestFinalize(void* handler,unsigned char* digestOut);␊ |
50 | virtual void digest(const std::string& digestName, unsigned char* data, unsigned int length, unsigned char* digestOut);␊ |
51 | ␊ |
52 | /* Random interface */␊ |
53 | virtual void randBytes(unsigned char* bytesOut, unsigned int length);␊ |
54 | ␊ |
55 | /* HTTP interface */␊ |
56 | virtual std::string sendHTTPRequest(const std::string& URL, const std::string& POSTData=std::string(""), const std::string& contentType=std::string(""), std::map<std::string, std::string>* responseHeaders=0, int fd=0, bool resume=false);␊ |
57 | ␊ |
58 | virtual void RSAPrivateEncrypt(const unsigned char* RSAKey, unsigned int RSAKeyLength,␊ |
59 | ␉␉␉␉ const RSA_KEY_TYPE keyType, const std::string& password,␊ |
60 | ␉␉␉␉ const unsigned char* data, unsigned dataLength,␊ |
61 | ␉␉␉␉ unsigned char* res);␊ |
62 | ␉␉␉ ␊ |
63 | virtual void RSAPrivateDecrypt(const unsigned char* RSAKey, unsigned int RSAKeyLength,␊ |
64 | ␉␉␉␉ const RSA_KEY_TYPE keyType, const std::string& password,␊ |
65 | ␉␉␉␉ const unsigned char* data, unsigned dataLength,␊ |
66 | ␉␉␉␉ unsigned char* res);␊ |
67 | ␊ |
68 | virtual void RSAPublicEncrypt(const unsigned char* RSAKey, unsigned int RSAKeyLength,␊ |
69 | ␉␉␉␉ const RSA_KEY_TYPE keyType,␊ |
70 | ␉␉␉␉ const unsigned char* data, unsigned dataLength,␊ |
71 | ␉␉␉␉ unsigned char* res);␊ |
72 | ␊ |
73 | virtual void* generateRSAKey(int keyLengthBits);␊ |
74 | virtual void destroyRSAHandler(void* handler);␊ |
75 | ␊ |
76 | virtual void extractRSAPublicKey(void* RSAKeyHandler, unsigned char** keyOut, unsigned int* keyOutLength);␊ |
77 | virtual void extractRSAPrivateKey(void* RSAKeyHandler, unsigned char** keyOut, unsigned int* keyOutLength);␊ |
78 | virtual void extractCertificate(const unsigned char* RSAKey, unsigned int RSAKeyLength,␊ |
79 | ␉␉␉␉ const RSA_KEY_TYPE keyType, const std::string& password,␊ |
80 | ␉␉␉␉ unsigned char** certOut, unsigned int* certOutLength);␊ |
81 | ␉␉␉␉ ␊ |
82 | /* Crypto interface */␊ |
83 | virtual void encrypt(CRYPTO_ALGO algo, CHAINING_MODE chaining,␊ |
84 | ␉␉␉ const unsigned char* key, unsigned int keyLength,␊ |
85 | ␉␉␉ const unsigned char* iv, unsigned int ivLength,␊ |
86 | ␉␉␉ const unsigned char* dataIn, unsigned int dataInLength,␊ |
87 | ␉␉␉ unsigned char* dataOut, unsigned int* dataOutLength);␊ |
88 | ␊ |
89 | virtual void* encryptInit(CRYPTO_ALGO algo, CHAINING_MODE chaining,␊ |
90 | ␉␉␉ const unsigned char* key, unsigned int keyLength,␊ |
91 | ␉␉␉ const unsigned char* iv=0, unsigned int ivLength=0);␊ |
92 | ␊ |
93 | ␊ |
94 | virtual void encryptUpdate(void* handler, const unsigned char* dataIn, unsigned int dataInLength,␊ |
95 | ␉␉␉␉ unsigned char* dataOut, unsigned int* dataOutLength);␊ |
96 | virtual void encryptFinalize(void* handler, unsigned char* dataOut, unsigned int* dataOutLength);␊ |
97 | ␊ |
98 | virtual void decrypt(CRYPTO_ALGO algo, CHAINING_MODE chaining,␊ |
99 | ␉␉␉ const unsigned char* key, unsigned int keyLength,␊ |
100 | ␉␉␉ const unsigned char* iv, unsigned int ivLength,␊ |
101 | ␉␉␉ const unsigned char* dataIn, unsigned int dataInLength,␊ |
102 | ␉␉␉ unsigned char* dataOut, unsigned int* dataOutLength);␊ |
103 | ␊ |
104 | virtual void* decryptInit(CRYPTO_ALGO algo, CHAINING_MODE chaining,␊ |
105 | ␉␉␉ const unsigned char* key, unsigned int keyLength,␊ |
106 | ␉␉␉ const unsigned char* iv=0, unsigned int ivLength=0);␊ |
107 | ␊ |
108 | virtual void decryptUpdate(void* handler, const unsigned char* dataIn, unsigned int dataInLength,␊ |
109 | ␉␉␉ unsigned char* dataOut, unsigned int* dataOutLength);␊ |
110 | virtual void decryptFinalize(void* handler, unsigned char* dataOut, unsigned int* dataOutLength);␊ |
111 | ␊ |
112 | /* ZIP Interface */␊ |
113 | virtual void* zipOpen(const std::string& path);␊ |
114 | ␊ |
115 | virtual void zipReadFile(void* handler, const std::string& path, gourou::ByteArray& result, bool decompress=true);␊ |
116 | ␊ |
117 | virtual void zipWriteFile(void* handler, const std::string& path, gourou::ByteArray& content);␊ |
118 | ␊ |
119 | virtual void zipDeleteFile(void* handler, const std::string& path);␊ |
120 | ␊ |
121 | virtual void zipClose(void* handler);␊ |
122 | ␊ |
123 | virtual void inflate(gourou::ByteArray& data, gourou::ByteArray& result,␊ |
124 | ␉␉␉ int wbits=-15);␊ |
125 | ␉␊ |
126 | virtual void deflate(gourou::ByteArray& data, gourou::ByteArray& result,␊ |
127 | ␉␉␉ int wbits=-15, int compressionLevel=8);␊ |
128 | ␊ |
129 | private:␊ |
130 | ␊ |
131 | void padWithPKCS1(unsigned char* out, unsigned int outLength,␊ |
132 | ␉␉ const unsigned char* in, unsigned int inLength);␊ |
133 | ␊ |
134 | #if OPENSSL_VERSION_MAJOR >= 3␊ |
135 | OSSL_PROVIDER *legacy, *deflt;␊ |
136 | #else␊ |
137 | void *legacy, *deflt;␊ |
138 | #endif␊ |
139 | };␊ |
140 | ␊ |
141 | #endif␊ |