예제 #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);
            }
        }
예제 #2
0
 public string ToOfflineData()
 {
     using (var memoryStream = new MemoryStream())
     {
         using (var writer = new BinaryWriter(memoryStream))
         {
             var header = (byte)((byte)Type << 7 | ClientId.Length);
             writer.Write(header);
             writer.Write(ClientId);
             if (Type == RequestType.Activate)
             {
                 //Write cert and license data
                 writer.Write((ushort)Certificate.Length);
                 writer.Write(Certificate);
                 if (LicenseKey.Length > 0)
                 {
                     writer.Write((ushort)LicenseKey.Length);
                     writer.Write(LicenseKey);
                 }
             }
             var data       = memoryStream.ToArray();
             var signBuffer = RSASigner.GetSignBuffer(data, new[] { HashSecret.GetSecret() });
             writer.Write(SHA512.Create().ComputeHash(signBuffer));
             return(DataEncoder.ToHexString(memoryStream.ToArray()));
         }
     }
 }
예제 #3
0
 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);
         }
     }
 }