~ubuntu-branches/ubuntu/saucy/openvpn/saucy-proposed

« back to all changes in this revision

Viewing changes to src/openvpn/crypto_openssl.c

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2013-05-24 17:42:45 UTC
  • mfrom: (1.1.19) (10.2.22 sid)
  • Revision ID: package-import@ubuntu.com-20130524174245-g9y6wlforycufqy5
Tags: 2.3.1-2ubuntu1
* Merge from Debian unstable. Remaining changes:
  - debian/openvpn.init.d:
    + Do not use start-stop-daemon and </dev/null to avoid blocking boot.
    + Show per-VPN result messages.
    + Add "--script-security 2" by default for backwards compatabliity.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  OpenVPN -- An application to securely tunnel IP networks
 
3
 *             over a single TCP/UDP port, with support for SSL/TLS-based
 
4
 *             session authentication and key exchange,
 
5
 *             packet encryption, packet authentication, and
 
6
 *             packet compression.
 
7
 *
 
8
 *  Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
 
9
 *  Copyright (C) 2010 Fox Crypto B.V. <openvpn@fox-it.com>
 
10
 *
 
11
 *  This program is free software; you can redistribute it and/or modify
 
12
 *  it under the terms of the GNU General Public License version 2
 
13
 *  as published by the Free Software Foundation.
 
14
 *
 
15
 *  This program is distributed in the hope that it will be useful,
 
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 *  GNU General Public License for more details.
 
19
 *
 
20
 *  You should have received a copy of the GNU General Public License
 
21
 *  along with this program (see the file COPYING included with this
 
22
 *  distribution); if not, write to the Free Software Foundation, Inc.,
 
23
 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
24
 */
 
25
 
 
26
/**
 
27
 * @file Data Channel Cryptography OpenSSL-specific backend interface
 
28
 */
 
29
 
 
30
#ifdef HAVE_CONFIG_H
 
31
#include "config.h"
 
32
#elif defined(_MSC_VER)
 
33
#include "config-msvc.h"
 
34
#endif
 
35
 
 
36
#include "syshead.h"
 
37
 
 
38
#if defined(ENABLE_CRYPTO) && defined(ENABLE_CRYPTO_OPENSSL)
 
39
 
 
40
#include "basic.h"
 
41
#include "buffer.h"
 
42
#include "integer.h"
 
43
#include "crypto_backend.h"
 
44
#include <openssl/objects.h>
 
45
#include <openssl/evp.h>
 
46
#include <openssl/des.h>
 
47
 
 
48
/*
 
49
 * Check for key size creepage.
 
50
 */
 
51
 
 
52
#if MAX_CIPHER_KEY_LENGTH < EVP_MAX_KEY_LENGTH
 
53
#warning Some OpenSSL EVP ciphers now support key lengths greater than MAX_CIPHER_KEY_LENGTH -- consider increasing MAX_CIPHER_KEY_LENGTH
 
54
#endif
 
55
 
 
56
#if MAX_HMAC_KEY_LENGTH < EVP_MAX_MD_SIZE
 
57
#warning Some OpenSSL HMAC message digests now support key lengths greater than MAX_HMAC_KEY_LENGTH -- consider increasing MAX_HMAC_KEY_LENGTH
 
58
#endif
 
59
 
 
60
/*
 
61
 *
 
62
 * Workarounds for incompatibilites between OpenSSL libraries.
 
63
 * Right now we accept OpenSSL libraries from 0.9.5 to 0.9.7.
 
64
 *
 
65
 */
 
66
 
 
67
#if SSLEAY_VERSION_NUMBER < 0x00907000L
 
68
 
 
69
/* Workaround: EVP_CIPHER_mode is defined wrong in OpenSSL 0.9.6 but is fixed in 0.9.7 */
 
70
#undef EVP_CIPHER_mode
 
71
#define EVP_CIPHER_mode(e)                (((e)->flags) & EVP_CIPH_MODE)
 
72
 
 
73
#define DES_cblock                        des_cblock
 
74
#define DES_is_weak_key                   des_is_weak_key
 
75
#define DES_check_key_parity              des_check_key_parity
 
76
#define DES_set_odd_parity                des_set_odd_parity
 
77
 
 
78
#define HMAC_CTX_init(ctx)                CLEAR (*ctx)
 
79
#define HMAC_Init_ex(ctx,sec,len,md,impl) HMAC_Init(ctx, sec, len, md)
 
