"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.1/libcanlock/src/canlock.c" (28 Aug 2013, 5271 Bytes) of package /linux/misc/tin-2.4.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "canlock.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * COPYRIGHT AND PERMISSION NOTICE
    3  * 
    4  * Copyright (c) 2003 G.J. Andruk
    5  * 
    6  * All rights reserved.
    7  * 
    8  * Permission is hereby granted, free of charge, to any person obtaining
    9  * a copy of this software and associated documentation files (the
   10  * "Software"), to deal in the Software without restriction, including
   11  * without limitation the rights to use, copy, modify, merge, publish,
   12  * distribute, and/or sell copies of the Software, and to permit persons
   13  * to whom the Software is furnished to do so, provided that the above
   14  * copyright notice(s) and this permission notice appear in all copies of
   15  * the Software and that both the above copyright notice(s) and this
   16  * permission notice appear in supporting documentation.
   17  * 
   18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
   21  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
   22  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
   23  * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
   24  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
   25  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
   26  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   27  * 
   28  * Except as contained in this notice, the name of a copyright holder
   29  * shall not be used in advertising or otherwise to promote the sale, use
   30  * or other dealings in this Software without prior written authorization
   31  * of the copyright holder.
   32  */
   33 
   34 #include <stdio.h>
   35 #include <stdlib.h>
   36 #include <string.h>
   37 #include <ctype.h>
   38 #include <sys/types.h>
   39 #include "base64.h"
   40 #include "sha1.h"
   41 #include "hmac_sha1.h"
   42 #include "canlock.h"
   43 
   44 /* 
   45  * Return a stripped cancel lock, that is, with the xx: prefix
   46  * removed, else NULL on failure.
   47  * type is set to the lock type, else zero on failure.
   48  */
   49 char *
   50 lock_strip_alpha(char *key, char *type)
   51 {
   52     char *ret;
   53     int offset;
   54     do {
   55       *type = tolower(*key);
   56       type++;
   57       key++;
   58     } while (*key && *key != ':');
   59     
   60     *type = '\0';
   61     key++;
   62     ret = strdup (key);
   63     /* Strip the "Clue-string", no longer part of the lastest
   64      * draft but could still be present */
   65     offset = 0;
   66     while (ret[offset] && ret[offset] != ':')
   67         offset++;
   68     ret[offset] = '\0';
   69     return ret;
   70 }
   71 
   72 
   73 char *
   74 lock_strip(char *key, char *type)
   75 {
   76     return lock_strip_alpha(key, type);
   77 }
   78 
   79 /* 
   80  * Generate an SHA1 cancel key.
   81  * Returns a malloc()'d buffer that the caller will need to free().
   82  */
   83 char *
   84 sha_key(const unsigned char *secret, size_t seclen,
   85         const unsigned char *message, size_t msglen)
   86 {
   87     char
   88         *cankey[1];
   89     unsigned char
   90         *hmacbuff;
   91     size_t
   92         keysize;
   93 
   94     hmacbuff = hmac_sha1(secret, seclen, message, msglen);
   95     if (!hmacbuff)
   96         return NULL;
   97     keysize = base64_encode(hmacbuff, SHA_DIGESTSIZE, cankey);
   98     free ((void *) hmacbuff);
   99     if (!keysize)
  100         return NULL;
  101     *cankey = (char *) realloc((void *) *cankey, keysize + 6);
  102     if (!*cankey)
  103         return NULL;
  104     memmove((void *) (*cankey + 5), (void *) *cankey, keysize + 1);
  105     strncpy(*cankey, "sha1:", 5);
  106     return (*cankey);
  107 }
  108 
  109 /* 
  110  * Generate an SHA1 cancel lock.
  111  * Returns a malloc()'d buffer that the caller will need to free().
  112  */
  113 char *
  114 sha_lock(const unsigned char *secret, size_t seclen,
  115          const unsigned char *message, size_t msglen)
  116 {
  117     char
  118         *canlock[1],
  119         *tmp,
  120         junk[SHA_DIGESTSIZE];
  121     unsigned char
  122         *cankey,
  123         hmacbuff[SHA_DIGESTSIZE];
  124     size_t
  125         locksize;
  126     SHA_CTX
  127         hash_ctx;
  128 
  129     tmp = sha_key(secret, seclen, message, msglen);
  130     cankey = (unsigned char *) lock_strip_alpha(tmp, junk);
  131     free(tmp);
  132     if (!cankey)
  133         return NULL;
  134     if (sha_init(&hash_ctx)) {
  135         free(cankey);
  136         return NULL;
  137     }
  138     if (sha_update(&hash_ctx, cankey, strlen((char *) cankey))) {
  139         free(cankey);
  140         return NULL;
  141     }
  142     free(cankey);
  143     if (sha_digest(&hash_ctx, hmacbuff))
  144         return NULL;
  145     locksize = base64_encode(hmacbuff, SHA_DIGESTSIZE, canlock);
  146     if (!locksize)
  147         return NULL;
  148     *canlock = (char *) realloc((void *) *canlock, locksize + 6);
  149     if (!*canlock)
  150         return NULL;
  151     memmove((void *) (*canlock + 5), (void *) *canlock, locksize + 1);
  152     strncpy(*canlock, "sha1:", 5);
  153     return (*canlock);
  154 }
  155 
  156 
  157 /* 
  158  * Verify an SHA cancel key against a cancel lock.
  159  * Returns 0 on success, nonzero on failure.
  160  */
  161 int
  162 sha_verify(const char *key, const char *lock)
  163 {
  164     unsigned char
  165         binkey[SHA_DIGESTSIZE + 4],
  166         hmacbuff[SHA_DIGESTSIZE];
  167     char
  168         *templock[1];
  169     size_t
  170         keysize,
  171         locksize;
  172     SHA_CTX
  173         hash_ctx;
  174 
  175 
  176     /* Convert the key back into binary */
  177     keysize = base64_decode(key, (void *) &binkey);
  178     if (!keysize)
  179         return -1;
  180 
  181     if (sha_init(&hash_ctx))
  182         return -1;
  183     if (sha_update(&hash_ctx, (unsigned char *)key, strlen(key)))
  184         return -1;
  185     if (sha_digest(&hash_ctx, hmacbuff))
  186         return -1;
  187 
  188     locksize = base64_encode(hmacbuff, SHA_DIGESTSIZE, templock);
  189     if (!locksize)
  190         return -1;
  191 
  192     return strcmp(*templock, lock);
  193 }