private void CreateServer() { CoAPEndPoint endpoint = new CoAPEndPoint(0); _server = new CoapServer(); // _resource = new StorageResource(TARGET, CONTENT_1); // _server.Add(_resource); Resource r2 = new EchoLocation("abc"); _server.Add(r2); r2.Add(new EchoLocation("def")); _server.AddEndPoint(endpoint); _server.Start(); _serverPort = ((System.Net.IPEndPoint)endpoint.LocalEndPoint).Port; Console.WriteLine($"Server port = {_serverPort}"); SecurityContextSet oscoapContexts = new SecurityContextSet(); _server.SecurityContexts.Add(SecurityContext.DeriveContext(secret, null, serverId, clientId)); _server.SecurityContexts.OscoreEvents += ServerEventHandler; }
private static SecurityContextSet LoadContextSet(string fileName) { if (fileName == null) { fileName = "ServerKeys.cbor"; } KeySet keys = new KeySet(); SecurityContextSet newSet = new SecurityContextSet(); FileStream fs = new FileStream(fileName, FileMode.Open); using (BinaryReader reader = new BinaryReader(fs)) { byte[] data = reader.ReadBytes((int)fs.Length); CBORObject obj = CBORObject.DecodeFromBytes(data); for (int i = 0; i < obj.Count; i++) { OneKey key = new OneKey(obj[i]); string[] usages = key[_UsageKey].AsString().Split(' '); foreach (String usage in usages) { if (usage == "oscoap") { SecurityContext ctx = SecurityContext.DeriveContext( key[CoseKeyParameterKeys.Octet_k].GetByteString(), null, key[CBORObject.FromObject("RecipID")].GetByteString(), key[CBORObject.FromObject("SenderID")].GetByteString(), null, key[CoseKeyKeys.Algorithm]); newSet.Add(ctx); break; } else if (usage == "oscoap-group") { SecurityContext ctx = SecurityContext.DeriveGroupContext( key[CoseKeyParameterKeys.Octet_k].GetByteString(), key[CBORObject.FromObject(2)].GetByteString(), key[CBORObject.FromObject("SenderID")].GetByteString(), null, null, null, null, null, key[CoseKeyKeys.Algorithm]); foreach (CBORObject recipient in key[CBORObject.FromObject("recipients")].Values) { ctx.AddRecipient(recipient[CBORObject.FromObject("RecipID")].GetByteString(), new OneKey(recipient[CBORObject.FromObject("sign")])); } newSet.Add(ctx); } } if ((usages.Length != 1) || (usages[0] != "oscoap")) { keys.AddKey(key); } } reader.Close(); } // return(newSet); }
private void CreateServer() { CoAPEndPoint endpoint = new CoAPEndPoint(0); _server = new CoapServer(); // _resource = new StorageResource(TARGET, CONTENT_1); // _server.Add(_resource); Resource r2 = new EchoLocation("abc"); _server.Add(r2); r2.Add(new EchoLocation("def")); _server.AddEndPoint(endpoint); _server.Start(); _serverPort = ((System.Net.IPEndPoint)endpoint.LocalEndPoint).Port; SecurityContextSet oscoapContexts = new SecurityContextSet(); SecurityContextSet.AllContexts.Add(SecurityContext.DeriveContext(_Secret, _ServerId, _ClientId)); }
public static void Main(String[] args) { String method = null; Uri uri = null; String payload = null; Boolean loop = false; Boolean byEvent = false; OneKey authKey = null; SecurityContext oscoap = null; SecurityContextSet contextSet = null; if (args.Length == 0) { PrintUsage(); } Int32 index = 0; foreach (String arg in args) { if (arg[0] == '-') { if (arg.Equals("-l")) { loop = true; } else if (arg.Equals("-e")) { byEvent = true; } else if (arg.StartsWith("-psk=")) { if (authKey == null) { authKey = new OneKey(); authKey.Add(COSE.CoseKeyKeys.KeyType, COSE.GeneralValues.KeyType_Octet); } authKey.Add(CoseKeyParameterKeys.Octet_k, CBORObject.FromObject(Encoding.UTF8.GetBytes(arg.Substring(5)))); } else if (arg.StartsWith("-psk-id=")) { if (authKey == null) { authKey = new OneKey(); authKey.Add(COSE.CoseKeyKeys.KeyType, COSE.GeneralValues.KeyType_Octet); } authKey.Add(COSE.CoseKeyKeys.KeyIdentifier, CBORObject.FromObject(Encoding.UTF8.GetBytes(arg.Substring(8)))); } else if (arg.StartsWith("-oscoap=")) { if (contextSet == null) { Console.WriteLine("Must have -oscoap-data before -oscoap"); Environment.Exit(1); } byte[] id = Encoding.UTF8.GetBytes(arg.Substring(8)); oscoap = contextSet.FindByGroupId(id).First(); } else if (arg.StartsWith("-oscoap-data=")) { contextSet = LoadContextSet(arg.Substring(13)); } else { Console.WriteLine("Unknown option: " + arg); } } else { switch (index) { case 0: method = arg.ToUpper(); break; case 1: try { uri = new Uri(arg); } catch (Exception ex) { Console.WriteLine("Failed parsing URI: " + ex.Message); Environment.Exit(1); } break; case 2: payload = arg; break; default: Console.WriteLine("Unexpected argument: " + arg); break; } index++; } } if (method == null || uri == null) { PrintUsage(); } Request request = NewRequest(method); if (request == null) { Console.WriteLine("Unknown method: " + method); Environment.Exit(1); } if ("OBSERVE".Equals(method)) { request.MarkObserve(); loop = true; } else if ("DISCOVER".Equals(method) && (String.IsNullOrEmpty(uri.AbsolutePath) || uri.AbsolutePath.Equals("/"))) { uri = new Uri(uri, "/.well-known/core"); } CoAPEndPoint ep = null; if (uri.Scheme == "coaps") { if (authKey == null) { Console.WriteLine("Must use the -psk option to provide an authentication key"); return; } ep = new DTLSClientEndPoint(authKey); ep.Start(); request.EndPoint = ep; } request.URI = uri; if (payload != null) { request.SetPayload(payload, MediaType.TextPlain); } if (oscoap != null) { request.OscoapContext = oscoap; } // uncomment the next line if you want to specify a draft to use // request.EndPoint = CoAP.Net.EndPointManager.Draft13; Console.WriteLine(Utils.ToString(request)); try { if (byEvent) { request.Respond += delegate(Object sender, ResponseEventArgs e) { Response response = e.Response; if (response == null) { Console.WriteLine("Request timeout"); } else { Console.WriteLine(Utils.ToString(response)); Console.WriteLine("Time (ms): " + response.RTT); } if (!loop) { if (ep != null) { ep.Stop(); } Environment.Exit(0); } }; request.Send(); while (true) { Console.ReadKey(); } } else { // uncomment the next line if you need retransmission disabled. // request.AckTimeout = -1; request.Send(); do { Console.WriteLine("Receiving response..."); Response response = null; response = request.WaitForResponse(); if (response == null) { Console.WriteLine("Request timeout"); break; } else { Console.WriteLine(Utils.ToString(response)); Console.WriteLine("Time elapsed (ms): " + response.RTT); if (response.ContentType == MediaType.ApplicationLinkFormat) { IEnumerable <WebLink> links = LinkFormat.Parse(response.PayloadString); if (links == null) { Console.WriteLine("Failed parsing link format"); Environment.Exit(1); } else { Console.WriteLine("Discovered resources:"); foreach (var link in links) { Console.WriteLine(link); } } } } } while (loop); } } catch (Exception ex) { Console.WriteLine("Failed executing request: " + ex.Message); Console.WriteLine(ex); if (ep != null) { ep.Stop(); } Environment.Exit(1); } }
static CoapServer SetupServer(ICoapConfig config, EndPoint endPoint, int port, KeySet dtlsSignKeys, KeySet dtlsValidateKeys) { // // CoapServer server = new CoapServer(config, endPoint, port); DTLSEndPoint ep2 = new DTLSEndPoint(dtlsSignKeys, dtlsValidateKeys, port + 1); server.AddEndPoint(ep2); IResource root = new HelloWorldResource("hello", true); server.Add(root); IResource x = new OscoapTest("coap"); root.Add(x); x = new OscoapTest("1"); root.Add(x); root.Add(new OscoapTest("2")); root.Add(new OscoapTest("3")); root.Add(new OscoapTest("6")); root.Add(new OscoapTest("7")); server.Add(new OscoapTest("test")); server.Add(new TimeResource("observe")); server.Add(new LargeResource("LargeResource")); #if DEV_VERSION AceTest.Setup(server, "RS1"); #if false server.Add(new Com.AugustCellars.CoAP.EDHOC.EdhocResource(edhocKeys, edhocSign)); #endif // Setup the ACE resources string UseAsServer = "coaps://localhost:5689/token"; // UseAsServer = "coaps://31.133.142.90/token"; // UseAsServer = "coaps://31.133.134.176/token"; KeySet myDecryptKeySet = new KeySet(); OneKey key = new OneKey(); key.Add(CoseKeyKeys.KeyType, GeneralValues.KeyType_Octet); key.Add(CoseKeyParameterKeys.Octet_k, CBORObject.FromObject(new byte[] { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 })); key.Add(CoseKeyKeys.KeyIdentifier, CBORObject.FromObject(Encoding.UTF8.GetBytes("SERVER_KID"))); key.Add(CoseKeyKeys.Algorithm, AlgorithmValues.AES_CCM_64_128_128); myDecryptKeySet.AddKey(key); key = new OneKey(); key.Add(CoseKeyKeys.KeyType, GeneralValues.KeyType_Octet); key.Add(CoseKeyParameterKeys.Octet_k, CBORObject.FromObject(new byte[] { (byte)'a', (byte)'b', (byte)'c', 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 })); key.Add(CoseKeyKeys.KeyIdentifier, CBORObject.FromObject(new byte[] { 0x70, 0x63, 0x6F, 0x61, 0x70, 0x3A, 0x2F, 0x2F, 0x6C, 0x6F, 0x63, 0x61, 0x6C, 0x68, 0x6F, 0x73, 0x74 })); key.Add(CoseKeyKeys.Algorithm, CBORObject.FromObject(5)); myDecryptKeySet.AddKey(key); AuthZ authZ = new AuthZ(myDecryptKeySet, null); server.Add(authZ); AceOAuthTest r = new AceOAuthTest("ace-echo", true, true, UseAsServer); r.AuthTokenProcessor = authZ; server.Add(r); OscoapContexts = SecurityContextSet.AllContexts; #endif // ep2.Add(new AceOAuthTest("ace/echo", true, true, null)); #if INCLUDE_RD ResourceDirectoryResource.CreateResources(server); #endif #if DEV_VERSION // server = new CoapServer(config); CoAPEndPoint tcp = new TcpEndPoint(port); tcp.Start(); server.AddEndPoint(tcp); // server.Add(new HelloWorldResource("hello", false)); // server.Add(new LargeResource("LargeResource")); server.Add(new LargeResource("ExtraLargeResource", 20 * 1024)); server.Add(new StorageResource("StorageHere")); server.Start(); // server = new CoapServer(config); tcp = new TLSEndPoint(dtlsSignKeys, dtlsValidateKeys, port + 1); tcp.Start(); server.AddEndPoint(tcp); AceTest.Setup(server, "RS2"); //server.Add(new HelloWorldResource("hello", false)); #endif server.Start(); return(server); }