80
#define HMAC_CTX_cleanup(ctx)             HMAC_cleanup(ctx)
 
81
#define EVP_MD_CTX_cleanup(md)            CLEAR (*md)
 
82
 
 
83
#define INFO_CALLBACK_SSL_CONST
 
84
 
 
85
#endif
 
86
 
 
87
static inline int
 
88
EVP_CipherInit_ov (EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, uint8_t *key, uint8_t *iv, int enc)
 
89
{
 
90
  return EVP_CipherInit (ctx, type, key, iv, enc);
 
91
}
 
92
 
 
93
static inline int
 
94
EVP_CipherUpdate_ov (EVP_CIPHER_CTX *ctx, uint8_t *out, int *outl, uint8_t *in, int inl)
 
95
{
 
96
  return EVP_CipherUpdate (ctx, out, outl, in, inl);
 
97
}
 
98
 
 
99
static inline bool
 
100
cipher_ok (const char* name)
 
101
{
 
102
  return true;
 
103
}
 
104
 
 
105
#ifndef EVP_CIPHER_name
 
106
#define EVP_CIPHER_name(e)              OBJ_nid2sn(EVP_CIPHER_nid(e))
 
107
#endif
 
108
 
 
109
#ifndef EVP_MD_name
 
110
#define EVP_MD_name(e)                  OBJ_nid2sn(EVP_MD_type(e))
 
111
#endif
 
112
 
 
113
#if HAVE_OPENSSL_ENGINE
 
114
#include <openssl/engine.h>
 
115
 
 
116
static bool engine_initialized = false; /* GLOBAL */
 
117
 
 
118
static ENGINE *engine_persist = NULL;   /* GLOBAL */
 
119
 
 
120
/* Try to load an engine in a shareable library */
 
121
static ENGINE *
 
122
try_load_engine (const char *engine)
 
123
{
 
124
  ENGINE *e = ENGINE_by_id ("dynamic");
 
125
  if (e)
 
126
    {
 
127
      if (!ENGINE_ctrl_cmd_string (e, "SO_PATH", engine, 0)
 
128
          || !ENGINE_ctrl_cmd_string (e, "LOAD", NULL, 0))
 
129
        {
 
130
          ENGINE_free (e);
 
131
          e = NULL;
 
132
        }
 
133
    }
 
134
  return e;
 
135
}
 
136
 
 
137
static ENGINE *
 
138
setup_engine (const char *engine)
 
139
{
 
140
  ENGINE *e = NULL;
 
141
 
 
142
  ENGINE_load_builtin_engines ();
 
143
 
 
144
  if (engine)
 
145
    {
 
146
      if (strcmp (engine, "auto") == 0)
 
147
        {
 
148
          msg (M_INFO, "Initializing OpenSSL auto engine support");
 
149
          ENGINE_register_all_complete ();
 
150
          return NULL;
 
151
        }
 
152
      if ((e = ENGINE_by_id (engine)) == NULL
 
153
         && (e = try_load_engine (engine)) == NULL)
 
154
        {
 
155
          msg (M_FATAL, "OpenSSL error: cannot load engine '%s'", engine);
 
156
        }
 
157
 
 
158
      if (!ENGINE_set_default (e, ENGINE_METHOD_ALL))
 
159
        {
 
160
          msg (M_FATAL, "OpenSSL error: ENGINE_set_default failed on engine '%s'",
 
161
               engine);
 
162
        }
 
163
 
 
164
      msg (M_INFO, "Initializing OpenSSL support for engine '%s'",
 
165
           ENGINE_get_id (e));
 
166
    }
 
167
  return e;
 
168
}
 
169
 
 
170
#endif /* HAVE_OPENSSL_ENGINE */
 
171
 
 
172
void
 
173
crypto_init_lib_engine (const char *engine_name)
 
174
{
 
175
#if HAVE_OPENSSL_ENGINE
 
176
  if (!engine_initialized)
 
177
    {
 
178
      ASSERT (engine_name);
 
179
      ASSERT (!engine_persist);
 
180
      engine_persist = setup_engine (engine_name);
 
181
      engine_initialized = true;
 
182
    }
 
183
#else
 
184
  msg (M_WARN, "Note: OpenSSL hardware crypto engine functionality is not available");
 
185
#endif
 
186
}
 
187
 
 
188
/*
 
189
 *
 
190
 * Functions related to the core crypto library
 
191
 *
 
192
 */
 
193
 
 
194
void
 
195
crypto_init_lib (void)
 
196
{
 
197
#ifndef USE_SSL
 
198
#ifndef ENABLE_SMALL
 
199
  ERR_load_crypto_strings ();
 
200
#endif
 
201
  OpenSSL_add_all_algorithms ();
 
202
#endif
 
203
 
 
204
  /*
 
205
   * If you build the OpenSSL library and OpenVPN with
 
206
   * CRYPTO_MDEBUG, you will get a listing of OpenSSL
 
207
   * memory leaks on program termination.
 
208
   */
 
209
 
 
210
#ifdef CRYPTO_MDEBUG
 
211
  CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
 
212
#endif
 
213
}
 
214
 
 
215
void
 
216
crypto_uninit_lib (void)
 
217
{
 
218
#ifndef USE_SSL
 
219
  EVP_cleanup ();
 
220
#ifndef ENABLE_SMALL
 
221
  ERR_free_strings ();
 
222
#endif
 
223
#endif
 
224
 
 
225
#ifdef CRYPTO_MDEBUG
 
226
  FILE* fp = fopen ("sdlog", "w");
 
227
  ASSERT (fp);
 
228
  CRYPTO_mem_leaks_fp (fp);
 
229
  fclose (fp);
 
230
#endif
 
231
 
 
232
#if HAVE_OPENSSL_ENGINE
 
233
  if (engine_initialized)
 
234
    {
 
235
      ENGINE_cleanup ();
 
236
      engine_persist = NULL;
 
237
      engine_initialized = false;
 
238
    }
 
239
#endif
 
240
}
 
241
 
 
242
void
 
243
crypto_clear_error (void)
 
244
{
 
245
  ERR_clear_error ();
 
246
}
 
247
 
 
248
/*
 
249
 *
 
250
 * OpenSSL memory debugging.  If dmalloc debugging is enabled, tell
 
251
 * OpenSSL to use our private malloc/realloc/free functions so that
 
252
 * we can dispatch them to dmalloc.
 
253
 *
 
254
 */
 
255
 
 
256
#ifdef DMALLOC
 
257
static void *
 
258
crypto_malloc (size_t size, const char *file, int line)
 
259
{
 
260
  return dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
 
261
}
 
262
 
 
263
static void *
 
264
crypto_realloc (void *ptr, size_t size, const char *file, int line)
 
265
{
 
266
  return dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
 
267
}
 
268
 
 
269
static void
 
270
crypto_free (void *ptr)
 
271
{
 
272
  dmalloc_free (__FILE__, __LINE__, ptr, DMALLOC_FUNC_FREE);
 
273
}
 
274
 
 
275
void
 
276
crypto_init_dmalloc (void)
 
277
{
 
278
  CRYPTO_set_mem_ex_functions (crypto_malloc,
 
279
                                crypto_realloc,
 
280
                                crypto_free);
 
281
}
 
282
#endif /* DMALLOC */
 
283
 
 
284
const char *
 
285
translate_cipher_name_from_openvpn (const char *cipher_name) {
 
286
  // OpenSSL doesn't require any translation
 
287
  return cipher_name;
 
288
}
 
289
 
 
290
const char *
 
291
translate_cipher_name_to_openvpn (const char *cipher_name) {
 
292
  // OpenSSL doesn't require any translation
 
293
  return cipher_name;
 
294
}
 
295
 
 
296
void
 
297
show_available_ciphers ()
 
298
{
 
299
  int nid;
 
300
 
 
301
#ifndef ENABLE_SMALL
 
302
  printf ("The following ciphers and cipher modes are available\n"
 
303
          "for use with " PACKAGE_NAME ".  Each cipher shown below may be\n"
 
304
          "used as a parameter to the --cipher option.  The default\n"
 
305
          "key size is shown as well as whether or not it can be\n"
 
306
          "changed with the --keysize directive.  Using a CBC mode\n"
 
307
          "is recommended.\n\n");
 
308
#endif
 
309
 
 
310
  for (nid = 0; nid < 10000; ++nid)     /* is there a better way to get the size of the nid list? */
 
311
    {
 
312
      const EVP_CIPHER *cipher = EVP_get_cipherbynid (nid);
 
313
      if (cipher && cipher_ok (OBJ_nid2sn (nid)))
 
314
        {
 
315
          const unsigned int mode = EVP_CIPHER_mode (cipher);
 
316
          if (mode == EVP_CIPH_CBC_MODE
 
317
#ifdef ALLOW_NON_CBC_CIPHERS
 
318
              || mode == EVP_CIPH_CFB_MODE || mode == EVP_CIPH_OFB_MODE
 
319
#endif
 
320
              )
 
321
            printf ("%s %d bit default key (%s)\n",
 
322
                    OBJ_nid2sn (nid),
 
323
                    EVP_CIPHER_key_length (cipher) * 8,
 
324
                    ((EVP_CIPHER_flags (cipher) & EVP_CIPH_VARIABLE_LENGTH) ?
 
325
                     "variable" : "fixed"));
 
326
        }
 
327
    }
 
328
  printf ("\n");
 
329
}
 
