File: canlock.c

package info (click to toggle)
canlock 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,944 kB
  • sloc: sh: 4,561; ansic: 4,308; yacc: 643; lex: 226; makefile: 94
file content (448 lines) | stat: -rw-r--r-- 11,731 bytes parent folder | download | duplicates (2)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 * COPYRIGHT AND PERMISSION NOTICE
 *
 * Copyright (c) 2017 Dennis Preiser
 * Copyright (c) 2003 G.J. Andruk
 *
 * All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, and/or sell copies of the Software, and to permit persons
 * to whom the Software is furnished to do so, provided that the above
 * copyright notice(s) and this permission notice appear in all copies of
 * the Software and that both the above copyright notice(s) and this
 * permission notice appear in supporting documentation.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
 * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Except as contained in this notice, the name of a copyright holder
 * shall not be used in advertising or otherwise to promote the sale, use
 * or other dealings in this Software without prior written authorization
 * of the copyright holder.
 */

/* C99 */
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base64.h"

/* Local */
#include "sha.h"
#include "canlock.h"


/* Map external hash type 'cl_hash_version' to local SHA implementation */
static enum SHAversion which_cl_hash(int which_hash)
{
   switch (which_hash)
   {
      case CL_SHA1:
         return SHA1;
      case CL_SHA224:
         return SHA224;
      case CL_SHA384:
         return SHA384;
      case CL_SHA512:
         return SHA512;

      /* Mandatory algorithm */
      case CL_SHA256:
      default:
         return SHA256;
   }
}


/*
 * Portable replacement for 'strdup()'
 *
 * 'strdup()' requires SUSv2, XSI extension or POSIX.1-2008
 * https://pubs.opengroup.org/onlinepubs/007908799/xsh/strdup.html
 * https://pubs.opengroup.org/onlinepubs/009695399/functions/strdup.html
 * https://pubs.opengroup.org/onlinepubs/9699919799/functions/strdup.html
 *
 * Note:
 * 'malloc()' already gives the correct return (and 'errno') values required by
 * POSIX.
 */
static char *my_strdup(const char *s)
{
   char *res;
   size_t len;

   len = strlen(s);
   res = (char *) malloc(len + (size_t) 1);
   if (NULL != res)
   {
      strncpy(res, s, len);
      res[len] = 0;
   }

   return res;
}


/*
 * Extract a <c-lock-string> or <c-key-string> element respectively from 'key'
 * (that is, with the <scheme>: prefix removed)
 *
 * The <scheme> is written to 'type' on success, else empty string on failure.
 * The caller must ensure that the provided buffer is large enough.
 *
 * Returns a malloc()'d buffer on success, that the caller will need to free().
 * Returns NULL (on failure).
 */
#if ! CL_API_V2
static
#endif  /* CL_API_V2 */
char *lock_strip_alpha(char *key, char *type)
{
   char *ret;
   int offset;

   /* Strip scheme and write it to 'type' */
   do
   {
      *type = (char) tolower((int) (unsigned char) *key);
      type++;
      key++;
   } while (*key && *key != ':');
   *type = '\0';

   key++;  /* Skip colon */

   /* Copy <c-lock-string> or <c-key-string> respectively */
   ret = my_strdup(key);
   if (NULL != ret)
   {
      /*
       * Strip potential trailing <clue-string> element
       * (that is, a :xx suffix is removed).
       *
       * Note:
       * This element was removed from an early draft, but could still be
       * present.
       */
      offset = 0;
      while (ret[offset] && ret[offset] != ':')
         offset++;
      ret[offset] = '\0';
   }
   return ret;
}


#if CL_API_V2
char *lock_strip(char *key, char *type)
{
   return lock_strip_alpha(key, type);
}
#endif  /* CL_API_V2 */


/*
 * Split a <c-lock> or <c-key> element respectively
 *
 * 'input' is split on the first colon into <scheme> and <c-lock-string> or
 * <c-key-string> elements respectively.
 *
 * If scheme is supported, the corresponding ID is returned.
 *
 * A pointer to <c-lock-string> or <c-key-string> respectively is written to
 * the location pointed to by 'klstring' (no memory is allocated, the address
 * points to the memory used for 'input').
 *
 * Returns the hash algorithm ID for scheme (on success).
 * Returns CL_INVALID (on failure, NULL was written to 'klstring' in this case).
 */
