|
1 | | -//// |
2 | | -//// Copyright (c) .NET Foundation and Contributors |
3 | | -//// See LICENSE file in the project root for full license information. |
4 | | -//// |
5 | | - |
6 | | -//using System; |
7 | | -//using System.Collections; |
8 | | -//using System.Diagnostics; |
9 | | -//using System.Security.Cryptography; |
10 | | -//using System.Text; |
11 | | - |
12 | | -//namespace nanoFramework.Aws.SignatureVersion4 |
13 | | -//{ |
14 | | -// /// <summary> |
15 | | -// /// AWS Signature Version 4 signer for signing requests |
16 | | -// /// using an 'Authorization' header. |
17 | | -// /// </summary> |
18 | | -// public class SignerForAuthorizationHeader : SignerBase |
19 | | -// { |
20 | | -// /// <summary> |
21 | | -// /// Computes an Version 4 signature for a request, ready for inclusion as an |
22 | | -// /// 'Authorization' header. |
23 | | -// /// </summary> |
24 | | -// /// <param name="headers"> |
25 | | -// /// The request headers; 'Host' and 'X-Amz-Date' will be added to this set. |
26 | | -// /// </param> |
27 | | -// /// <param name="queryParameters"> |
28 | | -// /// Any query parameters that will be added to the endpoint. The parameters |
29 | | -// /// should be specified in canonical format. |
30 | | -// /// </param> |
31 | | -// /// <param name="bodyHash"> |
32 | | -// /// Precomputed SHA256 hash of the request body content; this value should also |
33 | | -// /// be set as the header 'X-Amz-Content-SHA256' for non-streaming uploads. |
34 | | -// /// </param> |
35 | | -// /// <param name="awsAccessKey"> |
36 | | -// /// The user's AWS Access Key. |
37 | | -// /// </param> |
38 | | -// /// <param name="awsSecretKey"> |
39 | | -// /// The user's AWS Secret Key. |
40 | | -// /// </param> |
41 | | -// /// <returns> |
42 | | -// /// The computed authorization string for the request. This value needs to be set as the |
43 | | -// /// header 'Authorization' on the subsequent HTTP request. |
44 | | -// /// </returns> |
45 | | -// public string ComputeSignature(IDictionary headers, |
46 | | -// string queryParameters, |
47 | | -// string bodyHash, |
48 | | -// string awsAccessKey, |
49 | | -// string awsSecretKey) |
50 | | -// { |
51 | | -// // first get the date and time for the subsequent request, and convert to ISO8601 format (without '-' and ':') |
52 | | -// // for use in signature generation |
53 | | -// var requestDateTime = DateTime.UtcNow; |
54 | | -// var dateTimeStamp = requestDateTime.ToString(ISO8601BasicFormat); |
55 | | - |
56 | | -// // update the headers with required 'x-amz-date' and 'host' values |
57 | | -// headers.Add(X_Amz_Date, dateTimeStamp); |
58 | | - |
59 | | -// var hostHeader = EndpointUri.Host; |
60 | | -// hostHeader += ":" + EndpointUri.Port; // FIXME: should use //if (!EndpointUri.IsDefaultPort) |
61 | | -// headers.Add("Host", hostHeader); |
62 | | - |
63 | | -// // canonicalize the headers; we need the set of header names as well as the |
64 | | -// // names and values to go into the signature process |
65 | | -// var canonicalizedHeaderNames = CanonicalizeHeaderNames(headers); |
66 | | -// var canonicalizedHeaders = CanonicalizeHeaders(headers); |
67 | | - |
68 | | -// // if any query string parameters have been supplied, canonicalize them |
69 | | -// // (note this sample assumes any required url encoding has been done already) |
70 | | -// var canonicalizedQueryParameters = string.Empty; |
71 | | -// if (!string.IsNullOrEmpty(queryParameters)) |
72 | | -// { |
73 | | -// var paramDictionary = new Hashtable(); |
74 | | - |
75 | | -// var qparam = queryParameters.Split('&'); |
76 | | -// foreach (string p in qparam) |
77 | | -// { |
78 | | -// var items = p.Split('='); |
79 | | -// if (items.Length == 1) |
80 | | -// { |
81 | | -// paramDictionary.Add(items[0], null); |
82 | | -// } |
83 | | -// else |
84 | | -// { |
85 | | -// paramDictionary.Add(items[0], items[1]); |
86 | | -// } |
87 | | -// } |
88 | | - |
89 | | -// var sb = new StringBuilder(); |
90 | | -// var paramKeys = new ArrayList(); |
91 | | - |
92 | | -// foreach (DictionaryEntry kvp in paramDictionary) |
93 | | -// { |
94 | | -// paramKeys.Add(kvp.Key); |
95 | | -// } |
96 | | - |
97 | | -// paramKeys.Sort(StringComparer.Ordinal); |
98 | | -// foreach (var p in paramKeys) |
99 | | -// { |
100 | | -// if (sb.Length > 0) |
101 | | -// sb.Append("&"); |
102 | | -// sb.Append($"{p}={paramDictionary[p]}"); |
103 | | -// } |
104 | | - |
105 | | -// canonicalizedQueryParameters = sb.ToString(); |
106 | | -// } |
107 | | - |
108 | | -// // canonicalize the various components of the request |
109 | | -// var canonicalRequest = CanonicalizeRequest(EndpointUri, |
110 | | -// HttpMethod, |
111 | | -// canonicalizedQueryParameters, |
112 | | -// canonicalizedHeaderNames, |
113 | | -// canonicalizedHeaders, |
114 | | -// bodyHash); |
115 | | -// Debug.WriteLine($"\nCanonicalRequest:\n{canonicalRequest}"); |
116 | | - |
117 | | -// // generate a hash of the canonical request, to go into signature computation |
118 | | -// var canonicalRequestHashBytes |
119 | | -// = CanonicalRequestHashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(canonicalRequest)); |
120 | | - |
121 | | -// // construct the string to be signed |
122 | | -// var stringToSign = new StringBuilder(); |
123 | | - |
124 | | -// var dateStamp = requestDateTime.ToString(DateStringFormat); |
125 | | -// var scope = $"{dateStamp}/{Region}/{Service}/{TERMINATOR}"; |
126 | | - |
127 | | -// stringToSign.Append($"{SCHEME}-{ALGORITHM}\n{dateTimeStamp}\n{scope}\n"); |
128 | | -// stringToSign.Append(ToHexString(canonicalRequestHashBytes, true)); |
129 | | - |
130 | | -// Debug.WriteLine($"\nStringToSign:\n{stringToSign}"); |
131 | | - |
132 | | -// // compute the signing key |
133 | | -// var kha = new HMACSHA256(DeriveSigningKey(awsSecretKey, Region, dateStamp, Service)); |
134 | | - |
135 | | -// // compute the AWS4 signature and return it |
136 | | -// var signature = kha.ComputeHash(Encoding.UTF8.GetBytes(stringToSign.ToString())); |
137 | | -// var signatureString = ToHexString(signature, true); |
138 | | -// Debug.WriteLine($"\nSignature:\n{signatureString}"); |
139 | | - |
140 | | -// var authString = new StringBuilder(); |
141 | | -// authString.Append($"{SCHEME}-{ALGORITHM} "); |
142 | | -// authString.Append($"Credential={awsAccessKey}/{scope}, "); |
143 | | -// authString.Append($"SignedHeaders={canonicalizedHeaderNames}, "); |
144 | | -// authString.Append($"Signature={signatureString}"); |
145 | | - |
146 | | -// var authorization = authString.ToString(); |
147 | | -// Debug.WriteLine($"\nAuthorization:\n{authorization}"); |
148 | | - |
149 | | -// return authorization; |
150 | | -// } |
151 | | -// } |
152 | | -//} |
| 1 | +// |
| 2 | +// Copyright (c) .NET Foundation and Contributors |
| 3 | +// See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections; |
| 8 | +using System.Diagnostics; |
| 9 | +using System.Security.Cryptography; |
| 10 | +using System.Text; |
| 11 | + |
| 12 | +namespace nanoFramework.Aws.SignatureVersion4 |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// AWS Signature Version 4 signer for signing requests |
| 16 | + /// using an 'Authorization' header. |
| 17 | + /// </summary> |
| 18 | + public class SignerForAuthorizationHeader : SignerBase |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// Computes an Version 4 signature for a request, ready for inclusion as an |
| 22 | + /// 'Authorization' header. |
| 23 | + /// </summary> |
| 24 | + /// <param name="headers"> |
| 25 | + /// The request headers; 'Host' and 'X-Amz-Date' will be added to this set. |
| 26 | + /// </param> |
| 27 | + /// <param name="queryParameters"> |
| 28 | + /// Any query parameters that will be added to the endpoint. The parameters |
| 29 | + /// should be specified in canonical format. |
| 30 | + /// </param> |
| 31 | + /// <param name="bodyHash"> |
| 32 | + /// Precomputed SHA256 hash of the request body content; this value should also |
| 33 | + /// be set as the header 'X-Amz-Content-SHA256' for non-streaming uploads. |
| 34 | + /// </param> |
| 35 | + /// <param name="awsAccessKey"> |
| 36 | + /// The user's AWS Access Key. |
| 37 | + /// </param> |
| 38 | + /// <param name="awsSecretKey"> |
| 39 | + /// The user's AWS Secret Key. |
| 40 | + /// </param> |
| 41 | + /// <returns> |
| 42 | + /// The computed authorization string for the request. This value needs to be set as the |
| 43 | + /// header 'Authorization' on the subsequent HTTP request. |
| 44 | + /// </returns> |
| 45 | + public string ComputeSignature(IDictionary headers, |
| 46 | + string queryParameters, |
| 47 | + string bodyHash, |
| 48 | + string awsAccessKey, |
| 49 | + string awsSecretKey) |
| 50 | + { |
| 51 | + // first get the date and time for the subsequent request, and convert to ISO8601 format (without '-' and ':') |
| 52 | + // for use in signature generation |
| 53 | + var requestDateTime = DateTime.UtcNow; |
| 54 | + var dateTimeStamp = requestDateTime.ToString(ISO8601BasicFormat); |
| 55 | + |
| 56 | + // update the headers with required 'x-amz-date' and 'host' values |
| 57 | + headers.Add(X_Amz_Date, dateTimeStamp); |
| 58 | + |
| 59 | + var hostHeader = EndpointUri.Host; |
| 60 | + hostHeader += ":" + EndpointUri.Port; // FIXME: should use //if (!EndpointUri.IsDefaultPort) |
| 61 | + headers.Add("Host", hostHeader); |
| 62 | + |
| 63 | + // canonicalize the headers; we need the set of header names as well as the |
| 64 | + // names and values to go into the signature process |
| 65 | + var canonicalizedHeaderNames = CanonicalizeHeaderNames(headers); |
| 66 | + var canonicalizedHeaders = CanonicalizeHeaders(headers); |
| 67 | + |
| 68 | + // if any query string parameters have been supplied, canonicalize them |
| 69 | + // (note this sample assumes any required url encoding has been done already) |
| 70 | + var canonicalizedQueryParameters = string.Empty; |
| 71 | + if (!string.IsNullOrEmpty(queryParameters)) |
| 72 | + { |
| 73 | + var paramDictionary = new Hashtable(); |
| 74 | + |
| 75 | + var qparam = queryParameters.Split('&'); |
| 76 | + foreach (string p in qparam) |
| 77 | + { |
| 78 | + var items = p.Split('='); |
| 79 | + if (items.Length == 1) |
| 80 | + { |
| 81 | + paramDictionary.Add(items[0], null); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + paramDictionary.Add(items[0], items[1]); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + var sb = new StringBuilder(); |
| 90 | + var paramKeys = new ArrayList(); |
| 91 | + |
| 92 | + foreach (DictionaryEntry kvp in paramDictionary) |
| 93 | + { |
| 94 | + paramKeys.Add(kvp.Key); |
| 95 | + } |
| 96 | + |
| 97 | + paramKeys.Sort(StringComparer.Ordinal); |
| 98 | + foreach (var p in paramKeys) |
| 99 | + { |
| 100 | + if (sb.Length > 0) |
| 101 | + sb.Append("&"); |
| 102 | + sb.Append($"{p}={paramDictionary[p]}"); |
| 103 | + } |
| 104 | + |
| 105 | + canonicalizedQueryParameters = sb.ToString(); |
| 106 | + } |
| 107 | + |
| 108 | + // canonicalize the various components of the request |
| 109 | + var canonicalRequest = CanonicalizeRequest(EndpointUri, |
| 110 | + HttpMethod, |
| 111 | + canonicalizedQueryParameters, |
| 112 | + canonicalizedHeaderNames, |
| 113 | + canonicalizedHeaders, |
| 114 | + bodyHash); |
| 115 | + Debug.WriteLine($"\nCanonicalRequest:\n{canonicalRequest}"); |
| 116 | + |
| 117 | + // generate a hash of the canonical request, to go into signature computation |
| 118 | + var canonicalRequestHashBytes |
| 119 | + = CanonicalRequestHashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(canonicalRequest)); |
| 120 | + |
| 121 | + // construct the string to be signed |
| 122 | + var stringToSign = new StringBuilder(); |
| 123 | + |
| 124 | + var dateStamp = requestDateTime.ToString(DateStringFormat); |
| 125 | + var scope = $"{dateStamp}/{Region}/{Service}/{TERMINATOR}"; |
| 126 | + |
| 127 | + stringToSign.Append($"{SCHEME}-{ALGORITHM}\n{dateTimeStamp}\n{scope}\n"); |
| 128 | + stringToSign.Append(ToHexString(canonicalRequestHashBytes, true)); |
| 129 | + |
| 130 | + Debug.WriteLine($"\nStringToSign:\n{stringToSign}"); |
| 131 | + |
| 132 | + // compute the signing key |
| 133 | + var kha = new HMACSHA256(DeriveSigningKey(awsSecretKey, Region, dateStamp, Service)); |
| 134 | + |
| 135 | + // compute the AWS4 signature and return it |
| 136 | + var signature = kha.ComputeHash(Encoding.UTF8.GetBytes(stringToSign.ToString())); |
| 137 | + var signatureString = ToHexString(signature, true); |
| 138 | + Debug.WriteLine($"\nSignature:\n{signatureString}"); |
| 139 | + |
| 140 | + var authString = new StringBuilder(); |
| 141 | + authString.Append($"{SCHEME}-{ALGORITHM} "); |
| 142 | + authString.Append($"Credential={awsAccessKey}/{scope}, "); |
| 143 | + authString.Append($"SignedHeaders={canonicalizedHeaderNames}, "); |
| 144 | + authString.Append($"Signature={signatureString}"); |
| 145 | + |
| 146 | + var authorization = authString.ToString(); |
| 147 | + Debug.WriteLine($"\nAuthorization:\n{authorization}"); |
| 148 | + |
| 149 | + return authorization; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments