Ejemplo n.º 1
0
        public bool ConnectToServer(string serverURI)
        {
            bool             result = false;
            SecurityResource securityResource;

            if (_SecurityResources.Children.Count() > 0)
            {
                securityResource = _SecurityResources.Children.First() as SecurityResource;
            }
            else
            {
                securityResource = new SecurityResource("1");
                _SecurityResources.Add(securityResource);
            }
            securityResource.ServerURI = serverURI;
            if (serverURI.StartsWith("coaps"))
            {
                if (!string.IsNullOrEmpty(_Channel.PSKIdentity))
                {
                    securityResource.SecurityMode    = TSecurityMode.PreSharedKey;
                    securityResource.ClientPublicKey = Encoding.UTF8.GetBytes(_Channel.PSKIdentity);
                    securityResource.SecretKey       = StringUtils.HexStringToByteArray(_Channel.PSKSecret);
                }
                else if (!string.IsNullOrEmpty(_Channel.CertificateFile))
                {
                    securityResource.SecurityMode = TSecurityMode.Certificate;
                }
            }
            else
            {
                securityResource.SecurityMode = TSecurityMode.NoSecurity;
            }
            result = ConnectToServer();
            return(result);
        }
Ejemplo n.º 2
0
        private SymmetricAlgorithm CreateAlgorithm(SecurityResource securityResource)
        {
            SymmetricAlgorithm alg = SymmetricAlgorithm.Create(securityResource.Algorithm);

            try
            {
                alg.Key = GetKeyBytes(securityResource);
                alg.IV  = Convert.FromBase64String(securityResource.IV);
                return(alg);
            }
            catch (Exception ex)
            {
                throw new Exception(String.Format("Key {0} is invalid.", securityResource.ID), ex);
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> CreateSecurity([FromBody] SecurityResource securityResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var security = Mapper.Map <SecurityResource, Security>(securityResource);

            _securityRepository.Add(security);
            await _uow.CompleteAsync();

            security = await _securityRepository.GetSecurity(security.Id);

            var result = _mapper.Map <Security, SecurityResource>(security);

            return(Ok(result));
        }
Ejemplo n.º 4
0
        private byte[] GetKeyBytes(SecurityResource securityResource)
        {
            byte[] key = Convert.FromBase64String(securityResource.Value);

            for (int i = 0; i < key.Length; i++)
            {
                key[i] ^= m_KeyComponent[i % m_KeyComponent.Length];
            }

            using (HashAlgorithm hAlg = SHA256.Create())
            {
                hAlg.TransformFinalBlock(key, 0, key.Length);
                if (Convert.ToBase64String(hAlg.Hash) != securityResource.Hash)
                {
                    throw new Exception(String.Format("The hash for key {0} is invalid", securityResource.ID));
                }
            }

            return(key);
        }
Ejemplo n.º 5
0
        public void LoadResources()
        {
            if (Directory.Exists(RESOURCES_CACHE))
            {
                foreach (string directory in Directory.GetDirectories(RESOURCES_CACHE))
                {
                    string id = Path.GetFileName(directory);
                    int    objectID;
                    if (int.TryParse(id, out objectID))
                    {
                        LWM2MResources resources = null;
                        switch (objectID)
                        {
                        case 0:
                            resources = _SecurityResources;
                            break;

                        case 1:
                            resources = _ServerResources;
                            break;

                        case 3:
                            resources = new DeviceResources();
                            break;

                        case 4:
                            resources = new ConnectivityMonitoringResources();
                            break;

                        case 5:
                            resources = new FirmwareUpdateResources();
                            break;

                        case 6:
                            resources = new LocationResources();
                            break;

                        case 7:
                            resources = new ConnectivityStatisticsResources();
                            break;

                        case 15:
                            resources = new DeviceCapabilityResources();
                            break;

                        case 20000:
                            resources = new FlowObjectResources();
                            break;

                        case 20001:
                            resources = new FlowAccessResources();
                            break;

                        case 20005:
                            resources = new FlowCommandResources();
                            EventHandler <ChildCreatedEventArgs> handler = (s, e) =>
                            {
                                FlowCommandResource flowCommandResource = e.Resource as FlowCommandResource;
                                if (flowCommandResource != null)
                                {
                                    flowCommandResource.Updated += new EventHandler(FlowCommand_Updated);
                                    FlowCommand_Updated(flowCommandResource, null);
                                }
                            };
                            resources.ChildCreated += handler;
                            break;

                        default:
                            break;
                        }
                        if (resources != null)
                        {
                            foreach (string fileName in Directory.GetFiles(directory, "*.tlv"))
                            {
                                LWM2MResource resource = null;
                                switch (objectID)
                                {
                                case 0:
                                    resource = new SecurityResource(Path.GetFileNameWithoutExtension(fileName));
                                    break;

                                case 1:
                                    resource = new ServerResource(Path.GetFileNameWithoutExtension(fileName));
                                    break;

                                case 3:
                                    resource = new DeviceResource();
                                    break;

                                case 4:
                                    resource = new ConnectivityMonitoringResource();
                                    break;

                                case 5:
                                    resource = new FirmwareUpdateResource();
                                    break;

                                case 6:
                                    resource = new LocationResource();
                                    break;

                                case 7:
                                    resource = new ConnectivityStatisticsResource();
                                    break;

                                case 15:
                                    resource = new DeviceCapabilityResource(Path.GetFileNameWithoutExtension(fileName));
                                    break;

                                case 20000:
                                    resource          = new FlowObjectResource();
                                    resource.Updated += new EventHandler(FlowObject_Updated);
                                    break;

                                case 20001:
                                    resource = new FlowAccessResource();
                                    break;

                                case 20005:
                                    resource          = new FlowCommandResource(Path.GetFileNameWithoutExtension(fileName));
                                    resource.Updated += new EventHandler(FlowCommand_Updated);
                                    break;

                                default:
                                    break;
                                }
                                if (resource != null)
                                {
                                    using (Stream stream = File.OpenRead(fileName))
                                    {
                                        TlvReader reader = new TlvReader(stream);
                                        if (resource.Deserialise(reader))
                                        {
                                            resources.Add(resource);
                                        }
                                    }

                                    if (objectID == 0)
                                    {
                                        SecurityResource securityResource = resource as SecurityResource;
                                        if (securityResource.SecurityMode == TSecurityMode.PreSharedKey)
                                        {
                                            UsePSK(System.Text.Encoding.UTF8.GetString(securityResource.ClientPublicKey), StringUtils.HexString(securityResource.SecretKey));
                                        }
                                    }
                                    else if (objectID == 20000)
                                    {
                                        FlowObjectResource flowObjectResource = resource as FlowObjectResource;
                                        if (flowObjectResource != null)
                                        {
                                            flowObjectResource.TenantHash = null;
                                        }
                                    }
                                }
                            }
                            this.AddResources(resources);
                        }
                    }
                }
            }
            else
            {
                LoadDefaultResources();
            }
        }