Skip to content

Commit f510606

Browse files
committed
Use std::vector for internal representation
1 parent 17f3a63 commit f510606

12 files changed

Lines changed: 57 additions & 49 deletions

include/cryptolens/RawLicenseKey.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,21 @@ class RawLicenseKey {
5454
{
5555
if (e) { return nullopt; }
5656

57-
optional<std::string> decoded = ::cryptolens_io::v20190401::internal::b64_decode(base64_license);
57+
optional<std::vector<unsigned char>> decoded = ::cryptolens_io::v20190401::internal::b64_decode(base64_license);
5858

5959
if (!decoded) {
6060
e.set(api::main(), errors::Subsystem::Base64);
6161
return nullopt;
6262
}
6363

6464
if (verifier.verify_message(e, *decoded, signature)) {
65+
std::string decoded_string(decoded->begin(), decoded->end());
66+
6567
return make_optional(
6668
RawLicenseKey
6769
( std::move(base64_license)
6870
, std::move(signature)
69-
, std::move(*decoded)
71+
, std::move(decoded_string)
7072
)
7173
);
7274
} else {

include/cryptolens/SignatureVerifier_BearSSL.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class SignatureVerifier_BearSSL
3636
void set_modulus_base64(basic_Error & e, std::string const& modulus_base64);
3737
void set_exponent_base64(basic_Error & e, std::string const& exponent_base64);
3838

39-
bool verify_message(basic_Error & e, std::string const& message, std::string const& signature_base64) const;
39+
bool verify_message(basic_Error & e, std::vector<unsigned char> const& message, std::string const& signature_base64) const;
4040

4141
private:
4242
br_rsa_public_key pk_;

include/cryptolens/SignatureVerifier_CryptoAPI.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class SignatureVerifier_CryptoAPI
4040

4141
void set_modulus_base64(basic_Error & e, std::string const& modulus_base64);
4242
void set_exponent_base64(basic_Error & e, std::string const& exponent_base64) {}
43-
bool verify_message(basic_Error & e, std::string const& message, std::string const& signature_base64) const;
43+
44+
bool verify_message(basic_Error & e, std::vector<unsigned char> const& message, std::string const& signature_base64) const;
4445
private:
4546
HCRYPTPROV hProv_;
4647
HCRYPTKEY hPubKey_;

include/cryptolens/SignatureVerifier_OpenSSL.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class SignatureVerifier_OpenSSL
4040
void set_modulus_base64(basic_Error & e, std::string const& modulus_base64);
4141
void set_exponent_base64(basic_Error & e, std::string const& exponent_base64);
4242

43-
bool verify_message(basic_Error & e, std::string const& message, std::string const& signature_base64) const;
43+
bool verify_message(basic_Error & e, std::vector<unsigned char> const& message, std::string const& signature_base64) const;
4444

4545
private:
4646
RSA * rsa;

include/cryptolens/SignatureVerifier_OpenSSL3.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class SignatureVerifier_OpenSSL3
3939
void set_modulus_base64(basic_Error & e, std::string const& modulus_base64);
4040
void set_exponent_base64(basic_Error & e, std::string const& exponent_base64);
4141

42-
bool verify_message(basic_Error & e, std::string const& message, std::string const& signature_base64) const;
42+
bool verify_message(basic_Error & e, std::vector<unsigned char> const& message, std::string const& signature_base64) const;
4343

4444
private:
4545
EVP_PKEY *pkey_;

include/cryptolens/SignatureVerifier_v20190401_to_v20180502.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ const
116116
return false;
117117
}
118118

119-
return verifier_.verify_message(e, message, signature_base64);
119+
std::vector<unsigned char> message_vec(message.cbegin(), message.cend());
120+
121+
return verifier_.verify_message(e, message_vec, signature_base64);
120122
}
121123

122124
} // namespace internal

include/cryptolens/base64.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <string>
4+
#include <vector>
45

56
#include "imports/std/optional"
67

@@ -16,7 +17,7 @@ namespace internal {
1617
int
1718
b64_pton(char const *src, unsigned char *target, size_t targsize);
1819

19-
optional<std::string>
20+
optional<std::vector<unsigned char>>
2021
b64_decode(std::string const& b64);
2122

2223
} // namespace internal

