aboutsummaryrefslogtreecommitdiff
path: root/src/crypto.cpp
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2019-06-21 17:55:05 -0400
committerDeterminant <[email protected]>2019-06-21 17:55:05 -0400
commit729eb87a12bb0efd3c2a4cc381f39e13dfd4fbf9 (patch)
tree8ed9cb6923bf7643fea3425b06ab5ec43845f012 /src/crypto.cpp
parent2c159f5a2d5fa34de4040fd262754c56b6decbbd (diff)
add crypto C API
Diffstat (limited to 'src/crypto.cpp')
-rw-r--r--src/crypto.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/crypto.cpp b/src/crypto.cpp
new file mode 100644
index 0000000..eb49ea4
--- /dev/null
+++ b/src/crypto.cpp
@@ -0,0 +1,67 @@
+#include "salticidae/config.h"
+#ifdef SALTICIDAE_CBINDINGS
+#include "salticidae/crypto.h"
+
+using namespace salticidae;
+
+extern "C" {
+
+x509_t *x509_new_from_pem_file(const char *pem_fname, const char *passwd, SalticidaeCError *cerror) {
+ SALTICIDAE_CERROR_TRY(cerror)
+ std::string tmp;
+ if (passwd) tmp = passwd;
+ auto ret = new x509_t(x509_t::create_from_pem_file(pem_fname, passwd ? &tmp : nullptr));
+ OPENSSL_cleanse(&tmp[0], tmp.size());
+ return std::move(ret);
+ SALTICIDAE_CERROR_CATCH(cerror)
+ return nullptr;
+}
+
+x509_t *x509_new_from_der(const bytearray_t *der, SalticidaeCError *cerror) {
+ SALTICIDAE_CERROR_TRY(cerror)
+ return new x509_t(x509_t::create_from_der(*der));
+ SALTICIDAE_CERROR_CATCH(cerror)
+ return nullptr;
+}
+
+void x509_free(const x509_t *self) { delete self; }
+
+pkey_t *x509_get_pubkey(const x509_t *self) {
+ return new pkey_t(self->get_pubkey());
+}
+
+bytearray_t *x509_get_der(const x509_t *self) {
+ return new bytearray_t(self->get_der());
+}
+
+pkey_t *pkey_new_privkey_from_pem_file(const char *pem_fname, const char *passwd, SalticidaeCError *cerror) {
+ SALTICIDAE_CERROR_TRY(cerror)
+ std::string tmp;
+ if (passwd) tmp = passwd;
+ auto ret = new pkey_t(pkey_t::create_privkey_from_pem_file(pem_fname, passwd ? &tmp: nullptr));
+ OPENSSL_cleanse(&tmp[0], tmp.size());
+ return std::move(ret);
+ SALTICIDAE_CERROR_CATCH(cerror)
+ return nullptr;
+}
+
+pkey_t *pkey_new_privkey_from_der(const bytearray_t *der, SalticidaeCError *cerror) {
+ SALTICIDAE_CERROR_TRY(cerror)
+ return new pkey_t(pkey_t::create_privkey_from_der(*der));
+ SALTICIDAE_CERROR_CATCH(cerror)
+ return nullptr;
+}
+
+void pkey_free(const pkey_t *self) { delete self; }
+
+bytearray_t *pkey_get_pubkey_der(const pkey_t *self) {
+ return new bytearray_t(self->get_pubkey_der());
+}
+
+bytearray_t *pkey_get_privkey_der(const pkey_t *self) {
+ return new bytearray_t(self->get_privkey_der());
+}
+
+}
+
+#endif