public static bool LoadSignKey(string desiredKeyName, byte[] keyData, out bool isNewKey, out string keyName) { isNewKey = false; byte[] sha256Hash = HashUtils.ComputeSha256(keyData); bool hashMatches = KnowsSignKeySHA256(sha256Hash, out keyName); if (hashMatches && SignkeyStorage[keyName].HasKeyData) { // Duplicate key, already loaded Console.WriteLine($"SignKey {keyName} is already loaded"); return(false); } if (hashMatches) { SignkeyStorage[keyName].SetKey(keyData); return(true); } // New key, using user-set keyname isNewKey = true; SignkeyStorage[desiredKeyName] = new DurangoKeyEntry(KeyType.Odk, keyData); return(true); }
public static int LoadCikKeys(byte[] keyData, out Guid[] loadedKeys) { int foundCount = 0; if (keyData.Length < 0x30 || keyData.Length % 0x30 != 0) { throw new Exception("Misaligned CIK, expecting array of 0x30 bytes: 0x10 bytes: GUID, 0x20 bytes: Key"); } int cikKeyCount = keyData.Length / 0x30; loadedKeys = new Guid[cikKeyCount]; using (BinaryReader br = new BinaryReader(new MemoryStream(keyData))) { for (int keyIndex = 0; keyIndex < cikKeyCount; keyIndex++) { var guid = new Guid(br.ReadBytes(0x10)); var cikKeyData = br.ReadBytes(0x20); var sha256Hash = HashUtils.ComputeSha256(cikKeyData); bool hashMatches = KnowsCikSHA256(sha256Hash, out Guid verifyGuid); var hashString = sha256Hash.ToHexString(""); if (hashMatches && verifyGuid != guid) { Console.WriteLine($"CIK {guid} with hash {hashString} is known as {verifyGuid}"); continue; } if (hashMatches && CikStorage[guid].HasKeyData) { // Duplicate key, already loaded Console.WriteLine($"CIK {guid} is already loaded"); continue; } CikStorage[guid] = new DurangoKeyEntry(KeyType.Cik, cikKeyData); foundCount++; } } return(foundCount); }
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); }