-
Notifications
You must be signed in to change notification settings - Fork 1
/
sign.rs
377 lines (347 loc) · 14.6 KB
/
sign.rs
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
use crate::{request::{Headers, Method, Request}};
use std::{borrow::Cow, sync::Arc};
use std::io::Write;
use base64::write::EncoderWriter;
use chrono::Utc;
use marching_buffer::*;
use ring::rand::SecureRandom;
use ring::signature::RsaKeyPair;
trait MethodExt {
fn is_body_mandatory(&self) -> bool;
fn required_object_storage_signing_elements(self) -> &'static [SignatureElement<'static>];
fn required_non_object_storage_signing_elements(&self) -> &'static [SignatureElement<'static>];
}
impl MethodExt for Method {
fn is_body_mandatory(&self) -> bool {
match self {
Self::Put | Self::Patch | Self::Post => true,
_ => false
}
}
fn required_object_storage_signing_elements(self) -> &'static [SignatureElement<'static>] {
match self {
Self::Get | Self::Head | Self::Delete | Self::Put | Self::Post | Self::Patch => &BASIC_SIGNATURE_ELEMENTS[..],
_ => &[]
}
}
fn required_non_object_storage_signing_elements(&self) -> &'static [SignatureElement<'static>] {
match self {
Self::Get | Self::Head | Self::Delete => &BASIC_SIGNATURE_ELEMENTS[..],
Self::Put | Self::Post | Self::Patch => &EXTENDED_SIGNATURE_ELEMENTS[..],
_ => &[]
}
}
}
static BASIC_SIGNATURE_ELEMENTS: [SignatureElement<'static>; 2] = [
SignatureElement::Date,
SignatureElement::RequestTarget
];
static EXTENDED_SIGNATURE_ELEMENTS: [SignatureElement<'static>; 5] = [
SignatureElement::Date,
SignatureElement::RequestTarget,
SignatureElement::StandardHeader("content-length"),
SignatureElement::StandardHeader("content-type"),
SignatureElement::XContentSHA256
];
#[derive(Clone, Debug)]
enum SignatureElement<'header> {
Date,
StandardHeader(&'header str),
XContentSHA256,
RequestTarget
}
impl<'header> SignatureElement<'header> {
fn eq_header(&self, header_name: &str) -> bool {
match self {
&SignatureElement::Date => header_name == "date" || header_name == "x-date",
&SignatureElement::StandardHeader(this_name) => this_name == header_name,
&SignatureElement::XContentSHA256 => header_name == "x-content-sha256",
&Self::RequestTarget => false
}
}
}
#[derive(Debug)]
pub enum SignError {
BodyNotExpected,
InternalError(&'static str),
MissingContentType,
MultivaluedHeader(Cow<'static, str>),
MissingAdditionalHeader(Cow<'static, str>),
}
pub struct Signer {
binary_buffer: MarchingBuffer<u8>,
key_id: String,
key: RsaKeyPair,
rng: Arc<dyn SecureRandom>
}
impl Signer {
pub fn new(key_id: String, key: RsaKeyPair, rng: Arc<dyn SecureRandom>) -> Self {
Self {
binary_buffer: MarchingBuffer::new(),
key_id,
key,
rng
}
}
/// https://tools.ietf.org/id/draft-cavage-http-signatures-12.html#canonicalization
pub fn sign<'additional_headers, R: Request>(
&mut self,
request: &mut R,
additional_headers_to_sign: impl IntoIterator<Item = &'additional_headers str>,
ignore_body_for_signing: bool,
allow_body_for_get: bool) -> Result<(), SignError> {
inject_missing_headers(&self.binary_buffer, request, ignore_body_for_signing, allow_body_for_get)?;
let mut signature_element_buffer = Vec::with_capacity(128);
let signature_elements = get_signature_elements(
&mut signature_element_buffer,
request,
additional_headers_to_sign.into_iter(),
ignore_body_for_signing,
allow_body_for_get)?;
let signature_input = calculate_signature_input(
&self.binary_buffer,
&signature_elements,
request)?;
let base64_signature = sign_and_base64_encode(&self.binary_buffer, signature_input, self.rng.as_ref(), &self.key)?;
let authorization_header = build_authorization_header(&self.binary_buffer, base64_signature, request, signature_elements, &self.key_id)?;
request.headers_mut().insert_header("authorization", authorization_header.access().as_slice());
Ok(())
}
}
fn inject_missing_headers<R: Request>(
binary_buffer: &MarchingBuffer<u8>,
request: &mut R,
ignore_body_for_signing: bool,
allow_body_for_get: bool
) -> Result<(), SignError> {
{
let mut buffer = binary_buffer.get_writer();
if !request.headers().contains_header("date") && !request.headers().contains_header("x-date") {
write!(buffer.access(), "{}", Utc::now().format("%a, %d %b %Y %H:%M:%S %Z"))
.map_err(|_err| SignError::InternalError("Failed to format Date header value"))?;
request.headers_mut().insert_header("date", buffer.access().as_slice());
} else {
if request.headers().header_values("date").count() > 1 {
return Err(SignError::MultivaluedHeader(Cow::Borrowed("date")));
}
if request.headers().header_values("x-date").count() > 1 {
return Err(SignError::MultivaluedHeader(Cow::Borrowed("x-date")));
}
}
}
let has_body = request.body().len() > 0;
if request.method().is_body_mandatory() {
if !ignore_body_for_signing || has_body {
set_body_headers(binary_buffer, request)?;
}
} else {
match (has_body, allow_body_for_get) {
(true, true) => set_body_headers(binary_buffer, request)?,
(true, false) => return Err(SignError::BodyNotExpected),
(_, _) => {}
}
}
Ok(())
}
fn set_body_headers<R: Request>(
binary_buffer: &MarchingBuffer<u8>,
request: &mut R
) -> Result<(), SignError> {
{
let mut buffer = binary_buffer.get_writer();
if !request.headers().contains_header("content-length") {
write!(buffer.access(), "{}", request.body().len())
.map_err(|_err| SignError::InternalError("Failed to format Content-Length header value"))?;
request.headers_mut().insert_header("content-length", buffer.access().as_slice());
} else if request.headers().header_values("content-length").count() > 1 {
return Err(SignError::MultivaluedHeader(Cow::Borrowed("content-length")));
}
}
{
if !request.headers().contains_header("x-content-sha256") {
let encoded_hash = calculate_sha256_base64(request.body(), binary_buffer)?;
request.headers_mut().insert_header("x-content-sha256", encoded_hash.access().as_slice());
} else if request.headers().header_values("x-content-sha256").count() > 1 {
return Err(SignError::MultivaluedHeader(Cow::Borrowed("x-content-sha256")));
}
}
if !request.headers().contains_header("content-type") {
return Err(SignError::MissingContentType);
} else if request.headers().header_values("content-type").count() > 1 {
return Err(SignError::MultivaluedHeader(Cow::Borrowed("content-type")));
}
Ok(())
}
fn calculate_sha256_base64(
data_to_hash: &[u8],
binary_buffer: &MarchingBuffer<u8>
) -> Result<Reader<u8>, SignError> {
let mut buffer = binary_buffer.get_writer();
let digest = ring::digest::digest(&ring::digest::SHA256, data_to_hash);
{
let mut encoder = EncoderWriter::new(buffer.access(), base64::STANDARD);
encoder.write_all(digest.as_ref())
.map_err(|_err| SignError::InternalError("Failed to base64-encode SHA256 hash"))?;
}
Ok(buffer.finish())
}
fn get_signature_elements<'additional_headers, 'buffer, R: Request>(
buffer: &'buffer mut Vec<SignatureElement<'additional_headers>>,
request: &mut R,
additional_headers_to_sign: impl Iterator<Item = &'additional_headers str>,
ignore_body_for_signing: bool,
allow_body_for_get: bool) -> Result<&'buffer [SignatureElement<'additional_headers>], SignError> {
if request.method().is_body_mandatory() {
if ignore_body_for_signing {
buffer.extend_from_slice(request.method().required_object_storage_signing_elements());
} else {
buffer.extend_from_slice(request.method().required_non_object_storage_signing_elements());
}
} else {
buffer.extend_from_slice(&BASIC_SIGNATURE_ELEMENTS[..]);
if allow_body_for_get {
buffer.extend_from_slice(&[
SignatureElement::StandardHeader("content-length"),
SignatureElement::StandardHeader("content-type"),
SignatureElement::XContentSHA256
]);
}
}
for name in additional_headers_to_sign {
// Note that eq_header treats Date and X-Date as identical.
let already_added = buffer.iter().any(|elem| elem.eq_header(name));
if already_added {
continue;
}
let value_count = request.headers().header_values(name).count();
if value_count > 1 {
return Err(SignError::MultivaluedHeader(Cow::Owned(name.to_string())));
}
if value_count == 0 {
return Err(SignError::MissingAdditionalHeader(Cow::Owned(name.to_string())));
}
buffer.push(SignatureElement::StandardHeader(name));
}
Ok(buffer.as_slice())
}
fn append_concatenated_headers<H: Headers>(buffer: &mut Writer<u8>, header_name: &str, headers: &H) {
buffer.access().extend_from_slice(header_name.as_bytes());
buffer.access().extend_from_slice(b": ");
if headers.header_values(header_name).any(|_| true) {
for value in headers.header_values(header_name) {
// If header value is a valid UTF-8 string, then trim it, otherwise use the raw bytes
if let Ok(value_str) = std::str::from_utf8(value) {
buffer.access().extend_from_slice(value_str.trim().as_bytes());
} else {
buffer.access().extend_from_slice(value);
}
buffer.access().extend_from_slice(b", ");
}
// remove last ", ". We know there is at least one.
buffer.access().pop();
buffer.access().pop();
buffer.access().push(b'\n');
}
}
fn calculate_signature_input<'buffer, R: Request>(
buffer: &MarchingBuffer<u8>,
signature_elements: &[SignatureElement],
request: &mut R,
) -> Result<Reader<u8>, SignError> {
let mut writer = buffer.get_writer();
for element in signature_elements {
match element {
SignatureElement::Date => {
if request.headers().contains_header("x-date") {
append_concatenated_headers(&mut writer, "x-date", request.headers());
} else if request.headers().contains_header("date") {
append_concatenated_headers(&mut writer, "date", request.headers());
} else {
return Err(SignError::InternalError("Neither Date nor X-Date header found, should have been added by inject_missing_headers"));
}
writer.access().push(b'\n');
}
SignatureElement::StandardHeader(name) => {
append_concatenated_headers(&mut writer, name, request.headers());
writer.access().push(b'\n');
}
SignatureElement::XContentSHA256 => {
append_concatenated_headers(&mut writer, "x-content-sha256", request.headers());
writer.access().push(b'\n');
}
SignatureElement::RequestTarget => {
// TODO: url-encode the path and query string?
writer.access().extend_from_slice(b"(request-target): ");
writer.access().extend_from_slice(request.method().lowercase());
writer.access().push(b' ');
writer.access().extend_from_slice(request.path().as_bytes());
if let Some(query) = request.query_string() {
writer.access().push(b'?');
writer.access().extend_from_slice(query.as_bytes());
}
writer.access().push(b'\n');
}
}
}
Ok(writer.finish())
}
fn build_authorization_header<R: Request>(
buffer: &MarchingBuffer<u8>,
base64_signature: Reader<u8>,
request: &R,
signature_elements: &[SignatureElement],
key_id: &str
) -> Result<Reader<u8>, SignError> {
let mut writer = buffer.get_writer();
writer.access().extend_from_slice(b"Signature headers=\"");
for element in signature_elements {
match element {
SignatureElement::Date => {
if request.headers().contains_header("x-date") {
writer.access().extend_from_slice(b"x-date ");
} else {
writer.access().extend_from_slice(b"date ");
}
}
SignatureElement::StandardHeader(header_name) => {
writer.access().extend_from_slice(header_name.as_bytes());
writer.access().push(b' ');
}
SignatureElement::XContentSHA256 => {
writer.access().extend_from_slice(b"x-content-sha256 ");
}
SignatureElement::RequestTarget => {
writer.access().extend_from_slice(b"(request-target) ");
}
}
}
if !signature_elements.is_empty() {
if writer.access().pop() != Some(b' ') {
return Err(SignError::InternalError("Popped byte should have been a space"));
}
}
writer.access().extend_from_slice(b"\",keyId=\"");
writer.access().extend_from_slice(key_id.as_bytes());
writer.access().extend_from_slice(b"\",algorithm=\"rsa-sha256\",signature=\"");
writer.copy_from::<4096>(&base64_signature);
writer.access().extend_from_slice(b"\",version=\"1\"");
Ok(writer.finish())
}
fn sign_and_base64_encode(
binary_buffer: &MarchingBuffer<u8>,
signature_input: Reader<u8>,
rng: &dyn ring::rand::SecureRandom,
key: &ring::signature::RsaKeyPair) -> Result<Reader<u8>, SignError> {
// 1024 bytes is enough for RSA-8192.
let mut signature = [0u8; 1024];
let signature = &mut signature[..key.public_modulus_len()];
key.sign(&ring::signature::RSA_PKCS1_SHA256, rng, signature_input.access().as_slice(), signature)
.map_err(|_| SignError::InternalError("Failed to compute RSA_PKCS1_SHA256 signature"))?;
let mut writer = binary_buffer.get_writer();
{
let mut encoder = EncoderWriter::new(writer.access(), base64::STANDARD);
encoder.write_all(signature)
.map_err(|_err| SignError::InternalError("Failed to base64-encode the signature"))?;
}
Ok(writer.finish())
}