330
 
 
331
void
 
332
show_available_digests ()
 
333
{
 
334
  int nid;
 
335
 
 
336
#ifndef ENABLE_SMALL
 
337
  printf ("The following message digests are available for use with\n"
 
338
          PACKAGE_NAME ".  A message digest is used in conjunction with\n"
 
339
          "the HMAC function, to authenticate received packets.\n"
 
340
          "You can specify a message digest as parameter to\n"
 
341
          "the --auth option.\n\n");
 
342
#endif
 
343
 
 
344
  for (nid = 0; nid < 10000; ++nid)
 
345
    {
 
346
      const EVP_MD *digest = EVP_get_digestbynid (nid);
 
347
      if (digest)
 
348
        {
 
349
          printf ("%s %d bit digest size\n",
 
350
                  OBJ_nid2sn (nid), EVP_MD_size (digest) * 8);
 
351
        }
 
352
    }
 
353
  printf ("\n");
 
354
}
 
355
 
 
356
void
 
357
show_available_engines ()
 
358
{
 
359
#if HAVE_OPENSSL_ENGINE /* Only defined for OpenSSL */
 
360
  ENGINE *e;
 
361
 
 
362
  printf ("OpenSSL Crypto Engines\n\n");
 
363
 
 
364
  ENGINE_load_builtin_engines ();
 
365
 
 
366
  e = ENGINE_get_first ();
 
367
  while (e)
 
368
    {
 
369
      printf ("%s [%s]\n",
 
370
              ENGINE_get_name (e),
 
371
              ENGINE_get_id (e));
 
372
      e = ENGINE_get_next (e);
 
373
    }
 
374
  ENGINE_cleanup ();
 
375
#else
 
376
  printf ("Sorry, OpenSSL hardware crypto engine functionality is not available.\n");
 
377
#endif
 
378
}
 
379
 
 
380
/*
 
381
 *
 
382
 * Random number functions, used in cases where we want
 
383
 * reasonably strong cryptographic random number generation
 
384
 * without depleting our entropy pool.  Used for random
 
385
 * IV values and a number of other miscellaneous tasks.
 
386
 *
 
387
 */
 
388
 
 
389
int rand_bytes(uint8_t *output, int len)
 
390
{
 
391
  return RAND_bytes (output, len);
 
392
}
 
393
 
 
394
/*
 
395
 *
 
396
 * Key functions, allow manipulation of keys.
 
397
 *
 
398
 */
 
399
 
 
400
 
 
401
int
 
402
key_des_num_cblocks (const EVP_CIPHER *kt)
 
403
{
 
404
  int ret = 0;
 
405
  const char *name = OBJ_nid2sn (EVP_CIPHER_nid (kt));
 
406
  if (name)
 
407
    {
 
408
      if (!strncmp (name, "DES-", 4))
 
409
        {
 
410
          ret = EVP_CIPHER_key_length (kt) / sizeof (DES_cblock);
 
411
        }
 
412
      else if (!strncmp (name, "DESX-", 5))
 
413
        {
 
414
          ret = 1;
 
415
        }
 
416
    }
 
417
  dmsg (D_CRYPTO_DEBUG, "CRYPTO INFO: n_DES_cblocks=%d", ret);
 
418
  return ret;
 
419
}
 
420
 
 
421
bool
 
422
key_des_check (uint8_t *key, int key_len, int ndc)
 
