Exemple #1
0
        public static void SetScope(string[] cmds)
        {
            if (cmds.Length != 2)
            {
                Console.WriteLine("Incorrect number of parameters");
                return;
            }

            CBORObject obj = CBORDiagnostics.Parse(cmds[1]);

            if (obj == null)
            {
                Console.WriteLine("Not a legal CBOR Diagnostic value");
                return;
            }

            if (obj.Type == CBORType.ByteString)
            {
                UseScopeValue = obj;
            }
            else if (obj.Type == CBORType.TextString)
            {
                UseScopeValue = obj;
            }
            else if (obj.Type == CBORType.SimpleValue && obj.IsNull)
            {
                UseScopeValue = null;
            }
            else
            {
                Console.WriteLine("CBOR type parsed to something  not useful");
            }
        }
Exemple #2
0
        private static void AddOscoreKey(string[] cmds)
        {
            if (cmds.Length != 3)
            {
                Console.WriteLine("Incorrect number of arguments: " + cmds.Length);
                return;
            }

            CBORObject cbor = CBORDiagnostics.Parse(cmds[2]);

            byte[] salt = null;
            if (cbor.ContainsKey(CBORObject.FromObject(6)))
            {
                salt = cbor[CBORObject.FromObject(6)].GetByteString();
            }

            byte[] contextId = null;
            if (cbor.ContainsKey(CBORObject.FromObject(7)))
            {
                contextId = cbor[CBORObject.FromObject(7)].GetByteString();
            }

            SecurityContext ctx = SecurityContext.DeriveContext(
                cbor[CBORObject.FromObject(1)].GetByteString(),
                contextId,
                cbor[CBORObject.FromObject(2)].GetByteString(),
                cbor[CBORObject.FromObject(3)].GetByteString(), salt,
                null /*cbor[CoseKeyKeys.Algorithm]*/);

            Program._OscoreKeys.Add(cmds[1], ctx);
        }
Exemple #3
0
        static void AddCwtRoot(string[] cmds)
        {
            if (cmds.Length != 2)
            {
                Console.WriteLine("wrong number of parameters");
                return;
            }
            CBORObject cbor = CBORDiagnostics.Parse(cmds[1]);

            CwtRootKeys.AddKey(new OneKey(cbor));
        }
Exemple #4
0
        static void RunSetPayload(string[] cmds)
        {
            if (cmds.Length != 2)
            {
                Console.WriteLine("Incorrect command");
                return;
            }

            CBORObject cbor = CBORDiagnostics.Parse(cmds[1]);

            Body = cbor.EncodeToBytes();
        }
Exemple #5
0
        private static void AddTlsKey(string[] commands)
        {
            if (commands.Length != 3)
            {
                Console.WriteLine("Incorrect number of arguments: " + commands.Length);
                return;
            }

            CBORObject cbor = CBORDiagnostics.Parse(commands[2]);
            OneKey     key  = new OneKey(cbor);

            _TlsKeys.Add(commands[1], key);
        }
Exemple #6
0
        private static void AddTlsCert(string[] commands)
        {
            if (commands.Length != 4)
            {
                Console.WriteLine($"Incorrect number of arguments: {commands.Length}");
                return;
            }

            byte[]          bytes = Hex.Decode(commands[2]);
            X509Certificate cert  = new X509CertificateParser().ReadCertificate(bytes);
            CBORObject      cbor  = CBORDiagnostics.Parse(commands[3]);

            _TlsKeys.Add(commands[1], new TlsKeyPair(bytes, new OneKey(cbor)));
        }
Exemple #7
0
        private static void AddTlsCwt(string[] commands)
        {
            if (commands.Length != 4)
            {
                Console.Write($"Incorrect number of arguments: {commands.Length}");
                return;
            }

            CBORObject cbor = CBORDiagnostics.Parse(commands[2]);
            CWT        cwt  = CWT.Decode(cbor.EncodeToBytes(), CwtRootKeys, CwtRootKeys);

            cbor = CBORDiagnostics.Parse(commands[3]);
            _TlsKeys.Add(commands[1], new TlsKeyPair(cwt, new OneKey(cbor)));
        }
Exemple #8
0
        /// <summary>
        /// What the CBOR structure needs to look like:
        ///
        /// </summary>
        /// <param name="cmds"></param>
        private static void AddGroupOscoreKey(string[] cmds)
        {
            if (cmds.Length != 3)
            {
                Console.WriteLine("Incorrect number of arguments: " + cmds.Length);
                return;
            }

            CBORObject cbor = CBORDiagnostics.Parse(cmds[2]);

            byte[] salt = null;
            if (cbor.ContainsKey(CoseKeyKeys.slt))
            {
                salt = cbor[CoseKeyKeys.slt].GetByteString();
            }

            SecurityContext ctx = SecurityContext.DeriveGroupContext(cbor[CoseKeyParameterKeys.Octet_k].GetByteString(),
                                                                     cbor[CBORObject.FromObject("GroupID")].GetByteString(),
                                                                     cbor[CBORObject.FromObject("sender")][CBORObject.FromObject("ID")].GetByteString(),
                                                                     cbor["sender"]["sign"][CoseKeyKeys.Algorithm],
                                                                     new OneKey(cbor["sender"]["sign"]),
                                                                     null, null, salt, cbor[CoseKeyKeys.Algorithm]);

            ctx.CountersignParams    = cbor["ParCS"];
            ctx.CountersignKeyParams = cbor["ParCSKey"];

            foreach (CBORObject recipient in cbor[CBORObject.FromObject("recipients")].Values)
            {
                OneKey signKey = null;
                if (recipient.ContainsKey("sign"))
                {
                    signKey = new OneKey(recipient["sign"]);
                }

                ctx.AddRecipient(recipient[CBORObject.FromObject("ID")].GetByteString(), signKey);
            }

            ctx.Locate = (context, kid) => {
                Console.WriteLine("Looking for a kid with a value of " + ByteArrayUtils.ToHexString(kid));
                return(null);
            };

            Program._OscoreKeys.Add(cmds[1], ctx);
        }
Exemple #9
0
        static void ETagCommands(string[] cmds)
        {
            if (cmds.Length == 1)
            {
                Console.WriteLine("Incorrect number of arguments");
                return;
            }

            switch (cmds[1].ToLower())
            {
            case "add":
                try {
                    CBORObject o = CBORDiagnostics.Parse(cmds[2]);
                    ETagList.Add(o.GetByteString());
                }
                catch {
                    Console.WriteLine("Error in command");
                }
                break;

            case "all":
                ETagOption = 2;
                break;

            case "clear":
                ETagList.Clear();
                break;

            case "last":
                ETagOption = 1;
                break;

            case "none":
                ETagOption = 0;
                break;

            default:
                Console.WriteLine("Unknown option");
                break;
            }
        }
Exemple #10
0
        public static CBORObject Parse(string input)
        {
            CBORDiagnostics diag = new CBORDiagnostics(input);

            return(diag.ParseToCBOR());
        }
