aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/https/lgl/gc-gnulib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/https/lgl/gc-gnulib.c')
-rw-r--r--src/daemon/https/lgl/gc-gnulib.c779
1 files changed, 0 insertions, 779 deletions
diff --git a/src/daemon/https/lgl/gc-gnulib.c b/src/daemon/https/lgl/gc-gnulib.c
deleted file mode 100644
index d5478fa9..00000000
--- a/src/daemon/https/lgl/gc-gnulib.c
+++ /dev/null
@@ -1,779 +0,0 @@
1/* gc-gnulib.c --- Common gnulib internal crypto interface functions
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Simon Josefsson
3 *
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1, or (at your
7 * option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this file; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 */
20
21/* Note: This file is only built if GC uses internal functions. */
22
23#include "MHD_config.h"
24
25/* Get prototype. */
26#include "gc.h"
27
28#include <stdlib.h>
29#include <string.h>
30
31/* For randomize. */
32#ifdef GNULIB_GC_RANDOM
33# include <unistd.h>
34# include <sys/types.h>
35# include <sys/stat.h>
36# include <fcntl.h>
37# include <errno.h>
38#endif
39
40/* Hashes. */
41#ifdef GNULIB_GC_MD5
42# include "md5.h"
43#endif
44#ifdef GNULIB_GC_SHA1
45# include "sha1.h"
46#endif
47#if defined(GNULIB_GC_HMAC_MD5) || defined(GNULIB_GC_HMAC_SHA1)
48# include "hmac.h"
49#endif
50
51/* Ciphers. */
52#ifdef GNULIB_GC_ARCFOUR
53# include "arcfour.h"
54#endif
55#ifdef GNULIB_GC_ARCTWO
56# include "arctwo.h"
57#endif
58#ifdef GNULIB_GC_DES
59# include "des.h"
60#endif
61#ifdef GNULIB_GC_RIJNDAEL
62# include "rijndael-api-fst.h"
63#endif
64
65/* The results of open() in this file are not used with fchdir,
66 therefore save some unnecessary work in fchdir.c. */
67#undef open
68#undef close
69
70Gc_rc
71MHD_gc_init (void)
72{
73 return GC_OK;
74}
75
76void
77MHD_gc_done (void)
78{
79 return;
80}
81
82#ifdef GNULIB_GC_RANDOM
83
84/* Randomness. */
85
86static Gc_rc
87randomize (int level, char *data, size_t datalen)
88{
89 int fd;
90 const char *device;
91 size_t len = 0;
92 int rc;
93
94 switch (level)
95 {
96 case 0:
97 device = NAME_OF_NONCE_DEVICE;
98 break;
99
100 case 1:
101 device = NAME_OF_PSEUDO_RANDOM_DEVICE;
102 break;
103
104 default:
105 device = NAME_OF_RANDOM_DEVICE;
106 break;
107 }
108
109 if (strcmp (device, "no") == 0)
110 return GC_RANDOM_ERROR;
111
112 fd = open (device, O_RDONLY);
113 if (fd < 0)
114 return GC_RANDOM_ERROR;
115
116 do
117 {
118 ssize_t tmp;
119
120 tmp = read (fd, data, datalen);
121
122 if (tmp < 0)
123 {
124 int save_errno = errno;
125 close (fd);
126 errno = save_errno;
127 return GC_RANDOM_ERROR;
128 }
129
130 len += tmp;
131 }
132 while (len < datalen);
133
134 rc = close (fd);
135 if (rc < 0)
136 return GC_RANDOM_ERROR;
137
138 return GC_OK;
139}
140
141Gc_rc
142MHD_gc_nonce (char *data, size_t datalen)
143{
144 return randomize (0, data, datalen);
145}
146
147Gc_rc
148MHD_gc_pseudo_random (char *data, size_t datalen)
149{
150 return randomize (1, data, datalen);
151}
152
153#endif
154/* Ciphers. */
155
156typedef struct _MHD_gc_cipher_ctx
157{
158 Gc_cipher alg;
159 Gc_cipher_mode mode;
160#ifdef GNULIB_GC_ARCTWO
161 arctwo_context arctwoContext;
162 char arctwoIV[ARCTWO_BLOCK_SIZE];
163#endif
164#ifdef GNULIB_GC_ARCFOUR
165 arcfour_context arcfourContext;
166#endif
167#ifdef GNULIB_GC_DES
168 MHD_gl_des_ctx desContext;
169#endif
170#ifdef GNULIB_GC_RIJNDAEL
171 rijndaelKeyInstance aesEncKey;
172 rijndaelKeyInstance aesDecKey;
173 rijndaelCipherInstance aesContext;
174#endif
175} _MHD_gc_cipher_ctx;
176
177Gc_rc
178MHD_gc_cipher_open (Gc_cipher alg,
179 Gc_cipher_mode mode, MHD_gc_cipher_handle * outhandle)
180{
181 _MHD_gc_cipher_ctx *ctx;
182 Gc_rc rc = GC_OK;
183
184 ctx = calloc (sizeof (*ctx), 1);
185 if (!ctx)
186 return GC_MALLOC_ERROR;
187
188 ctx->alg = alg;
189 ctx->mode = mode;
190
191 switch (alg)
192 {
193#ifdef GNULIB_GC_ARCTWO
194 case GC_ARCTWO40:
195 switch (mode)
196 {
197 case GC_ECB:
198 case GC_CBC:
199 break;
200
201 default:
202 rc = GC_INVALID_CIPHER;
203 }
204 break;
205#endif
206
207#ifdef GNULIB_GC_ARCFOUR
208 case GC_ARCFOUR128:
209 case GC_ARCFOUR40:
210 switch (mode)
211 {
212 case GC_STREAM:
213 break;
214
215 default:
216 rc = GC_INVALID_CIPHER;
217 }
218 break;
219#endif
220
221#ifdef GNULIB_GC_DES
222 case GC_DES:
223 switch (mode)
224 {
225 case GC_ECB:
226 break;
227
228 default:
229 rc = GC_INVALID_CIPHER;
230 }
231 break;
232#endif
233
234#ifdef GNULIB_GC_RIJNDAEL
235 case GC_AES128:
236 case GC_AES192:
237 case GC_AES256:
238 switch (mode)
239 {
240 case GC_ECB:
241 case GC_CBC:
242 break;
243
244 default:
245 rc = GC_INVALID_CIPHER;
246 }
247 break;
248#endif
249
250 default:
251 rc = GC_INVALID_CIPHER;
252 }
253
254 if (rc == GC_OK)
255 *outhandle = ctx;
256 else
257 free (ctx);
258
259 return rc;
260}
261
262Gc_rc
263MHD_gc_cipher_setkey (MHD_gc_cipher_handle handle, size_t keylen,
264 const char *key)
265{
266 _MHD_gc_cipher_ctx *ctx = handle;
267
268 switch (ctx->alg)
269 {
270#ifdef GNULIB_GC_ARCTWO
271 case GC_ARCTWO40:
272 arctwo_setkey (&ctx->arctwoContext, keylen, key);
273 break;
274#endif
275
276#ifdef GNULIB_GC_ARCFOUR
277 case GC_ARCFOUR128:
278 case GC_ARCFOUR40:
279 arcfour_setkey (&ctx->arcfourContext, key, keylen);
280 break;
281#endif
282
283#ifdef GNULIB_GC_DES
284 case GC_DES:
285 if (keylen != 8)
286 return GC_INVALID_CIPHER;
287 MHD_gl_des_setkey (&ctx->desContext, key);
288 break;
289#endif
290
291#ifdef GNULIB_GC_RIJNDAEL
292 case GC_AES128:
293 case GC_AES192:
294 case GC_AES256:
295 {
296 rijndael_rc rc;
297 size_t i;
298 char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
299
300 for (i = 0; i < keylen; i++)
301 sprintf (&keyMaterial[2 * i], "%02x", key[i] & 0xFF);
302
303 rc = MHD_rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
304 keylen * 8, keyMaterial);
305 if (rc < 0)
306 return GC_INVALID_CIPHER;
307
308 rc = MHD_rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
309 keylen * 8, keyMaterial);
310 if (rc < 0)
311 return GC_INVALID_CIPHER;
312
313 rc =
314 MHD_MHD_rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB,
315 NULL);
316 if (rc < 0)
317 return GC_INVALID_CIPHER;
318 }
319 break;
320#endif
321
322 default:
323 return GC_INVALID_CIPHER;
324 }
325
326 return GC_OK;
327}
328
329Gc_rc
330MHD_gc_cipher_setiv (MHD_gc_cipher_handle handle, size_t ivlen,
331 const char *iv)
332{
333 _MHD_gc_cipher_ctx *ctx = handle;
334
335 switch (ctx->alg)
336 {
337#ifdef GNULIB_GC_ARCTWO
338 case GC_ARCTWO40:
339 if (ivlen != ARCTWO_BLOCK_SIZE)
340 return GC_INVALID_CIPHER;
341 memcpy (ctx->arctwoIV, iv, ivlen);
342 break;
343#endif
344
345#ifdef GNULIB_GC_RIJNDAEL
346 case GC_AES128:
347 case GC_AES192:
348 case GC_AES256:
349 switch (ctx->mode)
350 {
351 case GC_ECB:
352 /* Doesn't use IV. */
353 break;
354
355 case GC_CBC:
356 {
357 rijndael_rc rc;
358 size_t i;
359 char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
360
361 for (i = 0; i < ivlen; i++)
362 sprintf (&ivMaterial[2 * i], "%02x", iv[i] & 0xFF);
363
364 rc =
365 MHD_MHD_rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
366 ivMaterial);
367 if (rc < 0)
368 return GC_INVALID_CIPHER;
369 }
370 break;
371
372 default:
373 return GC_INVALID_CIPHER;
374 }
375 break;
376#endif
377
378 default:
379 return GC_INVALID_CIPHER;
380 }
381
382 return GC_OK;
383}
384
385Gc_rc
386MHD_gc_cipher_encrypt_inline (MHD_gc_cipher_handle handle, size_t len,
387 char *data)
388{
389 _MHD_gc_cipher_ctx *ctx = handle;
390
391 switch (ctx->alg)
392 {
393#ifdef GNULIB_GC_ARCTWO
394 case GC_ARCTWO40:
395 switch (ctx->mode)
396 {
397 case GC_ECB:
398 arctwo_encrypt (&ctx->arctwoContext, data, data, len);
399 break;
400
401 case GC_CBC:
402 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
403 data += ARCTWO_BLOCK_SIZE)
404 {
405 size_t i;
406 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
407 data[i] ^= ctx->arctwoIV[i];
408 arctwo_encrypt (&ctx->arctwoContext, data, data,
409 ARCTWO_BLOCK_SIZE);
410 memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE);
411 }
412 break;
413
414 default:
415 return GC_INVALID_CIPHER;
416 }
417 break;
418#endif
419
420#ifdef GNULIB_GC_ARCFOUR
421 case GC_ARCFOUR128:
422 case GC_ARCFOUR40:
423 arcfour_stream (&ctx->arcfourContext, data, data, len);
424 break;
425#endif
426
427#ifdef GNULIB_GC_DES
428 case GC_DES:
429 for (; len >= 8; len -= 8, data += 8)
430 MHD_gl_des_ecb_encrypt (&ctx->desContext, data, data);
431 break;
432#endif
433
434#ifdef GNULIB_GC_RIJNDAEL
435 case GC_AES128:
436 case GC_AES192:
437 case GC_AES256:
438 {
439 int nblocks;
440
441 nblocks = MHD_rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
442 data, 8 * len, data);
443 if (nblocks < 0)
444 return GC_INVALID_CIPHER;
445 }
446 break;
447#endif
448
449 default:
450 return GC_INVALID_CIPHER;
451 }
452
453 return GC_OK;
454}
455
456Gc_rc
457MHD_gc_cipher_decrypt_inline (MHD_gc_cipher_handle handle, size_t len,
458 char *data)
459{
460 _MHD_gc_cipher_ctx *ctx = handle;
461
462 switch (ctx->alg)
463 {
464#ifdef GNULIB_GC_ARCTWO
465 case GC_ARCTWO40:
466 switch (ctx->mode)
467 {
468 case GC_ECB:
469 arctwo_decrypt (&ctx->arctwoContext, data, data, len);
470 break;
471
472 case GC_CBC:
473 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
474 data += ARCTWO_BLOCK_SIZE)
475 {
476 char tmpIV[ARCTWO_BLOCK_SIZE];
477 size_t i;
478 memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE);
479 arctwo_decrypt (&ctx->arctwoContext, data, data,
480 ARCTWO_BLOCK_SIZE);
481 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
482 data[i] ^= ctx->arctwoIV[i];
483 memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE);
484 }
485 break;
486
487 default:
488 return GC_INVALID_CIPHER;
489 }
490 break;
491#endif
492
493#ifdef GNULIB_GC_ARCFOUR
494 case GC_ARCFOUR128:
495 case GC_ARCFOUR40:
496 arcfour_stream (&ctx->arcfourContext, data, data, len);
497 break;
498#endif
499
500#ifdef GNULIB_GC_DES
501 case GC_DES:
502 for (; len >= 8; len -= 8, data += 8)
503 MHD_gl_des_ecb_decrypt (&ctx->desContext, data, data);
504 break;
505#endif
506
507#ifdef GNULIB_GC_RIJNDAEL
508 case GC_AES128:
509 case GC_AES192:
510 case GC_AES256:
511 {
512 int nblocks;
513
514 nblocks = MHD_rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
515 data, 8 * len, data);
516 if (nblocks < 0)
517 return GC_INVALID_CIPHER;
518 }
519 break;
520#endif
521
522 default:
523 return GC_INVALID_CIPHER;
524 }
525
526 return GC_OK;
527}
528
529Gc_rc
530MHD_gc_cipher_close (MHD_gc_cipher_handle handle)
531{
532 _MHD_gc_cipher_ctx *ctx = handle;
533
534 if (ctx)
535 free (ctx);
536
537 return GC_OK;
538}
539
540/* Hashes. */
541
542#define MAX_DIGEST_SIZE 20
543
544typedef struct _MHD_gc_hash_ctx
545{
546 Gc_hash alg;
547 Gc_hash_mode mode;
548 char hash[MAX_DIGEST_SIZE];
549#ifdef GNULIB_GC_MD5
550 struct MHD_md5_ctx md5Context;
551#endif
552#ifdef GNULIB_GC_SHA1
553 struct MHD_sha1_ctx sha1Context;
554#endif
555} _MHD_gc_hash_ctx;
556
557Gc_rc
558MHD_gc_hash_open (Gc_hash hash, Gc_hash_mode mode,
559 MHD_gc_hash_handle * outhandle)
560{
561 _MHD_gc_hash_ctx *ctx;
562 Gc_rc rc = GC_OK;
563
564 ctx = calloc (sizeof (*ctx), 1);
565 if (!ctx)
566 return GC_MALLOC_ERROR;
567
568 ctx->alg = hash;
569 ctx->mode = mode;
570
571 switch (hash)
572 {
573#ifdef GNULIB_GC_MD5
574 case GC_MD5:
575 MHD_md5_init_ctx (&ctx->md5Context);
576 break;
577#endif
578
579#ifdef GNULIB_GC_SHA1
580 case GC_SHA1:
581 MHD_sha1_init_ctx (&ctx->sha1Context);
582 break;
583#endif
584
585 default:
586 rc = GC_INVALID_HASH;
587 break;
588 }
589
590 switch (mode)
591 {
592 case 0:
593 break;
594
595 default:
596 rc = GC_INVALID_HASH;
597 break;
598 }
599
600 if (rc == GC_OK)
601 *outhandle = ctx;
602 else
603 free (ctx);
604
605 return rc;
606}
607
608Gc_rc
609MHD_gc_hash_clone (MHD_gc_hash_handle handle, MHD_gc_hash_handle * outhandle)
610{
611 _MHD_gc_hash_ctx *in = handle;
612 _MHD_gc_hash_ctx *out;
613
614 *outhandle = out = calloc (sizeof (*out), 1);
615 if (!out)
616 return GC_MALLOC_ERROR;
617
618 memcpy (out, in, sizeof (*out));
619
620 return GC_OK;
621}
622
623size_t
624MHD_gc_hash_digest_length (Gc_hash hash)
625{
626 size_t len;
627
628 switch (hash)
629 {
630 case GC_MD2:
631 len = GC_MD2_DIGEST_SIZE;
632 break;
633
634 case GC_MD4:
635 len = GC_MD4_DIGEST_SIZE;
636 break;
637
638 case GC_MD5:
639 len = GC_MD5_DIGEST_SIZE;
640 break;
641
642 case GC_RMD160:
643 len = GC_RMD160_DIGEST_SIZE;
644 break;
645
646 case GC_SHA1:
647 len = GC_SHA1_DIGEST_SIZE;
648 break;
649
650 default:
651 return 0;
652 }
653
654 return len;
655}
656
657void
658MHD_gc_hash_write (MHD_gc_hash_handle handle, size_t len, const char *data)
659{
660 _MHD_gc_hash_ctx *ctx = handle;
661
662 switch (ctx->alg)
663 {
664#ifdef GNULIB_GC_MD5
665 case GC_MD5:
666 MHD_md5_process_bytes (data, len, &ctx->md5Context);
667 break;
668#endif
669
670#ifdef GNULIB_GC_SHA1
671 case GC_SHA1:
672 MHD_sha1_process_bytes (data, len, &ctx->sha1Context);
673 break;
674#endif
675
676 default:
677 break;
678 }
679}
680
681const char *
682MHD_gc_hash_read (MHD_gc_hash_handle handle)
683{
684 _MHD_gc_hash_ctx *ctx = handle;
685 const char *ret = NULL;
686
687 switch (ctx->alg)
688 {
689#ifdef GNULIB_GC_MD5
690 case GC_MD5:
691 MHD_md5_finish_ctx (&ctx->md5Context, ctx->hash);
692 ret = ctx->hash;
693 break;
694#endif
695
696#ifdef GNULIB_GC_SHA1
697 case GC_SHA1:
698 MHD_sha1_finish_ctx (&ctx->sha1Context, ctx->hash);
699 ret = ctx->hash;
700 break;
701#endif
702
703 default:
704 return NULL;
705 }
706
707 return ret;
708}
709
710void
711MHD_gc_hash_close (MHD_gc_hash_handle handle)
712{
713 _MHD_gc_hash_ctx *ctx = handle;
714
715 free (ctx);
716}
717
718Gc_rc
719MHD_gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
720{
721 switch (hash)
722 {
723#ifdef GNULIB_GC_MD5
724 case GC_MD5:
725 MHD_md5_buffer (in, inlen, resbuf);
726 break;
727#endif
728
729#ifdef GNULIB_GC_SHA1
730 case GC_SHA1:
731 MHD_sha1_buffer (in, inlen, resbuf);
732 break;
733#endif
734
735 default:
736 return GC_INVALID_HASH;
737 }
738
739 return GC_OK;
740}
741
742#ifdef GNULIB_GC_MD5
743Gc_rc
744MHD_gc_md5 (const void *in, size_t inlen, void *resbuf)
745{
746 MHD_md5_buffer (in, inlen, resbuf);
747 return GC_OK;
748}
749#endif
750
751#ifdef GNULIB_GC_SHA1
752Gc_rc
753MHD_gc_sha1 (const void *in, size_t inlen, void *resbuf)
754{
755 MHD_sha1_buffer (in, inlen, resbuf);
756 return GC_OK;
757}
758#endif
759
760#ifdef GNULIB_GC_HMAC_MD5
761Gc_rc
762MHD_gc_MHD_hmac_md5 (const void *key, size_t keylen,
763 const void *in, size_t inlen, char *resbuf)
764{
765 MHD_hmac_md5 (key, keylen, in, inlen, resbuf);
766 return GC_OK;
767}
768#endif
769
770#ifdef GNULIB_GC_HMAC_SHA1
771Gc_rc
772MHD_gc_MHD_hmac_sha1 (const void *key,
773 size_t keylen, const void *in, size_t inlen,
774 char *resbuf)
775{
776 MHD_hmac_sha1 (key, keylen, in, inlen, resbuf);
777 return GC_OK;
778}
779#endif