Example #1
0
        ///////////////////////////////////////////////////////////////////////

        private static bool MatchCertificatePublicKey(
            X509Certificate certificate,
            byte[] publicKey
            )
        {
            //
            // NOTE: Make sure the certificate public key matches what we
            //       expect it to be for our own software updates.
            //
            if (certificate != null)
            {
                byte[] certificatePublicKey = certificate.GetPublicKey();

                if ((certificatePublicKey != null) &&
                    (certificatePublicKey.Length > 0))
                {
                    if ((publicKey != null) && (publicKey.Length > 0))
                    {
                        return(GenericOps <byte> .Equals(
                                   certificatePublicKey, publicKey));
                    }
                }
            }

            return(false);
        }
Example #2
0
        ///////////////////////////////////////////////////////////////////////////////////////////////

        public static ReturnCode Combine(
            IList <IList <StringBuilder> > lists, /* in */
            ref IList <StringBuilder> list,       /* in, out */
            ref Result error                      /* out */
            )
        {
            if (lists == null)
            {
                error = "invalid list of lists";
                return(ReturnCode.Error);
            }

            if (lists.Count == 0)
            {
                error = "no lists in list";
                return(ReturnCode.Error);
            }

            IList <StringBuilder> list1 = lists[0];

            if (lists.Count > 1)
            {
                for (int index = 1; index < lists.Count; index++)
                {
                    IList <StringBuilder> list2 = lists[index];
                    IList <StringBuilder> list3 = null;

                    if (Combine(
                            list1, list2, ref list3, ref error) != ReturnCode.Ok)
                    {
                        return(ReturnCode.Error);
                    }

                    list1 = list3;
                }
            }

            if (list != null)
            {
                GenericOps <StringBuilder> .AddRange(list, list1);
            }
            else
            {
                list = new List <StringBuilder>(list1);
            }

            return(ReturnCode.Ok);
        }
Example #3
0
        ///////////////////////////////////////////////////////////////////////

        public ReturnCode DumpCommands(
            ref Result result
            )
        {
            CheckDisposed();

            QueueList <string, string> queue = GetQueue();

            if (queue == null)
            {
                result = "debugger command queue not available";
                return(ReturnCode.Error);
            }

            result = GenericOps <string> .ListToString(
                queue.Values, ToStringFlags.None, Characters.Space.ToString(),
                null, false);

            return(ReturnCode.Ok);
        }
Example #4
0
        ///////////////////////////////////////////////////////////////////////

        public static bool IsDefaultPublicKeyToken(
            byte[] publicKeyToken
            )
        {
            if (publicKeyToken == null)
            {
                return(true);
            }

            byte[] defaultPublicKeyToken = PublicKeyToken.Default;

            if (defaultPublicKeyToken == null)
            {
                return(false);
            }

            if (GenericOps <byte> .Equals(publicKeyToken, defaultPublicKeyToken))
            {
                return(true);
            }

            return(false);
        }
