[461541a] | 1 | |
---|
| 2 | /* abac_openssl.c */ |
---|
| 3 | |
---|
| 4 | #define _GNU_SOURCE |
---|
| 5 | #include <stdio.h> |
---|
| 6 | #include <stdlib.h> |
---|
| 7 | #include <assert.h> |
---|
| 8 | #include <ctype.h> |
---|
| 9 | #include <unistd.h> |
---|
| 10 | |
---|
| 11 | #include <fcntl.h> |
---|
| 12 | #include <sys/types.h> |
---|
| 13 | #include <sys/stat.h> |
---|
| 14 | #include <sys/mman.h> |
---|
| 15 | #include <time.h> |
---|
| 16 | |
---|
| 17 | #include <stdbool.h> |
---|
| 18 | |
---|
| 19 | #include <openssl/conf.h> |
---|
| 20 | #include <openssl/x509.h> |
---|
| 21 | #include <openssl/x509v3.h> |
---|
| 22 | #include <openssl/x509_vfy.h> |
---|
| 23 | |
---|
| 24 | #include <openssl/rsa.h> |
---|
| 25 | #include <openssl/evp.h> |
---|
| 26 | #include <openssl/err.h> |
---|
| 27 | #include <openssl/pem.h> |
---|
| 28 | #include <openssl/ssl.h> |
---|
[bfaec48] | 29 | #include <openssl/sha.h> |
---|
[461541a] | 30 | #include <openssl/rand.h> |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | #ifdef HAVE_READPASSPHRASE |
---|
| 34 | # include <readpassphrase.h> |
---|
| 35 | #else |
---|
| 36 | # include "compat/readpassphrase.h" |
---|
| 37 | #endif |
---|
| 38 | |
---|
| 39 | static int debug=0; |
---|
| 40 | |
---|
| 41 | int _potato_cb(char *buf, int sz, int rwflag, void *u); |
---|
| 42 | |
---|
| 43 | /***********************************************************************/ |
---|
| 44 | int init_openssl() { |
---|
[b73c5d05] | 45 | if(debug) ERR_load_crypto_strings(); |
---|
[461541a] | 46 | OpenSSL_add_all_algorithms(); |
---|
| 47 | return 0; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | int deinit_openssl() { |
---|
| 51 | CRYPTO_cleanup_all_ex_data(); |
---|
| 52 | return 0; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | /* int RAND_bytes(unsigned char *buf, int num); */ |
---|
| 56 | unsigned char *abac_generate_serial() { |
---|
| 57 | unsigned char *serial=(unsigned char *) malloc(sizeof(unsigned char)*8); |
---|
| 58 | |
---|
| 59 | memset(serial, '\0', 8); |
---|
| 60 | |
---|
| 61 | if(!RAND_bytes(serial,8)) { |
---|
| 62 | fprintf(stderr,"RAT, RAN^D out of seeds!!!\n"); |
---|
| 63 | assert(0); |
---|
| 64 | } |
---|
| 65 | // zap leading 0's |
---|
| 66 | while (serial[0] == 0) |
---|
| 67 | RAND_bytes(&serial[0],1); |
---|
| 68 | |
---|
| 69 | RAND_cleanup(); |
---|
| 70 | return serial; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | static BIGNUM *_make_bn_from_string(unsigned char *str) |
---|
| 75 | { |
---|
| 76 | assert(str); |
---|
| 77 | BIGNUM *tmp; |
---|
| 78 | tmp=BN_bin2bn(str,8,NULL); |
---|
| 79 | /* BN_print_fp(stderr,tmp); */ |
---|
| 80 | int n=BN_num_bytes(tmp); |
---|
| 81 | if(n) return tmp; |
---|
| 82 | return NULL; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | unsigned char *_encode_m64(unsigned char *orig_ptr, int orig_len) |
---|
| 86 | { |
---|
| 87 | BIO *mbio,*b64bio,*bio; |
---|
| 88 | |
---|
| 89 | unsigned char *m64_ptr=NULL; |
---|
| 90 | int m64_len=0; |
---|
| 91 | |
---|
| 92 | unsigned char *ptr=NULL; |
---|
| 93 | |
---|
| 94 | if(orig_len==0) return NULL; |
---|
| 95 | |
---|
| 96 | /*bio pointing at b64->mem, the base64 bio encodes on |
---|
| 97 | write and decodes on read */ |
---|
| 98 | mbio=BIO_new(BIO_s_mem()); |
---|
| 99 | b64bio=BIO_new(BIO_f_base64()); |
---|
| 100 | bio=BIO_push(b64bio,mbio); |
---|
| 101 | |
---|
| 102 | BIO_write(bio,orig_ptr,orig_len); |
---|
| 103 | |
---|
| 104 | /* We need to 'flush' things to push out the encoding of the |
---|
| 105 | * last few bytes. There is special encoding if it is not a |
---|
| 106 | * multiple of 3 |
---|
| 107 | */ |
---|
| 108 | BIO_flush(bio); |
---|
| 109 | |
---|
| 110 | /* pointer to the data and the number of elements. */ |
---|
| 111 | m64_len=(int)BIO_ctrl(mbio,BIO_CTRL_INFO,0,ptr); |
---|
| 112 | |
---|
| 113 | if(m64_len!=0) { |
---|
| 114 | m64_ptr=malloc(m64_len+1); |
---|
| 115 | if(m64_ptr) { |
---|
| 116 | strcpy((char *)m64_ptr, (char *)ptr); |
---|
| 117 | } else { |
---|
| 118 | fprintf(stderr,"ERROR: malloc failed\n"); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /* This call will walk the chain freeing all the BIOs */ |
---|
| 123 | BIO_free_all(bio); |
---|
| 124 | return m64_ptr; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | unsigned char *_decode_m64(unsigned char *m64_ptr, int m64_len) |
---|
| 128 | { |
---|
| 129 | unsigned char *orig_ptr=NULL; |
---|
| 130 | int orig_len=0; |
---|
| 131 | |
---|
| 132 | BIO *b64bio, *mbio, *bio; |
---|
| 133 | char *ptr=NULL; |
---|
| 134 | |
---|
| 135 | if(m64_len==0) return NULL; |
---|
| 136 | |
---|
| 137 | ptr = (char *)malloc(sizeof(char)*m64_len); |
---|
| 138 | memset(ptr, '\0', m64_len); |
---|
| 139 | |
---|
| 140 | b64bio = BIO_new(BIO_f_base64()); |
---|
| 141 | mbio = BIO_new_mem_buf(m64_ptr, m64_len); |
---|
| 142 | bio = BIO_push(b64bio, mbio); |
---|
| 143 | |
---|
| 144 | orig_len=BIO_read(bio, ptr, m64_len); |
---|
| 145 | |
---|
| 146 | if(orig_len) { |
---|
| 147 | orig_ptr=malloc(orig_len+1); |
---|
| 148 | if(orig_ptr) |
---|
| 149 | strcpy((char *)orig_ptr, ptr); |
---|
| 150 | else fprintf(stderr,"ERROR: malloc failed..\n"); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | BIO_free_all(bio); |
---|
| 154 | return orig_ptr; |
---|
| 155 | } |
---|
| 156 | |
---|
[9ac7fb4] | 157 | /*** not used |
---|
[461541a] | 158 | static char *_read_blob_from_file(char *fname, int *len) |
---|
| 159 | { |
---|
| 160 | struct stat sb; |
---|
| 161 | char *dptr=NULL; |
---|
| 162 | |
---|
| 163 | int fd = open(fname, O_RDONLY); |
---|
| 164 | if (fd == -1) { return NULL; } |
---|
| 165 | if(stat(fname, &sb) == -1) { |
---|
| 166 | close(fd); |
---|
| 167 | return NULL; |
---|
| 168 | } |
---|
| 169 | dptr= (char *)mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
---|
| 170 | close(fd); |
---|
| 171 | |
---|
| 172 | if(dptr == MAP_FAILED) { |
---|
| 173 | return NULL; |
---|
| 174 | } |
---|
| 175 | *len=sb.st_size; |
---|
| 176 | return dptr; |
---|
| 177 | } |
---|
[9ac7fb4] | 178 | ***/ |
---|
[461541a] | 179 | |
---|
| 180 | /* Read ID in PEM */ |
---|
| 181 | X509 *abac_load_id_from_fp(FILE *fp) |
---|
| 182 | { |
---|
| 183 | X509 *cert=PEM_read_X509(fp,NULL,NULL,NULL); |
---|
| 184 | |
---|
| 185 | if (cert == NULL) { |
---|
[b73c5d05] | 186 | if(debug) ERR_print_errors_fp (stderr); |
---|
[461541a] | 187 | return NULL; |
---|
| 188 | } |
---|
| 189 | return cert; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | X509 *abac_load_id_from_chunk(unsigned char *chunk_ptr, int chunk_len) |
---|
| 193 | { |
---|
| 194 | X509 *n509=NULL; |
---|
| 195 | BIO *mbio=BIO_new(BIO_s_mem()); |
---|
| 196 | |
---|
| 197 | BIO_write(mbio,chunk_ptr,chunk_len); |
---|
| 198 | BIO_flush(mbio); |
---|
| 199 | |
---|
| 200 | PEM_read_bio_X509(mbio,&n509,NULL,NULL); |
---|
| 201 | |
---|
| 202 | BIO_free_all(mbio); |
---|
| 203 | |
---|
| 204 | if (n509 == NULL) { |
---|
[b73c5d05] | 205 | if(debug) ERR_print_errors_fp (stderr); |
---|
[461541a] | 206 | return NULL; |
---|
| 207 | } |
---|
| 208 | return n509; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | int abac_write_id_to_fp(X509 *cert, FILE *fp) |
---|
| 212 | { |
---|
| 213 | assert(cert); |
---|
| 214 | |
---|
[7e3f5e2] | 215 | if(!PEM_write_X509(fp,cert)) { |
---|
[b73c5d05] | 216 | if(debug) ERR_print_errors_fp (stderr); |
---|
[7e3f5e2] | 217 | return 1; |
---|
[461541a] | 218 | } |
---|
| 219 | return 0; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | /* make stringfy a private key PEM struct */ |
---|
| 223 | unsigned char *abac_string_privkey(EVP_PKEY *key) |
---|
| 224 | { |
---|
| 225 | unsigned char *ptr=NULL; |
---|
| 226 | unsigned char *tmp=NULL; |
---|
| 227 | |
---|
| 228 | assert(key); |
---|
| 229 | |
---|
| 230 | BIO *mbio=BIO_new(BIO_s_mem()); |
---|
| 231 | /* PEM_write_PrivateKey(fp,key,NULL,NULL,0,_potato_cb, "privateKey to file"); */ |
---|
| 232 | PEM_write_bio_PrivateKey(mbio,key,NULL,NULL,0,_potato_cb,"stringify privateKey"); |
---|
| 233 | BIO_flush(mbio); |
---|
| 234 | int len=(int)BIO_ctrl(mbio,BIO_CTRL_INFO,0,tmp); |
---|
| 235 | |
---|
| 236 | if(debug) fprintf(stderr,"CHUNKING PrivateKey... %d\n",len); |
---|
| 237 | |
---|
| 238 | if(len) { |
---|
| 239 | ptr=(unsigned char *)malloc(sizeof(unsigned char *)*(len+1)); |
---|
| 240 | int ret=BIO_read(mbio, (void *)ptr, len); |
---|
| 241 | if(ret==0) |
---|
| 242 | fprintf(stderr," abac_string_privkey failed!!\n"); |
---|
| 243 | ptr[len]='\0'; |
---|
| 244 | } |
---|
| 245 | BIO_free_all(mbio); |
---|
| 246 | return ptr; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | /* make stringfy a x509 PEM struct */ |
---|
| 250 | unsigned char *abac_string_cert(X509 *cert) { |
---|
| 251 | unsigned char *ptr=NULL; |
---|
| 252 | unsigned char *tmp=NULL; |
---|
| 253 | |
---|
| 254 | assert(cert); |
---|
| 255 | |
---|
| 256 | BIO *mbio=BIO_new(BIO_s_mem()); |
---|
| 257 | PEM_write_bio_X509(mbio,cert); |
---|
| 258 | BIO_flush(mbio); |
---|
| 259 | int len=(int)BIO_ctrl(mbio,BIO_CTRL_INFO,0,tmp); |
---|
| 260 | if(debug) fprintf(stderr,"CHUNKING X509... %d\n",len); |
---|
| 261 | |
---|
| 262 | if(len) { |
---|
| 263 | ptr=(unsigned char *)malloc(sizeof(unsigned char *)*(len+1)); |
---|
| 264 | int ret=BIO_read(mbio, (void *)ptr, len); |
---|
| 265 | if(ret==0) |
---|
| 266 | fprintf(stderr," abac_string_cert failed!!\n"); |
---|
| 267 | ptr[len]='\0'; |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | BIO_free_all(mbio); |
---|
| 271 | return ptr; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | |
---|
[9ac7fb4] | 275 | /* not used, sign data with privkey |
---|
[461541a] | 276 | static int _sign_with_privkey(EVP_PKEY *privkey, char *data, |
---|
| 277 | unsigned char* signed_buf) |
---|
| 278 | { |
---|
| 279 | int err; |
---|
| 280 | unsigned int signed_len; |
---|
| 281 | EVP_MD_CTX md_ctx; |
---|
| 282 | |
---|
| 283 | EVP_SignInit (&md_ctx, EVP_md5()); |
---|
| 284 | EVP_SignUpdate (&md_ctx, data, strlen(data)); |
---|
| 285 | signed_len = sizeof(signed_buf); |
---|
| 286 | err = EVP_SignFinal (&md_ctx, |
---|
| 287 | signed_buf, |
---|
| 288 | &signed_len, |
---|
| 289 | privkey); |
---|
| 290 | if (err != 1) { |
---|
[b73c5d05] | 291 | if(debug) ERR_print_errors_fp (stderr); |
---|
[461541a] | 292 | return 1; |
---|
| 293 | } |
---|
| 294 | return 0; |
---|
| 295 | } |
---|
[9ac7fb4] | 296 | **/ |
---|
[461541a] | 297 | |
---|
[9ac7fb4] | 298 | /* nost used, verify the signature.. |
---|
[461541a] | 299 | static int _verify_with_pubkey(EVP_PKEY *pubkey, char *data, |
---|
| 300 | unsigned char* signed_buf ) |
---|
| 301 | { |
---|
| 302 | int err; |
---|
| 303 | int signed_len; |
---|
| 304 | EVP_MD_CTX md_ctx; |
---|
| 305 | |
---|
| 306 | EVP_VerifyInit (&md_ctx, EVP_sha1()); |
---|
| 307 | EVP_VerifyUpdate (&md_ctx, data, strlen((char*)data)); |
---|
| 308 | signed_len=sizeof(signed_buf); |
---|
| 309 | err = EVP_VerifyFinal (&md_ctx, |
---|
| 310 | signed_buf, |
---|
| 311 | signed_len, |
---|
| 312 | pubkey); |
---|
| 313 | |
---|
| 314 | if (err != 1) { |
---|
[b73c5d05] | 315 | if(debug) ERR_print_errors_fp (stderr); |
---|
[461541a] | 316 | return 1; |
---|
| 317 | } |
---|
| 318 | fprintf(stderr, "Signature Verified Ok.\n"); |
---|
| 319 | return 0; |
---|
| 320 | } |
---|
[9ac7fb4] | 321 | ***/ |
---|
[461541a] | 322 | |
---|
| 323 | |
---|
| 324 | #define PWLEN 128 |
---|
| 325 | /* EVP_PKEY *PEM_read_PrivateKey(FILE *,EVP_PKEY **,pem_cb *,void *) */ |
---|
| 326 | int _potato_cb(char *buf, int sz, int rwflag, void *u) |
---|
| 327 | { |
---|
| 328 | int len; |
---|
| 329 | char *prompt=NULL; |
---|
[3c30b59] | 330 | int rc; |
---|
[461541a] | 331 | if(u) |
---|
[3c30b59] | 332 | rc=asprintf(&prompt,"Enter passphrase for %s:", (char *)u); |
---|
| 333 | else rc=asprintf(&prompt,"Enter passphrase :"); |
---|
[461541a] | 334 | char *secret = malloc(PWLEN); |
---|
| 335 | memset(secret, '\0', PWLEN); |
---|
| 336 | if(!secret) { |
---|
| 337 | perror("malloc()"); |
---|
| 338 | free(prompt); |
---|
| 339 | return 0; |
---|
| 340 | } |
---|
| 341 | if (readpassphrase( prompt, secret, PWLEN, RPP_ECHO_OFF) == NULL) { |
---|
| 342 | perror("readpassphrase()"); |
---|
| 343 | memset(secret, '\0', PWLEN); |
---|
| 344 | len=0; |
---|
| 345 | } else { |
---|
| 346 | len=strlen(secret); |
---|
| 347 | memcpy(buf, secret, len); |
---|
| 348 | memset(secret, '\0', len); |
---|
| 349 | } |
---|
| 350 | free(secret); |
---|
| 351 | free(prompt); |
---|
| 352 | return len; |
---|
| 353 | } |
---|
| 354 | |
---|
[a02c849] | 355 | EVP_PKEY *abac_load_privkey_from_chunk(unsigned char *chunk_ptr, int chunk_len) |
---|
| 356 | { |
---|
| 357 | EVP_PKEY *nkey=NULL; |
---|
| 358 | BIO *mbio=BIO_new(BIO_s_mem()); |
---|
| 359 | |
---|
| 360 | BIO_write(mbio,chunk_ptr,chunk_len); |
---|
| 361 | BIO_flush(mbio); |
---|
| 362 | |
---|
| 363 | PEM_read_bio_PrivateKey(mbio,&nkey,NULL,NULL); |
---|
| 364 | |
---|
| 365 | BIO_free_all(mbio); |
---|
| 366 | |
---|
| 367 | if (nkey == NULL) { |
---|
[b73c5d05] | 368 | if(debug) ERR_print_errors_fp (stderr); |
---|
[a02c849] | 369 | return NULL; |
---|
| 370 | } |
---|
| 371 | return nkey; |
---|
| 372 | } |
---|
| 373 | |
---|
[461541a] | 374 | EVP_PKEY *abac_load_privkey_from_fp(FILE *fp) |
---|
| 375 | { |
---|
| 376 | assert(fp); |
---|
| 377 | |
---|
| 378 | EVP_PKEY *privkey = PEM_read_PrivateKey(fp, NULL, _potato_cb, "privateKey from file"); |
---|
| 379 | |
---|
| 380 | if (privkey == NULL) { |
---|
[b73c5d05] | 381 | if(debug) ERR_print_errors_fp (stderr); |
---|
[461541a] | 382 | return NULL; |
---|
| 383 | } |
---|
| 384 | return privkey; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | /* not adding passphrase */ |
---|
| 388 | int abac_write_privkey_to_fp(EVP_PKEY *key, FILE *fp) { |
---|
| 389 | assert(key); |
---|
| 390 | |
---|
[7e3f5e2] | 391 | if(!PEM_write_PrivateKey(fp,key,NULL,NULL,0,NULL, NULL)) { |
---|
[b73c5d05] | 392 | if(debug) ERR_print_errors_fp (stderr); |
---|
[7e3f5e2] | 393 | return 1; |
---|
| 394 | } |
---|
[461541a] | 395 | return 0; |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | /* adding passphrase */ |
---|
| 399 | int abac_write_encrypt_privkey_to_fp(EVP_PKEY *key, FILE *fp) { |
---|
| 400 | assert(key); |
---|
| 401 | |
---|
[7e3f5e2] | 402 | if(!PEM_write_PrivateKey(fp,key,NULL,NULL,0,_potato_cb, "privateKey to file")) { |
---|
[b73c5d05] | 403 | if(debug) ERR_print_errors_fp (stderr); |
---|
[7e3f5e2] | 404 | return 1; |
---|
| 405 | } |
---|
[461541a] | 406 | return 0; |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | EVP_PKEY *extract_pubkey_from_cert(X509 *cert) |
---|
| 410 | { |
---|
| 411 | EVP_PKEY *pubkey=X509_get_pubkey(cert); |
---|
| 412 | return pubkey; |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | |
---|
[9ac7fb4] | 416 | /** not used, |
---|
[461541a] | 417 | static void _callback(int p, int n, void *arg) |
---|
| 418 | { |
---|
| 419 | char c='B'; |
---|
| 420 | |
---|
| 421 | if (p == 0) c='.'; |
---|
| 422 | if (p == 1) c='+'; |
---|
| 423 | if (p == 2) c='*'; |
---|
| 424 | if (p == 3) c='\n'; |
---|
| 425 | fputc(c,stderr); |
---|
| 426 | } |
---|
[9ac7fb4] | 427 | ***/ |
---|
[461541a] | 428 | |
---|
| 429 | /* |
---|
| 430 | RSA *RSA_generate_key(int num, unsigned long e, |
---|
| 431 | void (*callback)(int,int,void *), void *cb_arg); |
---|
| 432 | The exponent is an odd number, typically 3, 17 or 65537 |
---|
| 433 | */ |
---|
| 434 | EVP_PKEY* abac_generate_key() |
---|
| 435 | { |
---|
| 436 | EVP_PKEY *pk=NULL; |
---|
| 437 | int keysize=2048; |
---|
| 438 | |
---|
| 439 | if((pk=EVP_PKEY_new()) == NULL){ |
---|
[b73c5d05] | 440 | if(debug) ERR_print_errors_fp (stderr); |
---|
[461541a] | 441 | return NULL; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | // RSA *rsa=RSA_generate_key(keysize,RSA_F4,_callback,NULL); |
---|
| 445 | RSA *rsa=RSA_generate_key(keysize,RSA_F4,NULL,NULL); |
---|
| 446 | if (!EVP_PKEY_assign_RSA(pk,rsa)) { |
---|
[b73c5d05] | 447 | if(debug) ERR_print_errors_fp (stderr); |
---|
[461541a] | 448 | return NULL; |
---|
| 449 | } |
---|
| 450 | rsa=NULL; |
---|
| 451 | |
---|
| 452 | return pk; |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | /* Add extension using V3 code: we can set the config file as NULL |
---|
| 456 | * because we wont reference any other sections. |
---|
| 457 | */ |
---|
| 458 | static int _add_ext(X509 *cert, int nid, char *value) |
---|
| 459 | { |
---|
| 460 | X509_EXTENSION *ex; |
---|
| 461 | X509V3_CTX ctx; |
---|
| 462 | /* This sets the 'context' of the extensions. */ |
---|
| 463 | /* No configuration database */ |
---|
| 464 | X509V3_set_ctx_nodb(&ctx); |
---|
| 465 | /* Issuer and subject certs: both the target since it is self signed, |
---|
| 466 | * no request and no CRL |
---|
| 467 | */ |
---|
| 468 | X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0); |
---|
| 469 | ex = X509V3_EXT_conf_nid(NULL, &ctx, nid, value); |
---|
| 470 | if (!ex) |
---|
| 471 | return 0; |
---|
| 472 | |
---|
| 473 | X509_add_ext(cert,ex,-1); |
---|
| 474 | X509_EXTENSION_free(ex); |
---|
| 475 | return 1; |
---|
| 476 | } |
---|
| 477 | |
---|
| 478 | |
---|
| 479 | /** |
---|
| 480 | * Generate ID certificate. |
---|
| 481 | * |
---|
| 482 | * validity is measured in seconds (as of 0.2.0) |
---|
| 483 | */ |
---|
| 484 | X509 *abac_generate_cert(EVP_PKEY *pkey, char *cn, long validity) { |
---|
| 485 | |
---|
[9e063cb] | 486 | /* must have a privkey before generating an ID cert */ |
---|
[461541a] | 487 | assert(pkey); |
---|
| 488 | X509 *cert=NULL; |
---|
| 489 | |
---|
| 490 | if((cert=X509_new()) == NULL) |
---|
| 491 | goto error; |
---|
| 492 | |
---|
| 493 | unsigned char *serial=abac_generate_serial(); |
---|
| 494 | |
---|
| 495 | if(validity == 0) validity=(long)(60*60*24*(365)); |
---|
| 496 | |
---|
| 497 | X509_set_version(cert,2); |
---|
| 498 | |
---|
| 499 | BIGNUM *bn=_make_bn_from_string(serial); |
---|
| 500 | BN_to_ASN1_INTEGER(bn, X509_get_serialNumber(cert)); |
---|
| 501 | /* this is prone to problem with very big days on 32 bit machines, |
---|
| 502 | In newer openssl, can migrate to X509_time_adj_ex */ |
---|
| 503 | X509_gmtime_adj(X509_get_notBefore(cert),0); |
---|
| 504 | X509_gmtime_adj(X509_get_notAfter(cert),validity); |
---|
| 505 | X509_set_pubkey(cert,pkey); |
---|
| 506 | |
---|
| 507 | X509_NAME *name=X509_get_subject_name(cert); |
---|
| 508 | |
---|
| 509 | if(!name) goto error; |
---|
| 510 | |
---|
| 511 | /* This function creates and adds the entry, working out the |
---|
| 512 | * correct string type and performing checks on its length. |
---|
| 513 | */ |
---|
| 514 | if(!(X509_NAME_add_entry_by_txt(name,"CN", MBSTRING_ASC, (unsigned char *)cn, -1, -1, 0))) |
---|
| 515 | goto error; // fail to add cn to cert |
---|
| 516 | |
---|
| 517 | /* Self signed, set the issuer name to be the same as the subject. */ |
---|
| 518 | if(!(X509_set_issuer_name(cert,name))) |
---|
| 519 | goto error; // fail to set issuer name on cert |
---|
| 520 | |
---|
| 521 | /* Add various extensions: standard extensions */ |
---|
| 522 | if(!(_add_ext(cert, NID_basic_constraints, "critical,CA:TRUE"))) |
---|
| 523 | goto error; // fail to set basic constraint |
---|
| 524 | if(!(_add_ext(cert, NID_key_usage, "critical,keyCertSign,cRLSign"))) |
---|
| 525 | goto error; // fail to set key usage |
---|
| 526 | if(!(_add_ext(cert, NID_subject_key_identifier, "hash"))) |
---|
| 527 | goto error; // fail to set subject key identifier |
---|
| 528 | if(!(_add_ext(cert, NID_authority_key_identifier, "keyid:always"))) |
---|
| 529 | goto error; // fail to set authority key identifier (self-signing) |
---|
| 530 | |
---|
| 531 | /* make sure it is signed */ |
---|
| 532 | if (!X509_sign(cert,pkey,EVP_sha1())) |
---|
| 533 | goto error; |
---|
| 534 | |
---|
| 535 | return cert; |
---|
| 536 | |
---|
| 537 | error: |
---|
| 538 | if(cert) X509_free(cert); |
---|
| 539 | if(serial) free(serial); |
---|
| 540 | if(bn) BN_free(bn); |
---|
| 541 | return NULL; |
---|
| 542 | } |
---|
| 543 | |
---|
[9ac7fb4] | 544 | /*** not used, |
---|
[461541a] | 545 | static char *_time_in_string(ASN1_TIME *tm) |
---|
| 546 | { |
---|
| 547 | char *ptr=NULL; |
---|
| 548 | BIO *mbio=BIO_new(BIO_s_mem()); |
---|
| 549 | ASN1_TIME_print(mbio, tm); |
---|
| 550 | BIO_flush(mbio); |
---|
| 551 | int len=BIO_number_written(mbio); |
---|
| 552 | ptr=(char *) malloc(sizeof(char *)*(len+1)); |
---|
| 553 | int ret=BIO_read(mbio, (void *)ptr, len); |
---|
| 554 | |
---|
| 555 | BIO_free_all(mbio); |
---|
| 556 | if(ret) |
---|
| 557 | return ptr; |
---|
| 558 | else return NULL; |
---|
| 559 | } |
---|
[9ac7fb4] | 560 | ***/ |
---|
[461541a] | 561 | |
---|
| 562 | |
---|
| 563 | /* atime->data, YYmmddHHMMSS or YYYYmmddHHMMSSZZ |
---|
| 564 | * V_ASN1_UTCTIME, V_ASN1_GENERALIZEDTIME |
---|
| 565 | */ |
---|
| 566 | static int _convert_time(struct tm *ttime, ASN1_TIME *atime) { |
---|
| 567 | assert(atime); assert(atime->data); |
---|
| 568 | |
---|
| 569 | int type=atime->type; |
---|
| 570 | int len=strlen((char *)atime->data); |
---|
| 571 | if(len==0) return 0; |
---|
| 572 | |
---|
| 573 | char *astring=strndup((char *)atime->data,len); |
---|
| 574 | |
---|
| 575 | /* setting ttime structure */ |
---|
| 576 | if (type == V_ASN1_UTCTIME) { |
---|
| 577 | strptime(astring, "%y%m%d%H%M%S", ttime); |
---|
| 578 | } else { |
---|
| 579 | if (type == V_ASN1_GENERALIZEDTIME) |
---|
| 580 | strptime(astring, "%Y%m%d%H%M%S", ttime); |
---|
| 581 | else fprintf(stderr,"ERROR,.. unknown type in ASN1_TIME struct\n"); |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | if(debug) { |
---|
| 585 | char *tstring=asctime(ttime); |
---|
| 586 | fprintf(stderr,"PPP final time string is %s\n",tstring); |
---|
| 587 | } |
---|
| 588 | return 1; |
---|
| 589 | } |
---|
| 590 | |
---|
| 591 | /* check whether the cert is still valid or not and also extract what its |
---|
[9e063cb] | 592 | not_before and not_after field, 0 is okay, 1 is not */ |
---|
[461541a] | 593 | int abac_check_validity(X509 *cert, struct tm *not_before, struct tm *not_after) { |
---|
| 594 | assert(cert); |
---|
| 595 | |
---|
| 596 | int valid=0; |
---|
| 597 | int ret=0; |
---|
| 598 | memset(not_before, 0, sizeof(struct tm)); |
---|
| 599 | memset(not_after, 0, sizeof(struct tm)); |
---|
| 600 | ASN1_TIME *notAfter= X509_get_notAfter(cert); |
---|
| 601 | ASN1_TIME *notBefore=X509_get_notBefore(cert); |
---|
| 602 | |
---|
| 603 | ret=_convert_time(not_before, notBefore); |
---|
[9e063cb] | 604 | if(ret==0) return 1; |
---|
[461541a] | 605 | ret=_convert_time(not_after, notAfter); |
---|
[9e063cb] | 606 | if(ret==0) return 1; |
---|
[461541a] | 607 | |
---|
| 608 | if((X509_cmp_current_time(notBefore) >=0) || |
---|
| 609 | (X509_cmp_current_time(notAfter) <=0) ) |
---|
| 610 | valid=1; |
---|
| 611 | |
---|
| 612 | if(valid) return 0; |
---|
| 613 | else return 1; |
---|
| 614 | } |
---|
| 615 | |
---|
| 616 | /* check if cert is still valid at current time, 1 for yes, 0 for no*/ |
---|
| 617 | int abac_still_valid(X509 *cert) |
---|
| 618 | { |
---|
| 619 | ASN1_TIME *notAfter= X509_get_notAfter(cert); |
---|
| 620 | ASN1_TIME *notBefore=X509_get_notBefore(cert); |
---|
| 621 | if(0) { |
---|
| 622 | fprintf(stderr,"((X509_cmp_current_time(notBefore) is %d\n", |
---|
| 623 | X509_cmp_current_time(notBefore)); |
---|
| 624 | fprintf(stderr,"((X509_cmp_current_time(notAfter) is %d\n", |
---|
| 625 | X509_cmp_current_time(notAfter)); |
---|
| 626 | } |
---|
| 627 | if((X509_cmp_current_time(notBefore) >=0) || |
---|
| 628 | (X509_cmp_current_time(notAfter) <=0) ) |
---|
| 629 | return 0; |
---|
| 630 | return 1; |
---|
| 631 | } |
---|
| 632 | |
---|
| 633 | char *abac_get_cn(X509 *cert) |
---|
| 634 | { |
---|
| 635 | X509_NAME *nptr=X509_get_subject_name (cert); |
---|
| 636 | int pos=X509_NAME_get_index_by_NID(nptr, NID_commonName,-1); |
---|
| 637 | X509_NAME_ENTRY *ent=X509_NAME_get_entry(nptr,pos); |
---|
| 638 | ASN1_STRING *adata=X509_NAME_ENTRY_get_data(ent); |
---|
| 639 | unsigned char *val=ASN1_STRING_data(adata); |
---|
| 640 | if(debug) fprintf(stderr," cn:(%d)(%s)\n", strlen((char *)val), val); |
---|
| 641 | return (char *) val; |
---|
| 642 | } |
---|
| 643 | |
---|
| 644 | char *abac_get_serial(X509 *cert) |
---|
| 645 | { |
---|
| 646 | char *ret=NULL; |
---|
| 647 | ASN1_INTEGER *num=X509_get_serialNumber(cert); |
---|
| 648 | BIGNUM *bnser=ASN1_INTEGER_to_BN(num,NULL); |
---|
| 649 | int n=BN_num_bytes(bnser); |
---|
| 650 | unsigned char buf[n]; |
---|
| 651 | int b=BN_bn2bin(bnser,buf); |
---|
| 652 | if(debug) { |
---|
| 653 | fprintf(stderr," extract serial number:->(%d)",b); |
---|
| 654 | BN_print_fp(stderr,bnser); |
---|
| 655 | } |
---|
| 656 | if(n) |
---|
| 657 | ret=strndup((char *)buf,n); |
---|
| 658 | return ret; |
---|
| 659 | } |
---|
| 660 | |
---|
| 661 | char *abac_get_subject(X509 *cert) |
---|
| 662 | { |
---|
| 663 | char *ptr=X509_NAME_oneline(X509_get_subject_name(cert),0,0); |
---|
| 664 | return ptr; |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | char *abac_get_issuer(X509 *cert) |
---|
| 668 | { |
---|
| 669 | char *ptr=X509_NAME_oneline(X509_get_issuer_name(cert),0,0); |
---|
| 670 | return ptr; |
---|
| 671 | } |
---|
| 672 | |
---|
[bfaec48] | 673 | // success: malloc'd calculated SHA1 of the key (as per RFC3280) |
---|
[461541a] | 674 | // fail: NULL |
---|
| 675 | unsigned char *abac_get_keyid(X509 *cert) |
---|
| 676 | { |
---|
[bfaec48] | 677 | char digest[SHA_DIGEST_LENGTH]; /* SSL computed key digest */ |
---|
| 678 | /* ASCII (UTF-8 compatible) text for the digest */ |
---|
| 679 | unsigned char *sha=(unsigned char *) malloc(2*SHA_DIGEST_LENGTH+1); |
---|
| 680 | int i; /* Scratch */ |
---|
| 681 | |
---|
| 682 | if ( !sha) return NULL; |
---|
| 683 | |
---|
| 684 | if ( !X509_pubkey_digest(cert, EVP_sha1(), digest, NULL)) { |
---|
| 685 | free(sha); |
---|
| 686 | return NULL; |
---|
[461541a] | 687 | } |
---|
| 688 | |
---|
[bfaec48] | 689 | /* Translate to ASCII */ |
---|
| 690 | for ( i = 0; i < SHA_DIGEST_LENGTH; i++) |
---|
| 691 | snprintf((char *) sha+2*i, 3, "%02x", digest[i] & 0xff); |
---|
| 692 | sha[2*SHA_DIGEST_LENGTH] = '\0'; |
---|
| 693 | |
---|
[461541a] | 694 | if(debug) fprintf(stderr,"SHA1 -> %s\n",sha); |
---|
[bfaec48] | 695 | return sha; |
---|
[461541a] | 696 | } |
---|