source: creddy/libcreddy.c @ 2579c1a

abac0-leakabac0-meicompt_changesmei-idmei-rt0-nmei_rt0tvf-new-xml
Last change on this file since 2579c1a was ef52155, checked in by Ted Faber <faber@…>, 12 years ago

Allow colon in roles

  • Property mode set to 100644
File size: 1.4 KB
Line 
1#include <assert.h>
2#include <ctype.h>
3#include <err.h>
4
5#include "libcreddy_common.h"
6
7void *creddy_xmalloc(size_t len) {
8    void *ret = malloc(len);
9    if (ret == NULL)
10        err(1, "couldn't malloc %d bytes\n", len);
11    return ret;
12}
13
14void *creddy_xrealloc(void *ptr, size_t size) {
15    void *ret = realloc(ptr, size);
16    if (ret == NULL)
17        err(1, "couldn't realloc %d bytes\n", size);
18    return ret;
19}
20
21char *creddy_xstrdup(char *string) {
22    char *dup = strdup(string);
23    if (dup == NULL)
24        err(1, "Can't dup %s", string);
25    return dup;
26}
27
28int creddy_clean_name(char *string) {
29    int i;
30
31    assert(string != NULL);
32
33    // must start with a letter/number
34    if (!isalnum(string[0])) return 0;
35
36    // Name must be alphanumeric or - or _ or :
37    for (i = 1; string[i] != '\0'; ++i)
38        if (!isalnum(string[i]) && string[i] != '-' && string[i] != '_' &&
39                string[i] != ':')
40            return 0;
41
42    return 1;
43}
44
45chunk_t creddy_generate_serial() {
46    chunk_t serial = chunk_empty;
47
48    // create a serial (stolen from strongswan pki)
49    rng_t *rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
50    if (!rng)
51        errx(1, "no random number generator");
52
53    rng->allocate_bytes(rng, 8, &serial);
54    while (serial.ptr[0] == 0)
55        // don't get leading 0's
56        rng->get_bytes(rng, 1, serial.ptr);
57    rng->destroy(rng);
58
59    return serial;
60}
Note: See TracBrowser for help on using the repository browser.