public static LicenseRequest FromOfflineData(string data)
 {
     using (var memoryStream = new MemoryStream(DataEncoder.FromHexString(data)))
     {
         using (var reader = new BinaryReader(memoryStream))
         {
             var header    = reader.ReadByte();
             var type      = (RequestType)(header >> 7);
             var cidLength = header ^ (byte)type << 7;
             var request   = new LicenseRequest(type)
             {
                 ClientId = reader.ReadBytes(cidLength)
             };
             if (type == RequestType.Activate)
             {
                 request.Certificate = reader.ReadBytes(reader.ReadUInt16());
                 if (memoryStream.Position < memoryStream.Length - 66 /*UInt32+SHA512*/)
                 {
                     var licenseLength = reader.ReadUInt16();
                     if (licenseLength > 0)
                     {
                         request.LicenseKey = reader.ReadBytes(licenseLength);
                     }
                 }
                 else
                 {
                     request.LicenseKey = new byte[0];
                 }
             }
             var dataEnd = memoryStream.Position;
             var sign    = reader.ReadBytes(64);
             //Verify sign
             memoryStream.Position = 0;
             var databuffer     = reader.ReadBytes((int)dataEnd);
             var signBuffer     = RSASigner.GetSignBuffer(databuffer, new[] { HashSecret.GetSecret() });
             var signToValidate = SHA512.Create().ComputeHash(signBuffer);
             if (sign.Where((t, i) => t != signToValidate[i]).Any())
             {
                 throw new LicenseValidationException("Signatures doesn't match");
             }
             return(request);
         }
     }
 }
        public static LicenseRequest FromNameValueCollection(NameValueCollection collection)
        {
            string typeString     = collection["type"];
            string clientIdString = collection["cid"];
            string key            = collection["sign"];

            //Validate key first
            string validationKey = GetValidationKey(typeString, clientIdString, HashSecret.GetSecret());

            if (!string.Equals(validationKey, key, StringComparison.Ordinal))
            {
                throw new SecurityException("Validation keys doesn't match");
            }

            var type     = (RequestType)Enum.Parse(typeof(RequestType), typeString, true);
            var clientId = DataEncoder.FromString(clientIdString);


            if (type == RequestType.Activate && (collection["cert"] == null || collection["lid"] == null))
            {
                throw new ArgumentException(
                          "Request form must contain certificate information and license id when activating");
            }

            var licenseRequest = new LicenseRequest(type)
            {
                ClientId = clientId
            };

            if (type == RequestType.Activate)
            {
                licenseRequest.Certificate = DataEncoder.FromString(collection["cert"]);
                licenseRequest.LicenseKey  = DataEncoder.FromString(collection["lid"]);
            }
            return(licenseRequest);
        }
Beispiel #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));
            }
        }