Skip to content

Commit

Permalink
Add support for decrypting raw data, see issue #5
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed Dec 29, 2018
1 parent 7e8797a commit b24ad33
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 12 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: gpg
Type: Package
Title: GNU Privacy Guard for R
Version: 0.5
Version: 0.5.9000
Authors@R: person("Jeroen", "Ooms", email = "jeroen@berkeley.edu", role = c("aut", "cre"))
Description: Bindings to GnuPG for working with OpenGPG (RFC4880) cryptographic methods.
Includes utilities for public key encryption, creating and verifying digital signatures,
Expand All @@ -11,7 +11,7 @@ Description: Bindings to GnuPG for working with OpenGPG (RFC4880) cryptographic
License: MIT + file LICENSE
SystemRequirements: GPGME: libgpgme11-dev (deb), gpgme-devel (rpm) gpgme (brew).
On Linux 'haveged' is recommended for generating entropy when using the GPG key generator.
RoxygenNote: 6.0.1
RoxygenNote: 6.1.1
Roxygen: list(markdown = TRUE)
Imports:
curl
Expand Down
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
0.6
- Add as_text parameter to decrypt to support raw output

0.5
- Extract autobrew script to separate repository

Expand Down
7 changes: 4 additions & 3 deletions R/encrypt.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ gpg_encrypt <- function(data, receiver, signer = NULL){
#' @rdname gpg_encrypt
#' @param verify automatically checks that all signatures (if any) can be verified and
#' raises an error otherwise
#' @param as_text convert output to text. Set to FALSE if you expect binary data.
#' @useDynLib gpg R_gpgme_decrypt R_gpgme_signed_decrypt
gpg_decrypt <- function(data, verify = TRUE){
gpg_decrypt <- function(data, verify = TRUE, as_text = TRUE){
data <- file_or_raw(data)
if(isTRUE(verify)){
.Call(R_gpgme_signed_decrypt, data)
.Call(R_gpgme_signed_decrypt, data, as_text)
} else {
.Call(R_gpgme_decrypt, data)
.Call(R_gpgme_decrypt, data, as_text)
}
}

Expand Down
5 changes: 4 additions & 1 deletion man/gpg_encrypt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/gpg_info.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/gpg_keygen.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/gpg_keys.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/gpg_sign.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
gpgme_ctx_t ctx;
gpgme_error_t pwprompt(void *hook, const char *uid_hint, const char *passphrase_info, int prev_was_bad, int fd);
SEXP data_to_string(gpgme_data_t buf);
SEXP data_to_raw(gpgme_data_t buf);

struct keylist {
gpgme_key_t key;
Expand Down
17 changes: 13 additions & 4 deletions src/encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ SEXP R_gpgme_encrypt(SEXP data, SEXP id) {
return data_to_string(output);
}

SEXP R_gpgme_decrypt(SEXP data) {
SEXP R_gpgme_decrypt(SEXP data, SEXP as_text) {
gpgme_data_t output, input;
bail(gpgme_data_new_from_mem(&input, (const char*) RAW(data), LENGTH(data), 0), "creating input buffer");
bail(gpgme_data_new(&output), "creating output buffer");
bail(gpgme_op_decrypt(ctx, input, output), "decrypt message");
return data_to_string(output);
return Rf_asLogical(as_text) ? data_to_string(output) : data_to_raw(output);
}

SEXP R_gpgme_signed_encrypt(SEXP data, SEXP receiver, SEXP sender) {
Expand Down Expand Up @@ -51,12 +51,13 @@ SEXP R_gpgme_signed_encrypt(SEXP data, SEXP receiver, SEXP sender) {
return data_to_string(output);
}

SEXP R_gpgme_signed_decrypt(SEXP data) {
SEXP R_gpgme_signed_decrypt(SEXP data, SEXP as_text) {
gpgme_data_t output, input;
bail(gpgme_data_new_from_mem(&input, (const char*) RAW(data), LENGTH(data), 0), "creating input buffer");
bail(gpgme_data_new(&output), "creating output buffer");
bail(gpgme_op_decrypt_verify(ctx, input, output), "verify signatures and decrypt message");
SEXP out = PROTECT(data_to_string(output));
SEXP out = Rf_asLogical(as_text) ? data_to_string(output) : data_to_raw(output);
PROTECT(out);

//check signatures
gpgme_verify_result_t result = gpgme_op_verify_result(ctx);
Expand All @@ -81,3 +82,11 @@ SEXP data_to_string(gpgme_data_t buf){
return out;
}

SEXP data_to_raw(gpgme_data_t buf){
size_t len;
char * sig = gpgme_data_release_and_get_mem(buf, &len);
SEXP out = allocVector(RAWSXP, len);
memcpy(RAW(out), sig, len);
gpgme_free(sig);
return out;
}

0 comments on commit b24ad33

Please sign in to comment.