public static X509Certificate GenerateLicense(CertificateAuthority ca, Uri installCode, Boolean isServerKey, UInt32 numLic, Boolean isTemp, DateTime?tempDate) { String installKey = null; Uri license = null; System.Reflection.Assembly asm = null; FileInfo p12File = null; try { String[] iParts = installCode.AbsolutePath.Trim("/".ToCharArray()).Split("/".ToCharArray()); IAMVersion version = IAMVersion.v100; switch (iParts[0].ToLower()) { case "v1": case "v100": version = IAMVersion.v100; break; default: throw new Exception("Install code version unrecognized"); break; } installKey = String.Join("/", iParts, 1, iParts.Length - 1); //Em caso de licença com data de expiração, adiciona 20 horas no tempo para evitar problemas com fuso tempDate += TimeSpan.FromHours(20); license = new Uri("license://safeid/" + version.ToString() + "/" + GeraKey(installKey, isServerKey, numLic, isTemp, tempDate, version)); try { CertificateAuthority.subjectAltName alt = new CertificateAuthority.subjectAltName(); alt.Uri.Add(installCode); alt.Uri.Add(license); String pkcs12Cert = ca.SignCert("SafeID IAM License", false, alt, false, (isTemp && tempDate.HasValue ? tempDate.Value : DateTime.Now + TimeSpan.FromDays(36500))); return(CATools.GetX509CertFromPKCS12(Convert.FromBase64String(pkcs12Cert), ca.SignedPassword)); } finally { try { File.Delete(p12File.FullName); File.Delete(p12File.FullName.Replace(p12File.Extension, ".cer")); } catch { } p12File = null; asm = null; } } finally { installKey = null; } }
private static IAMKeyData CheckKey(String installKey, IAMVersion version, String sKey) { IAMKeyData kData = new IAMKeyData(); byte[] key = new byte[0]; kData.InstallKey = "installkey://safeid/" + version.ToString() + "/" + installKey; key = StringToByteArray(sKey.Replace("-", "").Replace("/", "").Replace("\\", "")); kData.NumLic = (UInt32)((key[4] << 8) | key[6]); UInt32 totalSeconds = (UInt32)((key[2] << 24) | (key[12] << 16) | (key[9] << 8) | (key[7])); kData.IsServerKey = (key[3] == 1); if (totalSeconds > 0) { kData.IsTemp = true; kData.TempDate = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(totalSeconds); } else { kData.IsTemp = false; } String cKey = GeraKey(installKey, kData.IsServerKey, kData.NumLic, kData.IsTemp, kData.TempDate, version); if (cKey.ToUpper().Replace("-", "").Replace("/", "").Replace("\\", "") != sKey.ToUpper().Replace("-", "").Replace("/", "").Replace("\\", "")) { throw new Exception("Invalid key"); } return(kData); }