Fix changelog email address
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-core / crypto.h
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * Cryptographic Abstraction Layer
4  *
5  * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #ifndef __CRYPTO_H
21 #define __CRYPTO_H
22
23 #ifdef _WIN32
24 #include "tcp.h"
25 #endif
26
27 #include <openssl/ssl.h>
28 #include <openssl/err.h>
29 #include <openssl/rc4.h>
30 #include <openssl/md5.h>
31 #include <openssl/sha.h>
32 #include <openssl/hmac.h>
33 #include <openssl/bn.h>
34 #include <openssl/x509v3.h>
35 #include <openssl/rand.h>
36
37 #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x0090800f)
38 #define D2I_X509_CONST const
39 #else
40 #define D2I_X509_CONST
41 #endif
42
43 #define EXPONENT_MAX_SIZE                       4
44 #define MODULUS_MAX_SIZE                        256
45
46 #include <freerdp/freerdp.h>
47 #include <freerdp/utils/blob.h>
48 #include <freerdp/utils/memory.h>
49
50 struct crypto_sha1_struct
51 {
52         SHA_CTX sha_ctx;
53 };
54
55 struct crypto_md5_struct
56 {
57         MD5_CTX md5_ctx;
58 };
59
60 struct crypto_rc4_struct
61 {
62         RC4_KEY rc4_key;
63 };
64
65 struct crypto_des3_struct
66 {
67         EVP_CIPHER_CTX des3_ctx;
68 };
69
70 struct crypto_hmac_struct
71 {
72         HMAC_CTX hmac_ctx;
73 };
74
75 struct crypto_cert_struct
76 {
77         X509 * px509;
78 };
79
80 #define CRYPTO_SHA1_DIGEST_LENGTH       SHA_DIGEST_LENGTH
81 typedef struct crypto_sha1_struct* CryptoSha1;
82 CryptoSha1 crypto_sha1_init(void);
83 void crypto_sha1_update(CryptoSha1 sha1, const uint8* data, uint32 length);
84 void crypto_sha1_final(CryptoSha1 sha1, uint8* out_data);
85
86 #define CRYPTO_MD5_DIGEST_LENGTH        MD5_DIGEST_LENGTH
87 typedef struct crypto_md5_struct* CryptoMd5;
88 CryptoMd5 crypto_md5_init(void);
89 void crypto_md5_update(CryptoMd5 md5, const uint8* data, uint32 length);
90 void crypto_md5_final(CryptoMd5 md5, uint8* out_data);
91
92 typedef struct crypto_rc4_struct* CryptoRc4;
93 CryptoRc4 crypto_rc4_init(const uint8* key, uint32 length);
94 void crypto_rc4(CryptoRc4 rc4, uint32 length, const uint8* in_data, uint8* out_data);
95 void crypto_rc4_free(CryptoRc4 rc4);
96
97 typedef struct crypto_des3_struct* CryptoDes3;
98 CryptoDes3 crypto_des3_encrypt_init(const uint8* key, const uint8* ivec);
99 CryptoDes3 crypto_des3_decrypt_init(const uint8* key, const uint8* ivec);
100 void crypto_des3_encrypt(CryptoDes3 des3, uint32 length, const uint8 *in_data, uint8 *out_data);
101 void crypto_des3_decrypt(CryptoDes3 des3, uint32 length, const uint8 *in_data, uint8* out_data);
102 void crypto_des3_free(CryptoDes3 des3);
103
104 typedef struct crypto_hmac_struct* CryptoHmac;
105 CryptoHmac crypto_hmac_new(void);
106 void crypto_hmac_sha1_init(CryptoHmac hmac, const uint8 *data, uint32 length);
107 void crypto_hmac_update(CryptoHmac hmac, const uint8 *data, uint32 length);
108 void crypto_hmac_final(CryptoHmac hmac, uint8 *out_data, uint32 length);
109 void crypto_hmac_free(CryptoHmac hmac);
110
111 typedef struct crypto_cert_struct* CryptoCert;
112
113 #include "certificate.h"
114
115 CryptoCert crypto_cert_read(uint8* data, uint32 length);
116 char* crypto_cert_fingerprint(X509* xcert);
117 char* crypto_cert_subject(X509* xcert);
118 char* crypto_cert_subject_common_name(X509* xcert, int* length);
119 char** crypto_cert_subject_alt_name(X509* xcert, int* count, int** lengths);
120 char* crypto_cert_issuer(X509* xcert);
121 void crypto_cert_print_info(X509* xcert);
122 void crypto_cert_free(CryptoCert cert);
123
124 boolean x509_verify_certificate(CryptoCert cert, char* certificate_store_path);
125 rdpCertificateData* crypto_get_certificate_data(X509* xcert, char* hostname);
126 boolean crypto_cert_get_public_key(CryptoCert cert, rdpBlob* public_key);
127
128 #define TSSK_KEY_LENGTH 64
129 extern const uint8 tssk_modulus[];
130 extern const uint8 tssk_privateExponent[];
131 extern const uint8 tssk_exponent[];
132
133 void crypto_rsa_public_encrypt(const uint8* input, int length, uint32 key_length, const uint8* modulus, const uint8* exponent, uint8* output);
134 void crypto_rsa_public_decrypt(const uint8* input, int length, uint32 key_length, const uint8* modulus, const uint8* exponent, uint8* output);
135 void crypto_rsa_private_encrypt(const uint8* input, int length, uint32 key_length, const uint8* modulus, const uint8* private_exponent, uint8* output);
136 void crypto_rsa_private_decrypt(const uint8* input, int length, uint32 key_length, const uint8* modulus, const uint8* private_exponent, uint8* output);
137 void crypto_reverse(uint8* data, int length);
138 void crypto_nonce(uint8* nonce, int size);
139
140 #endif /* __CRYPTO_H */