Skip to content

Commit

Permalink
Fix #43
Browse files Browse the repository at this point in the history
Trim CRLF from user input
    Added tests

Fixed some OpenPGP::Message behavior
    Compressed data should always be treated as raw, not armored

Renamed Packet::read to Packet::actual_read, and wrapped new function in new Packet::read
Renamed some tests
Added extract_decrypted function into tests to easily extract plaintext message packets
  • Loading branch information
calccrypto committed Mar 1, 2019
1 parent 80e5790 commit 272e079
Show file tree
Hide file tree
Showing 58 changed files with 476 additions and 321 deletions.
15 changes: 10 additions & 5 deletions cli/modules/decrypt_pka.h
Expand Up @@ -91,19 +91,24 @@ const Module decrypt_pka(
const OpenPGP::Message decrypted = OpenPGP::Decrypt::pka(pri, args.at("passphrase"), message);

if (!decrypted.meaningful()){
err << "Error: Decrypted data is not meaningul." << std::endl;
err << "Error: Decrypted data is not meaningful." << std::endl;
return -1;
}

// extract data
std::string cleartext = "";
for(OpenPGP::Packet::Tag::Ptr const & p : decrypted.get_packets()){
if (p -> get_tag() == OpenPGP::Packet::LITERAL_DATA){
cleartext += std::static_pointer_cast <OpenPGP::Packet::Tag11> (p) -> out(false);
switch (p -> get_tag()) {
case OpenPGP::Packet::COMPRESSED_DATA:
cleartext += std::static_pointer_cast <OpenPGP::Packet::Tag8> (p) -> get_body().show();
break;
case OpenPGP::Packet::LITERAL_DATA:
cleartext += std::static_pointer_cast <OpenPGP::Packet::Tag11> (p) -> out(false);
break;
}
}

cleartext += "\n";
cleartext += "\n";
}

// if signing key provided, check the signature
if (signer){
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/modules/fingerprint.cpp
Expand Up @@ -3,7 +3,7 @@
#include <gtest/gtest.h>

#include "cli/modules/fingerprint.h"
#include "tests/testvectors/read_pgp.h"
#include "tests/read_pgp.h"

#define GPG_DIR "tests/testvectors/gpg/"

Expand Down
4 changes: 2 additions & 2 deletions cli/tests/modules/module.cpp
Expand Up @@ -3,7 +3,7 @@
#include <gtest/gtest.h>

#include "cli/modules/module.h"
#include "tests/testvectors/read_pgp.h"
#include "tests/read_pgp.h"

TEST(Module, module){
// bad name
Expand Down Expand Up @@ -171,4 +171,4 @@ TEST(Module, module){
})
);
}
}
}
4 changes: 4 additions & 0 deletions include/Message.h
Expand Up @@ -29,6 +29,7 @@ THE SOFTWARE.
#include <list>

#include "PGP.h"
#include "Packets/Tag8.h"

