summaryrefslogtreecommitdiffstats
path: root/openpgp-ffi/src/parse/stream.rs
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-03-20 13:55:56 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-03-20 13:55:56 +0100
commitaba46b434dd0d344a916a610e250d6469c261d12 (patch)
treedc6e2173620b387a3d1c46c9d68e39fa1a237b0a /openpgp-ffi/src/parse/stream.rs
parentce6786b8b8aca4309b27d57d957a154e47367a0e (diff)
openpgp-ffi: Add test.
Diffstat (limited to 'openpgp-ffi/src/parse/stream.rs')
-rw-r--r--openpgp-ffi/src/parse/stream.rs128
1 files changed, 128 insertions, 0 deletions
diff --git a/openpgp-ffi/src/parse/stream.rs b/openpgp-ffi/src/parse/stream.rs
index 69205140..643c0a30 100644
--- a/openpgp-ffi/src/parse/stream.rs
+++ b/openpgp-ffi/src/parse/stream.rs
@@ -479,6 +479,134 @@ fn decrypt_real<'a>(input: &'a mut io::ReaderKind,
/// first parameter to each of them.
///
/// Note: all of the parameters are required; none may be NULL.
+///
+/// # Example
+///
+/// ```c
+/// #define _GNU_SOURCE
+/// #include <assert.h>
+/// #include <error.h>
+/// #include <errno.h>
+/// #include <stdio.h>
+/// #include <stdlib.h>
+/// #include <string.h>
+///
+/// #include <sequoia/openpgp.h>
+///
+/// struct decrypt_cookie {
+/// pgp_tpk_t key;
+/// int get_secret_keys_called;
+/// };
+///
+/// static pgp_status_t
+/// get_public_keys_cb (void *cookie_raw,
+/// pgp_keyid_t *keyids, size_t keyids_len,
+/// pgp_tpk_t **tpks, size_t *tpk_len,
+/// void (**our_free)(void *))
+/// {
+/// /* Feed the TPKs to the verifier here. */
+/// *tpks = NULL;
+/// *tpk_len = 0;
+/// *our_free = free;
+/// return PGP_STATUS_SUCCESS;
+/// }
+///
+/// static pgp_status_t
+/// check_signatures_cb(void *cookie_opaque,
+/// pgp_verification_results_t results, size_t levels)
+/// {
+/// /* Implement your verification policy here. */
+/// return PGP_STATUS_SUCCESS;
+/// }
+///
+/// static pgp_status_t
+/// get_secret_keys_cb (void *cookie_opaque,
+/// pgp_pkesk_t *pkesks, size_t pkesk_count,
+/// pgp_skesk_t *skesks, size_t skesk_count,
+/// pgp_secret_t *secret)
+/// {
+/// pgp_error_t err;
+/// struct decrypt_cookie *cookie = cookie_opaque;
+///
+/// /* Prevent iterations, we only have one key to offer. */
+/// if (cookie->get_secret_keys_called)
+/// return PGP_STATUS_UNKNOWN_ERROR;
+/// cookie->get_secret_keys_called = 1;
+///
+/// for (int i = 0; i < pkesk_count; i++) {
+/// pgp_pkesk_t pkesk = pkesks[i];
+/// pgp_keyid_t keyid = pgp_pkesk_recipient (pkesk);
+///
+/// pgp_tpk_key_iter_t key_iter = pgp_tpk_key_iter_all (cookie->key);
+/// pgp_key_t key;
+/// while ((key = pgp_tpk_key_iter_next (key_iter, NULL, NULL))) {
+/// pgp_keyid_t this_keyid = pgp_key_keyid (key);
+/// int match = pgp_keyid_equal (this_keyid, keyid);
+/// pgp_keyid_free (this_keyid);
+/// if (match)
+/// break;
+/// pgp_key_free (key);
+/// }
+/// pgp_tpk_key_iter_free (key_iter);
+/// pgp_keyid_free (keyid);
+/// if (! key)
+/// continue;
+///
+/// uint8_t algo;
+/// uint8_t session_key[1024];
+/// size_t session_key_len = sizeof session_key;
+/// if (pgp_pkesk_decrypt (&err,
+/// pkesk, key, &algo,
+/// session_key, &session_key_len)) {
+/// error (1, 0, "pgp_pkesk_decrypt: %s", pgp_error_to_string (err));
+/// }
+/// pgp_key_free (key);
+///
+/// *secret = pgp_secret_cached (algo, session_key, session_key_len);
+/// return PGP_STATUS_SUCCESS;
+/// }
+///
+/// return PGP_STATUS_UNKNOWN_ERROR;
+/// }
+///
+/// int
+/// main (int argc, char **argv)
+/// {
+/// pgp_status_t rc;
+/// pgp_error_t err;
+/// pgp_tpk_t tpk;
+/// pgp_reader_t source;
+/// pgp_writer_t sink;
+/// uint8_t *buf = NULL;
+/// size_t buf_len = 0;
+///
+/// tpk = pgp_tpk_from_file (
+/// NULL, "../openpgp/tests/data/keys/testy-private.pgp");
+/// assert(tpk);
+///
+/// source = pgp_reader_from_file (
+/// NULL, "../openpgp/tests/data/messages/encrypted-to-testy.gpg");
+/// assert (source);
+/// sink = pgp_writer_alloc ((void **) &buf, &buf_len);
+/// assert (sink);
+///
+/// struct decrypt_cookie cookie = {
+/// .key = tpk,
+/// .get_secret_keys_called = 0,
+/// };
+/// rc = pgp_decrypt (&err, source, sink,
+/// get_public_keys_cb, get_secret_keys_cb,
+/// check_signatures_cb, &cookie);
+/// assert (rc == PGP_STATUS_SUCCESS);
+/// assert (cookie.get_secret_keys_called);
+/// pgp_writer_free (sink);
+/// assert (memcmp (buf, "Test, 1-2-3.\n", buf_len) == 0);
+/// free (buf);
+/// pgp_reader_free (source);
+/// pgp_tpk_free (tpk);
+/// return 0;
+/// }
+/// ```
#[::sequoia_ffi_macros::extern_fn] #[no_mangle] pub extern "system"
fn pgp_decrypt<'a>(errp: Option<&mut *mut ::error::Error>,
input: *mut io::Reader,