implemented http basic authentication

This commit is contained in:
Andreas Steffen
2013-03-31 15:59:32 +02:00
parent e8675cb35d
commit 4581767493
3 changed files with 80 additions and 46 deletions
@@ -74,6 +74,11 @@ struct private_tnc_ifmap2_soap_t {
*/ */
char *uri; char *uri;
/**
* Optional base64-encoded username:password for HTTP Basic Authentication
*/
chunk_t user_pass;
/** /**
* IF-MAP Server (IP address and port) * IF-MAP Server (IP address and port)
*/ */
@@ -107,7 +112,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->uri, this->tls); soap_msg = tnc_ifmap2_soap_msg_create(this->uri, this->user_pass, 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);
@@ -145,7 +150,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->uri, this->tls); soap_msg = tnc_ifmap2_soap_msg_create(this->uri, this->user_pass, 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);
@@ -517,7 +522,7 @@ METHOD(tnc_ifmap2_soap_t, publish_ike_sa, bool,
} }
e1->destroy(e1); e1->destroy(e1);
soap_msg = tnc_ifmap2_soap_msg_create(this->uri, this->tls); soap_msg = tnc_ifmap2_soap_msg_create(this->uri, this->user_pass, 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);
@@ -541,7 +546,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->uri, this->tls); soap_msg = tnc_ifmap2_soap_msg_create(this->uri, this->user_pass, 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);
@@ -565,7 +570,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->uri, this->tls); soap_msg = tnc_ifmap2_soap_msg_create(this->uri, this->user_pass, 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);
@@ -585,7 +590,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->uri, this->tls); soap_msg = tnc_ifmap2_soap_msg_create(this->uri, this->user_pass, 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);
@@ -611,17 +616,18 @@ METHOD(tnc_ifmap2_soap_t, destroy, void,
} }
lib->credmgr->remove_set(lib->credmgr, &this->creds->set); lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
this->creds->destroy(this->creds); this->creds->destroy(this->creds);
free(this->user_pass.ptr);
free(this); free(this);
} }
static bool soap_init(private_tnc_ifmap2_soap_t *this) static bool soap_init(private_tnc_ifmap2_soap_t *this)
{ {
char *server_uri, *server_str, *port_str, *uri_str; char *server_uri, *server_str, *port_str, *uri_str;
char *server_cert, *client_cert, *client_key, *username, *password; char *server_cert, *client_cert, *client_key, *user_pass;
int port; 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 = NULL;
/* getting configuration parameters from strongswan.conf */ /* getting configuration parameters from strongswan.conf */
server_uri = lib->settings->get_str(lib->settings, server_uri = lib->settings->get_str(lib->settings,
@@ -632,10 +638,8 @@ static bool soap_init(private_tnc_ifmap2_soap_t *this)
"%s.plugins.tnc-ifmap2.client_cert", NULL, charon->name); "%s.plugins.tnc-ifmap2.client_cert", NULL, charon->name);
client_key = lib->settings->get_str(lib->settings, client_key = lib->settings->get_str(lib->settings,
"%s.plugins.tnc-ifmap2.client_key", NULL, charon->name); "%s.plugins.tnc-ifmap2.client_key", NULL, charon->name);
username = lib->settings->get_str(lib->settings, user_pass = lib->settings->get_str(lib->settings,
"%s.plugins.tnc-ifmap.username", NULL, charon->name); "%s.plugins.tnc-ifmap2.username_password", NULL, charon->name);
password = lib->settings->get_str(lib->settings,
"%s.plugins.tnc-ifmap.password", NULL, charon->name);
/* load [self-signed] MAP server certificate */ /* load [self-signed] MAP server certificate */
if (!server_cert) if (!server_cert)
@@ -655,40 +659,48 @@ 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 */ /* check availability of client credentials */
if (!client_cert) if (!((client_cert && client_key) || user_pass))
{ {
DBG1(DBG_TNC, "MAP client certificate not defined"); DBG1(DBG_TNC, "neither MAP client certificate and private key "
"nor username:password defined");
return FALSE; return FALSE;
} }
cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
BUILD_FROM_FILE, client_cert, BUILD_END);
if (!cert)
{
DBG1(DBG_TNC, "loading MAP client certificate from '%s' failed",
client_cert);
return FALSE;
}
DBG1(DBG_TNC, "loaded MAP client certificate from '%s'", client_cert);
client_id = cert->get_subject(cert);
this->creds->add_cert(this->creds, TRUE, cert);
/* load MAP client private key */ if (client_cert)
if (!client_key)
{ {
DBG1(DBG_TNC, "MAP client private key not defined"); /* load MAP client certificate */
return FALSE; cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
BUILD_FROM_FILE, client_cert, BUILD_END);
if (!cert)
{
DBG1(DBG_TNC, "loading MAP client certificate from '%s' failed",
client_cert);
return FALSE;
}
DBG1(DBG_TNC, "loaded MAP client certificate from '%s'", client_cert);
this->creds->add_cert(this->creds, TRUE, cert);
/* load MAP client private key */
key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA,
BUILD_FROM_FILE, client_key, BUILD_END);
if (!key)
{
DBG1(DBG_TNC, "loading MAP client private key from '%s' failed",
client_key);
return FALSE;
}
DBG1(DBG_TNC, "loaded MAP client RSA private key from '%s'", client_key);
this->creds->add_key(this->creds, key);
/* set client ID to certificate distinguished name */
client_id = cert->get_subject(cert);
} }
key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, KEY_RSA, else
BUILD_FROM_FILE, client_key, BUILD_END);
if (!key)
{ {
DBG1(DBG_TNC, "loading MAP client private key from '%s' failed", /* set base64-encoded username:password for HTTP Basic Authentication */
client_key); this->user_pass = chunk_to_base64(chunk_from_str(user_pass), NULL);
return FALSE;
} }
DBG1(DBG_TNC, "loaded MAP client RSA private key from '%s'", client_key);
this->creds->add_key(this->creds, key);
/* remove HTTPS prefix if any */ /* remove HTTPS prefix if any */
if (strlen(server_uri) >= 8 && strncaseeq(server_uri, "https://", 8)) if (strlen(server_uri) >= 8 && strncaseeq(server_uri, "https://", 8))
@@ -41,6 +41,11 @@ struct private_tnc_ifmap2_soap_msg_t {
*/ */
char *uri; char *uri;
/**
* Optional base64-encoded username:password for HTTP Basic Authentication
*/
chunk_t user_pass;
/** /**
* TLS Socket * TLS Socket
*/ */
@@ -59,7 +64,7 @@ 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 *host, *path, *request, response[2048]; char *host, *path, *request, buf[2048];
chunk_t line, http, parameter; chunk_t line, http, parameter;
int len, code, content_len = 0; int len, code, content_len = 0;
@@ -77,15 +82,28 @@ static bool http_post(private_tnc_ifmap2_soap_msg_t *this, chunk_t out,
*path = '/'; *path = '/';
} }
/* Use Basic Authentication? */
if (this->user_pass.len)
{
snprintf(buf, sizeof(buf), "Authorization: Basic %.*s\r\n",
this->user_pass.len, this->user_pass.ptr);
}
else
{
*buf = '\0';
}
/* Write HTTP POST request */ /* Write HTTP POST request */
len = asprintf(&request, len = asprintf(&request,
"POST %s HTTP/1.1\r\n" "POST %s HTTP/1.1\r\n"
"Host: %.*s\r\n" "Host: %.*s\r\n"
"%s"
"Content-Type: application/soap+xml;charset=utf-8\r\n" "Content-Type: application/soap+xml;charset=utf-8\r\n"
"Content-Length: %d\r\n" "Content-Length: %d\r\n"
"\r\n" "\r\n"
"%.*s", path, (path-host), host, out.len, out.len, out.ptr); "%.*s", path, (path-host), host, buf, out.len, out.len, out.ptr);
free(host); free(host);
if (len == -1) if (len == -1)
{ {
return FALSE; return FALSE;
@@ -97,12 +115,12 @@ static bool http_post(private_tnc_ifmap2_soap_msg_t *this, chunk_t out,
free(request); free(request);
/* Read HTTP response */ /* Read HTTP response */
len = this->tls->read(this->tls, response, sizeof(response), TRUE); len = this->tls->read(this->tls, buf, sizeof(buf), TRUE);
if (len == -1) if (len == -1)
{ {
return FALSE; return FALSE;
} }
*in = chunk_create(response, len); *in = chunk_create(buf, len);
/* Process HTTP protocol version */ /* Process HTTP protocol version */
if (!fetchline(in, &line) || !extract_token(&http, ' ', &line) || if (!fetchline(in, &line) || !extract_token(&http, ' ', &line) ||
@@ -297,7 +315,8 @@ METHOD(tnc_ifmap2_soap_msg_t, destroy, void,
/** /**
* See header * See header
*/ */
tnc_ifmap2_soap_msg_t *tnc_ifmap2_soap_msg_create(char *uri, tls_socket_t *tls) tnc_ifmap2_soap_msg_t *tnc_ifmap2_soap_msg_create(char *uri, chunk_t user_pass,
tls_socket_t *tls)
{ {
private_tnc_ifmap2_soap_msg_t *this; private_tnc_ifmap2_soap_msg_t *this;
@@ -307,6 +326,7 @@ tnc_ifmap2_soap_msg_t *tnc_ifmap2_soap_msg_create(char *uri, tls_socket_t *tls)
.destroy = _destroy, .destroy = _destroy,
}, },
.uri = uri, .uri = uri,
.user_pass = user_pass,
.tls = tls, .tls = tls,
); );
@@ -52,9 +52,11 @@ 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 uri HTTPS URI with https:// prefix removed
* @param tls TLS socket protecting the SOAP message * @param user_pass Optional username:password for HTTP Basic Authentication
* @param tls TLS socket protecting the SOAP message
*/ */
tnc_ifmap2_soap_msg_t *tnc_ifmap2_soap_msg_create(char *uri, tls_socket_t *tls); tnc_ifmap2_soap_msg_t *tnc_ifmap2_soap_msg_create(char *uri, chunk_t user_pass,
tls_socket_t *tls);
#endif /** TNC_IFMAP2_SOAP_MSG_H_ @}*/ #endif /** TNC_IFMAP2_SOAP_MSG_H_ @}*/