Debian Codegraph @

src/canl.c

raw
1 #include "canl_locl.h"
2
3 #define tv_sub(a,b) {\
4 (a).tv_usec -= (b).tv_usec;\
5 (a).tv_sec -= (b).tv_sec;\
6 if ((a).tv_usec < 0) {\
7 (a).tv_sec--;\
8 (a).tv_usec += 1000000;\
9 }\
10 }
11
12 static struct canl_mech *mechs[] = {
13 &canl_mech_ssl,
14 };
15
16 static void io_destroy(glb_ctx *cc, io_handler *io);
17 static int init_io_content(glb_ctx *cc, io_handler *io);
18 static int try_connect(glb_ctx *glb_cc, io_handler *io_cc, char *addr,
19 int addrtype, int port, struct timeval *timeout);
20
21 canl_ctx canl_create_ctx()
22 {
23 glb_ctx *ctx = NULL;
24 int i;
25
26 /*create context*/
27 ctx = (glb_ctx *) calloc(1, sizeof(*ctx));
28 if (!ctx)
29 return NULL;
30
31 for (i = 0; i < sizeof(mechs)/sizeof(mechs[0]); i++)
32 mechs[i]->initialize(ctx); /*TODO every mech must have its own ctx*/
33
34 return ctx;
35 }
36
37 void canl_free_ctx(canl_ctx cc)
38 {
39 glb_ctx *ctx = (glb_ctx*) cc;
40 struct canl_mech *mech = find_mech(GSS_C_NO_OID);
41
42 if (!cc)
43 return;
44
45 /*delete content*/
46 if (ctx->err_msg) {
47 free(ctx->err_msg);
48 ctx->err_msg = NULL;
49 }
50 /*TODO delete ctx content for real*/
51 if (mech)
52 mech->free_ctx(ctx);
53
54 if (ctx->err_msg){
55 free(ctx->err_msg);
56 ctx->err_msg = NULL;
57 }
58 free(ctx);
59 }
60
61
62 canl_err_code
63 canl_create_io_handler(canl_ctx cc, canl_io_handler *io)
64 {
65 io_handler *new_io_h = NULL;
66 glb_ctx *g_cc = cc;
67 int err = 0;
68
69 if (!g_cc)
70 return EINVAL;
71 if (!io)
72 return set_error(g_cc, EINVAL, POSIX_ERROR, "IO handler not"
73 " initialized");
74
75 /*create io handler*/
76 new_io_h = (io_handler *) calloc(1, sizeof(*new_io_h));
77 if (!new_io_h)
78 return set_error(g_cc, ENOMEM, POSIX_ERROR, "Not enough memory");
79
80 /* allocate memory and initialize io content*/
81 if ((err = init_io_content(g_cc, new_io_h))){
82 free(new_io_h);
83 return err;
84 }
85
86 *io = new_io_h;
87 return 0;
88 }
89
90 static int init_io_content(glb_ctx *cc, io_handler *io)
91 {
92 io->oid = GSS_C_NO_OID;
93 io->sock = -1;
94 return 0;
95 }
96
97 canl_err_code
98 canl_io_connect(canl_ctx cc, canl_io_handler io, const char *host,
99 const char *service, int port, gss_OID_set auth_mechs,
100 int flags, canl_principal *peer, struct timeval *timeout)
101 {
102 int err = 0;
103 io_handler *io_cc = (io_handler*) io;
104 glb_ctx *glb_cc = (glb_ctx*) cc;
105 struct _asyn_result ar;
106 int i = 0, k;
107 int addr_types[] = {AF_INET, AF_INET6}; //TODO ip versions policy?
108 int ipver = AF_INET6;
109 int j = 0, done;
110 struct canl_mech *mech;
111 gss_OID oid;
112
113 memset(&ar, 0, sizeof(ar));
114
115 if (!glb_cc) {
116 return EINVAL;
117 }
118
119 if (!io_cc)
120 return set_error(glb_cc, EINVAL, POSIX_ERROR,
121 "IO handler not initialized");
122
123 done = 0;
124 for (k = 0; k < sizeof(addr_types)/sizeof(*addr_types); k++) {
125 ipver = addr_types[k];
126 if (ar.ent) {
127 free_hostent(ar.ent);
128 memset(&ar, 0, sizeof(ar));
129 }
130
131 ar.ent = (struct hostent *) calloc (1, sizeof(struct hostent));
132 if (ar.ent == NULL)
133 return set_error(cc, ENOMEM, POSIX_ERROR, "Not enough memory");
134
135 switch (err = canl_asyn_getservbyname(ipver, &ar, host, NULL)) {
136 case NETDB_SUCCESS:
137 err = 0;
138 break;
139 case TRY_AGAIN:
140 err = update_error(glb_cc, ETIMEDOUT, POSIX_ERROR,
141 " Timeout reached when connecting to (%s)", host);
142 goto end;
143 case NETDB_INTERNAL:
144 err = update_error(glb_cc, errno, POSIX_ERROR,
145 "Cannot resolve the server hostname (%s)", host);
146 continue;
147 default:
148 err = update_error(glb_cc, err, NETDB_ERROR,
149 "Cannot resolve the server hostname (%s)", host);
150 continue;
151 }
152
153 j = 0;
154 do {
155 if (auth_mechs == GSS_C_NO_OID_SET || auth_mechs->count == 0)
156 oid = GSS_C_NO_OID;
157 else
158 oid = &auth_mechs->elements[j];
159
160 mech = find_mech(oid);
161
162 err = 0;
163 for (i = 0; ar.ent->h_addr_list[i]; i++) {
164 void *ctx = NULL;
165
166 if (err == ETIMEDOUT)
167 goto end;
168
169 err = try_connect(glb_cc, io_cc, ar.ent->h_addr_list[i],
170 ar.ent->h_addrtype, port, timeout);//TODO timeout
171 if (err)
172 continue;
173
174 err = mech->client_init(glb_cc, &ctx);
175 if (err) {
176 canl_io_close(glb_cc, io_cc);
177 continue;
178 }
179
180 err = mech->connect(glb_cc, io_cc, ctx, timeout, host);
181 if (err) {
182 canl_io_close(glb_cc, io_cc);
183 mech->finish(glb_cc, ctx);
184 ctx = NULL;
185 continue;
186 }
187 io_cc->conn_ctx = ctx;
188 done = 1;
189 /* If peer != NULL then client certificate is mandatory*/
190 if (peer) {
191 err = mech->get_peer(glb_cc, io_cc, ctx, peer);
192 if (err)
193 goto end;
194 }
195
196 break;
197 }
198 if (err == ETIMEDOUT)
199 goto end;
200 j++;
201 } while (auth_mechs != GSS_C_NO_OID_SET && j < auth_mechs->count && !done);
202
203 free_hostent(ar.ent);
204 ar.ent = NULL;
205 if (done)
206 break;
207 }
208
209 if (!done) {
210 err = ECONNREFUSED;
211 goto end;
212 }
213
214 err = 0;
215
216 end:
217 if (err) /* XXX: rather invent own error */
218 err = update_error(glb_cc, ECONNREFUSED, POSIX_ERROR,
219 "Failed to make network connection to server %s", host);
220
221 if (ar.ent != NULL)
222 free_hostent(ar.ent);
223
224 return err;
225 }
226 /* try to connect to addr with port (both ipv4 and 6)
227 * return 0 when successful
228 * errno otherwise*/
229 /* XXX use set_error on errors and return a CANL return code */
230 static int try_connect(glb_ctx *glb_cc, io_handler *io_cc, char *addr,
231 int addrtype, int port, struct timeval *timeout)
232 {
233 struct sockaddr_storage a;
234 struct sockaddr_storage *p_a=&a;
235 socklen_t a_len;
236 socklen_t err_len;
237 int sock;
238 int sock_err;
239
240 struct timeval before,after,to;
241
242 struct sockaddr_in *p4 = (struct sockaddr_in *)p_a;
243 struct sockaddr_in6 *p6 = (struct sockaddr_in6 *)p_a;
244
245 memset(p_a, 0, sizeof *p_a);
246 p_a->ss_family = addrtype;
247 switch (addrtype) {
248 case AF_INET:
249 memcpy(&p4->sin_addr, addr, sizeof(struct in_addr));
250 p4->sin_port = htons(port);
251 a_len = sizeof (struct sockaddr_in);
252 break;
253 case AF_INET6:
254 memcpy(&p6->sin6_addr, addr, sizeof(struct in6_addr));
255 p6->sin6_port = htons(port);
256 a_len = sizeof (struct sockaddr_in6);
257 break;
258 default:
259 return update_error(glb_cc, EINVAL, POSIX_ERROR,
260 "Unsupported address type (%d)", addrtype);
261 break;
262 }
263
264 sock = socket(a.ss_family, SOCK_STREAM, 0);
265 if (sock == -1)
266 return update_error(glb_cc, errno, POSIX_ERROR,
267 "Failed to create network socket");
268
269 /* TODO from L&B; do we need it?
270 opt = 1;
271 setsockopt(sock,IPPROTO_TCP,TCP_NODELAY,&opt,sizeof opt);
272 */
273
274 if (timeout) {
275 int flags = fcntl(sock, F_GETFL, 0);
276 if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
277 return update_error(glb_cc, errno, POSIX_ERROR,
278 "Failed to set socket file status flags");
279 gettimeofday(&before,NULL);
280 }
281
282 if (connect(sock,(struct sockaddr *) &a, a_len) < 0) {
283 if (timeout && errno == EINPROGRESS) {
284 fd_set fds;
285 FD_ZERO(&fds);
286 FD_SET(sock, &fds);
287 /*TODO why don't we use timeout explicitly?*/
288 memcpy(&to, timeout, sizeof to);
289 gettimeofday(&before, NULL);
290 switch (select(sock+1, NULL, &fds, NULL, &to)) {
291 case -1: close(sock);
292 return update_error(glb_cc, errno, POSIX_ERROR,
293 "Connection error");
294 case 0: close(sock);
295 timeout->tv_sec = 0;
296 timeout->tv_usec = 0;
297 return update_error(glb_cc, errno, POSIX_ERROR,
298 "Connection timeout reached");
299 }
300 gettimeofday(&after, NULL);
301 tv_sub(after, before);
302 tv_sub(*timeout, after);
303
304 err_len = sizeof sock_err;
305 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &sock_err, &err_len)) {
306 close(sock);
307 return update_error(glb_cc, errno, POSIX_ERROR,
308 "Cannost get socket options");
309 }
310 if (sock_err) {
311 close(sock);
312 errno = sock_err;
313 return update_error(glb_cc, errno, POSIX_ERROR,
314 "Connection error");
315 }
316 }
317 else {
318 close(sock);
319 return update_error(glb_cc, errno, POSIX_ERROR,
320 "Connection error");
321 }
322 }
323
324 io_cc->sock = sock;
325 return 0;
326 }
327
328 /*TODO select + timeout, EINTR!!! */
329 canl_err_code
330 canl_io_accept(canl_ctx cc, canl_io_handler io, int new_fd,
331 struct sockaddr s_addr, int flags, canl_principal *peer,
332 struct timeval *timeout)
333 {
334 int err = 0;
335 io_handler *io_cc = (io_handler*) io;
336 glb_ctx *glb_cc = (glb_ctx*) cc;
337 struct canl_mech *mech = find_mech(GSS_C_NO_OID);
338 void *conn_ctx = NULL;
339
340 if (!glb_cc)
341 return EINVAL; /* XXX Should rather be a CANL error */
342
343 if (!io_cc)
344 return set_error(cc, EINVAL, POSIX_ERROR, "IO handler not initialized");
345
346 io_cc->sock = new_fd;
347
348 err = mech->server_init(glb_cc, &conn_ctx);
349 if (err)
350 goto end;
351
352 err = mech->accept(glb_cc, io_cc, conn_ctx, timeout);
353 if (err)
354 goto end;
355
356 /* If peer != NULL then client certificate is mandatory*/
357 if (peer) {
358 err = mech->get_peer(glb_cc, io_cc, conn_ctx, peer);
359 if (err)
360 goto end;
361 }
362
363 io_cc->conn_ctx = conn_ctx;
364 io_cc->oid = GSS_C_NO_OID;
365
366 err = 0;
367
368 end:
369 if (err) {
370 (io_cc)->sock = -1;
371 if (conn_ctx)
372 mech->finish(glb_cc, conn_ctx);
373 }
374
375 return err;
376 }
377
378 /* close connection, preserve some info for the future reuse */
379 canl_err_code
380 canl_io_close(canl_ctx cc, canl_io_handler io)
381 {
382 io_handler *io_cc = (io_handler*) io;
383 glb_ctx *glb_cc = (glb_ctx*) cc;
384 int err = 0;
385 canl_mech *mech;
386
387 /*check cc and io*/
388 if (!cc) {
389 return EINVAL; /* XXX Should rather be a CANL error */
390 }
391
392 if (!io)
393 return set_error(cc, EINVAL, POSIX_ERROR, "IO handler not initialized");
394
395 if (io_cc->conn_ctx) {
396 mech = find_mech(io_cc->oid);
397 mech->close(glb_cc, io, io_cc->conn_ctx);
398 /* XXX can it be safely reopened ?*/
399 }
400
401 if (io_cc->sock != -1) {
402 close (io_cc->sock);
403 io_cc->sock = -1;
404 }
405
406 return err;
407 }
408
409 static void io_destroy(glb_ctx *cc, io_handler *io)
410 {
411 io_handler *io_cc = (io_handler*) io;
412 canl_mech *mech;
413
414 if (io == NULL)
415 return;
416
417 if (io_cc->conn_ctx) {
418 mech = find_mech(io->oid);
419 mech->finish(cc, io_cc->conn_ctx);
420 io_cc->conn_ctx = NULL;
421 io_cc->oid = GSS_C_NO_OID;
422 }
423
424 return;
425 }
426
427
428 canl_err_code
429 canl_io_destroy(canl_ctx cc, canl_io_handler io)
430 {
431 int err = 0;
432 glb_ctx *glb_cc = (glb_ctx*) cc;
433 io_handler *io_cc = (io_handler*) io;
434 /*check cc and io*/
435
436 if (!glb_cc) {
437 return EINVAL; /* XXX Should rather be a CANL error */
438 }
439
440 if (!io_cc)
441 return set_error(glb_cc, EINVAL, POSIX_ERROR, "Invalid io handler");
442
443 canl_io_close(cc, io);
444
445 io_destroy(glb_cc, io_cc);
446 free (io_cc);
447
448 return err;
449 }
450
451 /* XXX: 0 returned returned by ssl_read() means error or EOF ? */
452 size_t canl_io_read(canl_ctx cc, canl_io_handler io, void *buffer, size_t size, struct timeval *timeout)
453 {
454 io_handler *io_cc = (io_handler*) io;
455 glb_ctx *glb_cc = (glb_ctx*) cc;
456 int b_recvd = 0;
457 struct canl_mech *mech;
458
459 if (!cc)
460 return -1;
461
462 if (!io) {
463 set_error(cc, EINVAL, POSIX_ERROR, "IO handler not initialized");
464 return -1;
465 }
466
467 if (io_cc->conn_ctx == NULL)
468 return set_error(cc, EINVAL, POSIX_ERROR, "Connection not secured");
469
470 if (!buffer || !size) {
471 set_error(cc, EINVAL, POSIX_ERROR, "No memory to write into");
472 return -1;
473 }
474
475 mech = find_mech(io_cc->oid);
476
477 b_recvd = mech->read(glb_cc, io_cc, io_cc->conn_ctx,
478 buffer, size, timeout);
479
480 return b_recvd;
481 }
482
483 size_t canl_io_write(canl_ctx cc, canl_io_handler io, void *buffer, size_t size, struct timeval *timeout)
484 {
485 io_handler *io_cc = (io_handler*) io;
486 glb_ctx *glb_cc = (glb_ctx*) cc;
487 int b_written = 0;
488 struct canl_mech *mech;
489
490 if (!cc)
491 return -1;
492
493 if (!io) {
494 set_error(cc, EINVAL, POSIX_ERROR, "IO handler not initialized");
495 return -1;
496 }
497
498 if (io_cc->conn_ctx == NULL)
499 return set_error(cc, EINVAL, POSIX_ERROR, "Connection not secured");
500
501 if (!buffer || !size) {
502 set_error(cc, EINVAL, POSIX_ERROR, "No memory to read from");
503 return -1;
504 }
505
506 mech = find_mech(io_cc->oid);
507
508 b_written = mech->write(glb_cc, io_cc, io_cc->conn_ctx,
509 buffer, size, timeout);
510
511 return b_written;
512 }
513
514 #if 0
515 int canl_set_ctx_own_cert(canl_ctx cc, canl_x509 cert,
516 canl_stack_of_x509 chain, canl_pkey key)
517 {
518 glb_ctx *glb_cc = (glb_ctx*) cc;
519 int err = 0;
520
521 if (!cc)
522 return EINVAL;
523 if(!cert)
524 return set_error(glb_cc, EINVAL, POSIX_ERROR, "invalid"
525 "parameter value");
526
527 err = do_set_ctx_own_cert(glb_cc, cert, chain, key);
528 if(err) {
529 update_error(glb_cc, "can't set cert or key to context");
530 }
531 return err;
532 }
533
534 //TODO callback and userdata process
535 int canl_set_ctx_own_cert_file(canl_ctx cc, char *cert, char *key,
536 canl_password_callback cb, void *userdata)
537 {
538 glb_ctx *glb_cc = (glb_ctx*) cc;
539 int err = 0;
540
541 if (!cc)
542 return EINVAL;
543 if(!cert ) {
544 set_error(glb_cc, EINVAL, POSIX_ERROR, "invalid parameter value");
545 return EINVAL;
546 }
547
548 err = do_set_ctx_own_cert_file(glb_cc, cert, key);
549 if(err) {
550 update_error(glb_cc, "can't set cert or key to context");
551 }
552 return err;
553 }
554 #endif
555
556 struct canl_mech *
557 find_mech(gss_OID oid)
558 {
559 /* XXX */
560 return &canl_mech_ssl;
561 }
562
563 canl_err_code
564 canl_princ_name(canl_ctx cc, const canl_principal princ, char **name)
565 {
566 struct _principal_int *p = (struct _principal_int *) princ;
567
568 if (cc == NULL)
569 return EINVAL;
570 if (princ == NULL)
571 return set_error(cc, EINVAL, POSIX_ERROR, "Principal not initialized");
572
573 if (name == NULL)
574 return set_error(cc, EINVAL, POSIX_ERROR, "invalid parameter value");
575
576 *name = strdup(p->name);
577 if (*name == NULL)
578 return set_error(cc, ENOMEM, POSIX_ERROR, "not enough memory");
579
580 return 0;
581 }
582
583 void
584 canl_princ_free(canl_ctx cc, canl_principal princ)
585 {
586 struct _principal_int *p = (struct _principal_int *) princ;
587
588 if (cc == NULL)
589 return;
590 if (princ == NULL)
591 return;
592
593 if (p->name)
594 free(p->name);
595 free(princ);
596
597 return;
598 }
599
600 canl_err_code CANL_CALLCONV
601 canl_ctx_set_pkcs11_lib(canl_ctx ctx, const char *lib)
602 {
603 setenv("PKCS11_LIB", lib, 1);
604 return 0;
605 }
606
607 canl_err_code CANL_CALLCONV
608 canl_ctx_set_pkcs11_init_args(canl_ctx ctx, const char *args)
609 {
610 setenv("PKCS11_INIT_ARGS", args, 1);
611 return 0;
612 }