2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <ctype.h>
2N/A#include <fcntl.h>
2N/A#include <poll.h>
2N/A#include <sys/errno.h>
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/socket.h>
2N/A#include <netdb.h>
2N/A#include <netinet/in.h>
2N/A#include <arpa/inet.h>
2N/A#include <string.h>
2N/A#include <unistd.h>
2N/A#include <libgen.h>
2N/A#include <kmfapi.h>
2N/A#include <kmfapiP.h>
2N/A#include <libxml2/libxml/uri.h>
2N/A
2N/Aextern int errno;
2N/A
2N/A#define OCSP_BUFSIZE 1024
2N/A
2N/Atypedef enum {
2N/A KMF_RESPONSE_OCSP = 1,
2N/A KMF_RESPONSE_FILE = 2
2N/A} KMF_RESPONSE_TYPE;
2N/A
2N/A#define TEMP_TEMPLATE "temp.XXXXXX"
2N/A
2N/A/*
2N/A * This function will establish a socket to the host on the specified port.
2N/A * If succeed, it return a socket descriptor; otherwise, return -1.
2N/A */
2N/Astatic int init_socket(char *host, short port)
2N/A{
2N/A struct sockaddr_in sin;
2N/A struct hostent *hp, hrec;
2N/A int sockfd, opt, herrno;
2N/A char hostbuf[BUFSIZ];
2N/A
2N/A sin.sin_family = PF_INET;
2N/A sin.sin_port = htons(port);
2N/A if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
2N/A if ((hp = gethostbyname_r(host, &hrec, hostbuf,
2N/A sizeof (hostbuf), &herrno)) == NULL) {
2N/A return (-1);
2N/A }
2N/A (void) memcpy((char *)&sin.sin_addr, hp->h_addr,
2N/A hp->h_length);
2N/A }
2N/A
2N/A if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
2N/A return (-1);
2N/A }
2N/A
2N/A opt = 1;
2N/A if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt,
2N/A sizeof (opt)) < 0) {
2N/A (void) close(sockfd);
2N/A return (-1);
2N/A }
2N/A
2N/A if (connect(sockfd, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
2N/A (void) close(sockfd);
2N/A return (-1);
2N/A }
2N/A
2N/A return (sockfd);
2N/A}
2N/A
2N/A/*
2N/A * This function will connect to host on the port.
2N/A * If succeed, return a socket descriptor; otherwise, return 0.
2N/A */
2N/Astatic int
2N/Aconnect_to_server(char *host, short port)
2N/A{
2N/A int retry = 1;
2N/A int sd = 0;
2N/A
2N/A while (retry) {
2N/A if ((sd = init_socket(host, port)) == -1) {
2N/A if (errno == ECONNREFUSED) {
2N/A retry = 1;
2N/A (void) sleep(1);
2N/A } else {
2N/A retry = 0;
2N/A }
2N/A } else {
2N/A retry = 0;
2N/A }
2N/A }
2N/A return (sd);
2N/A}
2N/A
2N/Astatic KMF_RETURN
2N/Asend_ocsp_request(int sock, char *reqfile, char *hostname)
2N/A{
2N/A KMF_RETURN ret = KMF_OK;
2N/A int filefd, bytes, n, total = 0;
2N/A char buf[OCSP_BUFSIZE];
2N/A struct stat s;
2N/A char req_header[256];
2N/A static char req_format[] =
2N/A"POST %s HTTP/1.0\r\n\
2N/AContent-Type: application/ocsp-request\r\n\
2N/AContent-Length: %d\r\n\r\n";
2N/A
2N/A if ((filefd = open(reqfile, O_RDONLY)) == -1) {
2N/A ret = KMF_ERR_OPEN_FILE;
2N/A return (ret);
2N/A }
2N/A
2N/A /* open the request file */
2N/A if (fstat(filefd, &s) < 0) {
2N/A ret = KMF_ERR_OPEN_FILE;
2N/A return (ret);
2N/A }
2N/A
2N/A
2N/A /* Send http header */
2N/A if (hostname != NULL) {
2N/A (void) snprintf(req_header, 256, req_format, hostname,
2N/A s.st_size);
2N/A } else {
2N/A (void) snprintf(req_header, 256, req_format, "/", s.st_size);
2N/A }
2N/A bytes = strlen(req_header);
2N/A
2N/A if ((n = write(sock, req_header, bytes)) < 0) {
2N/A ret = KMF_ERR_SEND_REQUEST;
2N/A goto exit;
2N/A }
2N/A
2N/A /* Send the request content */
2N/A while ((bytes = read(filefd, buf, OCSP_BUFSIZE)) > 0) {
2N/A if ((n = write(sock, buf, bytes)) < 0) {
2N/A ret = KMF_ERR_SEND_REQUEST;
2N/A goto exit;
2N/A }
2N/A total += n;
2N/A (void) memset(buf, 0, sizeof (buf));
2N/A }
2N/A
2N/Aexit:
2N/A (void) close(filefd);
2N/A return (ret);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Perform a write that can handle EINTR.
2N/A */
2N/Astatic int
2N/Alooping_write(int fd, void *buf, int len)
2N/A{
2N/A char *p = buf;
2N/A int cc, len2 = 0;
2N/A
2N/A if (len == 0)
2N/A return (0);
2N/A do {
2N/A cc = write(fd, p, len);
2N/A if (cc < 0) {
2N/A if (errno == EINTR)
2N/A continue;
2N/A return (cc);
2N/A } else if (cc == 0) {
2N/A return (len2);
2N/A } else {
2N/A p += cc;
2N/A len2 += cc;
2N/A len -= cc;
2N/A }
2N/A } while (len > 0);
2N/A
2N/A return (len2);
2N/A}
2N/A
2N/A/*
2N/A * This function will get the response from the server, check the http status
2N/A * line, and write the response content to a file. If this is a OCSP response,
2N/A * it will check the content type also.
2N/A */
2N/Astatic KMF_RETURN
2N/Aget_encoded_response(int sock, KMF_RESPONSE_TYPE resptype, int filefd,
2N/A unsigned int maxsecs)
2N/A{
2N/A int ret = KMF_OK;
2N/A char *buf = NULL;
2N/A int buflen = 0;
2N/A int offset = 0;
2N/A int search_offset;
2N/A const int buf_incre = OCSP_BUFSIZE; /* 1 KB at a time */
2N/A const int maxBufSize = 8 * buf_incre; /* 8 KB max */
2N/A const char *CRLF = "\r\n";
2N/A const char *headerEndMark = "\r\n\r\n";
2N/A const char *httpprotocol = "HTTP/";
2N/A const int CRLFlen = strlen(CRLF);
2N/A const int marklen = strlen(headerEndMark);
2N/A const int httplen = strlen(httpprotocol);
2N/A char *headerEnd = NULL;
2N/A boolean_t EOS = B_FALSE;
2N/A const char *httpcode = NULL;
2N/A const char *contenttype = NULL;
2N/A int contentlength = 0;
2N/A int bytes = 0;
2N/A char *statusLineEnd = NULL;
2N/A char *space = NULL;
2N/A char *nextHeader = NULL;
2N/A struct pollfd pfd;
2N/A int sock_flag;
2N/A int poll_ret;
2N/A boolean_t timeout = B_FALSE;
2N/A
2N/A /* set O_NONBLOCK flag on socket */
2N/A if ((sock_flag = fcntl(sock, F_GETFL, 0)) == -1) {
2N/A return (KMF_ERR_RECV_RESPONSE);
2N/A }
2N/A sock_flag |= O_NONBLOCK;
2N/A if (fcntl(sock, F_SETFL, sock_flag) == -1) {
2N/A return (KMF_ERR_RECV_RESPONSE);
2N/A }
2N/A
2N/A /* set up poll */
2N/A pfd.fd = sock;
2N/A pfd.events = POLLIN;
2N/A
2N/A /*
2N/A * First read HTTP status line and headers. We will read up to at
2N/A * least the end of the HTTP headers
2N/A */
2N/A do {
2N/A if ((buflen - offset) < buf_incre) {
2N/A buflen += buf_incre;
2N/A buf = realloc(buf, buflen + 1);
2N/A if (buf == NULL) {
2N/A ret = KMF_ERR_MEMORY;
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/A pfd.revents = 0;
2N/A poll_ret = poll(&pfd, 1, maxsecs * MILLISEC);
2N/A if (poll_ret == 0) {
2N/A timeout = B_TRUE;
2N/A break;
2N/A } else if (poll_ret < 0) {
2N/A ret = KMF_ERR_RECV_RESPONSE;
2N/A goto out;
2N/A } else {
2N/A if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
2N/A ret = KMF_ERR_RECV_RESPONSE;
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/A bytes = read(sock, buf + offset, buf_incre);
2N/A if (bytes < 0) {
2N/A if (errno == EWOULDBLOCK) { /* no data this time */
2N/A continue;
2N/A } else {
2N/A ret = KMF_ERR_RECV_RESPONSE;
2N/A goto out;
2N/A }
2N/A } else if (bytes == 0) { /* no more data */
2N/A EOS = B_TRUE;
2N/A } else { /* bytes > 0 */
2N/A search_offset = (offset - marklen) > 0 ?
2N/A offset - marklen : 0;
2N/A offset += bytes;
2N/A *(buf + offset) = '\0'; /* NULL termination */
2N/A
2N/A headerEnd = strstr((const char *)buf + search_offset,
2N/A headerEndMark);
2N/A }
2N/A
2N/A } while ((!headerEnd) && (EOS == B_FALSE) && (buflen < maxBufSize));
2N/A
2N/A if (timeout == B_TRUE) {
2N/A ret = KMF_ERR_RECV_TIMEOUT;
2N/A goto out;
2N/A } else if (headerEnd == NULL) {
2N/A /* could not find the end of headers */
2N/A ret = KMF_ERR_BAD_HTTP_RESPONSE;
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Parse the HTTP status line, which will look like this:
2N/A * "HTTP/1.1 200 OK".
2N/A */
2N/A statusLineEnd = strstr((const char *)buf, CRLF);
2N/A if (statusLineEnd == NULL) {
2N/A ret = KMF_ERR_BAD_HTTP_RESPONSE;
2N/A goto out;
2N/A }
2N/A *statusLineEnd = '\0';
2N/A
2N/A space = strchr((const char *)buf, ' ');
2N/A if (space == NULL ||
2N/A (strncasecmp((const char *)buf, httpprotocol, httplen) != 0)) {
2N/A ret = KMF_ERR_BAD_HTTP_RESPONSE;
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Check the HTTP status code. If it is not 200, the HTTP response
2N/A * is not good.
2N/A */
2N/A httpcode = space + 1;
2N/A space = strchr(httpcode, ' ');
2N/A if (space == NULL) {
2N/A ret = KMF_ERR_BAD_HTTP_RESPONSE;
2N/A goto out;
2N/A }
2N/A
2N/A *space = 0;
2N/A if (strcmp(httpcode, "200") != 0) {
2N/A ret = KMF_ERR_BAD_HTTP_RESPONSE;
2N/A goto out;
2N/A }
2N/A
2N/A /*
2N/A * Parse the HTTP headers in the buffer. Save content-type and
2N/A * content-length only.
2N/A */
2N/A nextHeader = statusLineEnd + CRLFlen;
2N/A *headerEnd = '\0'; /* terminate */
2N/A do {
2N/A char *thisHeaderEnd = NULL;
2N/A char *value = NULL;
2N/A char *colon = strchr(nextHeader, ':');
2N/A
2N/A if (colon == NULL) {
2N/A ret = KMF_ERR_BAD_HTTP_RESPONSE;
2N/A goto out;
2N/A }
2N/A *colon = '\0';
2N/A
2N/A value = colon + 1;
2N/A if (*value != ' ') {
2N/A ret = KMF_ERR_BAD_HTTP_RESPONSE;
2N/A goto out;
2N/A }
2N/A value++;
2N/A
2N/A thisHeaderEnd = strstr(value, CRLF);
2N/A if (thisHeaderEnd != NULL)
2N/A *thisHeaderEnd = '\0';
2N/A
2N/A if (strcasecmp(nextHeader, "content-type") == 0) {
2N/A contenttype = value;
2N/A } else if (strcasecmp(nextHeader, "content-length") == 0) {
2N/A contentlength = atoi(value);
2N/A }
2N/A
2N/A if (thisHeaderEnd != NULL) {
2N/A nextHeader = thisHeaderEnd + CRLFlen;
2N/A } else {
2N/A nextHeader = NULL;
2N/A }
2N/A
2N/A } while (nextHeader && (nextHeader < (headerEnd + CRLFlen)));
2N/A
2N/A /* Check the contenttype if this is an OCSP response */
2N/A if (resptype == KMF_RESPONSE_OCSP) {
2N/A if (contenttype == NULL) {
2N/A ret = KMF_ERR_BAD_HTTP_RESPONSE;
2N/A goto out;
2N/A } else if (strcasecmp(contenttype,
2N/A "application/ocsp-response") != 0) {
2N/A ret = KMF_ERR_BAD_HTTP_RESPONSE;
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/A /* Now we are ready to read the body of the response */
2N/A offset = offset - (int)(headerEnd - (const char *)buf) - marklen;
2N/A if (offset) {
2N/A /* move all data to the beginning of the buffer */
2N/A (void) memmove(buf, headerEnd + marklen, offset);
2N/A }
2N/A
2N/A /* resize buffer to only what's needed to hold the current response */
2N/A buflen = (1 + (offset-1) / buf_incre) * buf_incre;
2N/A
2N/A while ((EOS == B_FALSE) &&
2N/A ((contentlength == 0) || (offset < contentlength)) &&
2N/A (buflen < maxBufSize)) {
2N/A /* we still need to receive more content data */
2N/A if ((buflen - offset) < buf_incre) {
2N/A buflen += buf_incre;
2N/A buf = realloc(buf, buflen + 1);
2N/A if (buf == NULL) {
2N/A ret = KMF_ERR_MEMORY;
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/A pfd.revents = 0;
2N/A poll_ret = poll(&pfd, 1, maxsecs * MILLISEC);
2N/A if (poll_ret == 0) {
2N/A timeout = B_TRUE;
2N/A break;
2N/A } else if (poll_ret < 0) {
2N/A ret = KMF_ERR_RECV_RESPONSE;
2N/A goto out;
2N/A } else {
2N/A if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
2N/A ret = KMF_ERR_RECV_RESPONSE;
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/A bytes = read(sock, buf + offset, buf_incre);
2N/A if (bytes < 0) {
2N/A if (errno == EWOULDBLOCK) {
2N/A continue;
2N/A } else {
2N/A ret = KMF_ERR_RECV_RESPONSE;
2N/A goto out;
2N/A }
2N/A } else if (bytes == 0) { /* no more data */
2N/A EOS = B_TRUE;
2N/A } else {
2N/A offset += bytes;
2N/A }
2N/A }
2N/A
2N/A if (timeout == B_TRUE) {
2N/A ret = KMF_ERR_RECV_TIMEOUT;
2N/A goto out;
2N/A } else if (((contentlength != 0) && (offset < contentlength)) ||
2N/A offset == 0) {
2N/A ret = KMF_ERR_BAD_HTTP_RESPONSE;
2N/A goto out;
2N/A }
2N/A
2N/A /* write to the file */
2N/A if (looping_write(filefd, buf, offset) != offset) {
2N/A ret = KMF_ERR_WRITE_FILE;
2N/A }
2N/A
2N/Aout:
2N/A free(buf);
2N/A return (ret);
2N/A}
2N/A
2N/AKMF_RETURN
2N/Akmf_get_encoded_ocsp_response(KMF_HANDLE_T handle,
2N/A char *reqfile, char *hostname,
2N/A int port, char *proxy, int proxy_port, char *respfile,
2N/A unsigned int maxsecs)
2N/A{
2N/A KMF_RETURN ret = KMF_OK;
2N/A int sock, respfd;
2N/A char http_hostname[256];
2N/A int final_proxy_port, final_port;
2N/A
2N/A CLEAR_ERROR(handle, ret);
2N/A if (ret != KMF_OK)
2N/A return (ret);
2N/A
2N/A if (hostname == NULL || reqfile == NULL || respfile == NULL) {
2N/A return (KMF_ERR_BAD_PARAMETER);
2N/A }
2N/A
2N/A final_proxy_port = (proxy_port == 0 || proxy_port == -1) ?
2N/A 80 : proxy_port;
2N/A final_port = (port == 0 || port == -1) ? 80 : port;
2N/A
2N/A /* Connect to server */
2N/A if (proxy != NULL) {
2N/A sock = connect_to_server(proxy, final_proxy_port);
2N/A } else {
2N/A sock = connect_to_server(hostname, final_port);
2N/A }
2N/A
2N/A if (sock == -1) {
2N/A return (KMF_ERR_CONNECT_SERVER);
2N/A }
2N/A
2N/A /* Send the OCSP request */
2N/A if (proxy != NULL) {
2N/A (void) snprintf(http_hostname, sizeof (http_hostname),
2N/A "http://%s:%d", hostname, final_port);
2N/A ret = send_ocsp_request(sock, reqfile, http_hostname);
2N/A } else {
2N/A ret = send_ocsp_request(sock, reqfile, NULL);
2N/A }
2N/A
2N/A if (ret != KMF_OK) {
2N/A goto out;
2N/A }
2N/A
2N/A /* Retrieve the OCSP response */
2N/A if (maxsecs == 0) {
2N/A maxsecs = 30; /* default poll time limit is 30 seconds */
2N/A }
2N/A
2N/A if ((respfd = open(respfile, O_CREAT |O_RDWR | O_EXCL, 0600)) == -1) {
2N/A ret = KMF_ERR_OPEN_FILE;
2N/A } else {
2N/A ret = get_encoded_response(sock, KMF_RESPONSE_OCSP,
2N/A respfd, maxsecs);
2N/A (void) close(respfd);
2N/A }
2N/A
2N/Aout:
2N/A (void) close(sock);
2N/A return (ret);
2N/A}
2N/A
2N/Astatic KMF_RETURN
2N/Asend_download_request(int sock, char *hostname, int port, boolean_t is_proxy,
2N/A char *loc)
2N/A{
2N/A KMF_RETURN ret = KMF_OK;
2N/A char url[256];
2N/A char req_header[1024];
2N/A static char req_format[] =
2N/A"GET %s HTTP/1.0\r\n\
2N/AHost: %s:%d\r\n\
2N/AAccept: */*\r\n\r\n";
2N/A
2N/A if (is_proxy) {
2N/A (void) snprintf(url, sizeof (url), "http://%s:%d/%s",
2N/A hostname, port, loc);
2N/A } else {
2N/A (void) snprintf(url, sizeof (url), "/%s", loc);
2N/A }
2N/A
2N/A (void) snprintf(req_header, sizeof (req_header), req_format, url,
2N/A hostname, port);
2N/A
2N/A if (write(sock, req_header, strlen(req_header)) < 0) {
2N/A ret = KMF_ERR_SEND_REQUEST;
2N/A }
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/Astatic KMF_RETURN
2N/Adownload_file(char *uri, char *proxy, int proxy_port,
2N/A unsigned int maxsecs, int filefd)
2N/A{
2N/A KMF_RETURN ret = KMF_OK;
2N/A xmlURIPtr uriptr;
2N/A int sock;
2N/A boolean_t is_proxy;
2N/A int final_proxy_port;
2N/A char *hostname = NULL;
2N/A char *path = NULL;
2N/A int port;
2N/A
2N/A if (uri == NULL || filefd == -1)
2N/A return (KMF_ERR_BAD_PARAMETER);
2N/A
2N/A /* Parse URI */
2N/A uriptr = xmlParseURI(uri);
2N/A if (uriptr == NULL) {
2N/A ret = KMF_ERR_BAD_URI;
2N/A goto out;
2N/A }
2N/A
2N/A if (uriptr->scheme == NULL ||
2N/A strncasecmp(uriptr->scheme, "http", 4) != 0) {
2N/A ret = KMF_ERR_BAD_URI; /* we support http only */
2N/A goto out;
2N/A }
2N/A
2N/A /* get the host name */
2N/A hostname = uriptr->server;
2N/A if (hostname == NULL) {
2N/A ret = KMF_ERR_BAD_URI;
2N/A goto out;
2N/A }
2N/A
2N/A /* get the port number */
2N/A port = uriptr->port;
2N/A if (port == 0) {
2N/A port = 80;
2N/A }
2N/A
2N/A /* Get the path */
2N/A path = uriptr->path;
2N/A if (path == NULL) {
2N/A ret = KMF_ERR_BAD_URI;
2N/A goto out;
2N/A }
2N/A
2N/A /* Connect to server */
2N/A if (proxy != NULL) {
2N/A final_proxy_port = (proxy_port == 0 || proxy_port == -1) ?
2N/A 80 : proxy_port;
2N/A is_proxy = B_TRUE;
2N/A sock = connect_to_server(proxy, final_proxy_port);
2N/A } else {
2N/A is_proxy = B_FALSE;
2N/A sock = connect_to_server(hostname, port);
2N/A }
2N/A if (sock == -1) {
2N/A ret = KMF_ERR_CONNECT_SERVER;
2N/A goto out;
2N/A }
2N/A
2N/A /* Send the request */
2N/A ret = send_download_request(sock, hostname, port, is_proxy, path);
2N/A if (ret != KMF_OK) {
2N/A goto out;
2N/A }
2N/A
2N/A /* Retrieve the response */
2N/A ret = get_encoded_response(sock, KMF_RESPONSE_FILE, filefd,
2N/A maxsecs == 0 ? 30 : maxsecs);
2N/A if (ret != KMF_OK) {
2N/A goto out;
2N/A }
2N/A
2N/Aout:
2N/A if (uriptr != NULL)
2N/A xmlFreeURI(uriptr);
2N/A
2N/A if (sock != -1)
2N/A (void) close(sock);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A
2N/AKMF_RETURN
2N/Akmf_download_crl(KMF_HANDLE_T handle, char *uri, char *proxy, int proxy_port,
2N/A unsigned int maxsecs, char *crlfile, KMF_ENCODE_FORMAT *pformat)
2N/A{
2N/A KMF_RETURN ret = KMF_OK;
2N/A char *filename = NULL;
2N/A char tempfn[MAXPATHLEN];
2N/A boolean_t temp_created = B_FALSE;
2N/A mode_t old_mode;
2N/A int fd = -1, tmpfd = -1;
2N/A
2N/A CLEAR_ERROR(handle, ret);
2N/A if (ret != KMF_OK)
2N/A return (ret);
2N/A
2N/A if (uri == NULL || crlfile == NULL || pformat == NULL)
2N/A return (KMF_ERR_BAD_PARAMETER);
2N/A
2N/A if ((fd = open(crlfile, O_CREAT |O_RDWR | O_EXCL, 0644)) == -1)
2N/A return (KMF_ERR_OPEN_FILE);
2N/A
2N/A /*
2N/A * Download the file and save it to a temp file. To make rename()
2N/A * happy, the temp file needs to be created in the same directory as
2N/A * the target file.
2N/A */
2N/A if ((filename = strdup(crlfile)) == NULL) {
2N/A ret = KMF_ERR_MEMORY;
2N/A goto out;
2N/A }
2N/A (void) snprintf(tempfn, MAXPATHLEN, "%s/%s", dirname(filename),
2N/A TEMP_TEMPLATE);
2N/A old_mode = umask(077);
2N/A tmpfd = mkstemp(tempfn);
2N/A (void) umask(old_mode);
2N/A if (tmpfd == -1) {
2N/A ret = KMF_ERR_INTERNAL;
2N/A goto out;
2N/A } else {
2N/A temp_created = B_TRUE;
2N/A }
2N/A
2N/A ret = download_file(uri, proxy, proxy_port, maxsecs, tmpfd);
2N/A (void) close(tmpfd);
2N/A if (ret != KMF_OK) {
2N/A goto out;
2N/A }
2N/A
2N/A /* Check if it is a CRL file and get its format */
2N/A if (kmf_is_crl_file(handle, tempfn, pformat) != KMF_OK) {
2N/A ret = KMF_ERR_BAD_CRLFILE;
2N/A goto out;
2N/A }
2N/A
2N/A /* Finally, change the temp filename to the target crlfile */
2N/A if (rename(tempfn, crlfile) == -1) {
2N/A ret = KMF_ERR_WRITE_FILE;
2N/A goto out;
2N/A }
2N/A
2N/Aout:
2N/A if (filename != NULL)
2N/A free(filename);
2N/A
2N/A if (ret != KMF_OK && temp_created == B_TRUE)
2N/A (void) unlink(tempfn);
2N/A
2N/A if (fd != -1)
2N/A (void) close(fd);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/A
2N/AKMF_RETURN
2N/Akmf_download_cert(KMF_HANDLE_T handle, char *uri, char *proxy, int proxy_port,
2N/A unsigned int maxsecs, char *certfile, KMF_ENCODE_FORMAT *pformat)
2N/A{
2N/A KMF_RETURN ret = KMF_OK;
2N/A char *filename = NULL;
2N/A char tempfn[MAXPATHLEN];
2N/A boolean_t temp_created = B_FALSE;
2N/A mode_t old_mode;
2N/A int fd = -1, tmpfd = -1;
2N/A
2N/A CLEAR_ERROR(handle, ret);
2N/A if (ret != KMF_OK)
2N/A return (ret);
2N/A
2N/A if (uri == NULL || certfile == NULL || pformat == NULL)
2N/A return (KMF_ERR_BAD_PARAMETER);
2N/A
2N/A if ((fd = open(certfile, O_CREAT |O_RDWR | O_EXCL, 0644)) == -1)
2N/A return (KMF_ERR_OPEN_FILE);
2N/A
2N/A /*
2N/A * Download the file and save it to a temp file. To make rename()
2N/A * happy, the temp file needs to be created in the same directory as
2N/A * the target file.
2N/A */
2N/A if ((filename = strdup(certfile)) == NULL) {
2N/A ret = KMF_ERR_MEMORY;
2N/A goto out;
2N/A }
2N/A (void) snprintf(tempfn, MAXPATHLEN, "%s/%s", dirname(filename),
2N/A TEMP_TEMPLATE);
2N/A
2N/A old_mode = umask(077);
2N/A tmpfd = mkstemp(tempfn);
2N/A (void) umask(old_mode);
2N/A if (tmpfd == -1) {
2N/A ret = KMF_ERR_INTERNAL;
2N/A goto out;
2N/A } else {
2N/A temp_created = B_TRUE;
2N/A }
2N/A
2N/A ret = download_file(uri, proxy, proxy_port, maxsecs, tmpfd);
2N/A (void) close(tmpfd);
2N/A if (ret != KMF_OK) {
2N/A goto out;
2N/A }
2N/A
2N/A /* Check if it is a Cert file and get its format */
2N/A if (kmf_is_cert_file(handle, tempfn, pformat) != KMF_OK) {
2N/A ret = KMF_ERR_BAD_CERTFILE;
2N/A goto out;
2N/A }
2N/A
2N/A /* Finally, change the temp filename to the target filename */
2N/A if (rename(tempfn, certfile) == -1) {
2N/A ret = KMF_ERR_WRITE_FILE;
2N/A goto out;
2N/A }
2N/A
2N/Aout:
2N/A if (filename != NULL)
2N/A free(filename);
2N/A
2N/A if (ret != KMF_OK && temp_created == B_TRUE)
2N/A (void) unlink(tempfn);
2N/A
2N/A if (fd != -1)
2N/A (void) close(fd);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/AKMF_RETURN
2N/Akmf_get_ocsp_for_cert(KMF_HANDLE_T handle,
2N/A KMF_DATA *user_cert,
2N/A KMF_DATA *ta_cert,
2N/A KMF_DATA *response)
2N/A{
2N/A KMF_POLICY_RECORD *policy;
2N/A KMF_RETURN ret = KMF_OK;
2N/A char *hostname = NULL, *host_uri = NULL, *proxyname = NULL;
2N/A char *proxy_port_s = NULL;
2N/A int host_port = 0, proxy_port = 0;
2N/A char ocsp_reqname[MAXPATHLEN];
2N/A char ocsp_respname[MAXPATHLEN];
2N/A KMF_X509EXT_AUTHINFOACCESS aia;
2N/A int i;
2N/A boolean_t found = B_FALSE;
2N/A KMF_X509EXT_ACCESSDESC *access_info;
2N/A xmlURIPtr uriptr = NULL;
2N/A KMF_ATTRIBUTE attrlist[10];
2N/A int numattr = 0;
2N/A int temp_fd;
2N/A
2N/A CLEAR_ERROR(handle, ret);
2N/A if (ret != KMF_OK)
2N/A return (ret);
2N/A
2N/A if (user_cert == NULL || ta_cert == NULL || response == NULL)
2N/A return (KMF_ERR_BAD_PARAMETER);
2N/A
2N/A policy = handle->policy;
2N/A
2N/A /* Create an OCSP request */
2N/A kmf_set_attr_at_index(attrlist, numattr,
2N/A KMF_ISSUER_CERT_DATA_ATTR, ta_cert,
2N/A sizeof (KMF_DATA));
2N/A numattr++;
2N/A
2N/A kmf_set_attr_at_index(attrlist, numattr,
2N/A KMF_USER_CERT_DATA_ATTR, user_cert,
2N/A sizeof (KMF_DATA));
2N/A numattr++;
2N/A
2N/A /*
2N/A * Create temporary files to hold the OCSP request & response data.
2N/A */
2N/A (void) strlcpy(ocsp_reqname, OCSPREQ_TEMPNAME,
2N/A sizeof (ocsp_reqname));
2N/A if ((temp_fd = mkstemp(ocsp_reqname)) == -1) {
2N/A return (KMF_ERR_INTERNAL);
2N/A }
2N/A (void) close(temp_fd);
2N/A
2N/A (void) strlcpy(ocsp_respname, OCSPRESP_TEMPNAME,
2N/A sizeof (ocsp_respname));
2N/A if ((temp_fd = mkstemp(ocsp_respname)) == -1) {
2N/A return (KMF_ERR_INTERNAL);
2N/A }
2N/A (void) close(temp_fd);
2N/A
2N/A kmf_set_attr_at_index(attrlist, numattr,
2N/A KMF_OCSP_REQUEST_FILENAME_ATTR, ocsp_respname,
2N/A strlen(ocsp_respname));
2N/A numattr++;
2N/A
2N/A ret = kmf_create_ocsp_request(handle, numattr, attrlist);
2N/A if (ret != KMF_OK) {
2N/A goto out;
2N/A }
2N/A
2N/A if (policy->VAL_OCSP_BASIC.uri_from_cert == 0) {
2N/A if (policy->VAL_OCSP_BASIC.responderURI == NULL) {
2N/A ret = KMF_ERR_OCSP_POLICY;
2N/A goto out;
2N/A }
2N/A host_uri = policy->VAL_OCSP_BASIC.responderURI;
2N/A
2N/A } else {
2N/A /*
2N/A * Get the responder URI from certificate
2N/A * Authority Information Access
2N/A * thru OID_PKIX_AD_OCSP
2N/A */
2N/A ret = kmf_get_cert_auth_info_access(user_cert, &aia);
2N/A if (ret != KMF_OK) {
2N/A goto out;
2N/A }
2N/A
2N/A for (i = 0; i < aia.numberOfAccessDescription; i++) {
2N/A access_info = &aia.AccessDesc[i];
2N/A if (IsEqualOid(&access_info->AccessMethod,
2N/A (KMF_OID *)&KMFOID_PkixAdOcsp)) {
2N/A host_uri =
2N/A (char *)access_info->AccessLocation.Data;
2N/A found = B_TRUE;
2N/A break;
2N/A }
2N/A }
2N/A
2N/A if (!found) {
2N/A ret = KMF_ERR_OCSP_POLICY;
2N/A goto out;
2N/A }
2N/A }
2N/A
2N/A /* Parse the URI string; get the hostname and port */
2N/A uriptr = xmlParseURI(host_uri);
2N/A if (uriptr == NULL) {
2N/A ret = KMF_ERR_BAD_URI;
2N/A goto out;
2N/A }
2N/A
2N/A if (strncasecmp(uriptr->scheme, "http", 4) != 0) {
2N/A ret = KMF_ERR_BAD_URI; /* we support http only */
2N/A goto out;
2N/A }
2N/A
2N/A hostname = uriptr->server;
2N/A if (hostname == NULL) {
2N/A ret = KMF_ERR_BAD_URI;
2N/A goto out;
2N/A }
2N/A
2N/A host_port = uriptr->port;
2N/A if (host_port == 0)
2N/A host_port = 80;
2N/A
2N/A /* get the proxy info */
2N/A if (policy->VAL_OCSP_BASIC.proxy != NULL) {
2N/A char *last;
2N/A proxyname =
2N/A strtok_r(policy->VAL_OCSP_BASIC.proxy, ":", &last);
2N/A proxy_port_s = strtok_r(NULL, "\0", &last);
2N/A if (proxy_port_s != NULL) {
2N/A proxy_port = strtol(proxy_port_s, NULL, 0);
2N/A } else {
2N/A proxy_port = 8080; /* default */
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * Send the request to an OCSP responder and receive an
2N/A * OCSP response.
2N/A */
2N/A ret = kmf_get_encoded_ocsp_response(handle, ocsp_reqname,
2N/A hostname, host_port, proxyname, proxy_port,
2N/A ocsp_respname, 30);
2N/A if (ret != KMF_OK) {
2N/A goto out;
2N/A }
2N/A
2N/A ret = kmf_read_input_file(handle, ocsp_respname, response);
2N/A
2N/Aout:
2N/A (void) unlink(ocsp_reqname);
2N/A (void) unlink(ocsp_respname);
2N/A
2N/A if (uriptr != NULL)
2N/A xmlFreeURI(uriptr);
2N/A
2N/A return (ret);
2N/A}