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
|
package ai.vespa.hosted.api;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.http.HttpRequest;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.Key;
import java.time.Clock;
import java.util.Base64;
import java.util.function.Supplier;
import static ai.vespa.hosted.api.Signatures.encrypted;
import static ai.vespa.hosted.api.Signatures.sha256Digest;
/**
* Signs HTTP request headers using a private key, for verification by the indicated public key.
*
* @author jonmv
*/
public class RequestSigner {
private final Key privateKey;
private final String keyId;
private final Clock clock;
/** Creates a new request signer from the PEM encoded RSA key at the specified path, owned by the given application. */
public RequestSigner(String pemPrivateKey, String keyId) {
this(pemPrivateKey, keyId, Clock.systemUTC());
}
/** Creates a new request signer with a custom clock. */
RequestSigner(String pemPrivateKey, String keyId, Clock clock) {
this.privateKey = Signatures.parsePrivatePemPkcs8RsaKey(pemPrivateKey);
this.keyId = keyId;
this.clock = clock;
}
/**
* Completes, signs and returns the given request builder and data.<br>
* <br>
* The request builder's method and data are set to the given arguments, and a hash of the
* content is computed and added to a header, together with other meta data, like the URI
* of the request, the current UTC time, and the id of the public key which shall be used to
* verify this signature.
* Finally, a signature is computed from these fields, based on the private key of this, and
* added to the request as another header.
*/
public HttpRequest signed(HttpRequest.Builder request, Method method, Supplier<InputStream> data) {
String timestamp = clock.instant().toString();
String contentHash = Base64.getEncoder().encodeToString(sha256Digest(data::get));
byte[] canonicalMessage = Signatures.canonicalMessageOf(method.name(), request.copy().build().uri(), timestamp, contentHash);
String signature = Base64.getEncoder().encodeToString(encrypted(canonicalMessage, privateKey));
request.setHeader("X-Timestamp", timestamp);
request.setHeader("X-Content-Hash", contentHash);
request.setHeader("X-Key-Id", keyId);
request.setHeader("X-Authorization", signature);
request.method(method.name(), HttpRequest.BodyPublishers.ofInputStream(data));
return request.build();
}
/**
* Completes, signs and returns the given request builder and data.
*
* This sets the Content-Type header from the given streamer, and returns
* {@code signed(request, method, streamer::data)}.
*/
public HttpRequest signed(HttpRequest.Builder request, Method method, MultiPartStreamer streamer) {
request.setHeader("Content-Type", streamer.contentType());
return signed(request, method, streamer::data);
}
/**
* Completes, signs and returns the given request builder.<br>
* <br>
* This is simply a convenience for<br>
* {@code signed(request, method, () -> new ByteArrayInputStream(data))}.
*/
public HttpRequest signed(HttpRequest.Builder request, Method method, byte[] data) {
return signed(request, method, () -> new ByteArrayInputStream(data));
}
/**
* Completes, signs and returns the given request builder.<br>
* <br>
* This sets the data of the request to be empty, and returns <br>
* {@code signed(request, method, InputStream::nullInputStream)}.
*/
public HttpRequest signed(HttpRequest.Builder request, Method method) {
return signed(request, method, InputStream::nullInputStream);
}
}
|