Exemplo n.º 1
0
        /* TIA 102.AACD-A 3.8.1 */
        public void Keyload(List <CmdKeyItem> keyItems)
        {
            List <List <CmdKeyItem> > keyGroups = KeyPartitioner.PartitionKeys(keyItems);

            Begin();

            try
            {
                InventoryCommandListActiveKsetIds cmdKmmBody1 = new InventoryCommandListActiveKsetIds();

                KmmBody rspKmmBody1 = TxRxKmm(cmdKmmBody1);

                int activeKeysetId = 0;

                if (rspKmmBody1 is InventoryResponseListActiveKsetIds)
                {
                    InventoryResponseListActiveKsetIds kmm = rspKmmBody1 as InventoryResponseListActiveKsetIds;

                    Logger.Debug("number of active keyset ids: {0}", kmm.KsetIds.Count);

                    for (int i = 0; i < kmm.KsetIds.Count; i++)
                    {
                        Logger.Debug("* keyset id index {0} *", i);
                        Logger.Debug("keyset id: {0} (dec), {0:X} (hex)", kmm.KsetIds[i]);
                    }

                    // TODO support more than one crypto group
                    if (kmm.KsetIds.Count > 0)
                    {
                        activeKeysetId = kmm.KsetIds[0];
                    }
                    else
                    {
                        activeKeysetId = 1; // to match KVL3000+ R3.53.03 behavior
                    }
                }
                else if (rspKmmBody1 is NegativeAcknowledgment)
                {
                    NegativeAcknowledgment kmm = rspKmmBody1 as NegativeAcknowledgment;

                    string statusDescr  = OperationStatusExtensions.ToStatusString(kmm.Status);
                    string statusReason = OperationStatusExtensions.ToReasonString(kmm.Status);
                    throw new Exception(string.Format("received negative acknowledgment{0}status: {1} (0x{2:X2}){0}{3}", Environment.NewLine, statusDescr, kmm.Status, statusReason));
                }
                else
                {
                    throw new Exception("unexpected kmm");
                }

                foreach (List <CmdKeyItem> keyGroup in keyGroups)
                {
                    ModifyKeyCommand modifyKeyCommand = new ModifyKeyCommand();

                    // TODO support more than one crypto group
                    if (keyGroup[0].UseActiveKeyset && !keyGroup[0].IsKek)
                    {
                        modifyKeyCommand.KeysetId = activeKeysetId;
                    }
                    else if (keyGroup[0].UseActiveKeyset && keyGroup[0].IsKek)
                    {
                        modifyKeyCommand.KeysetId = 0xFF; // to match KVL3000+ R3.53.03 behavior
                    }
                    else
                    {
                        modifyKeyCommand.KeysetId = keyGroup[0].KeysetId;
                    }

                    modifyKeyCommand.AlgorithmId = keyGroup[0].AlgorithmId;

                    foreach (CmdKeyItem key in keyGroup)
                    {
                        Logger.Debug(key.ToString());

                        KeyItem keyItem = new KeyItem();
                        keyItem.SLN   = key.Sln;
                        keyItem.KeyId = key.KeyId;
                        keyItem.Key   = key.Key.ToArray();
                        keyItem.KEK   = key.IsKek;
                        keyItem.Erase = false;

                        modifyKeyCommand.KeyItems.Add(keyItem);
                    }

                    KmmBody rspKmmBody2 = TxRxKmm(modifyKeyCommand);

                    if (rspKmmBody2 is RekeyAcknowledgment)
                    {
                        RekeyAcknowledgment kmm = rspKmmBody2 as RekeyAcknowledgment;

                        Logger.Debug("number of key status: {0}", kmm.Keys.Count);

                        for (int i = 0; i < kmm.Keys.Count; i++)
                        {
                            KeyStatus status = kmm.Keys[i];

                            Logger.Debug("* key status index {0} *", i);
                            Logger.Debug("algorithm id: {0} (dec), {0:X} (hex)", status.AlgorithmId);
                            Logger.Debug("key id: {0} (dec), {0:X} (hex)", status.KeyId);
                            Logger.Debug("status: {0} (dec), {0:X} (hex)", status.Status);

                            if (status.Status != 0)
                            {
                                string statusDescr  = OperationStatusExtensions.ToStatusString((OperationStatus)status.Status);
                                string statusReason = OperationStatusExtensions.ToReasonString((OperationStatus)status.Status);
                                throw new Exception(string.Format("received unexpected key status{0}status: {1} (0x{2:X2}){0}{3}", Environment.NewLine, statusDescr, status.Status, statusReason));
                            }
                        }
                    }
                    else if (rspKmmBody2 is NegativeAcknowledgment)
                    {
                        NegativeAcknowledgment kmm = rspKmmBody2 as NegativeAcknowledgment;

                        string statusDescr  = OperationStatusExtensions.ToStatusString(kmm.Status);
                        string statusReason = OperationStatusExtensions.ToReasonString(kmm.Status);
                        throw new Exception(string.Format("received negative acknowledgment{0}status: {1} (0x{2:X2}){0}{3}", Environment.NewLine, statusDescr, kmm.Status, statusReason));
                    }
                    else
                    {
                        throw new Exception("received unexpected kmm");
                    }
                }
            }
            catch
            {
                End();

                throw;
            }

            End();
        }
