|
| 1 | +using System; |
| 2 | +using System.Security.Cryptography; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace SmartStore.Web.Framework.WebApi.Security |
| 6 | +{ |
| 7 | + public class HmacAuthentication |
| 8 | + { |
| 9 | + private static readonly string _delimiterRepresentation = "\n"; |
| 10 | + private static readonly string _scheme = "SmNetHmac"; |
| 11 | + |
| 12 | + public static string Scheme1 { get { return _scheme + "1"; } } |
| 13 | + public static string SignatureMethod { get { return "HMAC-SHA256"; } } |
| 14 | + |
| 15 | + /// <summary>Creates two random unequal keys.</summary> |
| 16 | + public bool CreateKeys(out string key1, out string key2, int length = 32) |
| 17 | + { |
| 18 | + key1 = key2 = null; |
| 19 | + |
| 20 | + using (var rng = RandomNumberGenerator.Create()) |
| 21 | + { |
| 22 | + for (int i = 0; i < 9999; ++i) |
| 23 | + { |
| 24 | + byte[] data1 = new byte[length]; |
| 25 | + byte[] data2 = new byte[length]; |
| 26 | + |
| 27 | + rng.GetNonZeroBytes(data1); |
| 28 | + rng.GetNonZeroBytes(data2); |
| 29 | + |
| 30 | + key1 = data1.ToHexString(length).ToLower(); |
| 31 | + key2 = data2.ToHexString(length).ToLower(); |
| 32 | + |
| 33 | + if (key1 != key2) |
| 34 | + break; |
| 35 | + } |
| 36 | + } |
| 37 | + return !string.IsNullOrWhiteSpace(key1) && !string.IsNullOrWhiteSpace(key2) && key1 != key2; |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary>Creates a base64 encoded hash for a content.</summary> |
| 41 | + public string CreateContentMd5Hash(byte[] content) |
| 42 | + { |
| 43 | + string result = ""; |
| 44 | + if (content != null && content.Length > 0) |
| 45 | + { |
| 46 | + using (var md5 = MD5.Create()) |
| 47 | + { |
| 48 | + byte[] hash = md5.ComputeHash(content); |
| 49 | + result = Convert.ToBase64String(hash); |
| 50 | + } |
| 51 | + } |
| 52 | + return result; |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary>Creates a base64 encoded HMAC-SHA256 signature.</summary> |
| 56 | + public string CreateSignature(string secretKey, string messageRepresentation) |
| 57 | + { |
| 58 | + if (string.IsNullOrWhiteSpace(secretKey) || string.IsNullOrWhiteSpace(messageRepresentation)) |
| 59 | + return ""; |
| 60 | + |
| 61 | + string signature; |
| 62 | + var secretBytes = Encoding.UTF8.GetBytes(secretKey); |
| 63 | + var valueBytes = Encoding.UTF8.GetBytes(messageRepresentation); |
| 64 | + |
| 65 | + using (var hmac = new HMACSHA256(secretBytes)) |
| 66 | + { |
| 67 | + var hash = hmac.ComputeHash(valueBytes); |
| 68 | + signature = Convert.ToBase64String(hash); |
| 69 | + } |
| 70 | + return signature; |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary>Creates a message representation as follows: |
| 74 | + /// HTTP method\n + |
| 75 | + /// Content-MD5\n + |
| 76 | + /// Response content type (accept header)\n + |
| 77 | + /// Canonicalized URI\n |
| 78 | + /// ISO-8601 UTC timestamp including milliseconds (e.g. 2013-09-23T09:24:43.5395441Z)\n |
| 79 | + /// Public-Key |
| 80 | + /// </summary> |
| 81 | + public string CreateMessageRepresentation(WebApiRequestContext context, string contentMd5Hash, string timestamp) |
| 82 | + { |
| 83 | + if (context == null || !context.IsValid) |
| 84 | + return null; |
| 85 | + |
| 86 | + string result = string.Join(_delimiterRepresentation, |
| 87 | + context.HttpMethod.ToLower(), |
| 88 | + contentMd5Hash ?? "", |
| 89 | + context.HttpAcceptType.ToLower(), |
| 90 | + context.Url.ToLower(), |
| 91 | + timestamp, |
| 92 | + context.PublicKey.ToLower() |
| 93 | + ); |
| 94 | + return result; |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary>Creates the value for the authorization header entry.</summary> |
| 98 | + public string CreateAuthorizationHeader(string signature) |
| 99 | + { |
| 100 | + if (string.IsNullOrWhiteSpace(signature)) |
| 101 | + return ""; |
| 102 | + |
| 103 | + return Scheme1 + " " + signature; |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary>Checks whether the authorization header is valid.</summary> |
| 107 | + public bool IsAuthorizationHeaderValid(string scheme, string signature) |
| 108 | + { |
| 109 | + return (!string.IsNullOrWhiteSpace(scheme) && scheme.StartsWith(_scheme) && !string.IsNullOrWhiteSpace(signature)); |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary>Returns a validated, versioned scheme value.</summary> |
| 113 | + public string GetWwwAuthenticateScheme(string schemeConsumer) |
| 114 | + { |
| 115 | + if (!string.IsNullOrWhiteSpace(schemeConsumer) && schemeConsumer == Scheme1) |
| 116 | + { |
| 117 | + return schemeConsumer; |
| 118 | + } |
| 119 | + return Scheme1; // fallback to first version |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + public enum HmacResult : int |
| 125 | + { |
| 126 | + Success = 0, |
| 127 | + FailedForUnknownReason, |
| 128 | + ApiUnavailable, |
| 129 | + InvalidAuthorizationHeader, |
| 130 | + InvalidSignature, |
| 131 | + InvalidTimestamp, |
| 132 | + TimestampOutOfPeriod, |
| 133 | + TimestampOlderThanLastRequest, |
| 134 | + MissingMessageRepresentationParameter, |
| 135 | + ContentMd5NotMatching, |
| 136 | + UserUnknown, |
| 137 | + UserDisabled, |
| 138 | + UserInvalid, |
| 139 | + UserHasNoPermission |
| 140 | + } |
| 141 | +} |
0 commit comments