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())); } } }
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 Uri GetUri(IClient client) { if (client == null) { throw new ArgumentNullException("client"); } var builder = new UriBuilder(client.GetLicenseServerUri()); //build query string var queryBuilder = new StringBuilder(); queryBuilder.AppendFormat("{1}={0}&", Uri.EscapeDataString(Type.ToString()), "type"); queryBuilder.AppendFormat("{1}={0}&", Uri.EscapeDataString(ClientId.Encode()), "cid"); //Make sign queryBuilder.AppendFormat("{1}={0}", Uri.EscapeDataString(GetValidationKey(Type.ToString(), ClientId.Encode(), HashSecret.GetSecret())), "sign"); builder.Query = queryBuilder.ToString(); return(builder.Uri); }
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); }