Ejemplo n.º 1
0
        public LicenseResult(byte[] licenseData, Client client)
        {
            try
            {
                var rsaSigner = new RSASigner();
                //We always should have the same format
                using (var sourceStream = new MemoryStream(licenseData))
                using (var reader = new BinaryReader(sourceStream))
                {
                    long dataLength = reader.ReadInt64();
                    byte[] data = reader.ReadBytes((int) dataLength);
                    byte[] sign = reader.ReadBytes((int) (licenseData.Length-8-dataLength));

                    var signBuffer = RSASigner.GetSignBuffer(data,new[]{BitConverter.GetBytes(DateTime.UtcNow.Year),client.GetSerialNumber()});
                    bool valid = rsaSigner.Validate(signBuffer, sign);
                    if (!valid)
                        throw new LicenseValidationException("License sign is invalid (maybe clock shifting)");
                    LicenseData = client.Decrypt(data);
                }
            }
            catch (Exception e)
            {
                throw new LicenseException("License corrupted", e);
            }
        }
Ejemplo n.º 2
0
        public LicenseResult(byte[] licenseData, Client client)
        {
            try
            {
                var rsaSigner = new RSASigner();
                //We always should have the same format
                using (var sourceStream = new MemoryStream(licenseData))
                    using (var reader = new BinaryReader(sourceStream))
                    {
                        long   dataLength = reader.ReadInt64();
                        byte[] data       = reader.ReadBytes((int)dataLength);
                        byte[] sign       = reader.ReadBytes((int)(licenseData.Length - 8 - dataLength));

                        var  signBuffer = RSASigner.GetSignBuffer(data, new[] { BitConverter.GetBytes(DateTime.UtcNow.Year), client.GetSerialNumber() });
                        bool valid      = rsaSigner.Validate(signBuffer, sign);
                        if (!valid)
                        {
                            throw new LicenseValidationException("License sign is invalid (maybe clock shifting)");
                        }
                        LicenseData = client.Decrypt(data);
                    }
            }
            catch (Exception e)
            {
                throw new LicenseException("License corrupted", e);
            }
        }
Ejemplo n.º 3
0
        private void Initialize(bool trylocal)
        {
            Errors   = new HashSet <LicenseException>();
            Warnings = new HashSet <LicenseException>();

            try
            {
                _client = new Client();
                //First try load from isolated store

                byte[] licenseData;
                if (trylocal)
                {
                    try
                    {
                        using (var isoStore = IsolatedStorageFile.GetMachineStoreForAssembly())
                            using (
                                var file = new IsolatedStorageFileStream(_client.GetSerialNumber().Encode(),
                                                                         FileMode.Open,
                                                                         isoStore))
                            {
                                var dataTs = new byte[8];
                                file.Read(dataTs, 0, dataTs.Length);
                                var dateTime = BitConverter.ToInt64(dataTs, 0);

                                if (dateTime > DateTime.UtcNow.Ticks)
                                {
                                    throw new LicenseException("Time shifting detected");
                                }

                                licenseData = file.ReadAllBytes(false);
                            }
                        ValidateLicense(licenseData);
                    }
                    catch (LicenseException e)
                    {
                        Warnings.Add(new LicenseException("Failed to load offline license", e));
                    }
                    catch (IOException e)
                    {
                        Warnings.Add(new LicenseException("Failed to access offline storage", e));
                    }
                }

                if (_info == null || !_info.IsValid())
                {
                    var request = new LicenseRequest(RequestType.License,
                                                     _client.GetSerialNumber(),
                                                     null,
                                                     null);
                    licenseData = request.ToWebRequest(_client).GetResponse().GetResponseStream().ReadAllBytes();
                    ValidateLicense(licenseData);
                }
            }
            catch (LicenseException e)
            {
                Errors.Add(e);
            }
            catch (WebException e)
            {
                try
                {
                    var response = Encoding.UTF8.GetString(e.Response.GetResponseStream().ReadAllBytes());
                    Errors.Add(new LicenseException(response, e));
                }
                catch (Exception)
                {
                    Errors.Add(new LicenseException("Can't read server error", e));
                }
            }
            catch (Exception e)
            {
                Errors.Add(new LicenseException("License error", e));
            }
        }