source: creddy/libcreddy.c @ 8f58012

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since 8f58012 was 1b39ee5, checked in by Mike Ryan <mikeryan@…>, 13 years ago

allow hyphen in name, but make sure the name starts with an alnum
fixes #21

  • Property mode set to 100644
File size: 1.3 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 _
37    for (i = 1; string[i] != '\0'; ++i)
38        if (!isalnum(string[i]) && string[i] != '-' && string[i] != '_')
39            return 0;
40
41    return 1;
42}
43
44chunk_t creddy_generate_serial() {
45    chunk_t serial = chunk_empty;
46
47    // create a serial (stolen from strongswan pki)
48    rng_t *rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
49    if (!rng)
50        errx(1, "no random number generator");
51
52    rng->allocate_bytes(rng, 8, &serial);
53    while (serial.ptr[0] == 0)
54        // don't get leading 0's
55        rng->get_bytes(rng, 1, serial.ptr);
56    rng->destroy(rng);
57
58    return serial;
59}
Note: See TracBrowser for help on using the repository browser.