Exemple #11
0
        static void RunCommand(string[] commands)
        {
            if (commands.Length == 0)
            {
                return;
            }



            switch (commands[0].ToUpper())
            {
            default:
                _dispatchTable.Execute(commands);
                break;


            case "SCRIPT":
                TextReader x = new StreamReader(commands[1]);
                RunScript(x);
                x.Dispose();
                break;



            case "COMMENT":
                break;

            case "EXIT":
                Environment.Exit(0);
                break;

            case "PAUSE":
                Console.ReadLine();
                break;

            case "TIMEOUT":
                break;

            case "LOG-LEVEL":
                if (commands.Length != 2)
                {
                    Console.WriteLine("Incorrect number of args");
                    return;
                }
                switch (commands[1].ToUpper())
                {
                case "INFO":
                    LogManager.Level = LogLevel.Info;
                    break;

                case "NONE":
                    LogManager.Level = LogLevel.None;
                    break;

                case "FATAL":
                    LogManager.Level = LogLevel.Fatal;
                    break;

                default:
                    Console.WriteLine("Unknown level");
                    break;
                }
                break;

            case "LOG-TO":
                break;

            case "OPTION":
                OptionType typ = GetOptionType(commands[1]);
                switch (typ)
                {
                case OptionType.ContentFormat:
                case OptionType.Accept:
                    if (commands.Length == 2)
                    {
                        _Options.Add(Option.Create(typ));
                    }
                    else
                    {
                        for (int i = 2; i < commands.Length; i++)
                        {
                            int val = MediaType.ApplicationLinkFormat;
                            if (int.TryParse(commands[i], out val))
                            {
                                _Options.Add(Option.Create(typ, val));
                            }
                            else
                            {
                                Console.WriteLine($"Bad option value '{commands[i]}'");
                            }
                        }
                    }
                    break;

                case OptionType.Unknown:
                    Console.WriteLine("Unrecognized type string");
                    return;

                default:
                    if (commands.Length == 2)
                    {
                        _Options.Add(Option.Create(typ));
                    }
                    else
                    {
                        for (int i = 2; i < commands.Length; i++)
                        {
                            _Options.Add(Option.Create(typ, commands[i]));
                        }
                    }
                    break;
                }
                break;

            case "CLEAR-OPTION":
                if (commands.Length == 1)
                {
                    _Options.Clear();
                    return;
                }
                typ = GetOptionType(commands[1]);
                List <Option> del = new List <Option>();
                foreach (Option op in _Options)
                {
                    if (op.Type == typ)
                    {
                        del.Add(op);
                    }
                }
                foreach (Option op in del)
                {
                    _Options.Remove(op);
                }
                break;

            case "BODY":
                if (commands.Length == 1)
                {
                    break;
                }
                byte[] b = File.ReadAllBytes(commands[1]);
                Body = b;
                break;



#if false
            case "EDHOC":
                RunEdhoc(commands);
                break;
#endif

            case "ADD-OSCOAP":
                if (commands.Length != 3)
                {
                    Console.WriteLine("Incorrect number of arguments: " + commands.Length);
                    return;
                }

                CBORObject      cbor = CBORDiagnostics.Parse(commands[2]);
                SecurityContext ctx  = SecurityContext.DeriveContext(
                    cbor[CoseKeyParameterKeys.Octet_k].GetByteString(),
                    cbor[CBORObject.FromObject("RecipID")].GetByteString(),
                    cbor[CBORObject.FromObject("SenderID")].GetByteString(), null,
                    cbor[CoseKeyKeys.Algorithm]);

                _OscopKeys.Add(commands[1], ctx);

                break;

#if DEV_VERSION
            case "ADD-OSCOAP-GROUP":
                if (commands.Length != 3)
                {
                    Console.WriteLine("Incorrect number of arguments: " + commands.Length);
                    return;
                }
                cbor = CBORDiagnostics.Parse(commands[2]);
                ctx  = SecurityContext.DeriveGroupContext(cbor[CoseKeyParameterKeys.Octet_k].GetByteString(), cbor[CoseKeyKeys.KeyIdentifier].GetByteString(),
                                                          cbor[CBORObject.FromObject("sender")][CBORObject.FromObject("ID")].GetByteString(), null, null, cbor[CoseKeyKeys.Algorithm]);
                ctx.Sender.SigningKey = new OneKey(cbor["sender"]["sign"]);
                foreach (CBORObject recipient in cbor[CBORObject.FromObject("recipients")].Values)
                {
                    ctx.AddRecipient(recipient[CBORObject.FromObject("ID")].GetByteString(), new OneKey(recipient["sign"]));
                }

                _OscopKeys.Add(commands[1], ctx);
                break;
#endif

            case "USE-OSCOAP":
                if (commands.Length != 2)
                {
                    Console.WriteLine("Incorrect number of arguments: " + commands.Length);
                    return;
                }

                if (commands[1] == "NONE")
                {
                    _CurrentOscoap = null;
                    return;
                }

                if (!_OscopKeys.ContainsKey(commands[1]))
                {
                    Console.WriteLine($"OSCOAP Key {commands[1]} is not defined");
                    return;
                }

                _CurrentOscoap = _OscopKeys[commands[1]];
                break;

            case "OSCOAP-TEST":
                OscoapTests.RunTest(Int32.Parse(commands[1]));
                break;

            case "OSCOAP-PIV":
                _CurrentOscoap.Sender.SequenceNumber = Int32.Parse(commands[1]);
                break;

            case "EDHOC-ADD-SERVER-KEY":
                if (commands.Length != 2)
                {
                    Console.WriteLine("Incorrect number of arguments: " + commands.Length);
                    return;
                }

                cbor = CBORDiagnostics.Parse(commands[2]);
                _EdhocServerKeys.AddKey(new OneKey(cbor));
                break;

            case "EDHOC-ADD-USER-KEY":
                if (commands.Length != 3)
                {
                    Console.WriteLine("Incorrect number of arguments: " + commands.Length);
                    return;
                }

                cbor = CBORDiagnostics.Parse(commands[2]);
                _EdhocValidateKeys.Add(commands[1], new OneKey(cbor));
                break;
            }
        }