Example #1
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;
            }
        }
Example #2
0
        static void RunCommand(string[] commands)
        {
            if (commands.Length == 0)
            {
                return;
            }


            try {
                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
                }
            }
            catch (Exception e) {
                Console.WriteLine("Exception processing command");
                Console.WriteLine(e);
            }
        }