Exemplo n.º 1
0
        public static bool KnowsOdkSHA256(byte[] sha256Hash, out OdkIndex keyId)
        {
            foreach (var kvp in OdkStorage)
            {
                if (!kvp.Value.SHA256Hash.IsEqualTo(sha256Hash))
                {
                    continue;
                }

                keyId = kvp.Key;
                return(true);
            }
            keyId = OdkIndex.Invalid;
            return(false);
        }
Exemplo n.º 2
0
        public static bool GetOdkIndexFromString(string name, out OdkIndex odkIndex)
        {
            // First, try to convert to know Enum values
            var success = Enum.TryParse(name, true, out odkIndex);

            if (success)
            {
                return(true);
            }

            odkIndex = OdkIndex.Invalid;
            success  = UInt32.TryParse(name, out uint odkUint);
            if (success)
            {
                // Odk Id is valid uint but we don't know its Enum name yet
                odkIndex = (OdkIndex)odkUint;
            }

            return(success);
        }
Exemplo n.º 3
0
        public static bool LoadOdkKey(OdkIndex keyId, byte[] keyData, out bool isNewKey)
        {
            isNewKey = false;
            byte[]          sha256Hash  = HashUtils.ComputeSha256(keyData);
            DurangoKeyEntry existingKey = GetOdkById(keyId);

            if (existingKey != null)
            {
                bool hashMatches = KnowsOdkSHA256(sha256Hash, out OdkIndex verifyKeyId);
                if (hashMatches && verifyKeyId != keyId)
                {
                    var hashString = sha256Hash.ToHexString("");
                    Console.WriteLine($"ODK {keyId} with hash {hashString} is known as ODK {verifyKeyId}");
                    return(false);
                }

                if (hashMatches && OdkStorage[keyId].HasKeyData)
                {
                    // Duplicate key, already loaded
                    Console.WriteLine($"ODK {keyId} is already loaded");
                    return(false);
                }

                if (!hashMatches)
                {
                    Console.WriteLine($"ODK {keyId} does not match expected hash");
                    return(false);
                }

                OdkStorage[keyId].SetKey(keyData);
                return(true);
            }

            isNewKey          = true;
            OdkStorage[keyId] = new DurangoKeyEntry(KeyType.Odk, keyData);
            return(true);
        }
Exemplo n.º 4
0
        public static bool IsOdkLoaded(OdkIndex keyId)
        {
            var key = GetOdkById(keyId);

            return(key != null && key.HasKeyData);
        }
Exemplo n.º 5
0
 public static DurangoKeyEntry GetOdkById(OdkIndex keyId)
 {
     return(OdkStorage.ContainsKey(keyId) ? OdkStorage[keyId] : null);
 }
Exemplo n.º 6
0
        public int PullKeysFromFile()
        {
            var exeData = File.ReadAllBytes(FilePath);

            int foundCount = 0;

            for (int i = 0; i < exeData.Length - 32; i++)
            {
                byte[]          hash32 = HashUtils.ComputeSha256(exeData, i, 32);
                DurangoKeyEntry keyEntry;
                foreach (var kvp in DurangoKeys.GetAllXvdSigningKeys())
                {
                    string keyName = kvp.Key;
                    keyEntry = kvp.Value;

                    if (keyEntry.HasKeyData)
                    {
                        continue;
                    }
                    if (keyEntry.DataSize > exeData.Length - i)
                    {
                        continue;
                    }

                    byte[] signKeyHash = HashUtils.ComputeSha256(exeData, i, keyEntry.DataSize);

                    if (!keyEntry.SHA256Hash.IsEqualTo(signKeyHash))
                    {
                        continue;
                    }

                    Console.WriteLine($"Found {keyEntry.KeyType} \"{keyName}\" at offset 0x{i:X}");

                    byte[] keyData = new byte[keyEntry.DataSize];
                    Array.Copy(exeData, i, keyData, 0, keyData.Length);

                    DurangoKeys.LoadSignKey(keyName, keyData, out bool newKey, out keyName);
                    foundCount++;
                }

                foreach (var kvp in DurangoKeys.GetAllODK())
                {
                    OdkIndex keyId = kvp.Key;
                    keyEntry = kvp.Value;

                    if (keyEntry.HasKeyData)
                    {
                        continue;
                    }

                    if (!hash32.IsEqualTo(keyEntry.SHA256Hash))
                    {
                        continue;
                    }

                    Console.WriteLine($"Found {keyEntry.KeyType} \"{keyId}\" at offset 0x{i:X}");

                    byte[] keyData = new byte[keyEntry.DataSize];
                    Array.Copy(exeData, i, keyData, 0, keyData.Length);

                    DurangoKeys.LoadOdkKey(keyId, keyData, out bool newKey);
                    foundCount++;
                }

                foreach (var kvp in DurangoKeys.GetAllCIK())
                {
                    Guid keyId = kvp.Key;
                    keyEntry = kvp.Value;

                    if (keyEntry.HasKeyData)
                    {
                        continue;
                    }

                    if (!hash32.IsEqualTo(keyEntry.SHA256Hash))
                    {
                        continue;
                    }

                    Console.WriteLine($"Found {keyEntry.KeyType} \"{keyId}\" at offset 0x{i:X}");

                    byte[] keyData = new byte[0x10 + keyEntry.DataSize];
                    Array.Copy(keyId.ToByteArray(), 0, keyData, 0, 0x10);
                    Array.Copy(exeData, i, keyData, 0x10, keyEntry.DataSize);

                    DurangoKeys.LoadCikKeys(keyData, out Guid[] keyGuid);
                    foundCount++;
                }
            }

            return(foundCount);
        }