source: creddy/libcreddy.c @ b19d1f0

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since b19d1f0 was 11e3eb7, checked in by Mike Ryan <mikeryan@…>, 14 years ago

return binary representations of ID and attr certs in abac_chunk_t
suitable for passing off to libabac
supported in perl and python natively
closes #9

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