forked from calccrypto/OpenPGP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPGPCleartextSignature.cpp
170 lines (142 loc) · 4.6 KB
/
PGPCleartextSignature.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "PGPCleartextSignature.h"
PGPCleartextSignature::PGPCleartextSignature():
Armor_Header(),
message(),
sig()
{}
PGPCleartextSignature::PGPCleartextSignature(const PGPCleartextSignature & copy):
Armor_Header(copy.Armor_Header),
message(copy.message),
sig(copy.sig)
{
sig.set_armored(true);
}
PGPCleartextSignature::PGPCleartextSignature(std::string & data):
PGPCleartextSignature()
{
read(data);
}
PGPCleartextSignature::PGPCleartextSignature(std::ifstream & f):
PGPCleartextSignature()
{
read(f);
}
void PGPCleartextSignature::read(std::string & data){
// remove extra data and parse unsecured data
unsigned int x = 0;
// find and remove header
while ((x < data.size()) && (data.substr(x, 15) != "-----BEGIN PGP ")){
x++;
}
data = data.substr(x, data.size() - x);
// remove carriage returns
unsigned int y = 0;
while (y < data.size()){
if (data[y] == '\r'){
data.replace(y, 1, "");
}
else{
y++;
}
}
if (data.substr(0, 34) != "-----BEGIN PGP SIGNED MESSAGE-----"){
throw std::runtime_error("Error: Data does not contain message section. Use PGP to parse this data.");
}
// remove newline after header
x = 0;
while ((x < data.size()) && data.substr(x, 1) != "\n"){
x++;
}
if (x == data.size()){
throw std::runtime_error("Error: End to Armor Header Line not found.");
}
data = data.substr(x + 1, data.size() - x - 1);
// find header keys
x = 0;
while ((x < data.size()) && (data.substr(x, 2) != "\n\n")){
x++;
}
std::string header_keys = data.substr(0, (++x)++);
// remove header keys + empty line
data = data.substr(x, data.size() - x);
// parse Armor Key
while (header_keys.size()){
x = 6;
while ((x < header_keys.size()) && (header_keys[x] != '\n')){
x++;
}
// find colon ':'
unsigned int y = 0;
while (header_keys[y] != ':') y++;
std::string header = header_keys.substr(0, y);
Armor_Header.push_back(std::pair <std::string, std::string>(header, header_keys.substr(y + 1, x - y - 1)));
bool found = false;
for(unsigned int i = 0; i < 5; i++){
if (header == ASCII_Armor_Key[i]){
found = true;
break;
}
}
if (!found){
std::cerr << "Warning: Unknown ASCII Armor Header Key \"" << header << "\"" << std::endl;
}
x++;
header_keys = header_keys.substr(x, header_keys.size() - x);
}
x = 0;
while ((x < data.size()) && (data.substr(x, 15) != "-----BEGIN PGP ")){
x++;
}
message = data.substr(0, x - 1); // get rid of last newline after text
data = data.substr(x, data.size() - x);
sig.read(data);
}
void PGPCleartextSignature::read(std::ifstream & file){
std::stringstream s;
s << file.rdbuf();
std::string data = s.str();
read(data);
}
std::string PGPCleartextSignature::show(const uint8_t indents, const uint8_t indent_size) const{
unsigned int tab = indents * indent_size;
return std::string(tab, ' ') + "Message:\n" + message + "\n\n" + std::string(tab, ' ') + sig.show(indents, indent_size);
}
std::string PGPCleartextSignature::write(uint8_t header) const{
std::string out = "-----BEGIN PGP SIGNED MESSAGE-----\n";
for(std::pair <std::string, std::string> const & k : Armor_Header){
out += k.first + ": " + k.second + "\n";
}
return out + "\n" + message + "\n" + sig.write(header);
}
std::vector <std::pair <std::string, std::string> > PGPCleartextSignature::get_Armor_Header() const{
return Armor_Header;
}
std::string PGPCleartextSignature::get_message() const{
return message;
}
PGPDetachedSignature PGPCleartextSignature::get_sig() const{
return sig;
}
void PGPCleartextSignature::set_Armor_Header(const std::vector <std::pair <std::string, std::string> > & a){
Armor_Header = a;
}
void PGPCleartextSignature::set_message(const std::string & data){
message = data;
}
void PGPCleartextSignature::set_sig(const PGPDetachedSignature & s){
sig = s;
sig.set_armored(true);
}
PGPCleartextSignature::Ptr PGPCleartextSignature::clone() const{
PGPCleartextSignature::Ptr out(new PGPCleartextSignature);
out -> Armor_Header = Armor_Header;
out -> message = message;
out -> sig = sig;
return out;
}
PGPCleartextSignature & PGPCleartextSignature::operator=(const PGPCleartextSignature & copy){
Armor_Header = copy.Armor_Header;
message = copy.message;
sig = copy.sig;
return *this;
}