int cl_split(char *input, char **klstring)
{
   int hash = CL_INVALID;
   char *scheme, *junk;

   *klstring = strchr(input, (int) ':');
   if (NULL != *klstring)
   {
      (*klstring)++;  /* Skip colon */
      scheme = (char *) malloc(strlen(input) + (size_t) 1);
      if (NULL == scheme) { *klstring = NULL; }
      else
      {
         junk = lock_strip_alpha(input, scheme);
         if (NULL != junk)
         {
            if (!strcmp(scheme, "sha1"))  { hash = CL_SHA1; }
            else if (!strcmp(scheme, "sha224"))  { hash = CL_SHA224; }
            else if (!strcmp(scheme, "sha256"))  { hash = CL_SHA256; }
            else if (!strcmp(scheme, "sha384"))  { hash = CL_SHA384; }
            else if (!strcmp(scheme, "sha512"))  { hash = CL_SHA512; }
            free((void *) junk);
         }
         free((void *) scheme);
      }
   }
   return hash;
}


#if CL_API_V2
/*
 * Generate a SHA1 cancel key
 * Returns a malloc()'d buffer that the caller will need to free() (on success).
 * Returns NULL (on failure).
 */
char *sha_key(const unsigned char *secret, size_t seclen,
        const unsigned char *message, size_t msglen)
{
   return cl_get_key(CL_SHA1, secret, seclen, message, msglen);
}
#endif  /* CL_API_V2 */


/*
 * Generate a cancel key
 * Returns a malloc()'d buffer that the caller will need to free() (on success).
 * Returns NULL (on failure).
 */
char *cl_get_key(int which_hash, const unsigned char *secret, size_t seclen,
                 const unsigned char *message, size_t msglen)
{
   char *cankey[1], *tmp;
   const char *scheme;
   enum SHAversion which_sha;
   size_t keysize, scheme_len;
   uint8_t hmacbuff[USHAMaxHashSize];

   which_sha = which_cl_hash(which_hash);

   /* Ensure that size data from external caller can be represented as 'int' */
   if ((size_t) INT_MAX < msglen || (size_t) INT_MAX < seclen)
      return NULL;

   if (RFC2104Hmac(which_sha, message, (int) msglen, secret, (int) seclen,
                   hmacbuff)
       != shaSuccess)
      return NULL;

   if (!(keysize = base64_encode(hmacbuff, USHAHashSize(which_sha), cankey)))
      return NULL;

   switch (which_sha)
   {
      case SHA1:
         scheme = "sha1:";
         break;
      case SHA224:
         scheme = "sha224:";
         break;
      case SHA256:
         scheme = "sha256:";
         break;
      case SHA384:
         scheme = "sha384:";
         break;
      case SHA512:
         scheme = "sha512:";
         break;
      default:
         return NULL;
   }

   scheme_len = strlen(scheme);

   tmp = (char *) realloc((void *) *cankey, keysize + scheme_len + 1);
   if (NULL != tmp) { *cankey = tmp; }
   else
   {
      free((void *) *cankey);
      return NULL;
   }

   memmove((void *) (*cankey + scheme_len), (void *) *cankey, keysize + 1);
   strncpy(*cankey, scheme, scheme_len);
   return (*cankey);
}


#if CL_API_V2
/*
 * Generate a SHA1 cancel lock
 * Returns a malloc()'d buffer that the caller will need to free() (on success).
 * Returns NULL (on failure).
 */
char *sha_lock(const unsigned char *secret, size_t seclen,
               const unsigned char *message, size_t msglen)
{
   return cl_get_lock(CL_SHA1, secret, seclen, message, msglen);
}
#endif  /* CL_API_V2 */


/*
 * Generate cancel lock
 * Returns a malloc()'d buffer that the caller will need to free() (on success).
 * Returns NULL (on failure).
 */
