Skip to content

Commit d994f64

Browse files
committed
Add new methods for setting public keys on SignatureVerifiers
1 parent f510606 commit d994f64

9 files changed

Lines changed: 190 additions & 27 deletions

include/cryptolens/SignatureVerifier_BearSSL.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class SignatureVerifier_BearSSL
3333
void operator=(SignatureVerifier_BearSSL &&) = delete;
3434
~SignatureVerifier_BearSSL();
3535

36+
void set_public_key_xml(basic_Error & e, std::string const& key_xml);
37+
void set_public_key_base64(basic_Error & e, std::string const& modulus_base64, std::string const& exponent_base64);
38+
3639
void set_modulus_base64(basic_Error & e, std::string const& modulus_base64);
3740
void set_exponent_base64(basic_Error & e, std::string const& exponent_base64);
3841

include/cryptolens/SignatureVerifier_CryptoAPI.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class SignatureVerifier_CryptoAPI
3838
#endif
3939
~SignatureVerifier_CryptoAPI();
4040

41+
void set_public_key_xml(basic_Error& e, std::string const& key_xml);
42+
void set_public_key_base64(basic_Error & e, std::string const& modulus_base64, std::string const& exponent_base64);
43+
4144
void set_modulus_base64(basic_Error & e, std::string const& modulus_base64);
4245
void set_exponent_base64(basic_Error & e, std::string const& exponent_base64) {}
4346

include/cryptolens/SignatureVerifier_OpenSSL.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class SignatureVerifier_OpenSSL
3737
#endif
3838
~SignatureVerifier_OpenSSL();
3939

40+
void set_public_key_xml(basic_Error & e, std::string const& key_xml);
41+
void set_public_key_base64(basic_Error & e, std::string const& modulus_base64, std::string const& exponent_base64);
42+
4043
void set_modulus_base64(basic_Error & e, std::string const& modulus_base64);
4144
void set_exponent_base64(basic_Error & e, std::string const& exponent_base64);
4245

include/cryptolens/SignatureVerifier_OpenSSL3.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SignatureVerifier_OpenSSL3
3333
void operator=(SignatureVerifier_OpenSSL3 &&) = delete;
3434
~SignatureVerifier_OpenSSL3();
3535

36-
void set_public_key_base64(basic_Error & e, std::string const& key);
36+
void set_public_key_xml(basic_Error & e, std::string const& key_xml);
3737
void set_public_key_base64(basic_Error & e, std::string const& modulus_base64, std::string const& exponent_base64);
3838

3939
void set_modulus_base64(basic_Error & e, std::string const& modulus_base64);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
#include "basic_Error.hpp"
6+
7+
namespace cryptolens_io {
8+
9+
namespace v20190401 {
10+
11+
namespace internal {
12+
13+
template<typename SignatureVerifier>
14+
void
15+
set_public_key_xml(basic_Error & e, SignatureVerifier & signature_verifier, std::string const& key_xml)
16+
{
17+
if (e) { return; }
18+
19+
using size_type = std::string::size_type;
20+
21+
size_type m_s = key_xml.find("<Modulus>");
22+
size_type m_e = key_xml.find("</Modulus>");
23+
24+
size_type e_s = key_xml.find("<Exponent>");
25+
size_type e_e = key_xml.find("</Exponent>");
26+
27+
if (m_s == std::string::npos || m_e == std::string::npos || m_e < m_s + 9 ||
28+
e_s == std::string::npos || e_e == std::string::npos || e_e < e_s + 10)
29+
{
30+
e.set(api::main(), errors::Subsystem::Base64, __LINE__); // TODO: Should this really be the Base64 subsystem?
31+
return;
32+
}
33+
34+
size_type m_start = m_s + 9;
35+
size_type m_length = m_e - m_s - 9;
36+
37+
size_type e_start = e_s + 10;
38+
size_type e_length = e_e - e_s - 10;
39+
40+
std::string modulus_base64 = key_xml.substr(m_start, m_length);
41+
std::string exponent_base64 = key_xml.substr(e_start, e_length);
42+
43+
signature_verifier.set_public_key_base64(e, modulus_base64, exponent_base64);
44+
}
45+
46+
} // namespace internal
47+
48+
} // namespace v20190401
49+
50+
} // namespace cryptolens_io
51+

src/SignatureVerifier_BearSSL.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "api.hpp"
77
#include "base64.hpp"
88
#include "SignatureVerifier_BearSSL.hpp"
9+
#include "SignatureVerifier_shared.hpp"
910