423
{
 
424
  int i;
 
425
  struct buffer b;
 
426
 
 
427
  buf_set_read (&b, key, key_len);
 
428
 
 
429
  for (i = 0; i < ndc; ++i)
 
430
    {
 
431
      DES_cblock *dc = (DES_cblock*) buf_read_alloc (&b, sizeof (DES_cblock));
 
432
      if (!dc)
 
433
        {
 
434
          msg (D_CRYPT_ERRORS, "CRYPTO INFO: check_key_DES: insufficient key material");
 
435
          goto err;
 
436
        }
 
437
      if (DES_is_weak_key(dc))
 
438
        {
 
439
          msg (D_CRYPT_ERRORS, "CRYPTO INFO: check_key_DES: weak key detected");
 
440
          goto err;
 
441
        }
 
442
      if (!DES_check_key_parity (dc))
 
443
        {
 
444
          msg (D_CRYPT_ERRORS, "CRYPTO INFO: check_key_DES: bad parity detected");
 
445
          goto err;
 
446
        }
 
447
    }
 
448
  return true;
 
449
 
 
450
 err:
 
451
  ERR_clear_error ();
 
452
  return false;
 
453
}
 
454
 
 
455
void
 
456
key_des_fixup (uint8_t *key, int key_len, int ndc)
 
457
{
 
458
  int i;
 
459
  struct buffer b;
 
460
 
 
461
  buf_set_read (&b, key, key_len);
 
462
  for (i = 0; i < ndc; ++i)
 
463
    {
 
464
      DES_cblock *dc = (DES_cblock*) buf_read_alloc(&b, sizeof(DES_cblock));
 
465
      if (!dc)
 
466
        {
 
467
          msg (D_CRYPT_ERRORS, "CRYPTO INFO: fixup_key_DES: insufficient key material");
 
468
          ERR_clear_error ();
 
469
          return;
 
470
        }
 
471
      DES_set_odd_parity (dc);
 
472
    }
 
473
}
 
474
 
 
475
 
 
476
/*
 
477
 *
 
478
 * Generic cipher key type functions
 
479
 *
 
480
 */
 
481
 
 
482
 
 
483
const EVP_CIPHER *
 
484
cipher_kt_get (const char *ciphername)
 
485
{
 
486
  const EVP_CIPHER *cipher = NULL;
 
487
 
 
488
  ASSERT (ciphername);
 
489
 
 
490
  cipher = EVP_get_cipherbyname (ciphername);
 
491
 
 
492
  if ((NULL == cipher) || !cipher_ok (OBJ_nid2sn (EVP_CIPHER_nid (cipher))))
 
493
    msg (M_SSLERR, "Cipher algorithm '%s' not found", ciphername);
 
494
 
 
495
  if (EVP_CIPHER_key_length (cipher) > MAX_CIPHER_KEY_LENGTH)
 
496
    msg (M_FATAL, "Cipher algorithm '%s' uses a default key size (%d bytes) which is larger than " PACKAGE_NAME "'s current maximum key size (%d bytes)",
 
497
         ciphername,
 
498
         EVP_CIPHER_key_length (cipher),
 
499
         MAX_CIPHER_KEY_LENGTH);
 
500
 
 
501
  return cipher;
 
502
}
 
503
 
 
504
const char *
 
505
cipher_kt_name (const EVP_CIPHER *cipher_kt)
 
506
{
 
507
  if (NULL == cipher_kt)
 
508
    return "[null-cipher]";
 
509
  return EVP_CIPHER_name (cipher_kt);
 
510
}
 
511
 
 
512
int
 
513
cipher_kt_key_size (const EVP_CIPHER *cipher_kt)
 
514
{
 
515
  return EVP_CIPHER_key_length (cipher_kt);
 
516
}
 
517
 
 
518
int
 
519
cipher_kt_iv_size (const EVP_CIPHER *cipher_kt)
 
520
{
 
521
  return EVP_CIPHER_iv_length (cipher_kt);
 
522
}
 
523
 
 
524
int
 
525
cipher_kt_block_size (const EVP_CIPHER *cipher_kt)
 
526
{
 
527
  return EVP_CIPHER_block_size (cipher_kt);
 
528
}
 
529
 
 
530
int
 
531
cipher_kt_mode (const EVP_CIPHER *cipher_kt)
 
532
{
 
533
  ASSERT(NULL != cipher_kt);
 
534
  return EVP_CIPHER_mode (cipher_kt);
 
535
}
 
536
 
 
537
/*
 
538
 *
 
539
 * Generic cipher context functions
 
540
 *
 
541
 */
 
