| 1 |
/* Copyright (c) 2021 Michael Baeuerle |
| 2 |
* |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 6 |
* a copy of this software and associated documentation files (the |
| 7 |
* "Software"), to deal in the Software without restriction, including |
| 8 |
* without limitation the rights to use, copy, modify, merge, publish, |
| 9 |
* distribute, and/or sell copies of the Software, and to permit persons |
| 10 |
* to whom the Software is furnished to do so, provided that the above |
| 11 |
* copyright notice(s) and this permission notice appear in all copies of |
| 12 |
* the Software and that both the above copyright notice(s) and this |
| 13 |
* permission notice appear in supporting documentation. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT |
| 18 |
* OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 19 |
* HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY |
| 20 |
* SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER |
| 21 |
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 22 |
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 23 |
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 24 |
* |
| 25 |
* Except as contained in this notice, the name of a copyright holder |
| 26 |
* shall not be used in advertising or otherwise to promote the sale, use |
| 27 |
* or other dealings in this Software without prior written authorization |
| 28 |
* of the copyright holder. |
| 29 |
* |
| 30 |
* SPDX-License-Identifier: ICU |
| 31 |
*/ |
| 32 |
|
| 33 |
/* Create Cancel-Lock header field |
| 34 |
* |
| 35 |
* Example program for POSIX operating systems. |
| 36 |
* |
| 37 |
* The local secret can be loaded from a file with portable stdio too. |
| 38 |
* This example uses the POSIX API to bypass the stdio buffering. |
| 39 |
* For programs with longer runtime the function cl_clear_secret() can |
| 40 |
* be used to overwrite the local secret in memory (after use). In this |
| 41 |
* case no additional copies are desired. |
| 42 |
* |
| 43 |
* For insertion into a message header, the canonical line termination |
| 44 |
* CRLF must be used instead. |
| 45 |
*/ |
| 46 |
|
| 47 |
#include <sys/types.h> |
| 48 |
#include <sys/stat.h> |
| 49 |
#include <errno.h> |
| 50 |
#include <fcntl.h> |
| 51 |
#include <stdio.h> |
| 52 |
#include <stdlib.h> |
| 53 |
#include <string.h> |
| 54 |
#include <unistd.h> |
| 55 |
|
| 56 |
#include <libcanlock-3/canlock.h> |
| 57 |
|
| 58 |
|
| 59 |
/* 512 bit secret, large enough with all supported hash algorithms */ |
| 60 |
#define SECRET_SIZE (size_t) 64 |
| 61 |
static unsigned char secret[SECRET_SIZE]; |
| 62 |
|
| 63 |
|
| 64 |
/* |
| 65 |
* Example User-ID and Message-ID |
| 66 |
* |
| 67 |
* For the algorithm recommended by RFC 8315 in Section 4, the Message-ID |
| 68 |
* without surrounding whitespace is used for <mid>. |
| 69 |
* |
| 70 |
* An optional User-ID <uid> can be prepended. |
| 71 |
* |
| 72 |
* Note that RFC 8315 uses angle brackets like quotes in the continuous text. |
| 73 |
* Therefore <uid> or <mid> does not mean that additional angle brackets are |
| 74 |
* required. A User-ID must not contain angle brackets and a Message-ID always |
| 75 |
* includes angle brackets (as defined by RFC 5536 in Section 3.1.3). |
| 76 |
*/ |
| 77 |
static const unsigned char umid[] = "testuser<[email protected]>"; |
| 78 |
|
| 79 |
|
| 80 |
int error_handler(const char *msg) |
| 81 |
{ |
| 82 |
fprintf(stderr, "Error: %s\n", msg); |
| 83 |
exit(EXIT_FAILURE); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/* |
| 88 |
* Load local secret from file |
| 89 |
* |
| 90 |
* Only the owner (of the secret) should have read permission for the file. |
| 91 |
* The secret should be a cryptographically random value. |
| 92 |
* The size should be at least the size of the hash used for <scheme>. |
| 93 |
*/ |
| 94 |
int load_secret(void) |
| 95 |
{ |
| 96 |
/* Open file with secret */ |
| 97 |
int fd = open("secret_512bits", O_RDONLY); |
| 98 |
if (-1 == fd) |
| 99 |
error_handler("Opening file failed"); |
| 100 |
|
| 101 |
/* Read secret from file */ |
| 102 |
{ |
| 103 |
ssize_t num; |
| 104 |
size_t i = 0; |
| 105 |
do |
| 106 |
{ |
| 107 |
num = read(fd, &secret[i], SECRET_SIZE - i); |
| 108 |
if (0 == num) |
| 109 |
error_handler("Secret is too short"); |
| 110 |
if (0 < num) |
| 111 |
i += (size_t) num; |
| 112 |
} |
| 113 |
while ( (ssize_t) SECRET_SIZE > num || |
| 114 |
((ssize_t) -1 == num && EINTR == errno) ); |
| 115 |
|
| 116 |
(void) close(fd); |
| 117 |
|
| 118 |
if ((ssize_t) -1 == num) |
| 119 |
return -1; |
| 120 |
else |
| 121 |
return 0; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/* Create an (initial) Cancel-Lock header field */ |
| 127 |
int main(void) |
| 128 |
{ |
| 129 |
/* Load secret */ |
| 130 |
if (-1 == load_secret()) |
| 131 |
error_handler("Reading secret failed"); |
| 132 |
|
| 133 |
/* Create (initial) Cancel-Lock header field */ |
| 134 |
{ |
| 135 |
char *lock = cl_get_lock(CL_SHA512, |
| 136 |
secret, SECRET_SIZE, |
| 137 |
umid, strlen(umid)); |
| 138 |
if (NULL == lock) |
| 139 |
error_handler("Creating Cancel-Lock failed"); |
| 140 |
|
| 141 |
printf("Cancel-Lock: %s\n", lock); |
| 142 |
|
| 143 |
/* Release memory allocated by libcanlock */ |
| 144 |
free(lock); |
| 145 |
} |
| 146 |
|
| 147 |
exit(EXIT_SUCCESS); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
/* EOF */ |