public static string Encode(byte[] data, MachineKeyProtection protectionOption)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            //////////////////////////////////////////////////////////////////////
            // Step 1: Get the MAC and add to the blob
            if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Validation)
            {
                byte[] bHash = MachineKeySection.HashData(data, null, 0, data.Length);
                byte[] bAll  = new byte[bHash.Length + data.Length];
                Buffer.BlockCopy(data, 0, bAll, 0, data.Length);
                Buffer.BlockCopy(bHash, 0, bAll, data.Length, bHash.Length);
                data = bAll;
            }

            if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Encryption)
            {
                //////////////////////////////////////////////////////////////////////
                // Step 2: Encryption
                data = MachineKeySection.EncryptOrDecryptData(true, data, null, 0, data.Length, false, false, IVType.Random, !AppSettings.UseLegacyMachineKeyEncryption);
            }

            //////////////////////////////////////////////////////////////////////
            // Step 3: Covert the buffer to HEX string and return it
            return(CryptoUtil.BinaryToHex(data));
        }
Пример #2
0
        internal static String Encrypt(FormsAuthenticationTicket ticket, bool hexEncodedTicket)
        {
            if (ticket == null)
            {
                throw new ArgumentNullException("ticket");
            }

            Initialize();
            //////////////////////////////////////////////////////////////////////
            // Step 1a: Make it into a binary blob
            byte[] bBlob = MakeTicketIntoBinaryBlob(ticket);
            if (bBlob == null)
            {
                return(null);
            }

            //////////////////////////////////////////////////////////////////////
            // Step 1b: If new crypto routines are enabled, call them instead.
            if (AspNetCryptoServiceProvider.Instance.IsDefaultProvider)
            {
                ICryptoService cryptoService = AspNetCryptoServiceProvider.Instance.GetCryptoService(Purpose.FormsAuthentication_Ticket);
                byte[]         protectedData = cryptoService.Protect(bBlob);
                bBlob = protectedData;
            }
            else
            {
#pragma warning disable 618 // calling obsolete methods
                // otherwise..

                //////////////////////////////////////////////////////////////////////
                // Step 2: Get the MAC and add to the blob
                if (_Protection == FormsProtectionEnum.All || _Protection == FormsProtectionEnum.Validation)
                {
                    byte[] bMac = MachineKeySection.HashData(bBlob, null, 0, bBlob.Length);
                    if (bMac == null)
                    {
                        return(null);
                    }
                    byte[] bAll = new byte[bMac.Length + bBlob.Length];
                    Buffer.BlockCopy(bBlob, 0, bAll, 0, bBlob.Length);
                    Buffer.BlockCopy(bMac, 0, bAll, bBlob.Length, bMac.Length);
                    bBlob = bAll;
                }

                if (_Protection == FormsProtectionEnum.All || _Protection == FormsProtectionEnum.Encryption)
                {
                    //////////////////////////////////////////////////////////////////////
                    // Step 3: Do the actual encryption
                    // DevDiv Bugs 137864: Include a random IV if under the right compat mode
                    // for improved encryption semantics
                    bBlob = MachineKeySection.EncryptOrDecryptData(true, bBlob, null, 0, bBlob.Length, false, false, IVType.Random);
                }
#pragma warning restore 618 // calling obsolete methods
            }

            //if (!hexEncodedTicket)
            //    return HttpServerUtility.UrlTokenEncode(bBlob);
            //else
            return(CryptoUtil.BinaryToHex(bBlob));
        }
