summaryrefslogtreecommitdiffstats
path: root/openpgp-ffi/examples
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-04-26 14:34:36 +0200
committerJustus Winter <justus@sequoia-pgp.org>2019-04-29 14:55:56 +0200
commit1fa018acc90194e1cd1ddf2bd0c6706000a3bc9e (patch)
treec29ecc4aac1d2d85e1ff08a111d1462b74c6f3e8 /openpgp-ffi/examples
parentd53cb0c4df33ebabd1af7e97e8469ed159763a6e (diff)
openpgp: New TSK type.
- With a1e226f8f1418de43e577fdaa1d087b68bbb09ae in place, we have a more general way to add components to a TPK. Retire the current `TSK` type and replace it with a thin shim that only allows serialization of secret keys. - Fixes #107.
Diffstat (limited to 'openpgp-ffi/examples')
-rw-r--r--openpgp-ffi/examples/Makefile1
-rw-r--r--openpgp-ffi/examples/generate-key.c37
2 files changed, 38 insertions, 0 deletions
diff --git a/openpgp-ffi/examples/Makefile b/openpgp-ffi/examples/Makefile
index 77e5aad9..f5dd0f8c 100644
--- a/openpgp-ffi/examples/Makefile
+++ b/openpgp-ffi/examples/Makefile
@@ -11,6 +11,7 @@ EXAMPLE_TARGET_DIR ?= $(CARGO_TARGET_DIR)/debug/c-examples/openpgp-ffi
EXAMPLES = \
example reader parser encrypt-for armor \
decrypt-with \
+ generate-key \
type-safety-demo use-after-free-demo immutable-reference-demo \
CFLAGS = -I../include -O0 -g -Wall -Werror
diff --git a/openpgp-ffi/examples/generate-key.c b/openpgp-ffi/examples/generate-key.c
new file mode 100644
index 00000000..268e5593
--- /dev/null
+++ b/openpgp-ffi/examples/generate-key.c
@@ -0,0 +1,37 @@
+#include <assert.h>
+#include <stdio.h>
+
+#include <sequoia/openpgp.h>
+
+int
+main () {
+ pgp_status_t rc;
+
+ /* First, generate the key. */
+ pgp_tpk_builder_t builder = pgp_tpk_builder_default ();
+ pgp_tpk_builder_set_cipher_suite (&builder, PGP_TPK_CIPHER_SUITE_CV25519);
+
+ pgp_tpk_t tpk;
+ pgp_signature_t revocation;
+ pgp_tpk_builder_generate (NULL, builder, &tpk, &revocation);
+ assert (tpk);
+ assert (revocation);
+ pgp_signature_free (revocation); /* Free the generated revocation. */
+
+ /* Now, setup an armor writer for stdout. */
+ pgp_writer_t sink = pgp_writer_from_fd (STDOUT_FILENO);
+ pgp_writer_t armor = pgp_armor_writer_new (NULL, sink,
+ PGP_ARMOR_KIND_SECRETKEY,
+ NULL, 0);
+ assert (armor);
+
+ /* Finally, derive a TSK object, and serialize it. */
+ pgp_tsk_t tsk = pgp_tpk_as_tsk (tpk);
+ rc = pgp_tsk_serialize (NULL, tsk, armor);
+ assert (rc == PGP_STATUS_SUCCESS);
+
+ pgp_tsk_free (tsk);
+ pgp_writer_free (armor);
+ pgp_writer_free (sink);
+ pgp_tpk_free (tpk);
+}