Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-core / license.h
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * RDP Licensing
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 __LICENSE_H
21 #define __LICENSE_H
22
23 typedef struct rdp_license rdpLicense;
24
25 #include "rdp.h"
26 #include "crypto.h"
27 #include "certificate.h"
28
29 #include <freerdp/freerdp.h>
30 #include <freerdp/utils/debug.h>
31 #include <freerdp/utils/stream.h>
32
33 /* Licensing Packet Types */
34 #define LICENSE_REQUEST                         0x01
35 #define PLATFORM_CHALLENGE                      0x02
36 #define NEW_LICENSE                             0x03
37 #define UPGRADE_LICENSE                         0x04
38 #define LICENSE_INFO                            0x12
39 #define NEW_LICENSE_REQUEST                     0x13
40 #define PLATFORM_CHALLENGE_RESPONSE             0x15
41 #define ERROR_ALERT                             0xFF
42
43 #define LICENSE_PKT_CS_MASK     (LICENSE_INFO | NEW_LICENSE_REQUEST | PLATFORM_CHALLENGE_RESPONSE | ERROR_ALERT)
44 #define LICENSE_PKT_SC_MASK     (LICENSE_REQUEST | PLATFORM_CHALLENGE | NEW_LICENSE | UPGRADE_LICENSE | ERROR_ALERT)
45 #define LICENSE_PKT_MASK        (LICENSE_PKT_CS_MASK | LICENSE_PKT_SC_MASK)
46
47 #define LICENSE_PREAMBLE_LENGTH                 4
48 #define LICENSE_PACKET_HEADER_MAX_LENGTH        (RDP_PACKET_HEADER_MAX_LENGTH + RDP_SECURITY_HEADER_LENGTH + LICENSE_PREAMBLE_LENGTH)
49
50 /* Cryptographic Lengths */
51 #define CLIENT_RANDOM_LENGTH                    32
52 #define SERVER_RANDOM_LENGTH                    32
53 #define MASTER_SECRET_LENGTH                    48
54 #define PREMASTER_SECRET_LENGTH                 48
55 #define SESSION_KEY_BLOB_LENGTH                 48
56 #define MAC_SALT_KEY_LENGTH                     16
57 #define LICENSING_ENCRYPTION_KEY_LENGTH         16
58 #define HWID_PLATFORM_ID_LENGTH                 4
59 #define HWID_UNIQUE_DATA_LENGTH                 16
60 #define HWID_LENGTH                             20
61 #define LICENSING_PADDING_SIZE                  8
62
63 /* Licensing Preamble Flags */
64 #define PREAMBLE_VERSION_2_0                    0x02
65 #define PREAMBLE_VERSION_3_0                    0x03
66 #define LicenseProtocolVersionMask              0x0F
67 #define EXTENDED_ERROR_MSG_SUPPORTED            0x80
68
69 /* Licensing Binary Blob Types */
70 #define BB_ANY_BLOB                             0x0000
71 #define BB_DATA_BLOB                            0x0001
72 #define BB_RANDOM_BLOB                          0x0002
73 #define BB_CERTIFICATE_BLOB                     0x0003
74 #define BB_ERROR_BLOB                           0x0004
75 #define BB_ENCRYPTED_DATA_BLOB                  0x0009
76 #define BB_KEY_EXCHG_ALG_BLOB                   0x000D
77 #define BB_SCOPE_BLOB                           0x000E
78 #define BB_CLIENT_USER_NAME_BLOB                0x000F
79 #define BB_CLIENT_MACHINE_NAME_BLOB             0x0010
80
81 /* Key Exchange Algorithms */
82 #define KEY_EXCHANGE_ALG_RSA                    0x00000001
83
84 /* Licensing Error Codes */
85 #define ERR_INVALID_SERVER_CERTIFICATE          0x00000001
86 #define ERR_NO_LICENSE                          0x00000002
87 #define ERR_INVALID_MAC                         0x00000003
88 #define ERR_INVALID_SCOPE                       0x00000004
89 #define ERR_NO_LICENSE_SERVER                   0x00000006
90 #define STATUS_VALID_CLIENT                     0x00000007
91 #define ERR_INVALID_CLIENT                      0x00000008
92 #define ERR_INVALID_PRODUCT_ID                  0x0000000B
93 #define ERR_INVALID_MESSAGE_LENGTH              0x0000000C
94
95 /* Licensing State Transition Codes */
96 #define ST_TOTAL_ABORT                          0x00000001
97 #define ST_NO_TRANSITION                        0x00000002
98 #define ST_RESET_PHASE_TO_START                 0x00000003
99 #define ST_RESEND_LAST_MESSAGE                  0x00000004
100
101 typedef struct
102 {
103         uint32 dwVersion;
104         uint32 cbCompanyName;
105         uint8* pbCompanyName;
106         uint32 cbProductId;
107         uint8* pbProductId;
108 } PRODUCT_INFO;
109
110 typedef struct
111 {
112         uint16 type;
113         uint16 length;
114         uint8* data;
115 } LICENSE_BLOB;
116
117 typedef struct
118 {
119         uint32 count;
120         LICENSE_BLOB* array;
121 } SCOPE_LIST;
122
123 typedef enum
124 {
125         LICENSE_STATE_AWAIT,
126         LICENSE_STATE_PROCESS,
127         LICENSE_STATE_ABORTED,
128         LICENSE_STATE_COMPLETED
129 } LICENSE_STATE;
130
131 struct rdp_license
132 {
133         LICENSE_STATE state;
134         struct rdp_rdp* rdp;
135         struct rdp_certificate* certificate;
136         uint8 hwid[HWID_LENGTH];
137         uint8 modulus[MODULUS_MAX_SIZE];
138         uint8 exponent[EXPONENT_MAX_SIZE];
139         uint8 client_random[CLIENT_RANDOM_LENGTH];
140         uint8 server_random[SERVER_RANDOM_LENGTH];
141         uint8 master_secret[MASTER_SECRET_LENGTH];
142         uint8 premaster_secret[PREMASTER_SECRET_LENGTH];
143         uint8 session_key_blob[SESSION_KEY_BLOB_LENGTH];
144         uint8 mac_salt_key[MAC_SALT_KEY_LENGTH];
145         uint8 licensing_encryption_key[LICENSING_ENCRYPTION_KEY_LENGTH];
146         PRODUCT_INFO* product_info;
147         LICENSE_BLOB* error_info;
148         LICENSE_BLOB* key_exchange_list;
149         LICENSE_BLOB* server_certificate;
150         LICENSE_BLOB* client_user_name;
151         LICENSE_BLOB* client_machine_name;
152         LICENSE_BLOB* platform_challenge;
153         LICENSE_BLOB* encrypted_premaster_secret;
154         LICENSE_BLOB* encrypted_platform_challenge;
155         LICENSE_BLOB* encrypted_hwid;
156         SCOPE_LIST* scope_list;
157 };
158
159 boolean license_recv(rdpLicense* license, STREAM* s);
160 boolean license_send(rdpLicense* license, STREAM* s, uint8 type);
161 STREAM* license_send_stream_init(rdpLicense* license);
162
163 void license_generate_randoms(rdpLicense* license);
164 void license_generate_keys(rdpLicense* license);
165 void license_generate_hwid(rdpLicense* license);
166 void license_encrypt_premaster_secret(rdpLicense* license);
167 void license_decrypt_platform_challenge(rdpLicense* license);
168
169 PRODUCT_INFO* license_new_product_info();
170 void license_free_product_info(PRODUCT_INFO* productInfo);
171 void license_read_product_info(STREAM* s, PRODUCT_INFO* productInfo);
172
173 LICENSE_BLOB* license_new_binary_blob(uint16 type);
174 void license_free_binary_blob(LICENSE_BLOB* blob);
175 void license_read_binary_blob(STREAM* s, LICENSE_BLOB* blob);
176 void license_write_binary_blob(STREAM* s, LICENSE_BLOB* blob);
177
178 SCOPE_LIST* license_new_scope_list();
179 void license_free_scope_list(SCOPE_LIST* scopeList);
180 void license_read_scope_list(STREAM* s, SCOPE_LIST* scopeList);
181
182 void license_read_license_request_packet(rdpLicense* license, STREAM* s);
183 void license_read_platform_challenge_packet(rdpLicense* license, STREAM* s);
184 void license_read_new_license_packet(rdpLicense* license, STREAM* s);
185 void license_read_upgrade_license_packet(rdpLicense* license, STREAM* s);
186 void license_read_error_alert_packet(rdpLicense* license, STREAM* s);
187
188 void license_write_new_license_request_packet(rdpLicense* license, STREAM* s);
189 void license_send_new_license_request_packet(rdpLicense* license);
190
191 void license_write_platform_challenge_response_packet(rdpLicense* license, STREAM* s, uint8* mac_data);
192 void license_send_platform_challenge_response_packet(rdpLicense* license);
193
194 boolean license_send_valid_client_error_packet(rdpLicense* license);
195
196 rdpLicense* license_new(rdpRdp* rdp);
197 void license_free(rdpLicense* license);
198
199 #ifdef WITH_DEBUG_LICENSE
200 #define DEBUG_LICENSE(fmt, ...) DEBUG_CLASS(LICENSE, fmt, ## __VA_ARGS__)
201 #else
202 #define DEBUG_LICENSE(fmt, ...) DEBUG_NULL(fmt, ## __VA_ARGS__)
203 #endif
204
205 #endif /* __LICENSE_H */