public bool ComputeSignatureAndCompare(byte[] stringToSign, SecretKeyListV3 keys) { bool flag; NephosAssertionException.Assert(stringToSign != null); NephosAssertionException.Assert(keys != null); if (keys.Count == 0) { throw new ArgumentException("Invalid number of keys"); } bool flag1 = string.IsNullOrEmpty(this.KeyName); List <SecretKeyV3> .Enumerator enumerator = keys.GetEnumerator(); try { while (enumerator.MoveNext()) { SecretKeyV3 current = enumerator.Current; if ((!flag1 || !current.IsDefault()) && !string.Equals(current.Name, this.KeyName) || !SASUtilities.ComputeSignatureAndCompare(stringToSign, current.Value, this.Signature)) { continue; } this.KeyUsedForSigning = current; flag = true; return(flag); } return(false); } finally { ((IDisposable)enumerator).Dispose(); } return(flag); }
public static Collection <AuthDataEntry> GetSharedKeys(AuthenticationInformation authInfo) { if (authInfo == null) { throw new ArgumentNullException("authInfo"); } if (string.Compare(authInfo.AuthScheme, SupportedAuthScheme.SharedKey.ToString(), StringComparison.OrdinalIgnoreCase) != 0) { throw new ArgumentException("Supplied authentication information is not for shared key scheme!", "authInfo"); } string authKeyName = authInfo.AuthKeyName; Collection <AuthDataEntry> authDataEntries = new Collection <AuthDataEntry>(); if (string.IsNullOrEmpty(authKeyName)) { foreach (AuthDataEntry authDatum in authInfo.AuthData) { if (!SecretKeyV3.IsDefaultKeyName(authDatum.KeyName)) { continue; } authDataEntries.Add(authDatum); } } else if (authInfo.NamedKeyAuthData != null) { authDataEntries.Add(authInfo.NamedKeyAuthData); } if (authDataEntries.Count == 0) { CultureInfo invariantCulture = CultureInfo.InvariantCulture; object[] objArray = new object[] { authKeyName ?? "N/A" }; throw new AuthenticationFailureException(string.Format(invariantCulture, "Could not find any keys to use for authentication. Key name specified is '{0}'", objArray)); } return(authDataEntries); }
public static string GenerateBlobSasUrl(string accountName, string containerName, string blobName, string blobSnapshot, IStorageAccount storageAccount, string requestUrlBase, bool includeWritePermission) { if (string.IsNullOrEmpty(accountName) || string.IsNullOrEmpty(containerName) || string.IsNullOrEmpty(blobName) || string.IsNullOrEmpty(requestUrlBase)) { IStringDataEventStream error = Logger <IRestProtocolHeadLogger> .Instance.Error; object[] objArray = new object[] { accountName, containerName, blobName, requestUrlBase }; error.Log("Source blob account, container, blob name or requestUrl should not be empty. sourceUnversionedAccountName: {0} sourceUnversionedContainerName: {1} sourceBlobName: {2} requestUrlBase: {3}", objArray); return(string.Empty); } NameValueCollection nameValueCollection = new NameValueCollection(); if (!includeWritePermission) { nameValueCollection.Add("sp", "r"); } else { nameValueCollection.Add("sp", "rw"); } nameValueCollection.Add("se", SASUtilities.EncodeTime(DateTime.MaxValue)); nameValueCollection.Add("sv", "2016-02-19"); nameValueCollection.Add("sr", "b"); byte[] sign = BlobSignedAccessHelper.ComputeUrlDecodedUtf8EncodedStringToSign(nameValueCollection, new NephosUriComponents(accountName, containerName, HttpUtility.UrlDecode(blobName))); string str = (new UTF8Encoding()).GetString(sign); str = str.Replace('\n', '.'); SecretKeyV3 secretKeyV3 = null; if (secretKeyV3 == null) { Logger <IRestProtocolHeadLogger> .Instance.Error.Log("GenerateBlobSasUrl: could not find a system key for the account"); return(string.Empty); } string str1 = BlobSignedAccessHelper.ComputeHMACSHA256(secretKeyV3.Value, sign); if (string.IsNullOrEmpty(str1)) { Logger <IRestProtocolHeadLogger> .Instance.Error.Log("GenerateBlobSasUrl: could not get HMACSHA256"); return(string.Empty); } nameValueCollection.Add("sig", str1); if (!string.IsNullOrEmpty(blobSnapshot)) { nameValueCollection.Add("snapshot", blobSnapshot); } UriBuilder uriBuilder = new UriBuilder(requestUrlBase); StringBuilder stringBuilder = new StringBuilder(); string[] allKeys = nameValueCollection.AllKeys; for (int i = 0; i < (int)allKeys.Length; i++) { string str2 = allKeys[i]; stringBuilder.Append(HttpUtilities.PathEncode(str2)); stringBuilder.Append("="); stringBuilder.Append(HttpUtilities.PathEncode(nameValueCollection[str2])); stringBuilder.Append("&"); } if (stringBuilder.Length > 0) { stringBuilder.Remove(stringBuilder.Length - 1, 1); } uriBuilder.Query = stringBuilder.ToString(); return(uriBuilder.ToString()); }