Example #5
0
        ///////////////////////////////////////////////////////////////////////

        #region Public Parsing Methods
        public static bool ParseData(
            Configuration configuration,
            string text,
            ref IEqualityComparer <Configuration> comparer,
            ref IDictionary <Configuration, Release> releases,
            ref int[] protocolCounts,
            ref string error
            )
        {
            if (configuration == null)
            {
                error = "Invalid configuration.";
                return(false);
            }

            if (text == null)
            {
                error = "Invalid release data.";
                return(false);
            }

            //
            // NOTE: This will contain the counts of the protocols encountered
            //       while parsing the release data (e.g. "1", "2", "3", or
            //       other).
            //
            if (protocolCounts == null)
            {
                protocolCounts = new int[4];
            }

            int parseCount = 0;

            string[] lines = text.Split(Line.Separators);

            for (int lineIndex = 0; lineIndex < lines.Length; lineIndex++)
            {
                string line = lines[lineIndex];

                if (line == null)
                {
                    continue;
                }

                line = line.Trim(Characters.Space);

                if (line.Length == 0)
                {
                    continue;
                }

                if (GenericOps <char> .Contains(Line.Comments, line[0]))
                {
                    continue;
                }

                Release release = ParseLine(
                    configuration, NextId(), lineIndex, line, ref error);

                if (release != null)
                {
                    /* IGNORED */
                    release.MaybeUseDownloadBaseUri(configuration.Assembly);

                    parseCount++;

                    if (release.ProtocolId != null)
                    {
                        switch (release.ProtocolId)
                        {
                        case Protocol.Build:     /* NOTE: Release build. */
                        {
                            protocolCounts[0]++;
                            break;
                        }

                        case Protocol.Script:     /* NOTE: Update script. */
                        {
                            protocolCounts[1]++;
                            break;
                        }

                        case Protocol.Self:     /* NOTE: Updater itself. */
                        {
                            protocolCounts[2]++;
                            break;
                        }

                        default:     /* NOTE: Other and/or unknown. */
                        {
                            protocolCounts[3]++;
                            break;
                        }
                        }
                    }

                    if (comparer == null)
                    {
                        comparer = new _Comparers._Configuration(
                            StringComparison.Ordinal, Defaults.Encoding);
                    }

                    if (releases == null)
                    {
                        releases = new Dictionary <Configuration, Release>(
                            comparer);
                    }

                    Configuration releaseConfiguration =
                        Configuration.CreateFrom(release);

                    if (releaseConfiguration == null)
                    {
                        Trace(configuration, String.Format(
                                  "Could not create configuration from parsed " +
                                  "release {0} on line #{1}, using the " +
                                  "pre-existing one...", FormatOps.ForDisplay(
                                      release), lineIndex), TraceCategory);

                        releaseConfiguration = configuration;
                    }

                    releases[releaseConfiguration] = release;
                }
            }

            return((parseCount > 0) ? true : false);
        }
Example #6
0
        ///////////////////////////////////////////////////////////////////////

        public bool VerifyFile(
            Configuration configuration,
            string fileName,
            bool strongName
            )
        {
            try
            {
                if (!File.Exists(fileName))
                {
                    Trace(configuration, String.Format(
                              "File \"{0}\" does not exist.", fileName),
                          TraceCategory);

                    return(false);
                }

                ///////////////////////////////////////////////////////////////

                string error = null;

                if (strongName)
                {
#if NATIVE && WINDOWS
                    if (VersionOps.IsWindowsOperatingSystem() &&
                        !StrongNameEx.IsStrongNameSigned(
                            configuration, fileName, true, ref error))
                    {
                        Trace(configuration, String.Format(
                                  "Assembly in file \"{0}\" is not signed.",
                                  fileName), TraceCategory);

                        Trace(configuration, String.Format(
                                  "Assembly signature error: {0}", error),
                              TraceCategory);

                        return(false);
                    }
#endif

                    ///////////////////////////////////////////////////////////

                    AssemblyName assemblyName =
                        AssemblyName.GetAssemblyName(fileName);

                    if (assemblyName == null)
                    {
                        Trace(configuration, String.Format(
                                  "Assembly in file \"{0}\" has no name.", fileName),
                              TraceCategory);

                        return(false);
                    }

                    byte[] filePublicKeyToken = assemblyName.GetPublicKeyToken();

                    if (!GenericOps <byte> .Equals(
                            filePublicKeyToken, publicKeyToken))
                    {
                        Trace(configuration, String.Format(
                                  "Assembly in file \"{0}\" has incorrect " +
                                  "public key token \"{1}\".", fileName,
                                  FormatOps.ToHexString(filePublicKeyToken)),
                              TraceCategory);

                        return(false);
                    }
                }

                ///////////////////////////////////////////////////////////////

                byte[] hash = null;

                if (FileOps.Hash(
                        configuration, "md5", fileName, ref hash, ref error))
                {
                    if (!GenericOps <byte> .Equals(hash, md5Hash))
                    {
                        Trace(configuration, String.Format(
                                  "File \"{0}\" MD5 hash mismatch, got: {1}.",
                                  fileName, FormatOps.ToHexString(hash)),
                              TraceCategory);

                        return(false);
                    }
                }
                else
                {
                    Trace(configuration, error, TraceCategory);

                    return(false);
                }

                ///////////////////////////////////////////////////////////////

                if (FileOps.Hash(
                        configuration, "sha1", fileName, ref hash, ref error))
                {
                    if (!GenericOps <byte> .Equals(hash, sha1Hash))
                    {
                        Trace(configuration, String.Format(
                                  "File \"{0}\" SHA1 hash mismatch, got: {1}.",
                                  fileName, FormatOps.ToHexString(hash)),
                              TraceCategory);

                        return(false);
                    }
                }
                else
                {
                    Trace(configuration, error, TraceCategory);

                    return(false);
                }

                ///////////////////////////////////////////////////////////////

                if (FileOps.Hash(
                        configuration, "sha512", fileName, ref hash, ref error))
                {
                    if (!GenericOps <byte> .Equals(hash, sha512Hash))
                    {
                        Trace(configuration, String.Format(
                                  "File \"{0}\" SHA512 hash mismatch, got: {1}.",
                                  fileName, FormatOps.ToHexString(hash)),
                              TraceCategory);

                        return(false);
                    }
                }
                else
                {
                    Trace(configuration, error, TraceCategory);

                    return(false);
                }

                return(true);
            }
            catch (Exception e)
            {
                Trace(configuration, e, TraceCategory);
            }

            return(false);
        }