src/SignatureVerifier_BearSSL.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ SignatureVerifier_BearSSL::set_modulus_base64_(basic_Error & e, std::string cons
8585
{
8686
if (e) { return; }
8787

88-
optional<std::string> modulus = ::cryptolens_io::v20190401::internal::b64_decode(modulus_base64);
88+
optional<std::vector<unsigned char>> modulus = ::cryptolens_io::v20190401::internal::b64_decode(modulus_base64);
8989
if (!modulus) { e.set(api::main(), errors::Subsystem::Base64); return; }
9090

9191
if (pk_.n) { delete [] pk_.n; pk_.n = NULL; }
@@ -99,7 +99,7 @@ SignatureVerifier_BearSSL::set_modulus_base64_(basic_Error & e, std::string cons
9999
return;
100100
}
101101

102-
memcpy(pk_.n, modulus->c_str(), len);
102+
memcpy(pk_.n, modulus->data(), len);
103103
pk_.nlen = len;
104104
}
105105

@@ -108,7 +108,7 @@ SignatureVerifier_BearSSL::set_exponent_base64_(basic_Error & e, std::string con
108108
{
109109
if (e) { return; }
110110

111-
optional<std::string> exponent = ::cryptolens_io::v20190401::internal::b64_decode(exponent_base64);
111+
optional<std::vector<unsigned char>> exponent = ::cryptolens_io::v20190401::internal::b64_decode(exponent_base64);
112112
if (!exponent) { e.set(api::main(), errors::Subsystem::Base64); return; }
113113

114114
if (pk_.e) { delete [] pk_.e; pk_.e = NULL; }
@@ -122,7 +122,7 @@ SignatureVerifier_BearSSL::set_exponent_base64_(basic_Error & e, std::string con
122122
return;
123123
}
124124

125-
memcpy(pk_.e, exponent->c_str(), len);
125+
memcpy(pk_.e, exponent->data(), len);
126126
pk_.elen = len;
127127
}
128128

@@ -132,7 +132,7 @@ SignatureVerifier_BearSSL::set_exponent_base64_(basic_Error & e, std::string con
132132
bool
133133
SignatureVerifier_BearSSL::verify_message
134134
( basic_Error & e
135-
, std::string const& message
135+
, std::vector<unsigned char> const& message
136136
, std::string const& signature_base64
137137
)
138138
const
@@ -144,14 +144,14 @@ const
144144

145145
if (pk_.n == NULL || pk_.e == NULL) { e.set(api::main(), 7827, 0, 0); return false; }
146146

147-
optional<std::string> sig = ::cryptolens_io::v20190401::internal::b64_decode(signature_base64);
147+
optional<std::vector<unsigned char>> sig = ::cryptolens_io::v20190401::internal::b64_decode(signature_base64);
148148
if (!sig) { e.set(api::main(), errors::Subsystem::Base64); return false; }
149149

150150
br_sha256_init(&hash_context);
151-
br_sha256_update(&hash_context, message.c_str(), message.size());
151+
br_sha256_update(&hash_context, message.data(), message.size());
152152
br_sha256_out(&hash_context, hash_out);
153153

154-
int r = br_rsa_i62_pkcs1_vrfy((unsigned char *)sig->c_str(), sig->size(), BR_HASH_OID_SHA256, br_sha256_SIZE, &pk_, hash_out);
154+
int r = br_rsa_i62_pkcs1_vrfy(sig->data(), sig->size(), BR_HASH_OID_SHA256, br_sha256_SIZE, &pk_, hash_out);
155155
if (!r) {
156156
api::main api;
157157
e.set(api, 1234, 2345);

src/SignatureVerifier_CryptoAPI.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace cryptolens_io {
2727
namespace v20190401 {
2828

2929
void
30-
verify(basic_Error & e, HCRYPTPROV hProv, HCRYPTKEY hPubKey, std::string const& message, std::string sig)
30+
verify(basic_Error & e, HCRYPTPROV hProv, HCRYPTKEY hPubKey, std::vector<unsigned char> const& message, std::vector<unsigned char> & sig)
3131
{
3232
using namespace errors;
3333
api::main api;
@@ -47,13 +47,13 @@ verify(basic_Error & e, HCRYPTPROV hProv, HCRYPTKEY hPubKey, std::string const&
4747
goto cleanup;
4848
}
4949

50-
if (!CryptHashData(hHash, (const BYTE*)message.c_str(), (DWORD)message.size(), 0)) {
50+
if (!CryptHashData(hHash, (const BYTE*)message.data(), (DWORD)message.size(), 0)) {
5151
DWORD code = GetLastError();
5252
e.set(api, Subsystem::SignatureVerifier, CRYPT_HASH_DATA_FAILED, code);
5353
goto cleanup;
5454
}
5555

56-
if (!CryptVerifySignature(hHash, (const BYTE*)sig.c_str(), (DWORD)sig.size(), hPubKey, NULL, 0)) {
56+
if (!CryptVerifySignature(hHash, (const BYTE*)sig.data(), (DWORD)sig.size(), hPubKey, NULL, 0)) {
5757
DWORD code = GetLastError();
5858
e.set(api, Subsystem::SignatureVerifier, CRYPT_VERIFY_SIGNATURE_FAILED, code);
5959
goto cleanup;
@@ -123,7 +123,7 @@ SignatureVerifier_CryptoAPI::set_modulus_base64_(basic_Error & e, std::string co
123123
if (e) { return; }
124124
}
125125

126-
optional<std::string> modulus = internal::b64_decode(modulus_base64);
126+
optional<std::vector<unsigned char>> modulus = internal::b64_decode(modulus_base64);
127127
if (!modulus) { e.set(api::main(), errors::Subsystem::Base64); return; }
128128

129129
const size_t blobLen = sizeof(BLOBHEADER) + sizeof(RSAPUBKEY) + modulus->size();
@@ -147,7 +147,7 @@ SignatureVerifier_CryptoAPI::set_modulus_base64_(basic_Error & e, std::string co
147147
rsapubkey->pubexp = 65537;
148148

149149
memcpy( pbKeyBlob.get() + sizeof(BLOBHEADER) + sizeof(RSAPUBKEY)
150-
, (const char *)modulus->c_str()
150+
, (const char *)modulus->data()
151151
, modulus->size()
152152
);
153153

@@ -164,15 +164,15 @@ SignatureVerifier_CryptoAPI::set_modulus_base64_(basic_Error & e, std::string co
164164
bool
165165
SignatureVerifier_CryptoAPI::verify_message
166166
(basic_Error & e
167-
, std::string const& message
167+
, std::vector<unsigned char> const& message
168168
, std::string const& signature_base64
169169
)
170170
const
171171
{
172172
if (e) { return false; }
173173
if (!hProv_ || !hPubKey_) { e.set(api::main(), errors::Subsystem::SignatureVerifier, SIGNATURE_VERIFIER_UNINITIALIZED); return false; }
174174

175-
optional<std::string> sig = internal::b64_decode(signature_base64);
175+
optional<std::vector<unsigned char>> sig = internal::b64_decode(signature_base64);
176176
if (!sig) { e.set(api::main(), errors::Subsystem::Base64); return false; }
177177

178178
verify(e, hProv_, hPubKey_, message, *sig);

src/SignatureVerifier_OpenSSL.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace cryptolens_io {
3030
namespace v20190401 {
3131

3232
void
33-
verify(basic_Error & e, RSA * rsa, std::string const& message, std::string const& sig)
33+
verify(basic_Error & e, RSA * rsa, std::vector<unsigned char> const& message, std::vector<unsigned char> const& sig)
3434
{
3535
using namespace errors;
3636
api::main api;
@@ -61,10 +61,10 @@ verify(basic_Error & e, RSA * rsa, std::string const& message, std::string const
6161
r = EVP_DigestVerifyInit(ctx, NULL, EVP_sha256(), NULL, pkey);
6262
if (r != 1) { e.set(api, Subsystem::SignatureVerifier, DIGEST_VERIFY_INIT_FAILED); goto end; }
6363

64-
r = EVP_DigestVerifyUpdate(ctx, (unsigned char*)message.c_str(), message.size());
64+
r = EVP_DigestVerifyUpdate(ctx, (unsigned char*)message.data(), message.size());
6565
if (r != 1) { e.set(api, Subsystem::SignatureVerifier, DIGEST_VERIFY_UPDATE_FAILED); goto end; }
6666

67-
r = EVP_DigestVerifyFinal(ctx, (unsigned char*)sig.c_str(), sig.size());
67+
r = EVP_DigestVerifyFinal(ctx, (unsigned char*)sig.data(), sig.size());
6868
if (r != 1) { e.set(api, Subsystem::SignatureVerifier, DIGEST_VERIFY_FINAL_FAILED); goto end; }
6969

7070
end:
@@ -164,19 +164,19 @@ SignatureVerifier_OpenSSL::set_modulus_base64_(basic_Error & e, std::string cons
164164
if (e) { return; }
165165
if (this->rsa == NULL) { e.set(api::main(), errors::Subsystem::SignatureVerifier, RSA_NULL); return; }
166166

167-
optional<std::string> modulus = ::cryptolens_io::v20190401::internal::b64_decode(modulus_base64);
167+
optional<std::vector<unsigned char>> modulus = ::cryptolens_io::v20190401::internal::b64_decode(modulus_base64);
168168
if (!modulus) { e.set(api::main(), errors::Subsystem::Base64); return; }
169169

170170
#if OPENSSL_VERSION_NUMBER < 0x10100000L
171-
BIGNUM * n = BN_bin2bn((unsigned char*)modulus->c_str(), modulus->size(), this->rsa->n);
171+
BIGNUM * n = BN_bin2bn((unsigned char*)modulus->data(), modulus->size(), this->rsa->n);
172172
if (n == NULL) { e.set(api::main(), errors::Subsystem::SignatureVerifier, BN_BIN2BN_FAILED); return; }
173173
#else
174174
BIGNUM const* exp_current;
175175

176176
// void return type
177177
RSA_get0_key(this->rsa, NULL, &exp_current, NULL);
178178

179-
BIGNUM * n = BN_bin2bn((unsigned char*)modulus->c_str(), modulus->size(), NULL);
179+
BIGNUM * n = BN_bin2bn((unsigned char*)modulus->data(), modulus->size(), NULL);
180180
if (n == NULL) { e.set(api::main(), errors::Subsystem::SignatureVerifier, BN_BIN2BN_FAILED); return; }
181181

182182
BIGNUM * exp;
@@ -201,19 +201,19 @@ SignatureVerifier_OpenSSL::set_exponent_base64_(basic_Error & e, std::string con
201201
if (e) { return; }
202202
if (this->rsa == NULL) { e.set(api::main(), errors::Subsystem::SignatureVerifier, RSA_NULL); return; }
203203

204-
optional<std::string> exponent = ::cryptolens_io::v20190401::internal::b64_decode(exponent_base64);
204+
optional<std::vector<unsigned char>> exponent = ::cryptolens_io::v20190401::internal::b64_decode(exponent_base64);
205205
if (!exponent) { e.set(api::main(), errors::Subsystem::Base64); return; }
206206

207207
#if OPENSSL_VERSION_NUMBER < 0x10100000L
208-
BIGNUM * exp = BN_bin2bn((unsigned char*)exponent->c_str(), exponent->size(), this->rsa->e);
208+
BIGNUM * exp = BN_bin2bn(exponent->data(), exponent->size(), this->rsa->e);
209209
if (exp == NULL) { e.set(api::main(), errors::Subsystem::SignatureVerifier, BN_BIN2BN_FAILED); return; }
210210
#else
211211
BIGNUM const* n_current;
212212

213213
// void return type
214214
RSA_get0_key(this->rsa, &n_current, NULL, NULL);
215215

216-
BIGNUM * exp = BN_bin2bn((unsigned char*)exponent->c_str(), exponent->size(), NULL);
216+
BIGNUM * exp = BN_bin2bn((unsigned char*)exponent->data(), exponent->size(), NULL);
217217
if (exp == NULL) { e.set(api::main(), errors::Subsystem::SignatureVerifier, BN_NEW_FAILED); return; }
218218

219219
BIGNUM * n;
@@ -238,15 +238,15 @@ SignatureVerifier_OpenSSL::set_exponent_base64_(basic_Error & e, std::string con
238238
bool
239239
SignatureVerifier_OpenSSL::verify_message
240240
( basic_Error & e
241-
, std::string const& message
241+
, std::vector<unsigned char> const& message
242242
, std::string const& signature_base64
243243
)
244244
const
245245
{
246246
if (e) { return false; }
247247
if (this->rsa == NULL) { e.set(api::main(), errors::Subsystem::SignatureVerifier, RSA_NULL); return false; }
248248

249-
optional<std::string> sig = ::cryptolens_io::v20190401::internal::b64_decode(signature_base64);
249+
optional<std::vector<unsigned char>> sig = ::cryptolens_io::v20190401::internal::b64_decode(signature_base64);
250250
if (!sig) { e.set(api::main(), errors::Subsystem::Base64); return false; }
251251

252252
verify(e, this->rsa, message, *sig);

0 commit comments

Comments
 (0)