Example #1
0
        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;
            }
        }
Example #2
0
        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);
        }
Example #3
0
        public static IAMKeyData ExtractFromCert(String base64CertData)
        {
            X509Certificate cert        = null;
            Uri             installCode = null;
            Uri             license     = null;
            String          key         = null;
            String          installKey  = null;

            try
            {
                try
                {
                    cert = CATools.LoadCert(Convert.FromBase64String(base64CertData));
                }
                catch (Exception ex)
                {
                    throw new Exception("Erro on load certificate: " + ex.Message);
                }

                installCode = GetDataCode(cert, "installkey");

                if (installCode == null)
                {
                    throw new Exception("Install code not found in certificate");
                }

                license = GetDataCode(cert, "license");

                if (license == null)
                {
                    throw new Exception("License not found in certificate");
                }

                String[] parts  = license.AbsolutePath.Trim("/".ToCharArray()).Split("/".ToCharArray());
                String[] iParts = installCode.AbsolutePath.Trim("/".ToCharArray()).Split("/".ToCharArray());

                IAMVersion version = IAMVersion.v100;
                switch (parts[0].ToLower())
                {
                case "v100":
                    version = IAMVersion.v100;
                    break;

                default:
                    throw new Exception("License version unrecognized");
                    break;
                }


                key        = String.Join("/", parts, 1, parts.Length - 1);
                installKey = String.Join("/", iParts, 1, iParts.Length - 1);

                return(CheckKey(installKey, version, key));
            }
            finally
            {
                cert        = null;
                installCode = null;
                license     = null;
                key         = null;
                installKey  = null;
            }
        }
Example #4
0
        private static string GeraKey(String installKey, Boolean isServerKey, UInt32 numLic, Boolean isTemp, DateTime?tempDate, IAMVersion version)
        {
            byte[] buffer       = new byte[0];
            uint   totalSeconds = 0;

            if (isTemp)
            {
                DateTime?nullable  = tempDate;
                DateTime time      = new DateTime(0x7b2, 1, 1, 0, 0, 0);
                TimeSpan?nullable3 = nullable.HasValue ? new TimeSpan?(nullable.GetValueOrDefault() - time) : null;
                totalSeconds = (uint)nullable3.Value.TotalSeconds;
                //totalSeconds += 0x1517f;
            }

            using (MemoryStream stream = new MemoryStream())
            {
                if (version == IAMVersion.v100)
                {
                    stream.Write(secret1_v100, 0, secret1_v100.Length);
                }

                byte[] bytes = Encoding.ASCII.GetBytes(installKey.ToLower().Replace("-", "").Replace("/", "").Replace("\\", ""));
                stream.Write(bytes, 0, bytes.Length);
                ushort num2 = (ushort)(numLic ^ 0x33bb);
                bytes = BitConverter.GetBytes(num2);
                stream.Write(bytes, 0, bytes.Length);
                uint num3 = 0;
                num3  = totalSeconds ^ 0xffbb0033;
                bytes = BitConverter.GetBytes(num3);
                stream.Write(bytes, 0, bytes.Length);

                if (version == IAMVersion.v100)
                {
                    stream.Write(secret2_v100, 0, secret2_v100.Length);
                }

                num2  = (ushort)(numLic ^ 0xbb33);
                bytes = BitConverter.GetBytes(num2);
                stream.Write(bytes, 0, bytes.Length);
                num3  = totalSeconds ^ 0x33ffbb;
                bytes = BitConverter.GetBytes(num3);
                stream.Write(bytes, 0, bytes.Length);
                bytes = Encoding.ASCII.GetBytes(installKey.Replace("-", "").Replace("/", "").Replace("\\", ""));
                stream.Write(bytes, 0, bytes.Length);

                if (version == IAMVersion.v100)
                {
                    stream.Write(secret3_v100, 0, secret3_v100.Length);
                }

                stream.Flush();
                buffer = stream.ToArray();
            }

            byte[] buffer3 = new SHA1CryptoServiceProvider().ComputeHash(buffer);
            buffer3[3]  = ToByte((uint)((isServerKey ? 1 : 0) & 0xff));
            buffer3[4]  = ToByte((numLic >> 8) & 0xff);
            buffer3[6]  = ToByte(numLic & 0xff);
            buffer3[2]  = ToByte((totalSeconds >> 0x18) & 0xff);
            buffer3[12] = ToByte((totalSeconds >> 0x10) & 0xff);
            buffer3[9]  = ToByte((totalSeconds >> 8) & 0xff);
            buffer3[7]  = ToByte(totalSeconds & 0xff);


            String key = BitConverter.ToString(buffer3).Replace("-", "");

            string str5 = "";

            for (int j = 0; j < key.Length; j++)
            {
                if ((j > 0) && ((j % 8) == 0))
                {
                    str5 = str5 + '/';
                }
                str5 = str5 + key[j];
            }
            key = str5;

            return(key);
        }