parse IF-MAP server URI

This commit is contained in:
Andreas Steffen
2013-03-31 11:39:06 +02:00
parent 4f4875ff13
commit e8675cb35d
3 changed files with 105 additions and 41 deletions
@@ -29,8 +29,7 @@
#define IFMAP_NS "http://www.trustedcomputinggroup.org/2010/IFMAP/2" #define IFMAP_NS "http://www.trustedcomputinggroup.org/2010/IFMAP/2"
#define IFMAP_META_NS "http://www.trustedcomputinggroup.org/2010/IFMAP-METADATA/2" #define IFMAP_META_NS "http://www.trustedcomputinggroup.org/2010/IFMAP-METADATA/2"
#define IFMAP_LOGFILE "strongswan_ifmap.log" #define IFMAP_URI "https://localhost:8444/imap"
#define IFMAP_SERVER "https://localhost:8443/"
#define IFMAP_NO_FD -1 #define IFMAP_NO_FD -1
typedef struct private_tnc_ifmap2_soap_t private_tnc_ifmap2_soap_t; typedef struct private_tnc_ifmap2_soap_t private_tnc_ifmap2_soap_t;
@@ -71,7 +70,12 @@ struct private_tnc_ifmap2_soap_t {
char *device_name; char *device_name;
/** /**
* IF-MAP Server host * HTTPS Server URI with https:// prefix removed
*/
char *uri;
/**
* IF-MAP Server (IP address and port)
*/ */
host_t *host; host_t *host;
@@ -103,7 +107,7 @@ METHOD(tnc_ifmap2_soap_t, newSession, bool,
this->ns = xmlNewNs(request, IFMAP_NS, "ifmap"); this->ns = xmlNewNs(request, IFMAP_NS, "ifmap");
xmlSetNs(request, this->ns); xmlSetNs(request, this->ns);
soap_msg = tnc_ifmap2_soap_msg_create(this->tls); soap_msg = tnc_ifmap2_soap_msg_create(this->uri, this->tls);
if (!soap_msg->post(soap_msg, request, "newSessionResult", &result)) if (!soap_msg->post(soap_msg, request, "newSessionResult", &result))
{ {
soap_msg->destroy(soap_msg); soap_msg->destroy(soap_msg);
@@ -141,7 +145,7 @@ METHOD(tnc_ifmap2_soap_t, purgePublisher, bool,
xmlNewProp(request, "session-id", this->session_id); xmlNewProp(request, "session-id", this->session_id);
xmlNewProp(request, "ifmap-publisher-id", this->ifmap_publisher_id); xmlNewProp(request, "ifmap-publisher-id", this->ifmap_publisher_id);
soap_msg = tnc_ifmap2_soap_msg_create(this->tls); soap_msg = tnc_ifmap2_soap_msg_create(this->uri, this->tls);
success = soap_msg->post(soap_msg, request, "purgePublisherReceived", NULL); success = soap_msg->post(soap_msg, request, "purgePublisherReceived", NULL);
soap_msg->destroy(soap_msg); soap_msg->destroy(soap_msg);
@@ -513,7 +517,7 @@ METHOD(tnc_ifmap2_soap_t, publish_ike_sa, bool,
} }
e1->destroy(e1); e1->destroy(e1);
soap_msg = tnc_ifmap2_soap_msg_create(this->tls); soap_msg = tnc_ifmap2_soap_msg_create(this->uri, this->tls);
success = soap_msg->post(soap_msg, request, "publishReceived", NULL); success = soap_msg->post(soap_msg, request, "publishReceived", NULL);
soap_msg->destroy(soap_msg); soap_msg->destroy(soap_msg);
@@ -537,7 +541,7 @@ METHOD(tnc_ifmap2_soap_t, publish_device_ip, bool,
xmlAddChild(update, create_ip_address(this, host)); xmlAddChild(update, create_ip_address(this, host));
xmlAddChild(update, create_metadata(this, "device-ip")); xmlAddChild(update, create_metadata(this, "device-ip"));
soap_msg = tnc_ifmap2_soap_msg_create(this->tls); soap_msg = tnc_ifmap2_soap_msg_create(this->uri, this->tls);
success = soap_msg->post(soap_msg, request, "publishReceived", NULL); success = soap_msg->post(soap_msg, request, "publishReceived", NULL);
soap_msg->destroy(soap_msg); soap_msg->destroy(soap_msg);
@@ -561,7 +565,7 @@ METHOD(tnc_ifmap2_soap_t, publish_enforcement_report, bool,
xmlAddChild(update, create_device(this)); xmlAddChild(update, create_device(this));
xmlAddChild(update, create_enforcement_report(this, action, reason)); xmlAddChild(update, create_enforcement_report(this, action, reason));
soap_msg = tnc_ifmap2_soap_msg_create(this->tls); soap_msg = tnc_ifmap2_soap_msg_create(this->uri, this->tls);
success = soap_msg->post(soap_msg, request, "publishReceived", NULL); success = soap_msg->post(soap_msg, request, "publishReceived", NULL);
soap_msg->destroy(soap_msg); soap_msg->destroy(soap_msg);
@@ -581,7 +585,7 @@ METHOD(tnc_ifmap2_soap_t, endSession, bool,
xmlSetNs(request, this->ns); xmlSetNs(request, this->ns);
xmlNewProp(request, "session-id", this->session_id); xmlNewProp(request, "session-id", this->session_id);
soap_msg = tnc_ifmap2_soap_msg_create(this->tls); soap_msg = tnc_ifmap2_soap_msg_create(this->uri, this->tls);
success = soap_msg->post(soap_msg, request, "endSessionResult", NULL); success = soap_msg->post(soap_msg, request, "endSessionResult", NULL);
soap_msg->destroy(soap_msg); soap_msg->destroy(soap_msg);
@@ -612,16 +616,28 @@ METHOD(tnc_ifmap2_soap_t, destroy, void,
static bool soap_init(private_tnc_ifmap2_soap_t *this) static bool soap_init(private_tnc_ifmap2_soap_t *this)
{ {
char *server, *server_cert, *client_cert, *client_key; char *server_uri, *server_str, *port_str, *uri_str;
char *server_cert, *client_cert, *client_key, *username, *password;
int port;
certificate_t *cert; certificate_t *cert;
private_key_t *key; private_key_t *key;
identification_t *server_id, *client_id; identification_t *server_id, *client_id;
/** /* getting configuration parameters from strongswan.conf */
* Load [self-signed] MAP server certificate server_uri = lib->settings->get_str(lib->settings,
*/ "%s.plugins.tnc-ifmap2.server_uri", IFMAP_URI, charon->name);
server_cert = lib->settings->get_str(lib->settings, server_cert = lib->settings->get_str(lib->settings,
"%s.plugins.tnc-ifmap2.server_cert", NULL, charon->name); "%s.plugins.tnc-ifmap2.server_cert", NULL, charon->name);
client_cert = lib->settings->get_str(lib->settings,
"%s.plugins.tnc-ifmap2.client_cert", NULL, charon->name);
client_key = lib->settings->get_str(lib->settings,
"%s.plugins.tnc-ifmap2.client_key", NULL, charon->name);
username = lib->settings->get_str(lib->settings,
"%s.plugins.tnc-ifmap.username", NULL, charon->name);
password = lib->settings->get_str(lib->settings,
"%s.plugins.tnc-ifmap.password", NULL, charon->name);
/* load [self-signed] MAP server certificate */
if (!server_cert) if (!server_cert)
{ {
DBG1(DBG_TNC, "MAP server certificate not defined"); DBG1(DBG_TNC, "MAP server certificate not defined");
@@ -639,11 +655,7 @@ static bool soap_init(private_tnc_ifmap2_soap_t *this)
server_id = cert->get_subject(cert); server_id = cert->get_subject(cert);
this->creds->add_cert(this->creds, TRUE, cert); this->creds->add_cert(this->creds, TRUE, cert);
/** /* load MAP client certificate */
* Load MAP client certificate
*/
client_cert = lib->settings->get_str(lib->settings,
"%s.plugins.tnc-ifmap2.client_cert", NULL, charon->name);
if (!client_cert) if (!client_cert)
{ {
DBG1(DBG_TNC, "MAP client certificate not defined"); DBG1(DBG_TNC, "MAP client certificate not defined");
@@ -661,11 +673,7 @@ static bool soap_init(private_tnc_ifmap2_soap_t *this)
client_id = cert->get_subject(cert); client_id = cert->get_subject(cert);
this->creds->add_cert(this->creds, TRUE, cert); this->creds->add_cert(this->creds, TRUE, cert);
/** /* load MAP client private key */
* Load MAP client private key
*/
client_key = lib->settings->get_str(lib->settings,
"%s.plugins.tnc-ifmap2.client_key", NULL, charon->name);
if (!client_key) if (!client_key)
{ {
DBG1(DBG_TNC, "MAP client private key not defined"); DBG1(DBG_TNC, "MAP client private key not defined");
@@ -682,16 +690,48 @@ static bool soap_init(private_tnc_ifmap2_soap_t *this)
DBG1(DBG_TNC, "loaded MAP client RSA private key from '%s'", client_key); DBG1(DBG_TNC, "loaded MAP client RSA private key from '%s'", client_key);
this->creds->add_key(this->creds, key); this->creds->add_key(this->creds, key);
/** /* remove HTTPS prefix if any */
* Open TCP socket and connect to MAP server if (strlen(server_uri) >= 8 && strncaseeq(server_uri, "https://", 8))
*/ {
server = "127.0.0.1"; server_uri += 8;
this->host = host_create_from_dns(server, 0, 8444); }
this->uri = server_uri;
/* duplicate server string since we are going to manipulate it */
server_str = strdup(server_uri);
/* extract server name and port from server URI */
port_str = strchr(server_str, ':');
if (port_str)
{
*port_str++ = '\0';
if (sscanf(port_str, "%d", &port) != 1)
{
DBG1(DBG_TNC, "parsing server port %s failed", port_str);
free(server_str);
return FALSE;
}
}
else
{
/* use default https port */
port = 443;
uri_str = strchr(server_str, '/');
if (uri_str)
{
*uri_str = '\0';
}
}
/* open TCP socket and connect to MAP server */
this->host = host_create_from_dns(server_str, 0, port);
if (!this->host) if (!this->host)
{ {
DBG1(DBG_TNC, "resolving hostname %s failed", server); DBG1(DBG_TNC, "resolving hostname %s failed", server_str);
free(server_str);
return FALSE; return FALSE;
} }
free(server_str);
this->fd = socket(this->host->get_family(this->host), SOCK_STREAM, 0); this->fd = socket(this->host->get_family(this->host), SOCK_STREAM, 0);
if (this->fd == IFMAP_NO_FD) if (this->fd == IFMAP_NO_FD)
@@ -708,9 +748,7 @@ static bool soap_init(private_tnc_ifmap2_soap_t *this)
return FALSE; return FALSE;
} }
/** /* open TLS socket */
* Open TLS socket
*/
this->tls = tls_socket_create(FALSE, server_id, client_id, this->fd, NULL); this->tls = tls_socket_create(FALSE, server_id, client_id, this->fd, NULL);
if (!this->tls) if (!this->tls)
{ {
@@ -36,6 +36,11 @@ struct private_tnc_ifmap2_soap_msg_t {
*/ */
tnc_ifmap2_soap_msg_t public; tnc_ifmap2_soap_msg_t public;
/**
* HTTPS Server URI with https:// prefix removed
*/
char *uri;
/** /**
* TLS Socket * TLS Socket
*/ */
@@ -54,21 +59,40 @@ struct private_tnc_ifmap2_soap_msg_t {
static bool http_post(private_tnc_ifmap2_soap_msg_t *this, chunk_t out, static bool http_post(private_tnc_ifmap2_soap_msg_t *this, chunk_t out,
chunk_t *in) chunk_t *in)
{ {
char header[] = char *host, *path, *request, response[2048];
"POST /ifmap HTTP/1.1\r\n"
"Content-Type: application/soap+xml;charset=utf-8\r\n"
"Content-Length: ";
char *request, response[2048];
chunk_t line, http, parameter; chunk_t line, http, parameter;
int len, code, content_len = 0; int len, code, content_len = 0;
/* Duplicate host[/path] string since we are going to manipulate it */
len = strlen(this->uri) + 2;
host = malloc(len);
memset(host, '\0', len);
strcpy(host, this->uri);
/* Extract appended path or set to root */
path = strchr(host, '/');
if (!path)
{
path = host + len - 2;
*path = '/';
}
/* Write HTTP POST request */ /* Write HTTP POST request */
len = asprintf(&request, "%s%d\r\n\r\n%.*s", header, out.len, len = asprintf(&request,
out.len, out.ptr); "POST %s HTTP/1.1\r\n"
"Host: %.*s\r\n"
"Content-Type: application/soap+xml;charset=utf-8\r\n"
"Content-Length: %d\r\n"
"\r\n"
"%.*s", path, (path-host), host, out.len, out.len, out.ptr);
free(host);
if (len == -1) if (len == -1)
{ {
return FALSE; return FALSE;
} }
http = chunk_create(request, len);
DBG3(DBG_TLS, "%B", &http);
this->tls->write(this->tls, request, len); this->tls->write(this->tls, request, len);
free(request); free(request);
@@ -273,7 +297,7 @@ METHOD(tnc_ifmap2_soap_msg_t, destroy, void,
/** /**
* See header * See header
*/ */
tnc_ifmap2_soap_msg_t *tnc_ifmap2_soap_msg_create(tls_socket_t *tls) tnc_ifmap2_soap_msg_t *tnc_ifmap2_soap_msg_create(char *uri, tls_socket_t *tls)
{ {
private_tnc_ifmap2_soap_msg_t *this; private_tnc_ifmap2_soap_msg_t *this;
@@ -282,6 +306,7 @@ tnc_ifmap2_soap_msg_t *tnc_ifmap2_soap_msg_create(tls_socket_t *tls)
.post = _post, .post = _post,
.destroy = _destroy, .destroy = _destroy,
}, },
.uri = uri,
.tls = tls, .tls = tls,
); );
@@ -52,8 +52,9 @@ struct tnc_ifmap2_soap_msg_t {
/** /**
* Create a tnc_ifmap2_soap_msg instance. * Create a tnc_ifmap2_soap_msg instance.
* *
* @param uri HTTPS URI with https:// prefix removed
* @param tls TLS socket protecting the SOAP message * @param tls TLS socket protecting the SOAP message
*/ */
tnc_ifmap2_soap_msg_t *tnc_ifmap2_soap_msg_create(tls_socket_t *tls); tnc_ifmap2_soap_msg_t *tnc_ifmap2_soap_msg_create(char *uri, tls_socket_t *tls);
#endif /** TNC_IFMAP2_SOAP_MSG_H_ @}*/ #endif /** TNC_IFMAP2_SOAP_MSG_H_ @}*/