From fa1153b4c3237d897a871331f0f9c0f8d30ca5a1 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 28 Dec 2009 13:34:08 +0100 Subject: [PATCH] Add C bindings. --- lib/certificate.cc | 28 ++++++++++ lib/certificate.h | 22 +++++++- lib/fides.cc | 127 +++++++++++++++++++++++++++++++++++++++++++++ lib/fides.h | 60 +++++++++++++++++++-- lib/privatekey.cc | 38 ++++++++++++++ lib/privatekey.h | 24 ++++++++- lib/publickey.cc | 44 ++++++++++++++++ lib/publickey.h | 25 +++++++++ lib/utility.cc | 18 +++++++ lib/utility.h | 13 +++++ 10 files changed, 393 insertions(+), 6 deletions(-) diff --git a/lib/certificate.cc b/lib/certificate.cc index b801403..5230772 100644 --- a/lib/certificate.cc +++ b/lib/certificate.cc @@ -15,6 +15,7 @@ License along with this program; if not, see . */ +#include #include #include "certificate.h" @@ -82,3 +83,30 @@ namespace Fides { return data; } } + +// C bindings + +fides_certificate *fides_certificate_new(const fides_publickey *pub, struct timeval timestamp, const char *statement, const char *signature) { + return new Fides::Certificate(pub, timestamp, statement, signature); +} + +fides_certificate *fides_certificate_new_priv(const fides_privatekey *priv, struct timeval timestamp, const char *statement) { + return new Fides::Certificate(priv, timestamp, statement); +} + +void fides_certificate_free(fides_certificate *c) { + delete c; +} + + +char *fides_certificate_to_string(fides_certificate *c) { + return strdup(c->to_string().c_str()); +} + +char *fides_certificate_fingerprint(fides_certificate *c, unsigned int bits) { + return strdup(c->fingerprint(bits).c_str()); +} + +bool fides_certificate_validate(fides_certificate *c) { + return c->validate(); +} diff --git a/lib/certificate.h b/lib/certificate.h index 2a605ea..0277522 100644 --- a/lib/certificate.h +++ b/lib/certificate.h @@ -18,11 +18,13 @@ #ifndef __FIDES_CERTIFICATE_H__ #define __FIDES_CERTIFICATE_H__ -#include #include #include "publickey.h" #include "privatekey.h" +#ifdef __cplusplus +#include + namespace Fides { class Certificate { friend class Manager; @@ -43,4 +45,22 @@ namespace Fides { }; } +extern "C" { +typedef Fides::Certificate fides_certificate; +#else +typedef struct fides_certificate fides_certificate; +#endif + +extern fides_certificate *fides_certificate_new(const fides_publickey *pub, struct timeval timestamp, const char *statement, const char *signature); +extern fides_certificate *fides_certificate_new_priv(const fides_privatekey *priv, struct timeval timestamp, const char *statement); +extern void fides_certificate_free(fides_certificate *c); + +extern char *fides_certificate_to_string(fides_certificate *c); +extern char *fides_certificate_fingerprint(fides_certificate *c, unsigned int bits); +extern bool fides_certificate_validate(fides_certificate *c); + +#ifdef __cplusplus +} +#endif + #endif diff --git a/lib/fides.cc b/lib/fides.cc index 7c3cabf..ea69bd6 100644 --- a/lib/fides.cc +++ b/lib/fides.cc @@ -681,3 +681,130 @@ namespace Fides { sign(full); } } + +// C bindings + +fides_manager *fides_init_manager(char *homedir) { + return new Fides::Manager(homedir); +} + +void fides_exit_manager(fides_manager *m) { + delete m; +} + +bool fides_is_firstrun(fides_manager *m) { + return m->is_firstrun(); +} + +bool fides_fsck(fides_manager *m) { + return m->fsck(); +} + +char *fides_get_homedir(fides_manager *m) { + return strdup(m->get_homedir().c_str()); +} + +void fides_sign(fides_manager *m, const char *statement) { + m->sign(statement); +} + +void fides_allow(fides_manager *m, const char *statement, const fides_publickey *key) { + m->allow(statement, key); +} + +void fides_dontcare(fides_manager *m, const char *statement, const fides_publickey *key) { + m->dontcare(statement, key); +} + +void fides_deny(fides_manager *m, const char *statement, const fides_publickey *key) { + m->deny(statement, key); +} + +bool fides_is_allowed(fides_manager *m, const char *statement, const fides_publickey *key) { + return m->is_allowed(statement, key); +} + +bool fides_is_denied(fides_manager *m, const char *statement, const fides_publickey *key) { + return m->is_denied(statement, key); +} + +void fides_auth_stats(fides_manager *m, const char *statement, int *self, int *trusted, int *all) { + return m->auth_stats(statement, *self, *trusted, *all); +} + +void fides_trust(fides_manager *m, const fides_publickey *key) { + m->trust(key); +} + +void fides_dctrust(fides_manager *m, const fides_publickey *key) { + m->dctrust(key); +} + +void fides_distrust(fides_manager *m, const fides_publickey *key) { + m->distrust(key); +} + +bool fides_is_trusted(fides_manager *m, const fides_publickey *key) { + return m->is_trusted(key); +} + +bool fides_is_distrusted(fides_manager *m, const fides_publickey *key) { + return m->is_distrusted(key); +} + +fides_publickey *fides_find_key(fides_manager *m, const char *fingerprint) { + return m->find_key(fingerprint); +} + +void fides_update_trust(fides_manager *m) { + m->update_trust(); +} + +#if 0 +const fides_certificate **find_certificates(fides_manager *m, const fides_publickey *key, const char *statement) { + fides_certificate **result = 0; + vector certs = m->find_certificates(key, statement); + if(certs->size()) { + result = malloc(sizeof *result * certs->size()); + for(int i = 0; i < certs->size(); i++) + result[i] = certs[i]; + } + return result; +} + +const fides_certificate *fides_import_certificate(fides_manager *m, const char *certificate) { + m->import_certificate(certificate); +} + +char *fides_export_certificate(fides_manager *m, const fides_certificate *certificate) { + return strdup(m->export_certificate(certificate).c_str()); +} + +const fides_publickey *fides_import_key(fides_manager *m, const char *key) { + return m->import_key(key); +} + +char *fides_export_key(fides_manager *m, const fides_publickey *key) { + return strdup(m->export_key(key).c_str()); +} + +void fides_import_all(fides_manager *m, FILE *in) { + m->import_all(in); +} + +void fides_export_all(fides_manager *m, FILE *out) { + m->export_all(out); +} +#endif + +fides_certificate *fides_certificate_from_string(fides_manager *m, const char *certificate) { + return m->certificate_from_string(certificate); +} + +fides_certificate *fides_certificate_load(fides_manager *m, const char *filename) { + return m->certificate_load(filename); +} + +void fides_certificate_save(fides_manager *m, const fides_certificate *cert, const char *filename) { + m->certificate_save(cert, filename); +} diff --git a/lib/fides.h b/lib/fides.h index 4ca088f..456ff52 100644 --- a/lib/fides.h +++ b/lib/fides.h @@ -18,16 +18,17 @@ #ifndef __FIDES_H__ #define __FIDES_H__ -#include #include -#include -#include - #include "certificate.h" #include "publickey.h" #include "privatekey.h" #include "utility.h" +#ifdef __cplusplus +#include +#include +#include + namespace Fides { class exception: public std::runtime_error { public: @@ -96,4 +97,55 @@ namespace Fides { }; } +extern "C" { +typedef Fides::Manager fides_manager; +#else +#include +#include +typedef struct fides_manager fides_manager; +#endif + +extern fides_manager *fides_init_manager(char *homedir); +extern void fides_exit_manager(fides_manager *m); + +extern bool fides_is_firstrun(fides_manager *m); +extern bool fides_fsck(fides_manager *m); +extern char *fides_get_homedir(fides_manager *m); + +extern void fides_sign(fides_manager *m, const char *statement); + +extern void fides_allow(fides_manager *m, const char *statement, const fides_publickey *key); +extern void fides_dontcare(fides_manager *m, const char *statement, const fides_publickey *key); +extern void fides_deny(fides_manager *m, const char *statement, const fides_publickey *key); +extern bool fides_is_allowed(fides_manager *m, const char *statement, const fides_publickey *key); +extern bool fides_is_denied(fides_manager *m, const char *statement, const fides_publickey *key); + +extern void fides_auth_stats(fides_manager *m, const char *statement, int *self, int *trusted, int *all); +extern void fides_trust(fides_manager *m, const fides_publickey *key); +extern void fides_dctrust(fides_manager *m, const fides_publickey *key); +extern void fides_distrust(fides_manager *m, const fides_publickey *key); +extern bool fides_is_trusted(fides_manager *m, const fides_publickey *key); +extern bool fides_is_distrusted(fides_manager *m, const fides_publickey *key); +extern fides_publickey *fides_find_key(fides_manager *m, const char *fingerprint); +extern void fides_update_trust(fides_manager *m); + +extern fides_certificate **find_certificates(fides_manager *m, const fides_publickey *key, const char *statement); + +extern const fides_certificate *fides_import_certificate(fides_manager *m, const char *certificate); +extern char *fides_export_certificate(fides_manager *m, const fides_certificate *certificcate); + +extern const fides_publickey *fides_import_key(fides_manager *m, const char *key); +extern char *fides_export_key(fides_manager *m, const fides_publickey *key); + +extern void fides_import_all(fides_manager *m, FILE *in); +extern void fides_export_all(fides_manager *m, FILE *out); + +extern fides_certificate *fides_certificate_from_string(fides_manager *m, const char *certificate); +extern fides_certificate *fides_certificate_load(fides_manager *m, const char *filename); +extern void fides_certificate_save(fides_manager *m, const fides_certificate *cert, const char *filename); + +#ifdef __cplusplus +} +#endif + #endif diff --git a/lib/privatekey.cc b/lib/privatekey.cc index e990c25..4a3f530 100644 --- a/lib/privatekey.cc +++ b/lib/privatekey.cc @@ -124,3 +124,41 @@ namespace Fides { return string((const char *)sig.begin(), (size_t)sig.size()); } } + +// C bindings + +fides_privatekey *fides_privatekey_new() { + return new Fides::PrivateKey(); +} + +void fides_privatekey_free(fides_privatekey *k) { + delete k; +} + +void fides_privatekey_load_public(fides_privatekey *k, const char *filename) { + k->load(filename); +} + +void fides_privatekey_save_public(fides_privatekey *k, const char *filename) { + k->save(filename); +} + +void fides_privatekey_load(fides_privatekey *k, const char *filename) { + k->load_private(filename); +} + +void fides_privatekey_save(fides_privatekey *k, const char *filename) { + k->save_private(filename); +} + +void fides_privatekey_generate_field(fides_privatekey *k, const char *field) { + k->generate(field); +} + +void fides_privatekey_generate(fides_privatekey *k, unsigned int bits) { + k->generate(bits); +} + +char *fides_privatekey_sign(fides_privatekey *k, const char *data) { + return strdup(k->sign(data).c_str()); +} diff --git a/lib/privatekey.h b/lib/privatekey.h index a648604..6270a2a 100644 --- a/lib/privatekey.h +++ b/lib/privatekey.h @@ -18,10 +18,12 @@ #ifndef __FIDES_PRIVATEKEY_H__ #define __FIDES_PRIVATEKEY_H__ +#include "publickey.h" + +#ifdef __cplusplus #include #include #include -#include "publickey.h" namespace Fides { class PrivateKey: public PublicKey { @@ -41,4 +43,24 @@ namespace Fides { }; } +extern "C" { +typedef Fides::PrivateKey fides_privatekey; +#else +typedef struct fides_privatekey fides_privatekey; +#endif + +extern fides_privatekey *fides_privatekey_new(); +extern void fides_privatekey_free(fides_privatekey *k); + +extern void fides_privatekey_load_public(fides_privatekey *k, const char *filename); +extern void fides_privatekey_save_public(fides_privatekey *k, const char *filename); +extern void fides_privatekey_load(fides_privatekey *k, const char *filename); +extern void fides_privatekey_save(fides_privatekey *k, const char *filename); +extern void fides_privatekey_generate_field(fides_privatekey *k, const char *field); +extern void fides_privatekey_generate(fides_privatekey *k, unsigned int bits); +extern char *fides_privatekey_sign(fides_privatekey *k, const char *data); +#ifdef __cplusplus +} +#endif + #endif diff --git a/lib/publickey.cc b/lib/publickey.cc index 4dd5b76..3a91f49 100644 --- a/lib/publickey.cc +++ b/lib/publickey.cc @@ -129,3 +129,47 @@ namespace Fides { return verifier->check_signature(sig); } } + +// C bindings + +fides_publickey *fides_publickey_new() { + return new Fides::PublicKey(); +} + +void fides_publickey_free(fides_publickey *k) { + delete k; +} + + +void fides_publickey_set_trust(fides_publickey *k, int trust) { + k->trust = trust; +} + +int fides_publickey_get_trust(fides_publickey *k) { + return k->trust; +} + + +void fides_publickey_load(fides_publickey *k, const char *filename) { + k->load(filename); +} + +void fides_publickey_save(fides_publickey *k, const char *filename) { + k->save(filename); +} + +bool fides_publickey_verify(fides_publickey *k, const char *data, const char *signature) { + return k->verify(data, signature); +} + +char *fides_publickey_to_string(fides_publickey *k) { + return strdup(k->to_string().c_str()); +} + +void fides_publickey_from_string(fides_publickey *k, const char *in) { + k->from_string(in); +} + +char *fides_publickey_fingerprint(fides_publickey *k, unsigned int bits) { + return strdup(k->fingerprint(bits).c_str()); +} diff --git a/lib/publickey.h b/lib/publickey.h index f8aae83..936bc1c 100644 --- a/lib/publickey.h +++ b/lib/publickey.h @@ -18,6 +18,7 @@ #ifndef __FIDES_PUBLICKEY_H__ #define __FIDES_PUBLICKEY_H__ +#ifdef __cplusplus #include #include #include @@ -44,4 +45,28 @@ namespace Fides { }; } +extern "C" { +typedef Fides::PublicKey fides_publickey; +#else +#include +typedef struct fides_publickey fides_publickey; +#endif + +extern fides_publickey *fides_publickey_new(); +extern void fides_publickey_free(fides_publickey *k); + +extern void fides_publickey_set_trust(fides_publickey *k, int trust); +extern int fides_publickey_get_trust(fides_publickey *k); + +extern void fides_publickey_load(fides_publickey *k, const char *filename); +extern void fides_publickey_save(fides_publickey *k, const char *filename); +extern bool fides_publickey_verify(fides_publickey *k, const char *data, const char *signature); +extern char *fides_publickey_to_string(fides_publickey *k); +extern void fides_publickey_from_string(fides_publickey *k, const char *in); +extern char *fides_publickey_fingerprint(fides_publickey *k, unsigned int bits); + +#ifdef __cplusplus +} +#endif + #endif diff --git a/lib/utility.cc b/lib/utility.cc index bf0915e..9d708fe 100644 --- a/lib/utility.cc +++ b/lib/utility.cc @@ -118,3 +118,21 @@ namespace Fides { return files; } } + +// C bindings + +char *fides_b64encode(const char *in) { + return strdup(Fides::b64encode(in).c_str()); +} + +char *fides_b64decode(const char *in) { + return strdup(Fides::b64decode(in).c_str()); +} + +char *fides_hexencode(const char *in) { + return strdup(Fides::hexencode(in).c_str()); +} + +char *fides_hexdecode(const char *in) { + return strdup(Fides::hexdecode(in).c_str()); +} diff --git a/lib/utility.h b/lib/utility.h index 4a90e7e..9437cb8 100644 --- a/lib/utility.h +++ b/lib/utility.h @@ -18,6 +18,7 @@ #ifndef __FIDES_UTILITY_H__ #define __FIDES_UTILITY_H__ +#ifdef __cplusplus #include #include #include @@ -80,4 +81,16 @@ namespace Fides { std::vector dirlist(const std::string &path); } +extern "C" { +#endif + +extern char *fides_b64encode(const char *in); +extern char *fides_b64decode(const char *in); +extern char *fides_hexencode(const char *in); +extern char *fides_hexdecode(const char *in); + +#ifdef __cplusplus +} +#endif + #endif -- 2.20.1