542
 
 
543
 
 
544
void
 
545
cipher_ctx_init (EVP_CIPHER_CTX *ctx, uint8_t *key, int key_len,
 
546
    const EVP_CIPHER *kt, int enc)
 
547
{
 
548
  ASSERT(NULL != kt && NULL != ctx);
 
549
 
 
550
  CLEAR (*ctx);
 
551
 
 
552
  EVP_CIPHER_CTX_init (ctx);
 
553
  if (!EVP_CipherInit_ov (ctx, kt, NULL, NULL, enc))
 
554
    msg (M_SSLERR, "EVP cipher init #1");
 
555
#ifdef HAVE_EVP_CIPHER_CTX_SET_KEY_LENGTH
 
556
  if (!EVP_CIPHER_CTX_set_key_length (ctx, key_len))
 
557
    msg (M_SSLERR, "EVP set key size");
 
558
#endif
 
559
  if (!EVP_CipherInit_ov (ctx, NULL, key, NULL, enc))
 
560
    msg (M_SSLERR, "EVP cipher init #2");
 
561
 
 
562
  /* make sure we used a big enough key */
 
563
  ASSERT (EVP_CIPHER_CTX_key_length (ctx) <= key_len);
 
564
}
 
565
 
 
566
void
 
567
cipher_ctx_cleanup (EVP_CIPHER_CTX *ctx)
 
568
{
 
569
  EVP_CIPHER_CTX_cleanup (ctx);
 
570
}
 
571
 
 
572
int
 
573
cipher_ctx_iv_length (const EVP_CIPHER_CTX *ctx)
 
574
{
 
575
  return EVP_CIPHER_CTX_iv_length (ctx);
 
576
}
 
577
 
 
578
int
 
579
cipher_ctx_block_size(const EVP_CIPHER_CTX *ctx)
 
580
{
 
581
  return EVP_CIPHER_CTX_block_size (ctx);
 
582
}
 
583
 
 
584
int
 
585
cipher_ctx_mode (const EVP_CIPHER_CTX *ctx)
 
586
{
 
587
  return EVP_CIPHER_CTX_mode (ctx);
 
588
}
 
589
 
 
590
int
 
591
cipher_ctx_reset (EVP_CIPHER_CTX *ctx, uint8_t *iv_buf)
 
592
{
 
593
  return EVP_CipherInit_ov (ctx, NULL, NULL, iv_buf, -1);
 
594
}
 
595
 
 
596
int
 
597
cipher_ctx_update (EVP_CIPHER_CTX *ctx, uint8_t *dst, int *dst_len,
 
598
    uint8_t *src, int src_len)
 
599
{
 
600
  return EVP_CipherUpdate_ov (ctx, dst, dst_len, src, src_len);
 
601
}
 
602
 
 
603
int
 
604
cipher_ctx_final (EVP_CIPHER_CTX *ctx, uint8_t *dst, int *dst_len)
 
605
{
 
606
  return EVP_CipherFinal (ctx, dst, dst_len);
 
607
}
 
608
 
 
609
 
 
610
void
 
611
cipher_des_encrypt_ecb (const unsigned char key[DES_KEY_LENGTH],
 
612
    unsigned char *src,
 
613
    unsigned char *dst)
 
614
{
 
615
    DES_key_schedule sched;
 
616
 
 
617
    DES_set_key_unchecked((DES_cblock*)key, &sched);
 
618
    DES_ecb_encrypt((DES_cblock *)src, (DES_cblock *)dst, &sched, DES_ENCRYPT);
 
619
}
 
620
 
 
621
/*
 
622
 *
 
623
 * Generic message digest information functions
 
624
 *
 
625
 */
 
626
 
 
627
 
 
628
const EVP_MD *
 
629
md_kt_get (const char *digest)
 
630
{
 
631
  const EVP_MD *md = NULL;
 
632
  ASSERT (digest);
 
633
  md = EVP_get_digestbyname (digest);
 
634
  if (!md)
 
635
    msg (M_SSLERR, "Message hash algorithm '%s' not found", digest);
 
636
  if (EVP_MD_size (md) > MAX_HMAC_KEY_LENGTH)
 
637
    msg (M_FATAL, "Message hash algorithm '%s' uses a default hash size (%d bytes) which is larger than " PACKAGE_NAME "'s current maximum hash size (%d bytes)",
 
638
         digest,
 
639
         EVP_MD_size (md),
 
640
         MAX_HMAC_KEY_LENGTH);
 
641
  return md;
 
642
}
 
