[8bd77b5] | 1 | |
---|
| 2 | /** |
---|
| 3 | ** abac_id.c |
---|
| 4 | **/ |
---|
| 5 | |
---|
| 6 | #include <assert.h> |
---|
| 7 | #include <err.h> |
---|
| 8 | #include <time.h> |
---|
| 9 | |
---|
| 10 | // include the GNU extension of asprintf |
---|
| 11 | #define _GNU_SOURCE |
---|
| 12 | #include <stdio.h> |
---|
| 13 | #include <err.h> |
---|
| 14 | |
---|
| 15 | #include "abac_internal.h" |
---|
| 16 | #include "abac_util.h" |
---|
| 17 | |
---|
[440ba20] | 18 | static int debug=0; |
---|
| 19 | |
---|
[8bd77b5] | 20 | #define KEY_SUFFIX "_private.pem" |
---|
| 21 | #define CERT_SUFFIX "_ID.pem" |
---|
[ba6027a] | 22 | |
---|
| 23 | extern private_key_t *abac_key_private_key(abac_key_t *ptr); |
---|
| 24 | extern abac_key_t *abac_key_file_new(char *filename, chunk_t potato); |
---|
| 25 | extern abac_key_t *abac_key_chunk_new(chunk_t keyblob, chunk_t potato); |
---|
| 26 | extern chunk_t abac_key_chunk(abac_key_t *); |
---|
[8bd77b5] | 27 | |
---|
[09496b3] | 28 | static int _abac_id_set_cn(abac_id_t *id, char* cn); |
---|
| 29 | |
---|
[8bd77b5] | 30 | // |
---|
| 31 | // abac_id object |
---|
| 32 | // |
---|
| 33 | struct _abac_id_t { |
---|
[ba6027a] | 34 | int idtype; /* default to string 'keyid' for now */ |
---|
[8bd77b5] | 35 | |
---|
| 36 | char *keyid; /* sha */ |
---|
| 37 | char *cn; |
---|
| 38 | |
---|
| 39 | certificate_t *cert; |
---|
[ba6027a] | 40 | abac_key_t *key; |
---|
[8bd77b5] | 41 | |
---|
| 42 | int refcount; |
---|
| 43 | }; |
---|
| 44 | |
---|
| 45 | /****************************************************************/ |
---|
| 46 | // |
---|
| 47 | // Helper functions below |
---|
| 48 | // |
---|
| 49 | static char *_get_keyid(certificate_t *cert) { |
---|
| 50 | // get the keyid |
---|
| 51 | x509_t *x509 = (x509_t *)cert; |
---|
| 52 | chunk_t keyid = x509->get_subjectKeyIdentifier(x509); |
---|
| 53 | chunk_t string = chunk_to_hex(keyid, NULL, 0); |
---|
| 54 | return (char *)string.ptr; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | static char *_create_dn(char *cn) { |
---|
| 58 | |
---|
| 59 | #define DN "cn=" |
---|
| 60 | |
---|
| 61 | char *dn = abac_xmalloc(sizeof(DN) + strlen(cn)); |
---|
| 62 | memcpy(dn, DN, sizeof(DN)); |
---|
| 63 | strcat(dn, cn); |
---|
| 64 | |
---|
| 65 | return dn; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | /** |
---|
[137b55f] | 69 | * Generate ID certificate. |
---|
[8bd77b5] | 70 | * |
---|
| 71 | * validity is measured in seconds (as of 0.2.0) |
---|
| 72 | */ |
---|
[ba6027a] | 73 | static certificate_t *_generate_cert(abac_key_t *keyptr, char *cn, int validity) { |
---|
[8bd77b5] | 74 | // build the DN |
---|
| 75 | char *dn_string = _create_dn(cn); |
---|
| 76 | libabac_init(); |
---|
| 77 | |
---|
| 78 | identification_t *id = identification_create_from_string(dn_string); |
---|
| 79 | if (id == NULL) |
---|
| 80 | errx(1, "couldn't create ID from DN %s", dn_string); |
---|
| 81 | free(dn_string); |
---|
| 82 | |
---|
[94a4dd2] | 83 | // get the private key |
---|
[ba6027a] | 84 | private_key_t *private = abac_key_private_key(keyptr); |
---|
| 85 | if(private == NULL) |
---|
| 86 | errx(1, "couldn't create ID without private key"); |
---|
| 87 | |
---|
[8bd77b5] | 88 | public_key_t *public = private->get_public_key(private); |
---|
| 89 | if (public == NULL) |
---|
| 90 | errx(1, "couldn't get public key from private key"); |
---|
| 91 | |
---|
| 92 | // create a serial (stolen from strongswan pki) |
---|
| 93 | rng_t *rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK); |
---|
| 94 | if (!rng) |
---|
| 95 | errx(1, "no random number generator"); |
---|
| 96 | |
---|
| 97 | // random serial |
---|
| 98 | chunk_t serial = abac_generate_serial(); |
---|
| 99 | |
---|
| 100 | // validity period |
---|
| 101 | time_t not_before = time(NULL); |
---|
| 102 | time_t not_after = not_before + validity; |
---|
| 103 | |
---|
| 104 | // create! |
---|
| 105 | certificate_t *cert = lib->creds->create(lib->creds, |
---|
| 106 | CRED_CERTIFICATE, CERT_X509, |
---|
| 107 | BUILD_SIGNING_KEY, private, |
---|
| 108 | BUILD_PUBLIC_KEY, public, |
---|
| 109 | BUILD_SUBJECT, id, |
---|
| 110 | BUILD_NOT_BEFORE_TIME, not_before, |
---|
| 111 | BUILD_NOT_AFTER_TIME, not_after, |
---|
| 112 | BUILD_SERIAL, serial, |
---|
| 113 | BUILD_DIGEST_ALG, HASH_SHA1, |
---|
| 114 | BUILD_X509_FLAG, X509_CA, |
---|
[ba6027a] | 115 | BUILD_PATHLEN, X509_NO_CONSTRAINT, |
---|
[8bd77b5] | 116 | BUILD_END |
---|
| 117 | ); |
---|
| 118 | if (cert == NULL) |
---|
| 119 | errx(1, "couldn't build cert :("); |
---|
| 120 | |
---|
| 121 | DESTROY_IF(id); |
---|
| 122 | DESTROY_IF(public); |
---|
[ba6027a] | 123 | DESTROY_IF(private); |
---|
[8bd77b5] | 124 | free(serial.ptr); |
---|
| 125 | |
---|
| 126 | return cert; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | #define BYTES_PER_LINE 64 |
---|
| 130 | |
---|
| 131 | // thx libstrongswan |
---|
| 132 | static void _encode_base64(FILE *out, chunk_t encoding) { |
---|
| 133 | int start; |
---|
| 134 | |
---|
| 135 | chunk_t b64 = chunk_to_base64(encoding, NULL); |
---|
| 136 | |
---|
| 137 | fprintf(out, "-----BEGIN CERTIFICATE-----\n"); |
---|
| 138 | |
---|
| 139 | for (start = 0; start < b64.len; start += BYTES_PER_LINE) { |
---|
| 140 | int left = b64.len - start; |
---|
| 141 | int len = left < BYTES_PER_LINE ? left : BYTES_PER_LINE; |
---|
| 142 | fwrite(b64.ptr + start, len, 1, out); |
---|
| 143 | fprintf(out, "\n"); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | fprintf(out, "-----END CERTIFICATE-----\n"); |
---|
| 147 | |
---|
| 148 | free(b64.ptr); |
---|
| 149 | } |
---|
| 150 | /****************************************************************/ |
---|
| 151 | int abac_id_idtype(abac_id_t *id) |
---|
| 152 | { |
---|
| 153 | assert(id != NULL); |
---|
| 154 | return id->idtype; |
---|
| 155 | } |
---|
| 156 | |
---|
[5110d42] | 157 | char* abac_id_idtype_string(abac_id_t *id) |
---|
| 158 | { |
---|
| 159 | assert(id != NULL); |
---|
| 160 | return abac_idtype_string(id->idtype); |
---|
| 161 | } |
---|
| 162 | |
---|
[8bd77b5] | 163 | char *abac_id_keyid(abac_id_t *id) { |
---|
| 164 | assert(id != NULL); |
---|
| 165 | return id->keyid; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | char *abac_id_cn(abac_id_t *id) { |
---|
| 169 | assert(id != NULL); |
---|
| 170 | return id->cn; |
---|
| 171 | } |
---|
| 172 | |
---|
[09496b3] | 173 | static int _abac_id_set_cn(abac_id_t *id, char* cn) { |
---|
| 174 | if(cn) { |
---|
| 175 | if(id->cn != NULL) |
---|
| 176 | free(id->cn); |
---|
| 177 | id->cn = abac_xstrdup(cn); |
---|
| 178 | } |
---|
| 179 | return 0; |
---|
| 180 | } |
---|
| 181 | |
---|
[5110d42] | 182 | |
---|
| 183 | char *abac_id_name(abac_id_t *id) |
---|
| 184 | { |
---|
| 185 | assert(id != NULL); |
---|
[5d06689] | 186 | if(USE("ABAC_CN")) |
---|
[5110d42] | 187 | return abac_id_cn(id); |
---|
| 188 | else return abac_id_keyid(id); |
---|
| 189 | } |
---|
| 190 | |
---|
[5d06689] | 191 | char *abac_id_string(abac_id_t *id) |
---|
| 192 | { |
---|
| 193 | char *tmp=NULL; |
---|
| 194 | if(abac_id_has_privkey(id)) |
---|
| 195 | asprintf(&tmp,"(%s,%s,y)",abac_id_name(id),abac_id_idtype_string(id)); |
---|
| 196 | else asprintf(&tmp,"(%s,%s,n)",abac_id_name(id),abac_id_idtype_string(id)); |
---|
| 197 | return tmp; |
---|
| 198 | } |
---|
| 199 | |
---|
[8bd77b5] | 200 | /** |
---|
| 201 | * Get the issuer of an ID cert. |
---|
| 202 | * Returns a malloc'd string that must be free'd. |
---|
| 203 | */ |
---|
| 204 | char *abac_id_issuer(abac_id_t *id) { |
---|
| 205 | char *ret; |
---|
| 206 | int rv = asprintf(&ret, "%Y", id->cert->get_issuer(id->cert)); |
---|
| 207 | |
---|
| 208 | if (rv < 0) |
---|
| 209 | err(1, "couldn't malloc string for issuer\n"); |
---|
| 210 | |
---|
| 211 | return ret; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | int abac_id_lastone(abac_id_t *ptr) |
---|
| 215 | { |
---|
| 216 | assert(ptr); |
---|
| 217 | if(ptr->refcount == 1) |
---|
| 218 | return 1; |
---|
| 219 | return 0; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | /** |
---|
| 224 | * Gets the subject DN of an ID cert. |
---|
| 225 | * Returns a malloc'd string that must be free'd. |
---|
| 226 | */ |
---|
| 227 | char *abac_id_subject(abac_id_t *id) { |
---|
| 228 | char *ret; |
---|
| 229 | int rv = asprintf(&ret, "%Y", id->cert->get_subject(id->cert)); |
---|
| 230 | |
---|
| 231 | if (rv < 0) |
---|
| 232 | err(1, "couldn't malloc string for subject\n"); |
---|
| 233 | |
---|
| 234 | return ret; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | /** |
---|
| 238 | * Get the validity period. |
---|
| 239 | */ |
---|
| 240 | void abac_id_validity(abac_id_t *id, time_t *not_before, time_t *not_after) { |
---|
| 241 | id->cert->get_validity(id->cert, NULL, not_before, not_after); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | certificate_t *abac_id_cert(abac_id_t *id) { |
---|
| 245 | assert(id != NULL); |
---|
| 246 | return id->cert; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | private_key_t *abac_id_privkey(abac_id_t *id) { |
---|
| 250 | assert(id != NULL); |
---|
[ba6027a] | 251 | if(id->key !=0) |
---|
| 252 | return abac_key_private_key(id->key); |
---|
| 253 | return NULL; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | abac_key_t *abac_id_keyptr(abac_id_t *id) |
---|
| 257 | { |
---|
[8bd77b5] | 258 | return id->key; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | int abac_id_has_privkey(abac_id_t *id) { |
---|
| 262 | if(id->key !=0) |
---|
| 263 | return 1; |
---|
| 264 | return 0; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | /** |
---|
| 269 | * Write the ID cert to an open file pointer. |
---|
| 270 | */ |
---|
| 271 | void abac_id_write_cert(abac_id_t *id, FILE *out) { |
---|
| 272 | assert(id != NULL); |
---|
| 273 | |
---|
[137b55f] | 274 | chunk_t encoding = chunk_empty; |
---|
| 275 | int rc=id->cert->get_encoding(id->cert,CERT_ASN1_DER,&encoding); |
---|
| 276 | if(rc) { |
---|
| 277 | _encode_base64(out, encoding); |
---|
| 278 | free(encoding.ptr); |
---|
| 279 | } |
---|
[8bd77b5] | 280 | } |
---|
| 281 | |
---|
| 282 | /** |
---|
| 283 | * Write the private key to a file. |
---|
| 284 | * Returns false if there's no private key loaded |
---|
| 285 | */ |
---|
[ba6027a] | 286 | int abac_id_write_privkey(abac_id_t *id, FILE *out) |
---|
| 287 | { |
---|
| 288 | return abac_key_write_privkey(id->key, out); |
---|
[8bd77b5] | 289 | } |
---|
| 290 | |
---|
| 291 | /** |
---|
| 292 | * Get a DER-encoded chunk representing the cert. |
---|
| 293 | */ |
---|
[09496b3] | 294 | abac_chunk_t abac_id_cert_chunk(abac_id_t *id) { |
---|
| 295 | assert(id->cert); |
---|
| 296 | chunk_t encoding = chunk_empty; |
---|
| 297 | int rc= id->cert->get_encoding(id->cert, CERT_ASN1_DER, &encoding); |
---|
| 298 | abac_chunk_t ret= { encoding.ptr, encoding.len }; |
---|
| 299 | return ret; |
---|
| 300 | } |
---|
| 301 | |
---|
[9b43fc3] | 302 | /** |
---|
| 303 | * Get a PEM-encoded chunk representing the private key |
---|
| 304 | */ |
---|
[09496b3] | 305 | abac_chunk_t abac_id_privkey_chunk(abac_id_t *id) |
---|
| 306 | { |
---|
| 307 | chunk_t tmp=chunk_empty; |
---|
| 308 | if(id->key) |
---|
| 309 | tmp=abac_key_chunk(id->key); |
---|
| 310 | abac_chunk_t ret= {tmp.ptr, tmp.len}; |
---|
[9b43fc3] | 311 | return ret; |
---|
| 312 | } |
---|
| 313 | |
---|
[ba6027a] | 314 | void abac_id_print_key_chunk(abac_id_t *id) |
---|
| 315 | { |
---|
| 316 | if(id->key) |
---|
| 317 | print_abac_key("abac_id_print_key_chunk",id->key); |
---|
| 318 | } |
---|
| 319 | |
---|
[8bd77b5] | 320 | /** |
---|
| 321 | * Copy an abac ID. Actually just increases its reference count. |
---|
| 322 | */ |
---|
| 323 | abac_id_t *abac_id_dup(abac_id_t *id) { |
---|
[5110d42] | 324 | if(id) |
---|
| 325 | ++id->refcount; |
---|
[8bd77b5] | 326 | return id; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | void abac_id_free(abac_id_t *id) { |
---|
| 330 | if (id == NULL) |
---|
| 331 | return; |
---|
| 332 | |
---|
| 333 | --id->refcount; |
---|
| 334 | if (id->refcount > 0) |
---|
| 335 | return; |
---|
| 336 | |
---|
[ba6027a] | 337 | if(debug) |
---|
[b92a620] | 338 | fprintf(stderr,"making abac_id_free call on (%s)!!!\n", id->cn); |
---|
[8bd77b5] | 339 | // free once the reference count reaches 0 |
---|
| 340 | DESTROY_IF(id->cert); |
---|
[ba6027a] | 341 | |
---|
| 342 | if(id->key) abac_key_free(id->key); |
---|
[8bd77b5] | 343 | |
---|
| 344 | free(id->keyid); |
---|
| 345 | free(id->cn); |
---|
| 346 | free(id); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | /****************************************************************/ |
---|
[2efdff5] | 350 | /** |
---|
| 351 | * Default private key filename. Value must be freed by caller. |
---|
| 352 | */ |
---|
| 353 | char *abac_id_privkey_filename(abac_id_t *id) { |
---|
| 354 | assert(id != NULL); |
---|
| 355 | assert(id->cn != NULL); |
---|
| 356 | |
---|
| 357 | /* this cn has pCN attached to it */ |
---|
| 358 | char *filename = NULL; |
---|
| 359 | char* ptr=id->cn; |
---|
| 360 | asprintf(&filename, "%s%s",(ptr+1),KEY_SUFFIX); |
---|
| 361 | return filename; |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | /** |
---|
| 365 | * Get the default filename for the cert. Value must be freed by caller. |
---|
| 366 | */ |
---|
| 367 | char *abac_id_cert_filename(abac_id_t *id) { |
---|
| 368 | assert(id != NULL); |
---|
| 369 | assert(id->cn != NULL); |
---|
| 370 | |
---|
| 371 | /* this cn has pCN attached to it */ |
---|
| 372 | char *filename = NULL; |
---|
| 373 | char* ptr=id->cn; |
---|
| 374 | asprintf(&filename, "%s%s",(ptr+1),CERT_SUFFIX); |
---|
| 375 | return filename; |
---|
| 376 | } |
---|
| 377 | |
---|
[8bd77b5] | 378 | /****************************************************************/ |
---|
[09496b3] | 379 | |
---|
| 380 | /* |
---|
| 381 | ** Create an ID from tidbits info |
---|
| 382 | */ |
---|
[8bd77b5] | 383 | abac_id_t *abac_id_new(int idtype,char *keyid, char *cn, |
---|
| 384 | certificate_t *cert) |
---|
| 385 | { |
---|
| 386 | abac_id_t *id = abac_xmalloc(sizeof(abac_id_t)); |
---|
| 387 | id->idtype = idtype; |
---|
| 388 | id->keyid = abac_xstrdup(keyid); |
---|
| 389 | id->cn = abac_xstrdup(cn); |
---|
| 390 | id->cert = cert; |
---|
| 391 | id->key = NULL; |
---|
| 392 | id->refcount = 1; |
---|
[b92a620] | 393 | if(debug) fprintf(stderr,"abac_id_new: made a new id, %ld\n",(long) id); |
---|
[8bd77b5] | 394 | return id; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | abac_id_t *abac_id_keyid_new(char *keyid, char *cn, certificate_t *cert) |
---|
| 398 | { |
---|
| 399 | return abac_id_new(e_KEYID,keyid,cn,cert); |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | static abac_id_t *_abac_id_from_cert(certificate_t *cert) { |
---|
| 403 | abac_id_t *id = abac_xmalloc(sizeof(abac_id_t)); |
---|
| 404 | id->idtype = e_KEYID; |
---|
| 405 | id->keyid = NULL; |
---|
| 406 | id->cn = NULL; |
---|
| 407 | id->cert = cert; |
---|
| 408 | id->key = NULL; |
---|
| 409 | |
---|
| 410 | id->keyid = _get_keyid(id->cert); |
---|
| 411 | // get the CN from the cert |
---|
| 412 | id_part_t type; |
---|
| 413 | chunk_t data; |
---|
| 414 | |
---|
| 415 | identification_t *cert_id = id->cert->get_subject(id->cert); |
---|
| 416 | enumerator_t *id_enum = cert_id->create_part_enumerator(cert_id); |
---|
| 417 | while (id_enum->enumerate(id_enum, &type, &data)) |
---|
| 418 | if (type == ID_PART_RDN_CN) { |
---|
[09496b3] | 419 | id->cn = abac_xmalloc(data.len + 2); |
---|
| 420 | id->cn[0]='p'; |
---|
| 421 | memcpy(&id->cn[1], data.ptr, data.len); |
---|
| 422 | id->cn[data.len+1] = 0; |
---|
[8bd77b5] | 423 | } |
---|
| 424 | id_enum->destroy(id_enum); |
---|
| 425 | |
---|
[09496b3] | 426 | if(id->cn != NULL) { |
---|
| 427 | if(debug) fprintf(stderr, "_abac_id_from_cert, found cn (%s)\n", id->cn); |
---|
| 428 | } else { /* have to prepend a p */ |
---|
| 429 | char *tmp=NULL; |
---|
| 430 | asprintf(&tmp,"p%s",id->keyid); |
---|
| 431 | _abac_id_set_cn(id,tmp); |
---|
| 432 | free(tmp); |
---|
| 433 | if(debug) fprintf(stderr,"_abac_id_from_cert, did not find cn, set it to (%s)\n", id->cn); |
---|
| 434 | } |
---|
| 435 | |
---|
[8bd77b5] | 436 | id->refcount = 1; |
---|
| 437 | |
---|
| 438 | return id; |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | /** |
---|
[09496b3] | 442 | * Create an ID cert from a file. |
---|
[2efdff5] | 443 | * this one does not add id into id hash list |
---|
[8bd77b5] | 444 | */ |
---|
| 445 | abac_id_t *abac_id_from_file(char *filename) { |
---|
| 446 | |
---|
[2efdff5] | 447 | /* make sure file exists */ |
---|
| 448 | if(!file_exist(filename)) return NULL; |
---|
| 449 | |
---|
| 450 | libabac_init(); |
---|
[8bd77b5] | 451 | certificate_t *cert = lib->creds->create(lib->creds, |
---|
| 452 | CRED_CERTIFICATE, CERT_X509, |
---|
| 453 | BUILD_FROM_FILE, filename, |
---|
| 454 | BUILD_X509_FLAG, X509_AA, |
---|
| 455 | BUILD_END |
---|
| 456 | ); |
---|
| 457 | |
---|
| 458 | if (cert == NULL) |
---|
| 459 | return NULL; |
---|
| 460 | |
---|
| 461 | return _abac_id_from_cert(cert); |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | /** |
---|
[09496b3] | 465 | * Create an ID cert from a chunk. |
---|
[8bd77b5] | 466 | */ |
---|
| 467 | abac_id_t *abac_id_from_chunk(abac_chunk_t achunk) { |
---|
| 468 | chunk_t chunk = { .ptr = achunk.ptr, .len = achunk.len }; |
---|
| 469 | |
---|
| 470 | certificate_t *cert = lib->creds->create(lib->creds, |
---|
| 471 | CRED_CERTIFICATE, CERT_X509, |
---|
| 472 | BUILD_BLOB_ASN1_DER, chunk, |
---|
| 473 | BUILD_X509_FLAG, X509_AA, |
---|
| 474 | BUILD_END |
---|
| 475 | ); |
---|
| 476 | |
---|
| 477 | if (cert == NULL) |
---|
| 478 | return NULL; |
---|
| 479 | |
---|
| 480 | return _abac_id_from_cert(cert); |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | /** |
---|
| 484 | * Generate an ID with the specified CN and validity. |
---|
| 485 | * |
---|
| 486 | * validity is measured in seconds (as of 0.2.0) |
---|
| 487 | */ |
---|
[ba6027a] | 488 | int abac_id_generate(abac_id_t **ret, char *cn, int validity) |
---|
| 489 | { |
---|
[8bd77b5] | 490 | if (cn == NULL || !abac_validate_clean_name(cn)) |
---|
| 491 | return ABAC_ID_GENERATE_INVALID_CN; |
---|
| 492 | |
---|
[b92a620] | 493 | if(debug) fprintf(stderr,"abac_id_generate: generating id with cn(%s)\n",cn); |
---|
[5110d42] | 494 | |
---|
[8bd77b5] | 495 | if (validity < 0) |
---|
| 496 | return ABAC_ID_GENERATE_INVALID_VALIDITY; |
---|
| 497 | |
---|
| 498 | if (validity == 0) validity = 1080 * 86400; |
---|
| 499 | |
---|
| 500 | abac_id_t *id = abac_xmalloc(sizeof(abac_id_t)); |
---|
| 501 | |
---|
[440ba20] | 502 | char *tmp=NULL; |
---|
| 503 | asprintf(&tmp,"p%s",cn); |
---|
[8bd77b5] | 504 | id->idtype = e_KEYID; |
---|
[440ba20] | 505 | id->cn = tmp; |
---|
[ba6027a] | 506 | id->key = abac_key_generate(); |
---|
| 507 | id->cert = _generate_cert(id->key, cn, validity); |
---|
| 508 | id->keyid = _get_keyid(id->cert); |
---|
| 509 | |
---|
| 510 | id->refcount = 1; |
---|
| 511 | |
---|
| 512 | *ret = id; |
---|
| 513 | return ABAC_ID_SUCCESS; |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | |
---|
| 517 | /** |
---|
| 518 | * Generate an ID with the specified CN and validity and a passphrase. |
---|
| 519 | * |
---|
| 520 | * validity is measured in seconds (as of 0.2.0) |
---|
| 521 | */ |
---|
| 522 | int abac_id_generate_with_key(abac_id_t **ret, char *cn, int validity, char *filename, char *pfile) { |
---|
| 523 | if (cn == NULL || !abac_validate_clean_name(cn)) |
---|
| 524 | return ABAC_ID_GENERATE_INVALID_CN; |
---|
| 525 | |
---|
| 526 | if(debug) { |
---|
[b92a620] | 527 | fprintf(stderr,"abac_id_generate: generating id with cn(%s) and privkey(%s)", |
---|
[ba6027a] | 528 | cn, filename); |
---|
[b92a620] | 529 | if(pfile) fprintf(stderr,"pfile(%s)\n",pfile); |
---|
| 530 | else fprintf(stderr,"\n"); |
---|
[ba6027a] | 531 | } |
---|
| 532 | |
---|
| 533 | if (validity < 0) |
---|
| 534 | return ABAC_ID_GENERATE_INVALID_VALIDITY; |
---|
| 535 | |
---|
| 536 | if (validity == 0) validity = 1080 * 86400; |
---|
| 537 | |
---|
| 538 | chunk_t pp=chunk_empty; |
---|
| 539 | if(pfile && strlen(pfile)!=0) { |
---|
| 540 | if(!file_exist(pfile)) |
---|
| 541 | errx(1, "passphrase file does not exist!!\n"); |
---|
| 542 | pp=extract_potato(filename,pfile); |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | abac_id_t *id = abac_xmalloc(sizeof(abac_id_t)); |
---|
| 546 | char *tmp=NULL; |
---|
| 547 | asprintf(&tmp,"p%s",cn); |
---|
| 548 | id->idtype = e_KEYID; |
---|
| 549 | id->cn = tmp; |
---|
| 550 | id->key = abac_key_file_new(filename,pp); |
---|
[8bd77b5] | 551 | id->cert = _generate_cert(id->key, cn, validity); |
---|
| 552 | id->keyid = _get_keyid(id->cert); |
---|
| 553 | |
---|
| 554 | id->refcount = 1; |
---|
| 555 | |
---|
| 556 | *ret = id; |
---|
| 557 | return ABAC_ID_SUCCESS; |
---|
| 558 | } |
---|
| 559 | |
---|
[ba6027a] | 560 | int abac_id_load_privkey_file(abac_id_t *ptr, char *keyfile) |
---|
| 561 | { |
---|
| 562 | assert(ptr); |
---|
| 563 | ptr->key=abac_key_file_new(keyfile, chunk_empty); |
---|
| 564 | return 1; |
---|
| 565 | } |
---|
| 566 | |
---|
| 567 | int abac_id_load_enc_privkey_file(abac_id_t *ptr, char *keyfile, char *pfile) |
---|
| 568 | { |
---|
| 569 | assert(ptr); |
---|
| 570 | chunk_t pp=extract_potato(keyfile,pfile); |
---|
| 571 | ptr->key=abac_key_file_new(keyfile, pp); |
---|
| 572 | return 1; |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | int abac_id_load_privkey_chunk(abac_id_t *ptr, chunk_t keychunk) |
---|
| 576 | { |
---|
| 577 | assert(ptr); |
---|
| 578 | ptr->key=abac_key_chunk_new(keychunk, chunk_empty); |
---|
| 579 | if(debug) |
---|
| 580 | print_abac_key("after abac_id_load_privkey_chunk", ptr->key); |
---|
| 581 | return 1; |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | int abac_id_load_enc_privkey_chunk(abac_id_t *ptr, chunk_t keychunk, char *pfile) |
---|
| 585 | { |
---|
| 586 | assert(ptr); |
---|
| 587 | chunk_t pp=extract_potato(NULL,pfile); |
---|
| 588 | ptr->key=abac_key_chunk_new(keychunk, pp); |
---|
| 589 | return 1; |
---|
| 590 | } |
---|
| 591 | |
---|