-
Notifications
You must be signed in to change notification settings - Fork 127
/
cryptography_client.cpp
242 lines (214 loc) · 8.55 KB
/
cryptography_client.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#include <azure/core/cryptography/hash.hpp>
#include <azure/core/exception.hpp>
#include <azure/core/http/http.hpp>
#include <azure/core/http/policies/policy.hpp>
#include <azure/keyvault/shared/keyvault_shared.hpp>
#include "azure/keyvault/keys/cryptography/cryptography_client.hpp"
#include "azure/keyvault/keys/key_client_models.hpp"
#include "../private/cryptography_serializers.hpp"
#include "../private/key_constants.hpp"
#include "../private/key_serializers.hpp"
#include "../private/key_sign_parameters.hpp"
#include "../private/key_verify_parameters.hpp"
#include "../private/key_wrap_parameters.hpp"
#include "../private/keyvault_protocol.hpp"
#include "../private/package_version.hpp"
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
using namespace Azure::Security::KeyVault::Keys::Cryptography;
using namespace Azure::Security::KeyVault::Keys::_detail;
using namespace Azure::Security::KeyVault::Keys::Cryptography::_detail;
using namespace Azure::Core::Http;
using namespace Azure::Core::Http::Policies;
using namespace Azure::Core::Http::Policies::_internal;
using namespace Azure::Core::Http::_internal;
namespace {
// 1Mb at a time
const size_t DefaultStreamDigestReadSize = 1024 * 1024;
inline std::vector<uint8_t> CreateDigest(
SignatureAlgorithm algorithm,
Azure::Core::IO::BodyStream& data)
{
// Use heap for the reading buffer.
auto heapBuffer = std::make_unique<std::vector<uint8_t>>(DefaultStreamDigestReadSize);
auto* buffer = heapBuffer.get()->data();
auto hashAlgorithm = algorithm.GetHashAlgorithm();
for (size_t read = data.Read(buffer, DefaultStreamDigestReadSize); read > 0;
read = data.Read(buffer, DefaultStreamDigestReadSize))
{
hashAlgorithm->Append(buffer, read);
}
return hashAlgorithm->Final();
}
inline std::vector<uint8_t> CreateDigest(
SignatureAlgorithm algorithm,
std::vector<uint8_t> const& data)
{
auto hashAlgorithm = algorithm.GetHashAlgorithm();
return hashAlgorithm->Final(data.data(), data.size());
}
} // namespace
Request CryptographyClient::CreateRequest(
HttpMethod method,
std::vector<std::string> const& path,
Azure::Core::IO::BodyStream* content) const
{
return Azure::Security::KeyVault::_detail::KeyVaultKeysCommonRequest::CreateRequest(
m_keyId, m_apiVersion, method, path, content);
}
std::unique_ptr<Azure::Core::Http::RawResponse> CryptographyClient::SendCryptoRequest(
std::vector<std::string> const& path,
std::string const& payload,
Azure::Core::Context const& context) const
{
// Payload for the request
Azure::Core::IO::MemoryBodyStream payloadStream(
reinterpret_cast<const uint8_t*>(payload.data()), payload.size());
// Request and settings
auto request = CreateRequest(HttpMethod::Post, path, &payloadStream);
request.SetHeader(HttpShared::ContentType, HttpShared::ApplicationJson);
request.SetHeader(HttpShared::Accept, HttpShared::ApplicationJson);
// Send, parse and validate respone
return Azure::Security::KeyVault::_detail::KeyVaultKeysCommonRequest::SendRequest(
*m_pipeline, request, context);
}
CryptographyClient::~CryptographyClient() = default;
CryptographyClient::CryptographyClient(
std::string const& keyId,
std::shared_ptr<Core::Credentials::TokenCredential const> credential,
CryptographyClientOptions const& options)
: m_keyId(Azure::Core::Url(keyId)), m_apiVersion(options.Version)
{
std::vector<std::unique_ptr<HttpPolicy>> perRetrypolicies;
{
Azure::Core::Credentials::TokenRequestContext tokenContext;
tokenContext.Scopes = {_internal::UrlScope::GetScopeFromUrl(m_keyId)};
perRetrypolicies.emplace_back(
std::make_unique<BearerTokenAuthenticationPolicy>(credential, tokenContext));
}
std::vector<std::unique_ptr<HttpPolicy>> perCallpolicies;
m_pipeline = std::make_shared<Azure::Core::Http::_internal::HttpPipeline>(
options,
"KeyVault",
PackageVersion::ToString(),
std::move(perRetrypolicies),
std::move(perCallpolicies));
}
Azure::Response<EncryptResult> CryptographyClient::Encrypt(
EncryptParameters const& parameters,
Azure::Core::Context const& context)
{
// Send and parse respone
auto rawResponse = SendCryptoRequest(
{EncryptValue}, EncryptParametersSerializer::EncryptParametersSerialize(parameters), context);
auto value = EncryptResultSerializer::EncryptResultDeserialize(*rawResponse);
value.Algorithm = parameters.Algorithm;
return Azure::Response<EncryptResult>(std::move(value), std::move(rawResponse));
}
Azure::Response<DecryptResult> CryptographyClient::Decrypt(
DecryptParameters const& parameters,
Azure::Core::Context const& context)
{
// Send and parse respone
auto rawResponse = SendCryptoRequest(
{DecryptValue}, DecryptParametersSerializer::DecryptParametersSerialize(parameters), context);
auto value = DecryptResultSerializer::DecryptResultDeserialize(*rawResponse);
value.Algorithm = parameters.Algorithm;
return Azure::Response<DecryptResult>(std::move(value), std::move(rawResponse));
}
Azure::Response<WrapResult> CryptographyClient::WrapKey(
KeyWrapAlgorithm algorithm,
std::vector<uint8_t> const& key,
Azure::Core::Context const& context)
{
// Send and parse respone
auto rawResponse = SendCryptoRequest(
{WrapKeyValue},
KeyWrapParametersSerializer::KeyWrapParametersSerialize(
KeyWrapParameters(algorithm.ToString(), key)),
context);
auto value = WrapResultSerializer::WrapResultDeserialize(*rawResponse);
value.Algorithm = algorithm;
return Azure::Response<WrapResult>(std::move(value), std::move(rawResponse));
}
Azure::Response<UnwrapResult> CryptographyClient::UnwrapKey(
KeyWrapAlgorithm algorithm,
std::vector<uint8_t> const& encryptedKey,
Azure::Core::Context const& context)
{
// Send and parse respone
auto rawResponse = SendCryptoRequest(
{UnwrapKeyValue},
KeyWrapParametersSerializer::KeyWrapParametersSerialize(
KeyWrapParameters(algorithm.ToString(), encryptedKey)),
context);
auto value = UnwrapResultSerializer::UnwrapResultDeserialize(*rawResponse);
value.Algorithm = algorithm;
return Azure::Response<UnwrapResult>(std::move(value), std::move(rawResponse));
}
Azure::Response<SignResult> CryptographyClient::Sign(
SignatureAlgorithm algorithm,
std::vector<uint8_t> const& digest,
Azure::Core::Context const& context)
{
// Send and parse respone
auto rawResponse = SendCryptoRequest(
{SignValue},
KeySignParametersSerializer::KeySignParametersSerialize(
KeySignParameters(algorithm.ToString(), digest)),
context);
auto value = SignResultSerializer::SignResultDeserialize(*rawResponse);
value.Algorithm = algorithm;
return Azure::Response<SignResult>(std::move(value), std::move(rawResponse));
}
Azure::Response<SignResult> CryptographyClient::SignData(
SignatureAlgorithm algorithm,
Azure::Core::IO::BodyStream& data,
Azure::Core::Context const& context)
{
return Sign(algorithm, CreateDigest(algorithm, data), context);
}
Azure::Response<SignResult> CryptographyClient::SignData(
SignatureAlgorithm algorithm,
std::vector<uint8_t> const& data,
Azure::Core::Context const& context)
{
return Sign(algorithm, CreateDigest(algorithm, data), context);
}
Azure::Response<VerifyResult> CryptographyClient::Verify(
SignatureAlgorithm algorithm,
std::vector<uint8_t> const& digest,
std::vector<uint8_t> const& signature,
Azure::Core::Context const& context)
{
// Send and parse respone
auto rawResponse = SendCryptoRequest(
{VerifyValue},
KeyVerifyParametersSerializer::KeyVerifyParametersSerialize(
KeyVerifyParameters(algorithm.ToString(), digest, signature)),
context);
auto value = VerifyResultSerializer::VerifyResultDeserialize(*rawResponse);
value.Algorithm = algorithm;
value.KeyId = this->m_keyId.GetAbsoluteUrl();
return Azure::Response<VerifyResult>(std::move(value), std::move(rawResponse));
}
Azure::Response<VerifyResult> CryptographyClient::VerifyData(
SignatureAlgorithm algorithm,
Azure::Core::IO::BodyStream& data,
std::vector<uint8_t> const& signature,
Azure::Core::Context const& context)
{
return Verify(algorithm, CreateDigest(algorithm, data), signature, context);
}
Azure::Response<VerifyResult> CryptographyClient::VerifyData(
SignatureAlgorithm algorithm,
std::vector<uint8_t> const& data,
std::vector<uint8_t> const& signature,
Azure::Core::Context const& context)
{
return Verify(algorithm, CreateDigest(algorithm, data), signature, context);
}