Libgourou

Libgourou Git Source Tree

Root/include/drmprocessorclient.h

1/*
2 Copyright 2021 Grégory Soutadé
3
4 This file is part of libgourou.
5
6 libgourou is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 libgourou is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with libgourou. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef _DRMPROCESSORCLIENT_H_
21#define _DRMPROCESSORCLIENT_H_
22
23#include <string>
24#include <bytearray.h>
25
26namespace gourou
27{
28 /**
29 * @brief All fucntions that must be implemented by a client
30 * This allow libgourou to have only few external libraries dependencies
31 * and improve code portability
32 */
33
34 class DigestInterface
35 {
36 public:
37/**
38 * @brief Create a digest handler (for now only SHA1 is used)
39 *
40 * @param digestName Digest name to instanciate
41 */
42virtual void* createDigest(const std::string& digestName) = 0;
43
44/**
45 * @brief Update digest engine with new data
46 *
47 * @param handler Digest handler
48 * @param data Data to digest
49 * @param length Length of data
50 *
51 * @return OK/KO
52 */
53virtual int digestUpdate(void* handler, unsigned char* data, unsigned int length) = 0;
54
55/**
56 * @brief Finalize digest with remained buffered data and destroy handler
57 *
58 * @param handler Digest handler
59 * @param digestOut Digest result (buffer must be pre allocated with right size)
60 *
61 * @return OK/KO
62 */
63virtual int digestFinalize(void* handler, unsigned char* digestOut) = 0;
64
65/**
66 * @brief Global digest function
67 *
68 * @param digestName Digest name to instanciate
69 * @param data Data to digest
70 * @param length Length of data
71 * @param digestOut Digest result (buffer must be pre allocated with right size)
72 *
73 * @return OK/KO
74 */
75virtual int digest(const std::string& digestName, unsigned char* data, unsigned int length, unsigned char* digestOut) = 0;
76 };
77
78 class RandomInterface
79 {
80 public:
81/**
82 * @brief Generate random bytes
83 *
84 * @param bytesOut Buffer to fill with random bytes
85 * @param length Length of bytesOut
86 */
87virtual void randBytes(unsigned char* bytesOut, unsigned int length) = 0;
88 };
89
90 class HTTPInterface
91 {
92 public:
93
94/**
95 * @brief Send HTTP (GET or POST) request
96 *
97 * @param URL HTTP URL
98 * @param POSTData POST data if needed, if not set, a GET request is done
99 * @param contentType Optional content type of POST Data
100 * @param responseHeaders Optional Response headers of HTTP request
101 *
102 * @return data of HTTP response
103 */
104virtual 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) = 0;
105 };
106
107 class RSAInterface
108 {
109 public:
110enum RSA_KEY_TYPE {
111 RSA_KEY_PKCS12 = 0,
112 RSA_KEY_X509
113};
114
115/**
116 * @brief Encrypt data with RSA private key. Data is padded using PKCS1.5
117 *
118 * @param RSAKey RSA key in binary form
119 * @param RSAKeyLength RSA key length
120 * @param keyType Key type
121 * @param password Optional password for RSA PKCS12 certificate
122 * @param data Data to encrypt
123 * @param dataLength Data length
124 * @param res Encryption result (pre allocated buffer)
125 */
126virtual void RSAPrivateEncrypt(const unsigned char* RSAKey, unsigned int RSAKeyLength,
127 const RSA_KEY_TYPE keyType, const std::string& password,
128 const unsigned char* data, unsigned dataLength,
129 unsigned char* res) = 0;
130
131/**
132 * @brief Encrypt data with RSA public key. Data is padded using PKCS1.5
133 *
134 * @param RSAKey RSA key in binary form
135 * @param RSAKeyLength RSA key length
136 * @param keyType Key type
137 * @param password Optional password for RSA PKCS12 certificate
138 * @param data Data to encrypt
139 * @param dataLength Data length
140 * @param res Encryption result (pre allocated buffer)
141 */
142virtual void RSAPublicEncrypt(const unsigned char* RSAKey, unsigned int RSAKeyLength,
143 const RSA_KEY_TYPE keyType,
144 const unsigned char* data, unsigned dataLength,
145 unsigned char* res) = 0;
146
147/**
148 * @brief Generate RSA key. Expnonent is fixed (65537 / 0x10001)
149 *
150 * @param keyLengthBits Length of key (in bits) to generate
151 *
152 * @return generatedKey
153 */
154virtual void* generateRSAKey(int keyLengthBits) = 0;
155
156/**
157 * @brief Destroy key previously generated
158 *
159 * @param handler Key to destroy
160 */
161 virtual void destroyRSAHandler(void* handler) = 0;
162
163/**
164 * @brief Extract public key (big number) from RSA handler
165 *
166 * @param handler RSA handler (generated key)
167 * @param keyOut Pre allocated buffer (if *keyOut != 0). If *keyOut is 0, memory is internally allocated (must be freed)
168 * @param keyOutLength Length of result
169 */
170virtual void extractRSAPublicKey(void* handler, unsigned char** keyOut, unsigned int* keyOutLength) = 0;
171
172/**
173 * @brief Extract private key (big number) from RSA handler
174 *
175 * @param handler RSA handler (generated key)
176 * @param keyOut Pre allocated buffer (if *keyOut != 0). If *keyOut is 0, memory is internally allocated (must be freed)
177 * @param keyOutLength Length of result
178 */
179virtual void extractRSAPrivateKey(void* handler, unsigned char** keyOut, unsigned int* keyOutLength) = 0;
180
181/**
182 * @brief Extract certificate from PKCS12 blob
183 *
184 * @param RSAKey RSA key in binary form
185 * @param RSAKeyLength RSA key length
186 * @param keyType Key type
187 * @param password Optional password for RSA PKCS12 certificate
188 * @param certOut Result certificate
189 * @param certOutLength Result certificate length
190 */
191virtual void extractCertificate(const unsigned char* RSAKey, unsigned int RSAKeyLength,
192const RSA_KEY_TYPE keyType, const std::string& password,
193unsigned char** certOut, unsigned int* certOutLength) = 0;
194 };
195
196 class CryptoInterface
197 {
198 public:
199enum CHAINING_MODE {
200 CHAIN_ECB=0,
201 CHAIN_CBC
202};
203
204/**
205 * @brief Do AES encryption. If length of data is not multiple of 16, PKCS#5 padding is done
206 *
207 * @param chaining Chaining mode
208 * @param key AES key
209 * @param keyLength AES key length
210 * @param iv IV key
211 * @param ivLength IV key length
212 * @param dataIn Data to encrypt
213 * @param dataInLength Data length
214 * @param dataOut Encrypted data
215 * @param dataOutLength Length of encrypted data
216 */
217virtual void AESEncrypt(CHAINING_MODE chaining,
218const unsigned char* key, unsigned int keyLength,
219const unsigned char* iv, unsigned int ivLength,
220const unsigned char* dataIn, unsigned int dataInLength,
221unsigned char* dataOut, unsigned int* dataOutLength) = 0;
222
223/**
224 * @brief Init AES CBC encryption
225 *
226 * @param chaining Chaining mode
227 * @param key AES key
228 * @param keyLength AES key length
229 * @param iv IV key
230 * @param ivLength IV key length
231 *
232 * @return AES handler
233 */
234virtual void* AESEncryptInit(CHAINING_MODE chaining,
235 const unsigned char* key, unsigned int keyLength,
236 const unsigned char* iv=0, unsigned int ivLength=0) = 0;
237
238/**
239 * @brief Encrypt data
240 *
241 * @param handler AES handler
242 * @param dataIn Data to encrypt
243 * @param dataInLength Data length
244 * @param dataOut Encrypted data
245 * @param dataOutLength Length of encrypted data
246 */
247virtual void AESEncryptUpdate(void* handler, const unsigned char* dataIn, unsigned int dataInLength,
248 unsigned char* dataOut, unsigned int* dataOutLength) = 0;
249
250/**
251 * @brief Finalize AES encryption (pad and encrypt last block if needed)
252 * Destroy handler at the end
253 *
254 * @param handler AES handler
255 * @param dataOut Last block of encrypted data
256 * @param dataOutLength Length of encrypted data
257 */
258virtual void AESEncryptFinalize(void* handler, unsigned char* dataOut, unsigned int* dataOutLength) = 0;
259
260/**
261 * @brief Do AES decryption. If length of data is not multiple of 16, PKCS#5 padding is done
262 *
263 * @param chaining Chaining mode
264 * @param key AES key
265 * @param keyLength AES key length
266 * @param iv IV key
267 * @param ivLength IV key length
268 * @param dataIn Data to encrypt
269 * @param dataInLength Data length
270 * @param dataOut Encrypted data
271 * @param dataOutLength Length of encrypted data
272 */
273virtual void AESDecrypt(CHAINING_MODE chaining,
274const unsigned char* key, unsigned int keyLength,
275const unsigned char* iv, unsigned int ivLength,
276const unsigned char* dataIn, unsigned int dataInLength,
277unsigned char* dataOut, unsigned int* dataOutLength) = 0;
278
279/**
280 * @brief Init AES decryption
281 *
282 * @param chaining Chaining mode
283 * @param key AES key
284 * @param keyLength AES key length
285 * @param iv IV key
286 * @param ivLength IV key length
287 *
288 * @return AES handler
289 */
290virtual void* AESDecryptInit(CHAINING_MODE chaining,
291 const unsigned char* key, unsigned int keyLength,
292 const unsigned char* iv=0, unsigned int ivLength=0) = 0;
293
294/**
295 * @brief Decrypt data
296 *
297 * @param handler AES handler
298 * @param dataIn Data to decrypt
299 * @param dataInLength Data length
300 * @param dataOut Decrypted data
301 * @param dataOutLength Length of decrypted data
302 */
303virtual void AESDecryptUpdate(void* handler, const unsigned char* dataIn, unsigned int dataInLength,
304 unsigned char* dataOut, unsigned int* dataOutLength) = 0;
305/**
306 * @brief Finalize AES decryption (decrypt last block and remove padding if it is set).
307 * Destroy handler at the end
308 *
309 * @param handler AES handler
310 * @param dataOut Last block decrypted data
311 * @param dataOutLength Length of decrypted data
312 */
313virtual void AESDecryptFinalize(void* handler, unsigned char* dataOut, unsigned int* dataOutLength) = 0;
314 };
315
316
317 class ZIPInterface
318 {
319 public:
320/**
321 * @brief Open a zip file and return an handler
322 *
323 * @param path Path of zip file
324 *
325 * @return ZIP file handler
326 */
327virtual void* zipOpen(const std::string& path) = 0;
328
329/**
330 * @brief Read zip internal file
331 *
332 * @param handler ZIP file handler
333 * @param path Internal path inside zip file
334 *
335 * @return File content
336 */
337virtual std::string zipReadFile(void* handler, const std::string& path) = 0;
338
339/**
340 * @brief Write zip internal file
341 *
342 * @param handler ZIP file handler
343 * @param path Internal path inside zip file
344 * @param content Internal file content
345 */
346virtual void zipWriteFile(void* handler, const std::string& path, const std::string& content) = 0;
347
348/**
349 * @brief Delete zip internal file
350 *
351 * @param handler ZIP file handler
352 * @param path Internal path inside zip file
353 */
354virtual void zipDeleteFile(void* handler, const std::string& path) = 0;
355
356/**
357 * @brief Close ZIP file handler
358 *
359 * @param handler ZIP file handler
360 */
361virtual void zipClose(void* handler) = 0;
362
363/**
364 * @brief Inflate algorithm
365 *
366 * @param data Data to inflate
367 * @param result Zipped data
368 * @param wbits Window bits value for libz
369 */
370virtual void inflate(std::string data, gourou::ByteArray& result,
371 int wbits=-15) = 0;
372
373/**
374 * @brief Deflate algorithm
375 *
376 * @param data Data to deflate
377 * @param result Unzipped data
378 * @param wbits Window bits value for libz
379 * @param compressionLevel Compression level for libz
380 */
381virtual void deflate(std::string data, gourou::ByteArray& result,
382 int wbits=-15, int compressionLevel=8) = 0;
383 };
384
385 class DRMProcessorClient: public DigestInterface, public RandomInterface, public HTTPInterface, \
386 public RSAInterface, public CryptoInterface, public ZIPInterface
387 {};
388}
389#endif

Archive Download this file