Exemplo n.º 2
0
        /* TIA 102.AACD-A 3.8.1 */
        public void Keyload(bool useActiveKeyset, int keysetId, int sln, int keyId, int algId, List <byte> key)
        {
            Begin();

            InventoryCommandListActiveKsetIds cmdKmmBody1 = new InventoryCommandListActiveKsetIds();

            KmmBody rspKmmBody1 = TxRxKmm(cmdKmmBody1);

            int ksid = 0;

            if (rspKmmBody1 is InventoryResponseListActiveKsetIds)
            {
                InventoryResponseListActiveKsetIds kmm = rspKmmBody1 as InventoryResponseListActiveKsetIds;

                Logger.Debug("number of active keyset ids: {0}", kmm.KsetIds.Count);

                for (int i = 0; i < kmm.KsetIds.Count; i++)
                {
                    Logger.Debug("* keyset id index {0} *", i);
                    Logger.Debug("keyset id: {0} (dec), {0:X} (hex)", kmm.KsetIds[i]);
                }

                if (!useActiveKeyset)
                {
                    ksid = keysetId;
                }
                else if (useActiveKeyset && kmm.KsetIds.Count > 0)
                {
                    ksid = kmm.KsetIds[0];
                }
                else
                {
                    throw new Exception("no active keyset");
                }
            }
            else
            {
                throw new Exception("unexpected kmm");
            }

            // TODO support more than one key per keyload operation

            KeyItem keyItem = new KeyItem();

            keyItem.SLN   = sln;
            keyItem.KeyId = keyId;
            keyItem.Key   = key.ToArray();
            keyItem.Erase = false;

            ModifyKeyCommand modifyKeyCommand = new ModifyKeyCommand();

            modifyKeyCommand.KeysetId = ksid;

            modifyKeyCommand.AlgorithmId = algId;
            modifyKeyCommand.KeyItems.Add(keyItem);

            KmmBody rspKmmBody2 = TxRxKmm(modifyKeyCommand);

            if (rspKmmBody2 is RekeyAcknowledgment)
            {
                RekeyAcknowledgment kmm = rspKmmBody2 as RekeyAcknowledgment;

                Logger.Debug("number of key status: {0}", kmm.Keys.Count);

                for (int i = 0; i < kmm.Keys.Count; i++)
                {
                    KeyStatus status = kmm.Keys[i];

                    Logger.Debug("* key status index {0} *", i);
                    Logger.Debug("algorithm id: {0} (dec), {0:X} (hex)", status.AlgorithmId);
                    Logger.Debug("key id: {0} (dec), {0:X} (hex)", status.KeyId);
                    Logger.Debug("status: {0} (dec), {0:X} (hex)", status.Status);

                    if (status.Status != 0)
                    {
                        throw new Exception("unexpected status");
                    }
                }
            }
            else
            {
                throw new Exception("unexpected kmm");
            }

            End();
        }
        /* TIA 102.AACD-A 3.8.5 */
        public void EraseKeys(bool useActiveKeyset, int keysetId, int sln)
        {
            Begin();

            InventoryCommandListActiveKsetIds cmdKmmBody1 = new InventoryCommandListActiveKsetIds();

            KmmBody rspKmmBody1 = TxRxKmm(cmdKmmBody1);

            int ksid = 0;

            if (rspKmmBody1 is InventoryResponseListActiveKsetIds)
            {
                InventoryResponseListActiveKsetIds kmm = rspKmmBody1 as InventoryResponseListActiveKsetIds;

                Logger.Debug("number of active keyset ids: {0}", kmm.KsetIds.Count);

                for (int i = 0; i < kmm.KsetIds.Count; i++)
                {
                    Logger.Debug("* keyset id index {0} *", i);
                    Logger.Debug("keyset id: {0} (dec), {0:X} (hex)", kmm.KsetIds[i]);
                }

                if (!useActiveKeyset)
                {
                    ksid = keysetId;
                }
                else if (useActiveKeyset && kmm.KsetIds.Count > 0)
                {
                    ksid = kmm.KsetIds[0];
                }
                else
                {
                    ksid = 1; // to match KVL3000+ R3.53.03 behavior
                }
            }
            else if (rspKmmBody1 is NegativeAcknowledgment)
            {
                NegativeAcknowledgment kmm = rspKmmBody1 as NegativeAcknowledgment;

                throw new Exception(string.Format("recieved negative acknowledgment, status {0} (0x{1:X2})", kmm.Status.ToString(), (byte)kmm.Status));
            }
            else
            {
                throw new Exception("unexpected kmm");
            }

            // TODO support more than one key per erase operation

            KeyItem keyItem = new KeyItem();

            keyItem.SLN   = sln;
            keyItem.KeyId = 65535;                                                         // to match KVL3000+ R3.53.03 behavior
            keyItem.Key   = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; // to match KVL3000+ R3.53.03 behavior
            keyItem.Erase = true;

            ModifyKeyCommand modifyKeyCommand = new ModifyKeyCommand();

            modifyKeyCommand.KeysetId = ksid;

            modifyKeyCommand.AlgorithmId = 0x81; // to match KVL3000+ R3.53.03 behavior
            modifyKeyCommand.KeyItems.Add(keyItem);

            KmmBody rspKmmBody2 = TxRxKmm(modifyKeyCommand);

            if (rspKmmBody2 is RekeyAcknowledgment)
            {
                RekeyAcknowledgment kmm = rspKmmBody2 as RekeyAcknowledgment;

                Logger.Debug("number of key status: {0}", kmm.Keys.Count);

                for (int i = 0; i < kmm.Keys.Count; i++)
                {
                    KeyStatus status = kmm.Keys[i];

                    Logger.Debug("* key status index {0} *", i);
                    Logger.Debug("algorithm id: {0} (dec), {0:X} (hex)", status.AlgorithmId);
                    Logger.Debug("key id: {0} (dec), {0:X} (hex)", status.KeyId);
                    Logger.Debug("status: {0} (dec), {0:X} (hex)", status.Status);

                    if (status.Status != 0)
                    {
                        throw new Exception("unexpected status");
                    }
                }
            }
            else if (rspKmmBody2 is NegativeAcknowledgment)
            {
                NegativeAcknowledgment kmm = rspKmmBody2 as NegativeAcknowledgment;

                throw new Exception(string.Format("recieved negative acknowledgment, status {0} (0x{1:X2})", kmm.Status.ToString(), (byte)kmm.Status));
            }
            else
            {
                throw new Exception("unexpected kmm");
            }

            End();
        }