/** * load the credential from a file descriptor */ static void *load_from_fd(int fd, credential_type_t type, int subtype, identification_t *subject, x509_flag_t flags) { char buf[8096]; char *pos = buf; ssize_t len, total = 0; while (TRUE) { len = read(fd, pos, buf + sizeof(buf) - pos); if (len < 0) { DBG1(DBG_LIB, "reading from file descriptor failed: %s", strerror(errno)); return NULL; } if (len == 0) { break; } total += len; if (total == sizeof(buf)) { DBG1(DBG_LIB, "buffer too small to read from file descriptor"); return NULL; } } return load_from_blob(chunk_create(buf, total), type, subtype, subject, flags); }
/** * Load all kind of PEM encoded credentials. */ static void *pem_load(credential_type_t type, int subtype, va_list args) { char *file = NULL; int fd = -1; chunk_t pem = chunk_empty; identification_t *subject = NULL; int flags = 0; while (TRUE) { switch (va_arg(args, builder_part_t)) { case BUILD_FROM_FILE: file = va_arg(args, char*); continue; case BUILD_FROM_FD: fd = va_arg(args, int); continue; case BUILD_BLOB_PEM: pem = va_arg(args, chunk_t); continue; case BUILD_SUBJECT: subject = va_arg(args, identification_t*); continue; case BUILD_X509_FLAG: flags = va_arg(args, int); continue; case BUILD_END: break; default: return NULL; } break; } if (pem.len) { return load_from_blob(pem, type, subtype, subject, flags); } if (file) { return load_from_file(file, type, subtype, subject, flags); } if (fd != -1) { return load_from_fd(fd, type, subtype, subject, flags); } return NULL; }
/** * load the credential from a file */ static void *load_from_file(char *file, credential_type_t type, int subtype, identification_t *subject, x509_flag_t flags) { void *cred; chunk_t *chunk; chunk = chunk_map(file, FALSE); if (!chunk) { DBG1(DBG_LIB, " opening '%s' failed: %s", file, strerror(errno)); return NULL; } cred = load_from_blob(*chunk, type, subtype, subject, flags); chunk_unmap(chunk); return cred; }
/** * load the credential from a file */ static void *load_from_file(char *file, credential_type_t type, int subtype, chunk_t(*cb)(void*,int), void *cb_data, x509_flag_t flags) { void *cred = NULL; struct stat sb; void *addr; int fd; fd = open(file, O_RDONLY); if (fd == -1) { DBG1(DBG_LIB, " opening '%s' failed: %s", file, strerror(errno)); return NULL; } if (fstat(fd, &sb) == -1) { DBG1(DBG_LIB, " getting file size of '%s' failed: %s", file, strerror(errno)); close(fd); return NULL; } addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); if (addr == MAP_FAILED) { DBG1(DBG_LIB, " mapping '%s' failed: %s", file, strerror(errno)); close(fd); return NULL; } cred = load_from_blob(chunk_create(addr, sb.st_size), type, subtype, cb, cb_data, flags); munmap(addr, sb.st_size); close(fd); return cred; }