-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsec.c
212 lines (185 loc) · 5.02 KB
/
sec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* vnc2rdp: proxy for RDP client connect to VNC server
*
* Copyright 2014 Yiwei Li <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "log.h"
#include "sec.h"
static const uint8_t pub_exp[4] = {0x01, 0x00, 0x01, 0x00};
static const uint8_t modulus[64] = {
0x67, 0xab, 0x0e, 0x6a, 0x9f, 0xd6, 0x2b, 0xa3, 0x32, 0x2f,
0x41, 0xd1, 0xce, 0xee, 0x61, 0xc3, 0x76, 0x0b, 0x26, 0x11,
0x70, 0x48, 0x8a, 0x8d, 0x23, 0x81, 0x95, 0xa0, 0x39, 0xf7,
0x5b, 0xaa, 0x3e, 0xf1, 0xed, 0xb8, 0xc4, 0xee, 0xce, 0x5f,
0x6a, 0xf5, 0x43, 0xce, 0x5f, 0x60, 0xca, 0x6c, 0x06, 0x75,
0xae, 0xc0, 0xd6, 0xa4, 0x0c, 0x92, 0xa4, 0xc6, 0x75, 0xea,
0x64, 0xb2, 0x50, 0x5b
};
static const uint8_t modulus_zero_padding[8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const uint8_t signature[64] = {
0x6a, 0x41, 0xb1, 0x43, 0xcf, 0x47, 0x6f, 0xf1, 0xe6, 0xcc,
0xa1, 0x72, 0x97, 0xd9, 0xe1, 0x85, 0x15, 0xb3, 0xc2, 0x39,
0xa0, 0xa6, 0x26, 0x1a, 0xb6, 0x49, 0x01, 0xfa, 0xa6, 0xda,
0x60, 0xd7, 0x45, 0xf7, 0x2c, 0xee, 0xe4, 0x8e, 0x64, 0x2e,
0x37, 0x49, 0xf0, 0x4c, 0x94, 0x6f, 0x08, 0xf5, 0x63, 0x4c,
0x56, 0x29, 0x55, 0x5a, 0x63, 0x41, 0x2c, 0x20, 0x65, 0x95,
0x99, 0xb1, 0x15, 0x7c
};
static const uint8_t signature_zero_padding[8] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
int
v2r_sec_build_conn(v2r_sec_t *s, int client_fd)
{
if (v2r_mcs_build_conn(s->mcs, client_fd) == -1) {
goto fail;
}
/* TODO receive Client Security Exchange PDU */
return 0;
fail:
return -1;
}
v2r_sec_t *
v2r_sec_init(v2r_session_t *session)
{
v2r_sec_t *s = NULL;
s = (v2r_sec_t *)malloc(sizeof(v2r_sec_t));
if (s == NULL) {
goto fail;
}
memset(s, 0, sizeof(v2r_sec_t));
s->session = session;
s->mcs = v2r_mcs_init(session);
if (s->mcs == NULL) {
goto fail;
}
return s;
fail:
v2r_sec_destory(s);
return NULL;
}
void
v2r_sec_destory(v2r_sec_t *s)
{
if (s == NULL) {
return;
}
if (s->mcs != NULL) {
v2r_mcs_destory(s->mcs);
}
free(s);
}
int
v2r_sec_recv(v2r_sec_t *s, v2r_packet_t *p, uint16_t *sec_flags,
uint16_t *channel_id)
{
uint8_t choice = 0;
if (v2r_mcs_recv(s->mcs, p, &choice, channel_id) == -1) {
goto fail;
}
/* check if is send data request */
if (choice != MCS_SEND_DATA_REQUEST) {
goto fail;
}
/* parse security header */
V2R_PACKET_READ_UINT16_LE(p, *sec_flags);
/* skip flagsHi */
V2R_PACKET_SEEK(p, 2);
return 0;
fail:
return -1;
}
int
v2r_sec_send(v2r_sec_t *s, v2r_packet_t *p, uint16_t sec_flags,
uint16_t channel_id)
{
p->current = p->sec;
V2R_PACKET_WRITE_UINT16_LE(p, sec_flags);
V2R_PACKET_WRITE_UINT16_LE(p, 0);
return v2r_mcs_send(s->mcs, p, MCS_SEND_DATA_INDICATION, channel_id);
}
void
v2r_sec_init_packet(v2r_packet_t *p)
{
v2r_mcs_init_packet(p);
p->sec = p->current;
V2R_PACKET_SEEK(p, 4);
}
int
v2r_sec_generate_server_random(v2r_sec_t *s)
{
int i;
unsigned int seed;
struct timespec tp;
if (clock_gettime(CLOCK_MONOTONIC_COARSE, &tp) == -1) {
v2r_log_error("get current time error: %s", ERRMSG);
goto fail;
}
seed = (unsigned int)tp.tv_nsec;
for (i = 0; i < SERVER_RANDOM_LEN; i++) {
s->server_random[i] = rand_r(&seed);
}
return 0;
fail:
return -1;
}
int
v2r_sec_write_server_certificate(v2r_sec_t *s, v2r_packet_t *p)
{
/* Currently we only support server proprietary certificate */
uint32_t keylen, bitlen, datalen;
bitlen = 64 * 8;
keylen = bitlen / 8 + 8;
datalen = bitlen / 8 - 1;
/* dwVersion */
V2R_PACKET_WRITE_UINT32_LE(p, CERT_CHAIN_VERSION_1);
/* dwSigAlgId */
V2R_PACKET_WRITE_UINT32_LE(p, 0x00000001);
/* dwKeyAlgId */
V2R_PACKET_WRITE_UINT32_LE(p, 0x00000001);
/* wPublicKeyBlobType */
V2R_PACKET_WRITE_UINT16_LE(p, 0x0006);
/* wPublicKeyBlobLen */
V2R_PACKET_WRITE_UINT16_LE(p, 92);
/* magic */
V2R_PACKET_WRITE_UINT32_LE(p, 0x31415352);
/* keylen */
V2R_PACKET_WRITE_UINT32_LE(p, keylen);
/* bitlen */
V2R_PACKET_WRITE_UINT32_LE(p, bitlen);
/* datalen */
V2R_PACKET_WRITE_UINT32_LE(p, datalen);
/* pubExp */
V2R_PACKET_WRITE_N(p, pub_exp, sizeof(pub_exp));
/* modulus */
V2R_PACKET_WRITE_N(p, modulus, sizeof(modulus));
/* 8 bytes of zero padding */
V2R_PACKET_WRITE_N(p, modulus_zero_padding, sizeof(modulus_zero_padding));
/* wSignatureBlobType */
V2R_PACKET_WRITE_UINT16_LE(p, 0x0008);
/* wSignatureBlobLen */
V2R_PACKET_WRITE_UINT16_LE(p, 0x0048);
/* SignatureBlob */
V2R_PACKET_WRITE_N(p, signature, sizeof(signature));
/* 8 bytes of zero padding */
V2R_PACKET_WRITE_N(p, signature_zero_padding,
sizeof(signature_zero_padding));
return 0;
}