Example #7
0
        ///////////////////////////////////////////////////////////////////////////////////////////////

        private static ReturnCode Combine(
            IList <StringBuilder> list1,     /* in */
            IList <StringBuilder> list2,     /* in */
            ref IList <StringBuilder> list3, /* in, out */
            ref Result error                 /* out */
            )
        {
            if (list1 == null)
            {
                if (list2 == null)
                {
                    error = "cannot combine, neither list is valid";
                    return(ReturnCode.Error);
                }

                if (list3 != null)
                {
                    GenericOps <StringBuilder> .AddRange(list3, list2);
                }
                else
                {
                    list3 = new List <StringBuilder>(list2);
                }
            }
            else if (list2 == null)
            {
                if (list3 != null)
                {
                    GenericOps <StringBuilder> .AddRange(list3, list1);
                }
                else
                {
                    list3 = new List <StringBuilder>(list1);
                }
            }
            else
            {
                if ((list1.Count > 0) || (list2.Count > 0))
                {
                    if (list3 == null)
                    {
                        list3 = new List <StringBuilder>();
                    }
                }

                foreach (StringBuilder element1 in list1)
                {
                    foreach (StringBuilder element2 in list2)
                    {
                        int capacity = 0;

                        if (element1 != null)
                        {
                            capacity += element1.Length;
                        }

                        if (element2 != null)
                        {
                            capacity += element2.Length;
                        }

                        StringBuilder element3 = StringOps.NewStringBuilder(
                            capacity);

                        element3.Append(element1);
                        element3.Append(element2);

                        list3.Add(element3);
                    }
                }
            }

            return(ReturnCode.Ok);
        }
Example #8
0
        ///////////////////////////////////////////////////////////////////////

        public static bool IsStrongNameSigned(
            Assembly assembly,
            ref byte[] publicKeyToken,
            ref string error
            )
        {
            if (assembly == null)
            {
                error = "assembly is invalid";
                return(false);
            }

            AssemblyName assemblyName = assembly.GetName();

            if (assemblyName == null)
            {
                error = "assembly has invalid name";
                return(false);
            }

            byte[] publicKey = assemblyName.GetPublicKey();

            if (publicKey == null)
            {
                error = "assembly has invalid public key";
                return(false);
            }

            Evidence evidence = assembly.Evidence;

            if (evidence == null)
            {
                error = "assembly has invalid evidence";
                return(false);
            }

            IEnumerator enumerator = evidence.GetHostEnumerator();

            if (enumerator == null)
            {
                error = "assembly has invalid evidence enumerator";
                return(false);
            }

            while (enumerator.MoveNext())
            {
                StrongName strongName = enumerator.Current as StrongName;

                if (strongName == null)
                {
                    continue;
                }

                StrongNamePublicKeyBlob strongNamePublicKey =
                    strongName.PublicKey;

                if (strongNamePublicKey == null)
                {
                    error = "assembly strong name has invalid public key";
                    return(false);
                }

                if (GenericOps <byte> .Equals(ParseOps.HexString(
                                                  strongNamePublicKey.ToString()), publicKey))
                {
                    publicKeyToken = assemblyName.GetPublicKeyToken();

                    if (publicKeyToken == null)
                    {
                        error = "assembly has invalid public key token";
                        return(false);
                    }

                    return(true);
                }
            }

            error = "assembly is not signed";
            return(false);
        }