Пример #3
0
 private static string Encrypt(FormsAuthenticationTicket ticket, bool hexEncodedTicket)
 {
     if (ticket == null)
     {
         throw new ArgumentNullException("ticket");
     }
     Initialize();
     byte[] buf = MakeTicketIntoBinaryBlob(ticket);
     if (buf == null)
     {
         return(null);
     }
     if ((_Protection == FormsProtectionEnum.All) || (_Protection == FormsProtectionEnum.Validation))
     {
         byte[] src = MachineKeySection.HashData(buf, null, 0, buf.Length);
         if (src == null)
         {
             return(null);
         }
         byte[] dst = new byte[src.Length + buf.Length];
         Buffer.BlockCopy(buf, 0, dst, 0, buf.Length);
         Buffer.BlockCopy(src, 0, dst, buf.Length, src.Length);
         buf = dst;
     }
     if ((_Protection == FormsProtectionEnum.All) || (_Protection == FormsProtectionEnum.Encryption))
     {
         buf = MachineKeySection.EncryptOrDecryptData(true, buf, null, 0, buf.Length, false, false, IVType.Random);
     }
     if (!hexEncodedTicket)
     {
         return(HttpServerUtility.UrlTokenEncode(buf));
     }
     return(MachineKeySection.ByteArrayToHexString(buf, 0));
 }
 internal static string Encode(CookieProtection cookieProtection, byte[] buf, int count)
 {
     if ((cookieProtection == CookieProtection.All) || (cookieProtection == CookieProtection.Validation))
     {
         byte[] src = MachineKeySection.HashData(buf, null, 0, count);
         if (src == null)
         {
             return(null);
         }
         if (buf.Length >= (count + src.Length))
         {
             Buffer.BlockCopy(src, 0, buf, count, src.Length);
         }
         else
         {
             byte[] buffer2 = buf;
             buf = new byte[count + src.Length];
             Buffer.BlockCopy(buffer2, 0, buf, 0, count);
             Buffer.BlockCopy(src, 0, buf, count, src.Length);
         }
         count += src.Length;
     }
     if ((cookieProtection == CookieProtection.All) || (cookieProtection == CookieProtection.Encryption))
     {
         buf   = MachineKeySection.EncryptOrDecryptData(true, buf, null, 0, count);
         count = buf.Length;
     }
     if (count < buf.Length)
     {
         byte[] buffer3 = buf;
         buf = new byte[count];
         Buffer.BlockCopy(buffer3, 0, buf, 0, count);
     }
     return(HttpServerUtility.UrlTokenEncode(buf));
 }
 public static byte[] Decode(string encodedData, MachineKeyProtection protectionOption)
 {
     if (encodedData == null)
     {
         throw new ArgumentNullException("encodedData");
     }
     if ((encodedData.Length % 2) != 0)
     {
         throw new ArgumentException(null, "encodedData");
     }
     byte[] buf = null;
     try
     {
         buf = MachineKeySection.HexStringToByteArray(encodedData);
     }
     catch
     {
         throw new ArgumentException(null, "encodedData");
     }
     if ((buf == null) || (buf.Length < 1))
     {
         throw new ArgumentException(null, "encodedData");
     }
     if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Encryption))
     {
         buf = MachineKeySection.EncryptOrDecryptData(false, buf, null, 0, buf.Length, false, false, IVType.Random, !AppSettings.UseLegacyMachineKeyEncryption);
         if (buf == null)
         {
             return(null);
         }
     }
     if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Validation))
     {
         if (buf.Length < MachineKeySection.HashSize)
         {
             return(null);
         }
         byte[] src = buf;
         buf = new byte[src.Length - MachineKeySection.HashSize];
         Buffer.BlockCopy(src, 0, buf, 0, buf.Length);
         byte[] buffer3 = MachineKeySection.HashData(buf, null, 0, buf.Length);
         if ((buffer3 == null) || (buffer3.Length != MachineKeySection.HashSize))
         {
             return(null);
         }
         for (int i = 0; i < buffer3.Length; i++)
         {
             if (buffer3[i] != src[buf.Length + i])
             {
                 return(null);
             }
         }
     }
     return(buf);
 }
        internal static string Encode(CookieProtection cookieProtection, byte [] buf, Purpose purpose)
        {
            if (AspNetCryptoServiceProvider.Instance.IsDefaultProvider)
            {
                // If we're configured to go through the new crypto routines, do so.
                ICryptoService cryptoService = AspNetCryptoServiceProvider.Instance.GetCryptoService(purpose);
                return(HttpServerUtility.UrlTokenEncode(cryptoService.Protect(buf)));
            }

#pragma warning disable 618 // calling obsolete methods
            // Otherwise fall back to using MachineKeySection.
            int count = buf.Length;
            if (cookieProtection == CookieProtection.All || cookieProtection == CookieProtection.Validation)
            {
                byte[] bMac = MachineKeySection.HashData(buf, null, 0, count);

                if (bMac == null)
                {
                    return(null);
                }
                if (buf.Length >= count + bMac.Length)
                {
                    Buffer.BlockCopy(bMac, 0, buf, count, bMac.Length);
                }
                else
                {
                    byte[] bTemp = buf;
                    buf = new byte[count + bMac.Length];
                    Buffer.BlockCopy(bTemp, 0, buf, 0, count);
                    Buffer.BlockCopy(bMac, 0, buf, count, bMac.Length);
                }
                count += bMac.Length;
            }

            if (cookieProtection == CookieProtection.All || cookieProtection == CookieProtection.Encryption)
            {
                buf   = MachineKeySection.EncryptOrDecryptData(true, buf, null, 0, count);
                count = buf.Length;
            }
            if (count < buf.Length)
            {
                byte[] bTemp = buf;
                buf = new byte[count];
                Buffer.BlockCopy(bTemp, 0, buf, 0, count);
            }
#pragma warning restore 618 // calling obsolete methods

            return(HttpServerUtility.UrlTokenEncode(buf));
        }
 public static string Encode(byte[] data, MachineKeyProtection protectionOption)
 {
     if (data == null)
     {
         throw new ArgumentNullException("data");
     }
     if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Validation))
     {
         byte[] src = MachineKeySection.HashData(data, null, 0, data.Length);
         byte[] dst = new byte[src.Length + data.Length];
         Buffer.BlockCopy(data, 0, dst, 0, data.Length);
         Buffer.BlockCopy(src, 0, dst, data.Length, src.Length);
         data = dst;
     }
     if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Encryption))
     {
         data = MachineKeySection.EncryptOrDecryptData(true, data, null, 0, data.Length, false, false, IVType.Random, !AppSettings.UseLegacyMachineKeyEncryption);
     }
     return(MachineKeySection.ByteArrayToHexString(data, 0));
 }
        public static byte[] Decode(string encodedData, MachineKeyProtection protectionOption)
        {
            if (encodedData == null)
            {
                throw new ArgumentNullException("encodedData");
            }

            if ((encodedData.Length % 2) != 0)
            {
                throw new ArgumentException(null, "encodedData");
            }

            byte[] data = null;
            try {
                //////////////////////////////////////////////////////////////////////
                // Step 1: Covert the HEX string to byte array
                data = CryptoUtil.HexToBinary(encodedData);
            }
            catch {
                throw new ArgumentException(null, "encodedData");
            }

            if (data == null || data.Length < 1)
            {
                throw new ArgumentException(null, "encodedData");
            }

            if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Encryption)
            {
                //////////////////////////////////////////////////////////////////
                // Step 2: Decrypt the data
                data = MachineKeySection.EncryptOrDecryptData(false, data, null, 0, data.Length, false, false, IVType.Random, !AppSettings.UseLegacyMachineKeyEncryption);
                if (data == null)
                {
                    return(null);
                }
            }

            if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Validation)
            {
                //////////////////////////////////////////////////////////////////
                // Step 3a: Remove the hash from the end of the data
                if (data.Length < MachineKeySection.HashSize)
                {
                    return(null);
                }
                byte[] originalData = data;
                data = new byte[originalData.Length - MachineKeySection.HashSize];
                Buffer.BlockCopy(originalData, 0, data, 0, data.Length);

                //////////////////////////////////////////////////////////////////
                // Step 3b: Calculate the hash and make sure it matches
                byte[] bHash = MachineKeySection.HashData(data, null, 0, data.Length);
                if (bHash == null || bHash.Length != MachineKeySection.HashSize)
                {
                    return(null); // Sizes don't match
                }
                for (int iter = 0; iter < bHash.Length; iter++)
                {
                    if (bHash[iter] != originalData[data.Length + iter])
                    {
                        return(null); // Mis-match found
                    }
                }
            }

            return(data);
        }
        internal static string CreateOutputCachedItemKey(string path, HttpVerb verb, HttpContext context, CachedVary cachedVary)
        {
            StringBuilder builder;

            if (verb == HttpVerb.POST)
            {
                builder = new StringBuilder("a1", path.Length + "a1".Length);
            }
            else
            {
                builder = new StringBuilder("a2", path.Length + "a2".Length);
            }
            builder.Append(CultureInfo.InvariantCulture.TextInfo.ToLower(path));
            if (cachedVary != null)
            {
                string      varyByCustomString;
                HttpRequest request = context.Request;
                for (int i = 0; i <= 2; i++)
                {
                    int                 num;
                    string[]            array = null;
                    NameValueCollection serverVarsWithoutDemand = null;
                    bool                flag = false;
                    switch (i)
                    {
                    case 0:
                        builder.Append("H");
                        array = cachedVary._headers;
                        if (array != null)
                        {
                            serverVarsWithoutDemand = request.GetServerVarsWithoutDemand();
                        }
                        break;

                    case 1:
                        builder.Append("Q");
                        array = cachedVary._params;
                        if (request.HasQueryString && ((array != null) || cachedVary._varyByAllParams))
                        {
                            serverVarsWithoutDemand = request.QueryString;
                            flag = cachedVary._varyByAllParams;
                        }
                        break;

                    default:
                        builder.Append("F");
                        if (verb == HttpVerb.POST)
                        {
                            array = cachedVary._params;
                            if (request.HasForm && ((array != null) || cachedVary._varyByAllParams))
                            {
                                serverVarsWithoutDemand = request.Form;
                                flag = cachedVary._varyByAllParams;
                            }
                        }
                        break;
                    }
                    if (flag && (serverVarsWithoutDemand.Count > 0))
                    {
                        array = serverVarsWithoutDemand.AllKeys;
                        num   = array.Length - 1;
                        while (num >= 0)
                        {
                            if (array[num] != null)
                            {
                                array[num] = CultureInfo.InvariantCulture.TextInfo.ToLower(array[num]);
                            }
                            num--;
                        }
                        Array.Sort(array, System.InvariantComparer.Default);
                    }
                    if (array != null)
                    {
                        num = 0;
                        int length = array.Length;
                        while (num < length)
                        {
                            string str = array[num];
                            if (serverVarsWithoutDemand == null)
                            {
                                varyByCustomString = "+n+";
                            }
                            else
                            {
                                varyByCustomString = serverVarsWithoutDemand[str];
                                if (varyByCustomString == null)
                                {
                                    varyByCustomString = "+n+";
                                }
                            }
                            builder.Append("N");
                            builder.Append(str);
                            builder.Append("V");
                            builder.Append(varyByCustomString);
                            num++;
                        }
                    }
                }
                builder.Append("C");
                if (cachedVary._varyByCustom != null)
                {
                    builder.Append("N");
                    builder.Append(cachedVary._varyByCustom);
                    builder.Append("V");
                    try
                    {
                        varyByCustomString = context.ApplicationInstance.GetVaryByCustomString(context, cachedVary._varyByCustom);
                        if (varyByCustomString == null)
                        {
                            varyByCustomString = "+n+";
                        }
                    }
                    catch (Exception exception)
                    {
                        varyByCustomString = "+e+";
                        HttpApplicationFactory.RaiseError(exception);
                    }
                    builder.Append(varyByCustomString);
                }
                builder.Append("D");
                if (((verb == HttpVerb.POST) && cachedVary._varyByAllParams) && (request.Form.Count == 0))
                {
                    int contentLength = request.ContentLength;
                    if ((contentLength > 0x3a98) || (contentLength < 0))
                    {
                        return(null);
                    }
                    if (contentLength > 0)
                    {
                        byte[] asByteArray = ((HttpInputStream)request.InputStream).GetAsByteArray();
                        if (asByteArray == null)
                        {
                            return(null);
                        }
                        varyByCustomString = Convert.ToBase64String(MachineKeySection.HashData(asByteArray, null, 0, asByteArray.Length));
                        builder.Append(varyByCustomString);
                    }
                }
                builder.Append("E");
                string[] strArray2 = cachedVary._contentEncodings;
                if (strArray2 != null)
                {
                    string httpHeaderContentEncoding = context.Response.GetHttpHeaderContentEncoding();
                    if (httpHeaderContentEncoding != null)
                    {
                        for (int j = 0; j < strArray2.Length; j++)
                        {
                            if (strArray2[j] == httpHeaderContentEncoding)
                            {
                                builder.Append(httpHeaderContentEncoding);
                                break;
                            }
                        }
                    }
                }
            }
            return(builder.ToString());
        }