libimcv: Moved REST API from imv_swid and imv_swima to libimcv

This commit is contained in:
Andreas Steffen
2017-07-08 23:19:51 +02:00
parent 7e796bba67
commit 8ba6bf511e
10 changed files with 114 additions and 241 deletions
+4 -1
View File
@@ -1003,7 +1003,7 @@ if test x$tss_tss2 = xtrue; then
AC_SUBST(tss2_LIBS)
fi
if test x$imv_swid = xtrue -o x$imv_swima = xtrue; then
if test x$imc_swima = xtrue -o $imv_swima = xtrue -o x$imv_swid = xtrue; then
PKG_CHECK_MODULES(json, [json-c], [],
[PKG_CHECK_MODULES(json, [json])])
AC_SUBST(json_CFLAGS)
@@ -1711,6 +1711,9 @@ fi
if test x$fuzzing = xtrue; then
AC_DEFINE([USE_FUZZING], [], [build code for fuzzing])
fi
if test x$imc_swima = xtrue -o x$imv_swima = xtrue -o x$imv_swid = xtrue ; then
AC_DEFINE([USE_JSON], [], [build code for JSON])
fi
# ====================================================
# options for enabled modules (see conf/Makefile.am)
+6 -1
View File
@@ -5,6 +5,9 @@ AM_CPPFLAGS = \
-DIPSEC_SCRIPT=\"${ipsec_script}\" \
-DSWID_DIRECTORY=\"${prefix}/share\"
AM_CFLAGS = \
$(json_CFLAGS)
ipseclib_LTLIBRARIES = libimcv.la
libimcv_la_LDFLAGS = \
@@ -13,7 +16,8 @@ libimcv_la_LDFLAGS = \
libimcv_la_LIBADD = \
$(top_builddir)/src/libstrongswan/libstrongswan.la \
$(top_builddir)/src/libtncif/libtncif.la \
$(top_builddir)/src/libtpmtss/libtpmtss.la
$(top_builddir)/src/libtpmtss/libtpmtss.la \
$(json_LIBS)
if USE_WINDOWS
libimcv_la_LIBADD += -lws2_32
@@ -88,6 +92,7 @@ libimcv_la_SOURCES = \
pts/components/tcg/tcg_comp_func_name.h pts/components/tcg/tcg_comp_func_name.c \
pwg/pwg_attr.h pwg/pwg_attr.c \
pwg/pwg_attr_vendor_smi_code.h pwg/pwg_attr_vendor_smi_code.c \
rest/rest.h rest/rest.c \
seg/seg_contract.h seg/seg_contract.c \
seg/seg_contract_manager.h seg/seg_contract_manager.c \
seg/seg_env.h seg/seg_env.c \
+1 -2
View File
@@ -16,7 +16,6 @@ imv_swid_la_LIBADD = \
imv_swid_la_SOURCES = \
imv_swid.c imv_swid_state.h imv_swid_state.c \
imv_swid_agent.h imv_swid_agent.c \
imv_swid_rest.h imv_swid_rest.c
imv_swid_agent.h imv_swid_agent.c
imv_swid_la_LDFLAGS = -module -avoid-version -no-undefined
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2013-2016 Andreas Steffen
* Copyright (C) 2013-2017 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
@@ -18,12 +18,12 @@
#include "imv_swid_agent.h"
#include "imv_swid_state.h"
#include "imv_swid_rest.h"
#include <imcv.h>
#include <imv/imv_agent.h>
#include <imv/imv_msg.h>
#include <ietf/ietf_attr_pa_tnc_error.h>
#include "rest/rest.h"
#include "tcg/seg/tcg_seg_attr_max_size.h"
#include "tcg/seg/tcg_seg_attr_seg_env.h"
#include "tcg/swid/tcg_swid_attr_req.h"
@@ -72,7 +72,7 @@ struct private_imv_swid_agent_t {
/**
* REST API to strongTNC manager
*/
imv_swid_rest_t *rest_api;
rest_t *rest_api;
};
@@ -719,7 +719,7 @@ imv_agent_if_t *imv_swid_agent_create(const char *name, TNC_IMVID id,
"%s.plugins.imv-swid.rest_api_timeout", 120, lib->ns);
if (rest_api_uri)
{
this->rest_api = imv_swid_rest_create(rest_api_uri, rest_api_timeout);
this->rest_api = rest_create(rest_api_uri, rest_api_timeout);
}
return &this->public;
@@ -1,124 +0,0 @@
/*
* Copyright (C) 2014 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include "imv_swid_rest.h"
typedef struct private_imv_swid_rest_t private_imv_swid_rest_t;
/**
* Private data of an imv_swid_rest_t object.
*/
struct private_imv_swid_rest_t {
/**
* Public members of imv_swid_rest_t
*/
imv_swid_rest_t public;
/**
* URI of REST API
*/
char *uri;
/**
* Timeout of REST API connection
*/
u_int timeout;
};
#define HTTP_STATUS_CODE_PRECONDITION_FAILED 412
METHOD(imv_swid_rest_t, post, status_t,
private_imv_swid_rest_t *this, char *command, json_object *jrequest,
json_object **jresponse)
{
struct json_tokener *tokener;
chunk_t data, response = chunk_empty;
status_t status;
char *uri;
int code;
if (asprintf(&uri, "%s%s",this->uri, command) < 0)
{
return FAILED;
}
data = chunk_from_str((char*)json_object_to_json_string(jrequest));
status = lib->fetcher->fetch(lib->fetcher, uri, &response,
FETCH_TIMEOUT, this->timeout,
FETCH_REQUEST_DATA, data,
FETCH_REQUEST_TYPE, "application/json; charset=utf-8",
FETCH_REQUEST_HEADER, "Accept: application/json",
FETCH_REQUEST_HEADER, "Expect:",
FETCH_RESPONSE_CODE, &code,
FETCH_END);
free(uri);
if (status != SUCCESS)
{
if (code != HTTP_STATUS_CODE_PRECONDITION_FAILED || !response.ptr)
{
DBG2(DBG_IMV, "REST http request failed with status code: %d", code);
status = FAILED;
}
else
{
if (jresponse)
{
/* Parse HTTP response into a JSON object */
tokener = json_tokener_new();
*jresponse = json_tokener_parse_ex(tokener, response.ptr,
response.len);
json_tokener_free(tokener);
}
status = NEED_MORE;
}
}
free(response.ptr);
return status;
}
METHOD(imv_swid_rest_t, destroy, void,
private_imv_swid_rest_t *this)
{
free(this->uri);
free(this);
}
/**
* Described in header.
*/
imv_swid_rest_t *imv_swid_rest_create(char *uri, u_int timeout)
{
private_imv_swid_rest_t *this;
INIT(this,
.public = {
.post = _post,
.destroy = _destroy,
},
.uri = strdup(uri),
.timeout = timeout,
);
return &this->public;
}
@@ -1,63 +0,0 @@
/*
* Copyright (C) 2013-2014 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
/**
* @defgroup imv_swid imv_swid
* @ingroup libimcv_plugins
*
* @defgroup imv_swid_rest_t imv_swid_rest
* @{ @ingroup imv_swid
*/
#ifndef IMV_SWID_REST_H_
#define IMV_SWID_REST_H_
#include <library.h>
#include <json.h>
typedef struct imv_swid_rest_t imv_swid_rest_t;
/**
* Public REST interface
*/
struct imv_swid_rest_t {
/**
* Post a HTTP request including a JSON object
*
* @param jreq JSON object in HTTP request
* @param jresp JSON object in HTTP response if NEED_MORE
* @return Status (SUCCESS, NEED_MORE or FAILED)
*/
status_t (*post)(imv_swid_rest_t *this, char *command, json_object *jreq,
json_object **jresp);
/**
* Destroy imv_swid_rest_t object
*/
void (*destroy)(imv_swid_rest_t *this);
};
/**
* Create an imv_swid_rest_t instance
*
* @param uri REST URI (http://username:password@hostname[:port]/api/)
* @param timeout Timeout of the REST connection
*/
imv_swid_rest_t* imv_swid_rest_create(char *uri, u_int timeout);
#endif /** IMV_SWID_REST_H_ @}*/
+1 -2
View File
@@ -16,7 +16,6 @@ imv_swima_la_LIBADD = \
imv_swima_la_SOURCES = \
imv_swima.c imv_swima_state.h imv_swima_state.c \
imv_swima_agent.h imv_swima_agent.c \
imv_swima_rest.h imv_swima_rest.c
imv_swima_agent.h imv_swima_agent.c
imv_swima_la_LDFLAGS = -module -avoid-version -no-undefined
@@ -18,11 +18,11 @@
#include "imv_swima_agent.h"
#include "imv_swima_state.h"
#include "imv_swima_rest.h"
#include <imcv.h>
#include <imv/imv_agent.h>
#include <imv/imv_msg.h>
#include "rest/rest.h"
#include "tcg/seg/tcg_seg_attr_max_size.h"
#include "tcg/seg/tcg_seg_attr_seg_env.h"
#include "ietf/swima/ietf_swima_attr_req.h"
@@ -73,7 +73,7 @@ struct private_imv_swima_agent_t {
/**
* REST API to strongTNC manager
*/
imv_swima_rest_t *rest_api;
rest_t *rest_api;
};
@@ -770,7 +770,7 @@ imv_agent_if_t *imv_swima_agent_create(const char *name, TNC_IMVID id,
"%s.plugins.imv-swima.rest_api_timeout", 120, lib->ns);
if (rest_api_uri)
{
this->rest_api = imv_swima_rest_create(rest_api_uri, rest_api_timeout);
this->rest_api = rest_create(rest_api_uri, rest_api_timeout);
}
return &this->public;
@@ -13,22 +13,24 @@
* for more details.
*/
#ifdef USE_JSON
#define _GNU_SOURCE
#include <stdio.h>
#include "imv_swima_rest.h"
#include "rest.h"
typedef struct private_imv_swima_rest_t private_imv_swima_rest_t;
typedef struct private_rest_t private_rest_t;
/**
* Private data of an imv_swima_rest_t object.
* Private data of an rest_t object.
*/
struct private_imv_swima_rest_t {
struct private_rest_t {
/**
* Public members of imv_swima_rest_t
* Public members of rest_t
*/
imv_swima_rest_t public;
rest_t public;
/**
* URI of REST API
@@ -42,10 +44,41 @@ struct private_imv_swima_rest_t {
};
METHOD(rest_t, get, status_t,
private_rest_t *this, char *command, json_object **jresponse)
{
struct json_tokener *tokener;
chunk_t response = chunk_empty;
status_t status;
char *uri;
if (asprintf(&uri, "%s%s",this->uri, command) < 0)
{
return FAILED;
}
status = lib->fetcher->fetch(lib->fetcher, uri, &response,
FETCH_TIMEOUT, this->timeout,
FETCH_END);
free(uri);
if (status == SUCCESS && jresponse)
{
/* Parse HTTP response into a JSON object */
tokener = json_tokener_new();
*jresponse = json_tokener_parse_ex(tokener, response.ptr, response.len);
json_tokener_free(tokener);
}
free(response.ptr);
return status;
}
#define HTTP_STATUS_CODE_NOT_FOUND 404
#define HTTP_STATUS_CODE_PRECONDITION_FAILED 412
METHOD(imv_swima_rest_t, post, status_t,
private_imv_swima_rest_t *this, char *command, json_object *jrequest,
METHOD(rest_t, post, status_t,
private_rest_t *this, char *command, json_object *jrequest,
json_object **jresponse)
{
struct json_tokener *tokener;
@@ -72,22 +105,31 @@ METHOD(imv_swima_rest_t, post, status_t,
if (status != SUCCESS)
{
if (code != HTTP_STATUS_CODE_PRECONDITION_FAILED || !response.ptr)
switch (code)
{
DBG2(DBG_IMV, "REST http request failed with status code: %d", code);
status = FAILED;
}
else
{
if (jresponse)
{
/* Parse HTTP response into a JSON object */
tokener = json_tokener_new();
*jresponse = json_tokener_parse_ex(tokener, response.ptr,
response.len);
json_tokener_free(tokener);
}
status = NEED_MORE;
case HTTP_STATUS_CODE_NOT_FOUND:
status = NOT_FOUND;
break;
case HTTP_STATUS_CODE_PRECONDITION_FAILED:
if (!response.ptr)
{
return FAILED;
}
if (jresponse)
{
/* Parse HTTP response into a JSON object */
tokener = json_tokener_new();
*jresponse = json_tokener_parse_ex(tokener, response.ptr,
response.len);
json_tokener_free(tokener);
}
status = NEED_MORE;
break;
default:
DBG2(DBG_IMV, "REST http request failed with status code: %d",
code);
status = FAILED;
break;
}
}
free(response.ptr);
@@ -95,8 +137,8 @@ METHOD(imv_swima_rest_t, post, status_t,
return status;
}
METHOD(imv_swima_rest_t, destroy, void,
private_imv_swima_rest_t *this)
METHOD(rest_t, destroy, void,
private_rest_t *this)
{
free(this->uri);
free(this);
@@ -105,12 +147,13 @@ METHOD(imv_swima_rest_t, destroy, void,
/**
* Described in header.
*/
imv_swima_rest_t *imv_swima_rest_create(char *uri, u_int timeout)
rest_t *rest_create(char *uri, u_int timeout)
{
private_imv_swima_rest_t *this;
private_rest_t *this;
INIT(this,
.public = {
.get = _get,
.post = _post,
.destroy = _destroy,
},
@@ -121,4 +164,4 @@ imv_swima_rest_t *imv_swima_rest_create(char *uri, u_int timeout)
return &this->public;
}
#endif /* USE_JSON */
@@ -17,47 +17,58 @@
* @defgroup imv_swima imv_swima
* @ingroup libimcv_plugins
*
* @defgroup imv_swima_rest_t imv_swima_rest
* @defgroup rest_t rest
* @{ @ingroup imv_swima
*/
#ifndef IMV_SWIMA_REST_H_
#define IMV_SWIMA_REST_H_
#ifndef REST_H_
#define REST_H_
#ifdef USE_JSON
#include <library.h>
#include <json.h>
typedef struct imv_swima_rest_t imv_swima_rest_t;
typedef struct rest_t rest_t;
/**
* Public REST interface
*/
struct imv_swima_rest_t {
struct rest_t {
/**
* Post a HTTP request including a JSON object
* Send an HTTP GET request returning a JSON object
*
* @param jresp JSON object in HTTP
* @return Status (SUCCESS or FAILED)
*/
status_t (*get)(rest_t *this, char *command, json_object **jresp);
/**
* Send an HTTP POST request including a JSON object
*
* @param jreq JSON object in HTTP request
* @param jresp JSON object in HTTP response if NEED_MORE
* @return Status (SUCCESS, NEED_MORE or FAILED)
*/
status_t (*post)(imv_swima_rest_t *this, char *command, json_object *jreq,
status_t (*post)(rest_t *this, char *command, json_object *jreq,
json_object **jresp);
/**
* Destroy imv_swima_rest_t object
* Destroy rest_t object
*/
void (*destroy)(imv_swima_rest_t *this);
void (*destroy)(rest_t *this);
};
/**
* Create an imv_swima_rest_t instance
* Create an rest_t instance
*
* @param uri REST URI (http://username:password@hostname[:port]/api/)
* @param timeout Timeout of the REST connection
*/
imv_swima_rest_t* imv_swima_rest_create(char *uri, u_int timeout);
rest_t* rest_create(char *uri, u_int timeout);
#endif /** IMV_SWIMA_REST_H_ @}*/
#endif /* USE_JSON */
#endif /** REST_H_ @}*/