1011
namespace {
1112

@@ -126,6 +127,46 @@ SignatureVerifier_BearSSL::set_exponent_base64_(basic_Error & e, std::string con
126127
pk_.elen = len;
127128
}
128129

130+
/*
131+
* TODO Add documentation and fix set_call() at the end
132+
*/
133+
void
134+
SignatureVerifier_BearSSL::set_public_key_base64
135+
( basic_Error & e
136+
, std::string const& modulus_base64
137+
, std::string const& exponent_base64
138+
)
139+
{
140+
if (e) { return; }
141+
142+
this->set_modulus_base64(e, modulus_base64);
143+
this->set_exponent_base64(e, exponent_base64);
144+
145+
if (e) { e.set_call(api::main(), errors::Call::SIGNATURE_VERIFIER_SET_EXPONENT_BASE64); } // TODO Update call
146+
}
147+
148+
/**
149+
* Sets the modulus of the public key used by the cryptolens.io Web API for signing
150+
* the responses.
151+
*
152+
* This value is unique for each account and can be found on cryptolens.io at the
153+
* "Account Settings" found in the personal menu ("Hello, <account name>!" in the upper
154+
* right corner). The public key is listed in XML format as something similar to
155+
*
156+
* <RSAKeyValue><Modulus>AbC=</Modulus><Exponent>deFG</Exponent></RSAKeyValue>
157+
*
158+
* and the full string can be supplied as the argument to this method.
159+
*/
160+
void
161+
SignatureVerifier_BearSSL::set_public_key_xml(basic_Error & e, std::string const& key_xml)
162+
{
163+
if (e) { return; }
164+
165+
::cryptolens_io::v20190401::internal::set_public_key_xml(e, *this, key_xml);
166+
167+
if (e) { e.set_call(api::main(), errors::Call::SIGNATURE_VERIFIER_SET_MODULUS_BASE64); } // TODO Update call
168+
}
169+
129170
/**
130171
* This function is used internally by the library and need not be called.
131172
*/

src/SignatureVerifier_CryptoAPI.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
#include "imports/std/optional"
55

6-
#include "SignatureVerifier_CryptoAPI.hpp"
76
#include "api.hpp"
87
#include "base64.hpp"
8+
#include "SignatureVerifier_CryptoAPI.hpp"
9+
#include "SignatureVerifier_shared.hpp"
910

1011
namespace {
1112

@@ -158,6 +159,46 @@ SignatureVerifier_CryptoAPI::set_modulus_base64_(basic_Error & e, std::string co
158159
}
159160
}
160161

162+
/*
163+
* TODO Add documentation and fix set_call() at the end
164+
*/
165+
void
166+
SignatureVerifier_CryptoAPI::set_public_key_base64
167+
( basic_Error & e
168+
, std::string const& modulus_base64
169+
, std::string const& exponent_base64
170+
)
171+
{
172+
if (e) { return; }
173+
174+
this->set_modulus_base64(e, modulus_base64);
175+
this->set_exponent_base64(e, exponent_base64);
176+
177+
if (e) { e.set_call(api::main(), errors::Call::SIGNATURE_VERIFIER_SET_EXPONENT_BASE64); } // TODO Update call
178+
}
179+
180+
/**
181+
* Sets the modulus of the public key used by the cryptolens.io Web API for signing
182+
* the responses.
183+
*
184+
* This value is unique for each account and can be found on cryptolens.io at the
185+
* "Account Settings" found in the personal menu ("Hello, <account name>!" in the upper
186+
* right corner). The public key is listed in XML format as something similar to
187+
*
188+
* <RSAKeyValue><Modulus>AbC=</Modulus><Exponent>deFG</Exponent></RSAKeyValue>
189+
*
190+
* and the full string can be supplied as the argument to this method.
191+
*/
192+
void
193+
SignatureVerifier_CryptoAPI::set_public_key_xml(basic_Error & e, std::string const& key_xml)
194+
{
195+
if (e) { return; }
196+
197+
::cryptolens_io::v20190401::internal::set_public_key_xml(e, *this, key_xml);
198+
199+
if (e) { e.set_call(api::main(), errors::Call::SIGNATURE_VERIFIER_SET_MODULUS_BASE64); } // TODO Update call
200+
}
201+
161202
/**
162203
* This function is used internally by the library and need not be called.
163204
*/

src/SignatureVerifier_OpenSSL.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "api.hpp"
1010
#include "base64.hpp"
1111
#include "SignatureVerifier_OpenSSL.hpp"
12+
#include "SignatureVerifier_shared.hpp"
1213

1314
namespace {
1415

@@ -232,6 +233,46 @@ SignatureVerifier_OpenSSL::set_exponent_base64_(basic_Error & e, std::string con
232233
#endif
233234
}
234235

236+
/*
237+
* TODO Add documentation and fix set_call() at the end
238+
*/
239+
void
240+
SignatureVerifier_OpenSSL::set_public_key_base64
241+
( basic_Error & e
242+
, std::string const& modulus_base64
243+
, std::string const& exponent_base64
244+
)
245+
{
246+
if (e) { return; }
247+
248+
this->set_modulus_base64(e, modulus_base64);
249+
this->set_exponent_base64(e, exponent_base64);
250+
251+
if (e) { e.set_call(api::main(), errors::Call::SIGNATURE_VERIFIER_SET_EXPONENT_BASE64); } // TODO Update call
252+
}
253+
254+
/**
255+
* Sets the modulus of the public key used by the cryptolens.io Web API for signing
256+
* the responses.
257+
*
258+
* This value is unique for each account and can be found on cryptolens.io at the
259+
* "Account Settings" found in the personal menu ("Hello, <account name>!" in the upper
260+
* right corner). The public key is listed in XML format as something similar to
261+
*
262+
* <RSAKeyValue><Modulus>AbC=</Modulus><Exponent>deFG</Exponent></RSAKeyValue>
263+
*
264+
* and the full string can be supplied as the argument to this method.
265+
*/
266+
void
267+
SignatureVerifier_OpenSSL::set_public_key_xml(basic_Error & e, std::string const& key_xml)
268+
{
269+
if (e) { return; }
270+
271+
::cryptolens_io::v20190401::internal::set_public_key_xml(e, *this, key_xml);
272+
273+
if (e) { e.set_call(api::main(), errors::Call::SIGNATURE_VERIFIER_SET_MODULUS_BASE64); } // TODO Update call
274+
}
275+
235276
/**
236277
* This function is used internally by the library and need not be called.
237278
*/

src/SignatureVerifier_OpenSSL3.cpp

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "api.hpp"
1111
#include "base64.hpp"
1212
#include "SignatureVerifier_OpenSSL3.hpp"
13+
#include "SignatureVerifier_shared.hpp"
1314

1415
namespace {
1516

@@ -148,34 +149,13 @@ SignatureVerifier_OpenSSL3::~SignatureVerifier_OpenSSL3()
148149
* and the full string can be supplied as the argument to this method.
149150
*/
150151
void
151-
SignatureVerifier_OpenSSL3::set_public_key_base64(basic_Error & e, std::string const& key)
152+
SignatureVerifier_OpenSSL3::set_public_key_xml(basic_Error & e, std::string const& key_xml)
152153
{
153154
if (e) { return; }
154155

155-
auto m_s = key.find("<Modulus>");
156-
auto m_e = key.find("</Modulus>");
156+
::cryptolens_io::v20190401::internal::set_public_key_xml(e, *this, key_xml);
157157

158-
auto e_s = key.find("<Exponent>");
159-
auto e_e = key.find("</Exponent>");
160-
161-
if (m_s == std::string::npos || m_e == std::string::npos || m_e < m_s + 9 ||
162-
e_s == std::string::npos || e_e == std::string::npos || e_e < e_s + 10)
163-
{
164-
e.set(api::main(), errors::Subsystem::Base64, __LINE__);
165-
} else {
166-
auto m_start = m_s + 9;
167-
auto m_length = m_e - m_s - 9;
168-
169-
auto e_start = e_s + 10;
170-
auto e_length = e_e - e_s - 10;
171-
172-
std::string modulus_base64 = key.substr(m_start, m_length);
173-
std::string exponent_base64 = key.substr(e_start, e_length);
174-
175-
this->set_public_key_base64_(e, modulus_base64, exponent_base64);
176-
}
177-
178-
if (e) { e.set_call(api::main(), errors::Call::SIGNATURE_VERIFIER_SET_MODULUS_BASE64); }
158+
if (e) { e.set_call(api::main(), errors::Call::SIGNATURE_VERIFIER_SET_MODULUS_BASE64); } // Update call
179159
}
180160

181161
/**
@@ -248,7 +228,7 @@ SignatureVerifier_OpenSSL3::set_modulus_base64(basic_Error & e, std::string cons
248228
void
249229
SignatureVerifier_OpenSSL3::set_exponent_base64(basic_Error & e, std::string const& exponent_base64)
250230
{
251-
// Exponent is set by set_modulus_base64()
231+
// Exponent is always AQAB in our API and we set it in set_modulus_base64()
252232
}
253233

254234
/**

0 commit comments

Comments
 (0)