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 void BuildCert() { System.Reflection.Assembly asm = System.Reflection.Assembly.GetAssembly(typeof(ServerKey2)); FileInfo p12File = new FileInfo(Path.Combine(Path.GetDirectoryName(asm.Location), "server" + DateTime.Now.ToString("yyyyMMddHHmss") + ".pfx")); try { CertificateAuthority.subjectAltName alt = new CertificateAuthority.subjectAltName(); if ((this.ServerInstallationKey == null)) { this.NewInstallationKey(); } alt.Uri.Add(ServerInstallationKey); ca = new CertificateAuthority("123456", "w0):X,\\Q4^NoIO,):Z!."); ca.LoadOrCreateCA(p12File.FullName, this.hostname, alt); Byte[] certData = File.ReadAllBytes(p12File.FullName); this.ServerCert = CATools.GetX509CertFromPKCS12(certData, "w0):X,\\Q4^NoIO,):Z!."); this.ServerCertString = CATools.X509ToBase64(this.ServerCert); this.ServerPKCS12String = Convert.ToBase64String(certData); this.ServerPKCS12Cert = CATools.LoadCert(certData, "w0):X,\\Q4^NoIO,):Z!."); } finally { try { File.Delete(p12File.FullName); File.Delete(p12File.FullName.Replace(p12File.Extension, ".cer")); } catch { } p12File = null; asm = null; } }