Commit 2db100af authored by ale's avatar ale

Run clang-format on the C code

parent 3073a8b5
......@@ -30,30 +30,22 @@
#include "sso.h"
static const unsigned char base64_enc_map[64] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '+', '/'
};
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
static const unsigned char base64_dec_map[128] = {
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
127, 64, 127, 127, 127, 0, 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, 127, 127, 127, 127, 127, 127, 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, 127, 127, 127, 127, 127
};
127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 62, 127,
127, 127, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
127, 64, 127, 127, 127, 0, 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, 127, 127, 127, 127, 127, 127, 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, 127, 127, 127, 127, 127};
size_t sso_base64_encode_size(size_t slen) {
size_t n = (slen << 3) / 6;
......@@ -191,11 +183,11 @@ int sso_base64_decode(unsigned char *dst, size_t *dlen,
if (++n == 4) {
n = 0;
if (j > 0)
*p++ = (unsigned char) (x >> 16);
*p++ = (unsigned char)(x >> 16);
if (j > 1)
*p++ = (unsigned char) (x >> 8);
*p++ = (unsigned char)(x >> 8);
if (j > 2)
*p++ = (unsigned char) (x);
*p++ = (unsigned char)(x);
}
}
......
......@@ -7,9 +7,9 @@
#include "tweetnacl.h"
#define FIELD_SEP_STR "|"
#define FIELD_SEP_CH '|'
#define FIELD_SEP_CH '|'
#define GROUP_SEP_STR ","
#define GROUP_SEP_CH ','
#define GROUP_SEP_CH ','
static char **group_list_dup(const char **groups) {
int i = 0;
......@@ -51,10 +51,8 @@ static char *strdup_or_null(const char *s) {
return strdup(s);
}
sso_ticket_t sso_ticket_new(const char *user,
const char *service,
const char *domain,
const char **groups,
sso_ticket_t sso_ticket_new(const char *user, const char *service,
const char *domain, const char **groups,
int validity_seconds) {
sso_ticket_t t = (sso_ticket_t)malloc(sizeof(struct sso_ticket));
t->user = strdup_or_null(user);
......@@ -183,7 +181,7 @@ static char **group_list_parse(char *s) {
groups = (char **)malloc(sizeof(char *) * (n_groups + 1));
for (ss = s, gg = groups; ; gg++, ss = NULL) {
for (ss = s, gg = groups;; gg++, ss = NULL) {
token = strtok_r(ss, GROUP_SEP_STR, &sp);
if (token == NULL) {
break;
......@@ -253,7 +251,8 @@ int sso_generate_keys(unsigned char *publicp, unsigned char *secretp) {
return crypto_sign_keypair(publicp, secretp);
}
int sso_ticket_sign(sso_ticket_t t, const unsigned char *secret_key, char *out, size_t out_size) {
int sso_ticket_sign(sso_ticket_t t, const unsigned char *secret_key, char *out,
size_t out_size) {
char *serialized;
size_t serialized_size, dlen;
unsigned char *signed_data;
......@@ -269,24 +268,23 @@ int sso_ticket_sign(sso_ticket_t t, const unsigned char *secret_key, char *out,
signed_size = crypto_sign_BYTES + serialized_size;
signed_data = (unsigned char *)malloc(signed_size);
if (crypto_sign(signed_data, &signed_size,
(unsigned char *)serialized,
(unsigned long long)serialized_size,
secret_key) != 0) {
if (crypto_sign(signed_data, &signed_size, (unsigned char *)serialized,
(unsigned long long)serialized_size, secret_key) != 0) {
free(serialized);
free(signed_data);
return SSO_ERR_SIGNATURE;
}
dlen = out_size;
r = sso_base64_encode((unsigned char *)out, &dlen,
signed_data, (size_t)signed_size);
r = sso_base64_encode((unsigned char *)out, &dlen, signed_data,
(size_t)signed_size);
free(serialized);
free(signed_data);
return r;
}
int sso_ticket_open(sso_ticket_t *t, const char *str, const unsigned char *public_key) {
int sso_ticket_open(sso_ticket_t *t, const char *str,
const unsigned char *public_key) {
size_t encoded_size, signed_size;
unsigned char *signed_data;
unsigned char *serialized;
......@@ -295,8 +293,8 @@ int sso_ticket_open(sso_ticket_t *t, const char *str, const unsigned char *publi
encoded_size = signed_size = strlen(str);
signed_data = (unsigned char *)malloc(encoded_size + 1);
r = sso_base64_decode(signed_data, &signed_size,
(const unsigned char *)str, encoded_size);
r = sso_base64_decode(signed_data, &signed_size, (const unsigned char *)str,
encoded_size);
if (r < 0) {
free(signed_data);
return r;
......@@ -307,8 +305,7 @@ int sso_ticket_open(sso_ticket_t *t, const char *str, const unsigned char *publi
if (crypto_sign_open(serialized, &serialized_size,
(const unsigned char *)signed_data,
(unsigned long long)signed_size,
public_key) != 0) {
(unsigned long long)signed_size, public_key) != 0) {
free(serialized);
free(signed_data);
return SSO_ERR_BAD_SIGNATURE;
......@@ -321,7 +318,8 @@ int sso_ticket_open(sso_ticket_t *t, const char *str, const unsigned char *publi
return r;
}
int sso_validate(sso_ticket_t t, const char *service, const char *domain, const char **groups) {
int sso_validate(sso_ticket_t t, const char *service, const char *domain,
const char **groups) {
if (t->service == NULL || strcmp(t->service, service) != 0) {
return SSO_ERR_BAD_SERVICE;
}
......@@ -331,7 +329,8 @@ int sso_validate(sso_ticket_t t, const char *service, const char *domain, const
if (t->expires < time(NULL)) {
return SSO_ERR_EXPIRED;
}
if (groups != NULL && (t->groups == NULL || !group_list_overlap(t->groups, groups))) {
if (groups != NULL &&
(t->groups == NULL || !group_list_overlap(t->groups, groups))) {
return SSO_ERR_NO_MATCHING_GROUPS;
}
return SSO_OK;
......@@ -365,4 +364,3 @@ const char *sso_strerror(int err) {
return "unknown error";
}
}
......@@ -9,21 +9,21 @@ extern "C" {
#define SSO_TICKET_VERSION "3"
#define SSO_OK 0
#define SSO_ERR_SERIALIZATION -1
#define SSO_ERR_DESERIALIZATION -2
#define SSO_ERR_SIGNATURE -3
#define SSO_ERR_BUFFER_TOO_SMALL -4
#define SSO_ERR_BAD_SIGNATURE -5
#define SSO_ERR_UNSUPPORTED_VERSION -6
#define SSO_ERR_EXPIRED -7
#define SSO_ERR_BAD_SERVICE -8
#define SSO_ERR_BAD_DOMAIN -9
#define SSO_ERR_NO_MATCHING_GROUPS -10
#define SSO_ERR_DECODE64 -11
#define SSO_PUBLIC_KEY_SIZE 32
#define SSO_SECRET_KEY_SIZE 64
#define SSO_OK 0
#define SSO_ERR_SERIALIZATION -1
#define SSO_ERR_DESERIALIZATION -2
#define SSO_ERR_SIGNATURE -3
#define SSO_ERR_BUFFER_TOO_SMALL -4
#define SSO_ERR_BAD_SIGNATURE -5
#define SSO_ERR_UNSUPPORTED_VERSION -6
#define SSO_ERR_EXPIRED -7
#define SSO_ERR_BAD_SERVICE -8
#define SSO_ERR_BAD_DOMAIN -9
#define SSO_ERR_NO_MATCHING_GROUPS -10
#define SSO_ERR_DECODE64 -11
#define SSO_PUBLIC_KEY_SIZE 32
#define SSO_SECRET_KEY_SIZE 64
struct sso_ticket {
char *user;
......@@ -35,17 +35,22 @@ struct sso_ticket {
typedef struct sso_ticket *sso_ticket_t;
sso_ticket_t sso_ticket_new(const char *user, const char *service, const char *domain, const char **groups, int validity_seconds);
sso_ticket_t sso_ticket_new(const char *user, const char *service,
const char *domain, const char **groups,
int validity_seconds);
void sso_ticket_free(sso_ticket_t t);
int sso_ticket_sign(sso_ticket_t t, const unsigned char *secret_key, char *out, size_t outsz);
int sso_ticket_sign(sso_ticket_t t, const unsigned char *secret_key, char *out,
size_t outsz);
int sso_generate_keys(unsigned char *publicp, unsigned char *secretp);
int sso_ticket_open(sso_ticket_t *t, const char *str, const unsigned char *public_key);
int sso_ticket_open(sso_ticket_t *t, const char *str,
const unsigned char *public_key);
int sso_validate(sso_ticket_t t, const char *service, const char *domain, const char **groups);
int sso_validate(sso_ticket_t t, const char *service, const char *domain,
const char **groups);
const char *sso_strerror(int err);
......
......@@ -31,16 +31,17 @@
#include "sso.h"
#define CHECK_OK(x) { \
int _err = (x); \
if (_err != SSO_OK) { \
fprintf(stderr, "SSO Error: %s\n", sso_strerror(_err)); \
exit(1); \
} \
#define CHECK_OK(x) \
{ \
int _err = (x); \
if (_err != SSO_OK) { \
fprintf(stderr, "SSO Error: %s\n", sso_strerror(_err)); \
exit(1); \
} \
}
static void write_to_file(const char *path, unsigned char *contents, size_t sz) {
static void write_to_file(const char *path, unsigned char *contents,
size_t sz) {
FILE *fp = fopen(path, "w");
if (fp == NULL) {
return;
......@@ -80,10 +81,8 @@ void ssotool_gen_keys(const char *public_key_file,
printf("secret key written to %s\n", secret_key_file);
}
void ssotool_sign(const char *secret_key_file,
const char *user,
const char *service,
const char *domain) {
void ssotool_sign(const char *secret_key_file, const char *user,
const char *service, const char *domain) {
unsigned char *secret_key = NULL;
char out[1024];
sso_ticket_t tkt;
......@@ -96,14 +95,12 @@ void ssotool_sign(const char *secret_key_file,
}
tkt = sso_ticket_new(user, service, domain, NULL, 9600);
CHECK_OK(sso_ticket_sign(tkt, secret_key, out, sizeof(out)-1));
CHECK_OK(sso_ticket_sign(tkt, secret_key, out, sizeof(out) - 1));
printf("%s\n", out);
}
void ssotool_verify(const char *public_key_file,
const char *service,
const char *domain,
const char *ticket) {
void ssotool_verify(const char *public_key_file, const char *service,
const char *domain, const char *ticket) {
unsigned char *public_key = NULL;
sso_ticket_t t = NULL;
size_t sz;
......@@ -120,26 +117,25 @@ void ssotool_verify(const char *public_key_file,
}
void show_help() {
fprintf(stderr,
"Usage: ssotool {--sign|--gen-keys} [<options>...]\n"
"Options:\n"
" --help show this help message\n"
" --gen-keys, -k generate a new public/secret keypair\n"
" --sign, -s create and sign a new ticket\n"
" --verify, -v verify a ticket\n"
"\n"
"Options for --gen-keys:\n"
"\n"
" --public-key FILE write the public key to FILE\n"
" --secret-key FILE write the secret key to FILE\n"
"\n"
"Options for --sign:\n"
"\n"
" --user USER username\n"
" --service SERVICE service name\n"
" --domain DOMAIN SSO domain\n"
" --secret-key FILE read the secret key from FILE\n"
"\n");
fprintf(stderr, "Usage: ssotool {--sign|--gen-keys} [<options>...]\n"
"Options:\n"
" --help show this help message\n"
" --gen-keys, -k generate a new public/secret keypair\n"
" --sign, -s create and sign a new ticket\n"
" --verify, -v verify a ticket\n"
"\n"
"Options for --gen-keys:\n"
"\n"
" --public-key FILE write the public key to FILE\n"
" --secret-key FILE write the secret key to FILE\n"
"\n"
"Options for --sign:\n"
"\n"
" --user USER username\n"
" --service SERVICE service name\n"
" --domain DOMAIN SSO domain\n"
" --secret-key FILE read the secret key from FILE\n"
"\n");
}
void die(const char *msg) {
......@@ -167,22 +163,21 @@ int main(int argc, char **argv) {
while (1) {
int c, option_index = 0;
static struct option long_options[] = {
{"help", 0, 0, 'h'},
{"sign", 0, 0, 's'},
{"verify", 0, 0, 'v'},
{"gen-keys", 0, 0, 'k'},
{"public-key", 1, 0, 'P'},
{"secret-key", 1, 0, 'S'},
{"user", 1, 0, 'u'},
{"service", 1, 0, 'z'},
{"domain", 1, 0, 'd'},
{NULL, 0, 0, 0},
{"help", 0, 0, 'h'},
{"sign", 0, 0, 's'},
{"verify", 0, 0, 'v'},
{"gen-keys", 0, 0, 'k'},
{"public-key", 1, 0, 'P'},
{"secret-key", 1, 0, 'S'},
{"user", 1, 0, 'u'},
{"service", 1, 0, 'z'},
{"domain", 1, 0, 'd'},
{NULL, 0, 0, 0},
};
c = getopt_long(argc, argv, "hskP:S:",
long_options, &option_index);
c = getopt_long(argc, argv, "hskP:S:", long_options, &option_index);
if (c == -1)
break;
break;
switch (c) {
case 'h':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment