internal static void SetValue(this KeyBagEntry item, string blockIdentifier, ReadOnlySpan <byte> value)
        {
            switch (blockIdentifier)
            {
            case KeyBagConstants.UuidTag:
                item.Uuid = KeyBagExtensions.ReadGuidBigEndian(value);
                break;

            case KeyBagConstants.ClassTag:
                item.ProtectionClass = (ProtectionClass)BinaryPrimitives.ReadInt32BigEndian(value);
                break;

            case KeyBagConstants.WrapTag:
                item.Wrap = (KeyWrapTypes)BinaryPrimitives.ReadInt32BigEndian(value);
                break;

            case KeyBagConstants.KeyTypeTag:
                item.KeyType = (KeyType)BinaryPrimitives.ReadInt32BigEndian(value);
                break;

            case KeyBagConstants.WrappedKeyTag:
                item.Wpky = value.ToArray();
                break;

            default:
                throw new InvalidDataException($"Unexpected block identifier \"{blockIdentifier}\"");
            }
        }
Beispiel #2
0
 public static void ConsoleWrite(this KeyBagEntry item)
 {
     Console.WriteLine($"UUID={item.Uuid}");
     Console.WriteLine($"CLAS={item.ProtectionClass}");
     Console.WriteLine($"WRAP={item.Wrap}");
     Console.WriteLine($"KTYP={item.KeyType}");
     Console.Write($"WPKY={CommonHelpers.ByteArrayToDebugString(item.Wpky)}");
 }
Beispiel #3
0
        private static byte[] UnwrapClassKey(KeyBagEntry item, byte[] kek)
        {
            byte[] result = default;

            if (item.Wpky != null)
            {
                if ((item.Wrap & KeyWrapTypes.Passcode) == KeyWrapTypes.Passcode)
                {
                    result = KeyWrapAlgorithm.UnwrapKey(kek, item.Wpky);
                }
            }

            return(result);
        }
Beispiel #4
0
        public static KeyBag Read(byte[] data)
        {
            if (data is null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            KeyBag result = default;

            var         state      = ReaderState.Invalid;
            KeyBagEntry currentKey = default;
            var         keys       = new List <KeyBagEntry>();
            var         length     = data.Length;
            var         position   = 0;

            while (position < length)
            {
                var blockIdentifier = Encoding.ASCII.GetString(data, position, 4);
                position += 4;

                var blockLength = BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(position, 4));
                position += 4;

                var value = data.AsSpan(position, blockLength);

                if (state == ReaderState.Invalid)
                {
                    state = ReaderState.ReadHeader;
                }
                if (state == ReaderState.ReadHeader)
                {
                    if (result is null)
                    {
                        result = new KeyBag();
                    }
                    if (blockIdentifier == KeyBagConstants.UuidTag && result.Uuid != Guid.Empty)
                    {
                        state = ReaderState.ReadKeyBagEntry;
                    }
                    else
                    {
                        result.SetValue(blockIdentifier, value);
                    }
                }
                if (state == ReaderState.ReadKeyBagEntry)
                {
                    if (blockIdentifier == KeyBagConstants.UuidTag)
                    {
                        if (currentKey != null)
                        {
                            keys.Add(currentKey);
                        }
                        currentKey = new KeyBagEntry();
                    }

                    currentKey.SetValue(blockIdentifier, value);
                }
                position += blockLength;
            }
            if (result != null)
            {
                if (currentKey != null)
                {
                    keys.Add(currentKey);
                }
                result.WrappedKeys = keys.ToArray();
            }

            return(result);
        }