643
 
 
644
const char *
 
645
md_kt_name (const EVP_MD *kt)
 
646
{
 
647
  if (NULL == kt)
 
648
    return "[null-digest]";
 
649
  return EVP_MD_name (kt);
 
650
}
 
651
 
 
652
int
 
653
md_kt_size (const EVP_MD *kt)
 
654
{
 
655
  return EVP_MD_size(kt);
 
656
}
 
657
 
 
658
 
 
659
/*
 
660
 *
 
661
 * Generic message digest functions
 
662
 *
 
663
 */
 
664
 
 
665
int
 
666
md_full (const EVP_MD *kt, const uint8_t *src, int src_len, uint8_t *dst)
 
667
{
 
668
  unsigned int in_md_len = 0;
 
669
 
 
670
  return EVP_Digest(src, src_len, dst, &in_md_len, kt, NULL);
 
671
}
 
672
 
 
673
void
 
674
md_ctx_init (EVP_MD_CTX *ctx, const EVP_MD *kt)
 
675
{
 
676
  ASSERT(NULL != ctx && NULL != kt);
 
677
 
 
678
  CLEAR (*ctx);
 
679
 
 
680
  EVP_MD_CTX_init (ctx);
 
681
  EVP_DigestInit(ctx, kt);
 
682
}
 
683
 
 
684
void
 
685
md_ctx_cleanup(EVP_MD_CTX *ctx)
 
686
{
 
687
  EVP_MD_CTX_cleanup(ctx);
 
688
}
 
689
 
 
690
int
 
691
md_ctx_size (const EVP_MD_CTX *ctx)
 
692
{
 
693
  return EVP_MD_CTX_size(ctx);
 
694
}
 
695
 
 
696
void
 
697
md_ctx_update (EVP_MD_CTX *ctx, const uint8_t *src, int src_len)
 
698
{
 
699
  EVP_DigestUpdate(ctx, src, src_len);
 
700
}
 
701
 
 
702
void
 
703
md_ctx_final (EVP_MD_CTX *ctx, uint8_t *dst)
 
704
{
 
705
  unsigned int in_md_len = 0;
 
706
 
 
707
  EVP_DigestFinal(ctx, dst, &in_md_len);
 
708
}
 
709
 
 
710
 
 
711
/*
 
712
 *
 
713
 * Generic HMAC functions
 
714
 *
 
715
 */
 
716
 
 
717
 
 
718
void
 
719
hmac_ctx_init (HMAC_CTX *ctx, const uint8_t *key, int key_len,
 
720
    const EVP_MD *kt)
 
721
{
 
722
  ASSERT(NULL != kt && NULL != ctx);
 
723
 
 
724
  CLEAR(*ctx);
 
725
 
 
726
  HMAC_CTX_init (ctx);
 
727
  HMAC_Init_ex (ctx, key, key_len, kt, NULL);
 
728
 
 
729
  /* make sure we used a big enough key */
 
730
  ASSERT (HMAC_size (ctx) <= key_len);
 
731
}
 
732
 
 
733
void
 
734
hmac_ctx_cleanup(HMAC_CTX *ctx)
 
735
{
 
736
  HMAC_CTX_cleanup (ctx);
 
737
}
 
738
 
 
739
int
 
740
hmac_ctx_size (const HMAC_CTX *ctx)
 
741
{
 
742
  return HMAC_size (ctx);
 
743
}
 
744
 
 
745
void
 
746
hmac_ctx_reset (HMAC_CTX *ctx)
 
747
{
 
748
  HMAC_Init_ex (ctx, NULL, 0, NULL, NULL);
 
749
}
 
750
 
 
751
void
 
752
hmac_ctx_update (HMAC_CTX *ctx, const uint8_t *src, int src_len)
 
753
{
 
754
  HMAC_Update (ctx, src, src_len);
 
755
}
 
756
 
 
757
void
 
758
hmac_ctx_final (HMAC_CTX *ctx, uint8_t *dst)
 
759
{
 
760
  unsigned int in_hmac_len = 0;
 
761
 
 
762
  HMAC_Final (ctx, dst, &in_hmac_len);
 
763
}
 
764
 
 
765
#endif /* ENABLE_CRYPTO && ENABLE_CRYPTO_OPENSSL */