namespace OpenPGP {

Expand Down Expand Up @@ -113,6 +114,9 @@ namespace OpenPGP {
Message(std::istream & stream);
~Message();

// Read Binary data
void read_raw(const std::string & data);

std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const; // display information; indents is used to tab the output if desired
std::string raw() const; // write packets only
std::string write(const Armored armor = DEFAULT) const;
Expand Down
5 changes: 1 addition & 4 deletions include/Misc/sigcalc.h
Expand Up @@ -26,15 +26,12 @@ THE SOFTWARE.
#ifndef __SIGNATURE__
#define __SIGNATURE__

#include <sstream>
#include <stdexcept>
#include <string>

#include "Hashes/Hashes.h"
#include "Packets/Packets.h"
#include "Misc/pgptime.h"

namespace OpenPGP {

// Modify data for signature version 3 or 4
//
// Once the data body is hashed, then a trailer is hashed. A V3
Expand Down
5 changes: 4 additions & 1 deletion include/PGP.h
Expand Up @@ -52,8 +52,11 @@ namespace OpenPGP {
static const Type_t SIGNATURE; // Used for detached signatures, OpenPGP/MIME signatures, and cleartext signatures. Note that PGP 2.x uses BEGIN PGP MESSAGE for detached signatures.
static const Type_t SIGNED_MESSAGE; // Used for cleartext signatures; Bad PGP type.

static const std::string ASCII_Armor_5_Dashes; // "-----";
static const std::string ASCII_Armor_Begin; // "-----BEGIN PGP ";
static const std::string ASCII_Armor_Header[]; // ASCII data at beginning and end of OpenPGP packet
static const std::string ASCII_Armor_Key[]; // ASCII descriptor of OpenPGP packet
static const std::string ASCII_Armor_End; // "-----END PGP ";

// used for write function
enum Armored {
Expand Down Expand Up @@ -103,7 +106,7 @@ namespace OpenPGP {
void read(std::istream & stream);

// Read Binary data
void read_raw(const std::string & data);
virtual void read_raw(const std::string & data);
void read_raw(std::istream & stream);

virtual std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const; // display information; indents is used to tab the output if desired
Expand Down
3 changes: 2 additions & 1 deletion include/Packets/Key.h
Expand Up @@ -53,6 +53,8 @@ namespace OpenPGP {

Key(uint8_t tag);

virtual void actual_read(const std::string & data);

public:
typedef std::shared_ptr <Key> Ptr;

Expand All @@ -61,7 +63,6 @@ namespace OpenPGP {
Key(const std::string & data);
virtual ~Key();

virtual void read(const std::string & data);
virtual std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
virtual std::string raw() const;

Expand Down
5 changes: 4 additions & 1 deletion include/Packets/Packet.h
Expand Up @@ -135,6 +135,9 @@ namespace OpenPGP {
// returns Tag data with new format Tag length
static std::string write_new_length(const uint8_t tag, const std::string & data, const uint8_t part);

// the public read() wraps actual_read()
virtual void actual_read(const std::string & data) = 0;

// returns first line of show functions (no tab or newline)
virtual std::string show_title() const; // virtual to allow for overriding for special cases

Expand All @@ -147,7 +150,7 @@ namespace OpenPGP {

Tag();
virtual ~Tag();
virtual void read(const std::string & data) = 0;
void read(const std::string & data);
virtual std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const = 0;
virtual std::string raw() const = 0;
virtual std::string write() const;
Expand Down
3 changes: 2 additions & 1 deletion include/Packets/Tag1.h
Expand Up @@ -99,13 +99,14 @@ namespace OpenPGP {
uint8_t pka;
PKA::Values mpi; // algorithm specific fields

void actual_read(const std::string & data);

public:
typedef std::shared_ptr <Packet::Tag1> Ptr;

Tag1();
Tag1(const Tag1 & copy);
Tag1(const std::string & data);
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;

Expand Down
3 changes: 2 additions & 1 deletion include/Packets/Tag10.h
Expand Up @@ -54,13 +54,14 @@ namespace OpenPGP {
private:
std::string pgp; // should always be "PGP"

void actual_read(const std::string & data);

public:
typedef std::shared_ptr <Packet::Tag10> Ptr;

Tag10();
Tag10(const Tag10 & copy);
Tag10(const std::string & data);
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;

Expand Down
2 changes: 1 addition & 1 deletion include/Packets/Tag11.h
Expand Up @@ -95,6 +95,7 @@ namespace OpenPGP {
uint32_t time;
std::string literal; // source data; no line ending conversion

void actual_read(const std::string & data);
std::string show_title() const;

public:
Expand All @@ -103,7 +104,6 @@ namespace OpenPGP {
Tag11(const PartialBodyLength &part = NOT_PARTIAL);
Tag11(const Tag11 & copy);
Tag11(const std::string & data);
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;
std::string write() const;
Expand Down
3 changes: 2 additions & 1 deletion include/Packets/Tag12.h
Expand Up @@ -47,14 +47,15 @@ namespace OpenPGP {
private:
std::string trust;

void actual_read(const std::string & data);

public:
typedef std::shared_ptr <Packet::Tag12> Ptr;

Tag12();
Tag12(const Tag12 & copy);
Tag12(const std::string & data);
Tag12(std::istream & stream);
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;

Expand Down
3 changes: 2 additions & 1 deletion include/Packets/Tag13.h
Expand Up @@ -43,13 +43,14 @@ namespace OpenPGP {
private:
std::string contents;

void actual_read(const std::string & data);

public:
typedef std::shared_ptr <Packet::Tag13> Ptr;

Tag13();
Tag13(const Tag13 & copy);
Tag13(const std::string & data);
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;

Expand Down
2 changes: 1 addition & 1 deletion include/Packets/Tag17.h
Expand Up @@ -78,6 +78,7 @@ namespace OpenPGP {
Attributes attributes;

void read_subpacket(const std::string & data, std::string::size_type & pos, std::string::size_type & length);
void actual_read(const std::string & data);

public:
typedef std::shared_ptr <Packet::Tag17> Ptr;
Expand All @@ -86,7 +87,6 @@ namespace OpenPGP {
Tag17(const Tag17 & copy);
Tag17(const std::string & data);
~Tag17();
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;

Expand Down
2 changes: 1 addition & 1 deletion include/Packets/Tag18.h
Expand Up @@ -134,6 +134,7 @@ namespace OpenPGP {
private:
std::string protected_data;

void actual_read(const std::string & data);
std::string show_title() const;

public:
Expand All @@ -142,7 +143,6 @@ namespace OpenPGP {
Tag18(const PartialBodyLength & part = NOT_PARTIAL);
Tag18(const Tag18 & copy);
Tag18(const std::string & data);
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;
std::string write() const;
Expand Down
3 changes: 2 additions & 1 deletion include/Packets/Tag19.h
Expand Up @@ -60,13 +60,14 @@ namespace OpenPGP {
private:
std::string hash;

void actual_read(const std::string & data);

public:
typedef std::shared_ptr <Packet::Tag19> Ptr;

Tag19();
Tag19(const Tag19 & copy);
Tag19(const std::string & data);
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;

Expand Down
3 changes: 2 additions & 1 deletion include/Packets/Tag2.h
Expand Up @@ -85,14 +85,15 @@ namespace OpenPGP {
// Function to parse all subpackets
void read_subpackets(const std::string & data, Subpackets & subpackets);

void actual_read(const std::string & data);

public:
typedef std::shared_ptr <Packet::Tag2> Ptr;

Tag2();
Tag2(const Tag2 & copy);
Tag2(const std::string & data);
~Tag2();
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;

Expand Down
3 changes: 2 additions & 1 deletion include/Packets/Tag3.h
Expand Up @@ -90,14 +90,15 @@ namespace OpenPGP {
S2K::S2K::Ptr s2k;
std::shared_ptr <std::string> esk; // encrypted session key

void actual_read(const std::string & data);

public:
typedef std::shared_ptr <Packet::Tag3> Ptr;

Tag3();
Tag3(const Tag3 & copy);
Tag3(const std::string & data);
~Tag3();
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;

Expand Down
3 changes: 2 additions & 1 deletion include/Packets/Tag4.h
Expand Up @@ -79,13 +79,14 @@ namespace OpenPGP {
std::string keyid; // 8 octets
uint8_t nested; // A zero value indicates that the next packet is another One-Pass Signature packet that describes another signature to be applied to the same message data.

void actual_read(const std::string & data);

public:
typedef std::shared_ptr <Packet::Tag4> Ptr;

Tag4();
Tag4(const Tag4 & copy);
Tag4(const std::string & data);
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;

Expand Down
3 changes: 2 additions & 1 deletion include/Packets/Tag5.h
Expand Up @@ -146,14 +146,15 @@ namespace OpenPGP {

Tag5(uint8_t tag);

void actual_read(const std::string & data);

public:
typedef std::shared_ptr <Packet::Tag5> Ptr;

Tag5();
Tag5(const Tag5 & copy);
Tag5(const std::string & data);
virtual ~Tag5();
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;

Expand Down
3 changes: 2 additions & 1 deletion include/Packets/Tag60.h
Expand Up @@ -35,13 +35,14 @@ namespace OpenPGP {
private:
std::string stream;

void actual_read(const std::string & data);

public:
typedef std::shared_ptr <Packet::Tag60> Ptr;

Tag60();
Tag60(const Tag60 & copy);
Tag60(const std::string & data);
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;

Expand Down
3 changes: 2 additions & 1 deletion include/Packets/Tag61.h
Expand Up @@ -35,13 +35,14 @@ namespace OpenPGP {
private:
std::string stream;

void actual_read(const std::string & data);

public:
typedef std::shared_ptr <Packet::Tag61> Ptr;

Tag61();
Tag61(const Tag61 & copy);
Tag61(const std::string & data);
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;

Expand Down
3 changes: 2 additions & 1 deletion include/Packets/Tag62.h
Expand Up @@ -35,13 +35,14 @@ namespace OpenPGP {
private:
std::string stream;

void actual_read(const std::string & data);

public:
typedef std::shared_ptr <Packet::Tag62> Ptr;

Tag62();
Tag62(const Tag62 & copy);
Tag62(const std::string & data);
void read(const std::string & data);
std::string show(const std::size_t indents = 0, const std::size_t indent_size = 4) const;
std::string raw() const;

Expand Down

0 comments on commit 272e079

Please sign in to comment.