Ejemplo n.º 1
0
        protected override void DoPost(CoapExchange exchange)
        {
            byte[]         body = exchange.Request.Payload;
            EdhocResponder edhoc;

            try {
                switch (body[1] & 0xf)
                {
                case 1:
                    edhoc            = EdhocResponder.ParseMessage1(body);
                    edhoc.SigningKey = _signKey;
                    body             = edhoc.CreateMessage2();
                    exchange.Respond(CoAP.StatusCode.Changed, body);
                    break;

                case 4:
                    edhoc = EdhocResponder.ParseMessage1(body);
                    OneKey y = null;
                    foreach (OneKey x in _allKeys)
                    {
                        if (x.ContainsName(CoseKeyKeys.KeyIdentifier))
                        {
                            if (x.HasKid(edhoc.KeyIdentifier))
                            {
                                if (y != null)
                                {
                                    exchange.Respond(CoAP.StatusCode.BadRequest);
                                    return;
                                }
                                y = new OneKey(x.AsCBOR());
                            }
                        }
                    }

                    if (y == null)
                    {
                        exchange.Respond(CoAP.StatusCode.BadRequest);
                        return;
                    }

                    if (!y[CoseKeyKeys.KeyType].Equals(GeneralValues.KeyType_Octet))
                    {
                        exchange.Respond(CoAP.StatusCode.BadRequest);
                        return;
                    }

                    edhoc.SharedSecret = y;

                    body = edhoc.CreateMessage2();
                    exchange.Respond(CoAP.StatusCode.Changed, body);
                    break;

                case 3:
                    edhoc = EdhocResponder.ParseMessage3(body, _allKeys);
                    exchange.Respond(StatusCode.Changed);

                    OSCOAP.SecurityContext ctx = edhoc.CreateSecurityContext();
                    OSCOAP.SecurityContextSet.AllContexts.Add(ctx);
                    break;

                case 6:
                    edhoc = EdhocResponder.ParseMessage3(body, _allKeys);
                    exchange.Respond(CoAP.StatusCode.Changed);

                    OSCOAP.SecurityContext ctx2 = edhoc.CreateSecurityContext();
                    OSCOAP.SecurityContextSet.AllContexts.Add(ctx2);
                    break;

                default:
                    exchange.Respond(CoAP.StatusCode.BadRequest);
                    break;
                }
            }
            catch (Exception e) {
                CBORObject obj = CBORObject.NewArray();
                obj.Add(0);
                obj.Add(e.ToString());
                exchange.Respond(CoAP.StatusCode.Content, obj.EncodeToBytes());
            }
        }