char *cl_get_lock(int which_hash, const unsigned char *secret, size_t seclen,
                  const unsigned char *message, size_t msglen)
{
   USHAContext hash_ctx;
   char *canlock[1], *tmp, *junk;
   const char *scheme;
   enum SHAversion which_sha;
   size_t hash_size, locksize, scheme_len;
   uint8_t *cankey, hmacbuff[USHAMaxHashSize];

   which_sha = which_cl_hash(which_hash);

   /* The function 'USHAHashSize()' never returns negative values */
   hash_size = (size_t) USHAHashSize(which_sha);

   if (!(tmp = cl_get_key(which_hash, secret, seclen, message, msglen)))
      return NULL;

   if (!(junk = malloc(hash_size + 1)))
   {
      free(tmp);
      return NULL;
   }

   cankey = (unsigned char *) lock_strip_alpha(tmp, junk);

   free(tmp);
   free(junk);

   if (USHAReset(&hash_ctx, which_sha) != shaSuccess)
   {
      free(cankey);
      return NULL;
   }

   if (USHAInput(&hash_ctx, cankey, (unsigned int) strlen((char *) cankey))
       != shaSuccess)
   {
      free(cankey);
      return NULL;
   }

   free(cankey);

   if (USHAResult(&hash_ctx, hmacbuff) != shaSuccess)
      return NULL;

   if (!(locksize = base64_encode(hmacbuff, (int) hash_size, canlock)))
      return NULL;

   switch (which_sha)
   {
      case SHA1:
         scheme = "sha1:";
         break;
      case SHA224:
         scheme = "sha224:";
         break;
      case SHA256:
         scheme = "sha256:";
         break;
      case SHA384:
         scheme = "sha384:";
         break;
      case SHA512:
         scheme = "sha512:";
         break;
      default:
         return NULL;
   }

   scheme_len = strlen(scheme);

   tmp = (char *) realloc((void *) *canlock, locksize + scheme_len + 1);
   if (NULL != tmp) { *canlock = tmp; }
   else
   {
      free((void *) *canlock);
      return NULL;
   }

   memmove((void *) (*canlock + scheme_len), (void *) *canlock, locksize + 1);
   strncpy(*canlock, scheme, scheme_len);
   return (*canlock);
}


#if CL_API_V2
/*
 * Verify a SHA1 cancel key against a cancel lock
 * Returns 0 on success, nonzero on failure.
 */
int sha_verify(const char *key, const char *lock)
{
   return cl_verify(CL_SHA1, key, lock);
}
#endif  /* CL_API_V2 */


/*
 * Verify a cancel key against a cancel lock
 * Returns 0 on success, nonzero on failure.
 */
int cl_verify(int which_hash, const char *key, const char *lock)
{
   int res;
   USHAContext hash_ctx;
   char *templock[1];
   enum SHAversion which_sha;
   size_t key_size, hash_size;
   uint8_t hashbuff[USHAMaxHashSize];

   /* Defeat the fallback to SHA256 default */
   if (CL_INVALID == which_hash)
      return -1;

   /*
    * Ensure that key length is supported
    * Currently the maximum length is for base64 encoded SHA512: 88
    * Theoretical limit: UINT_MAX
    * (must always be representable as 'unsigned int')
    */
   key_size = strlen(key);
   if ((size_t) 88 < key_size)
      return -1;

   which_sha = which_cl_hash(which_hash);

   /* The function 'USHAHashSize()' never returns negative values */
   hash_size = (size_t) USHAHashSize(which_sha);

   if (USHAReset(&hash_ctx, which_sha) != shaSuccess)
      return -1;

   if (USHAInput(&hash_ctx, (const uint8_t *) key, (unsigned int) key_size)
       != shaSuccess)
      return -1;

   if (USHAResult(&hash_ctx, hashbuff) != shaSuccess)
      return -1;

   if (!base64_encode(hashbuff, (int) hash_size, templock))
      return -1;

   res = strcmp(*templock, lock);
   free((void*) *templock);

   return res;
}