void ValidateLicenseStrings() { bool allStringsAreValid = true; foreach (string s in licenseStrings) { LicenseKey licenseKey = new LicenseKey(s); if (!licenseKey.IsHashValid(masterKey)) { Console.WriteLine("License string {0} is not valid!", s); allStringsAreValid = false; } } if (allStringsAreValid) { Console.WriteLine("[All license strings are valid.]"); } }
void Run(string[] args) { // Load default params from configuration file. LoadDefaultParams(); // Override default params with any specified on command line. ProcessCommandLine(args); #if !INTERNAL if ( license.IsEvaluation ) { System.Console.WriteLine(); System.Console.WriteLine("Evaluation Version Note:"); System.Console.WriteLine( " Because you have an evaluation version of License Express (tm), this\n" + " utility will only generate evaluation license keys with a maximum\n" + " expiration time of 2 days. Visit http://www.wanderlust-software.com\n" + " to purchase a retail license which provides full functionality.\n" ); LicenseKey licenseKey = new LicenseKey( license.LicenseKey ); if ( ( numberOfLicenseStringsToGenerate != 0 ) && ((!this.isEval) || (this.isEval && this.evalDays > 2)) ) { System.Console.Error.WriteLine( "Keys not generated: Specify an evaluation license of 2 days or less.\n"+ "For example:\n"+ " KeyGen /eval 2 /password \"My Password\" /count 5" ); return; } } #endif // Generate license strings. GenerateLicenseStrings(); //ValidateLicenseStrings(); // Write license strings to stdout. WriteStringsToStdout(); // Write license strings to clipboard WriteStringsToClipboard(); }
void ValidateLicenseStrings() { bool allStringsAreValid = true; foreach(string s in licenseStrings) { LicenseKey licenseKey = new LicenseKey( s ); if ( !licenseKey.IsHashValid(masterKey) ) { Console.WriteLine("License string {0} is not valid!", s); allStringsAreValid = false; } } if ( allStringsAreValid ) { Console.WriteLine("[All license strings are valid.]"); } }
void GenerateLicenseStrings() { int currentSerialNumber = startingSerialNumber; licenseStrings = new string[numberOfLicenseStringsToGenerate]; for(int i=0;i<numberOfLicenseStringsToGenerate;i++) { LicenseKey licenseKey = new LicenseKey(); if ( expires ) { licenseKey.Version = 2; licenseKey.Options = LicenseKeyOptions.AbsoluteExpirationDate; licenseKey.ExpirationDate = expirationDate; } if ( isEval ) { licenseKey.Type = LicenseKeyType.DesignTimeEvaluation; } else { licenseKey.Type = LicenseKeyType.DesignTimeRetail; } licenseKey.EvaluationDays = evalDays; licenseKey.SerialNumber = currentSerialNumber; licenseKey.Hash = licenseKey.CalculateHash( masterKey ); licenseStrings[i] = licenseKey.ToString(); currentSerialNumber++; } }
// // This routine gets the runtime license key associated with an // application. It returns true if licence runtime key was // retrieved from registry, or created and stored in registry. // This is necessary ... // private bool GetApplicationRuntimeLicenseKey(LicenseKey licenseKey, Type type) { Microsoft.Win32.RegistryKey regKey = null; bool SuccessfulWriteToRegisty = false; // // Here we check the registry to find if a runtime license exists. // If we do not find one, assume that this is the first time this // application is running. // String regKeyName = registryCacheName + "\\" + type.GUID.ToString(); try { regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(regKeyName, true); } catch (ArgumentException e) { Debug.WriteLine("Exception trying to read registry."); Debug.WriteLine(e); return(false); } catch (System.Security.SecurityException e) { Debug.WriteLine("Exception reading registry."); Debug.WriteLine(e); return(false); } if (regKey == null) { // // No key found in registry, so generate one and save it. // SuccessfulWriteToRegisty = GenerateRunTimeLicenseKeyFromDesignTime(licenseKey, type); // // If there was a problem writing to the registry, // return false thereby failing the license // if (!SuccessfulWriteToRegisty) { licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return(false); } try { regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(regKeyName, true); } catch (ArgumentException e) { Debug.WriteLine("Exception trying to read registry."); Debug.WriteLine(e); return(false); } catch (System.Security.SecurityException e) { Debug.WriteLine("Exception reading registry."); Debug.WriteLine(e); return(false); } } if (regKey != null) { this.licenseKeyString = (string)regKey.GetValue("License"); if (this.licenseKeyString == null) { // // Someone probably deleted key manually. Fail the license. // licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; regKey.Close(); return(false); } regKey.Close(); } else { licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return(false); } // // this.licenseKeyString updated // return(true); }
// // GetKey() is defined by LicFileLicenseProvider. It is called during // GetLicense() processing in order to populate the LicenseKey property // of the License object returned to the caller. // protected override string GetKey(Type type) { string key; LicenseKey licenseKey; if (traceSwitch.TraceVerbose) { Trace.WriteLine(String.Format("GetKey(type={0})", type)); } Trace.Indent(); // // We return whichever license key string we were presented in IsKeyValid(), // unless we had been presented a 'design time evaluation' license. // In the latter case, we generate a new license key that encodes the // evaluation expiration date in it. This license key will subsequenty // be embedded in the executable being built. // key = this.licenseKeyString; // // Myk, what do we do if we catch an exception here? we should // never, because key should be valid. If we return null, will we // still get a license? In that case, we should never throw any // exceptions. // try { licenseKey = new LicenseKey(key); } catch (ArgumentException) { licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return(null); } if (traceSwitch.TraceVerbose) { Trace.WriteLine("GetKey: License type is " + licenseKey.Type); } if (licenseKey.Type == LicenseKeyType.DesignTimeEvaluation) { // // Modify the license key to be a 'runtime evaluation' license by // changing its type, setting the expiration date, and recalculating // its hash value. // licenseKey.Type = LicenseKeyType.RuntimeEvaluation; licenseKey.ExpirationDate = DateTime.Now.AddDays(licenseKey.EvaluationDays); licenseKey.Hash = licenseKey.CalculateHash(licenseComponent.MasterLicensePassword); key = licenseKey.ToString(); } if (traceSwitch.TraceVerbose) { Trace.WriteLine("Generated license key: " + key); } Trace.Unindent(); // // Return the new license key string to be embedded. // return(key); }
// // This routine gets the runtime license key assocaited with an // application. This is necessary ... //protected LicenseKey GetApplicationRuntimeLicenseKey() //{ //} // // IsKeyValid is defined by LicFileLicenseProvider, and is called // during processing of GetLicense to determine if a given license // key string represents a valid license. // // Notice that we are only given the string and the type of the // class being licensed. We need to use the LicXLicenseComponent // that is a member of the licensed instance, so we pull that out // of the class field where it had been stored earlier. // // Note that in the case of an application licensed with an evaluation // license, we may have to go to the registry to find an appropriate // license key string. // protected override bool IsKeyValid(string licenseKeyString, Type type) { bool keyIsValid; bool RunTimKeyFound = false; LicenseKey licenseKey; if (traceSwitch.TraceVerbose) { Trace.WriteLine(String.Format("IsKeyValid({0}, {1})", licenseKeyString, type)); } Trace.Indent(); // // Save LicenseKeyString for later use in GetKey. If this is a // runtime application using a design time key, it will be changed // and stored for GetKey() // this.licenseKeyString = licenseKeyString; // // Create a LicenseKey object with the given license key string. // The LicenseKey can throw an Exception for the int32.parse // method. Fail it if does, b/c something is a matter with our // license key string. // try { licenseKey = new LicenseKey(licenseKeyString); } catch (ArgumentException) { licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return(false); } // // If we are being invoked at runtime, and the license key string // we are given is of type "design time evaluation", we are dealing // with a licensed application (as opposed to a component). In // this case, the runtime license we use needs to be in the // registry. (The first time the application is run, we need to // create this registry license). // // BUGBUG: It doesn't appear that the check for // BUGBUG: "!licenseKey.AbsoluteExpirationDate is ever needed? // BUGBUG: Need to do a full test cycle to make sure... // if ( (LicenseManager.CurrentContext.UsageMode == LicenseUsageMode.Runtime) && (licenseKey.Type == LicenseKeyType.DesignTimeEvaluation) && (!licenseKey.AbsoluteExpirationDate) ) { RunTimKeyFound = GetApplicationRuntimeLicenseKey(licenseKey, type); if (!RunTimKeyFound) { // there was a problem writing or reading from the registry, so // fail the license. licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return(false); } else { // // this.licenseKeyString has changed to runtime application, so update // licenseKey // try { licenseKey = new LicenseKey(this.licenseKeyString); } catch (ArgumentException) { licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return(false); } } } #if delete /* // * // Here we check the registry to find if a runtime license exists. * // If we do not find one, assume that this is the first time this * // application is running. * // * * String regKeyName = registryCacheName + "\\" + type.GUID.ToString(); * * try * { * regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey( regKeyName, true ); * } * catch( ArgumentException e ) * { * Trace.WriteLine("Exception building license."); * Trace.WriteLine(e); * licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; * return false; * } * // BUGBUG: SecurityException ? * * * if ( regKey == null ) * { * generateruntimefromdesign(); * regKey = * } * * // * // Read the license key string from the registry, and use it * // to create a new licenseKey object. regKey will * // be null (undefined in VS) if the value doesn't exist. * // * * if ( regKey != null ) * { * // BUGBUG: SecurityException ? * this.licenseKeyString = (string) regKey.GetValue( "License" ); * if ( this.licenseKeyString == NULL ) * { * // fail. * } * * try * { * licenseKey = new LicenseKey(this.licenseKeyString); * } * catch( ArgumentException e) * { * licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; * return false; * } * * regKey.Close(); * } * else * { * noRunTimeKeyFound = true; * } * * * // * // If the license key string was not found in the registry, * // it's assumed that this is the first run of an application. * // Create a new runtime license key and save it * // to the registry. * // * * if( noRunTimeKeyFound ) * { * // * // Modify the license key to be a 'runtime evaluation' license by * // changing its type, setting the expiration date, and recalculating * // its hash value. The serial number stays the same. * // * * licenseKey.Type = LicenseKeyType.RuntimeEvaluation; * licenseKey.ExpirationDate = DateTime.Now.AddDays( licenseKey.EvaluationDays ); * licenseKey.Hash = licenseKey.CalculateHash( licenseComponent.MasterLicensePassword ); * * * // * // Save the new runtime license key string, as it is needed * // later by GetKey(). * // * * this.licenseKeyString = licenseKey.ToString(); * * * // * // Write out to the Registry. CreateSubKey returns null if it fails. * // If an exceptions occurs, fail the license. * // * // Note that CreateSubKey may return a SecurityException, but the * // compiler throws an error. * // * // BUGBUG: proibably just need to refer to it as System.Security.SecurityException * // * try * { * regKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey( registryCacheName + "\\" + type.GUID.ToString() ); * if ( regKey == null ) * { * // BUGBUG: handle error. * } * regKey.SetValue( "License", (string) this.licenseKeyString ); * } * catch( UnauthorizedAccessException e ) * { * Trace.WriteLine("Exception writing to registry."); * Trace.WriteLine(e); * licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; * return false; * } * //catch( SecurityException e) * //{ * // Trace.WriteLine("Exception writing to registry."); * // Trace.WriteLine(e); * // licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; * // return false; * //} * * if( regKey != null ) * { * regKey.Close(); * } * } * } // end of if licenseType is runtime using design time eval license */ #endif //delete // // Now we have ensured that we have the right key ( The .lic file // was possibly replaced with a runtime key saved in the registry). // First validate that the key is authentic. // keyIsValid = licenseKey.IsHashValid(licenseComponent.MasterLicensePassword); if (!keyIsValid) { if (traceSwitch.TraceVerbose) { Trace.WriteLine("IsKeyValid: License key is not properly signed. Key=" + licenseKeyString); } Trace.Unindent(); licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return(false); } // // If the licenseKey is a runtime evaluation, check if: // - expiration date is expired // - expiration period is longer than maximum allowed // - it has an absolute evaluation date, check if its // not expired // - a 'time-bomb' has been set for the component itself, // and if so, should it go boom. // // If any of these returns a expired or invalid value, declare // that the key is not valid by returning false after setting // the licenseCheckFailureReason. Otherwise return true. // if (licenseKey.Type == LicenseKeyType.RuntimeEvaluation) { if (traceSwitch.TraceVerbose) { Trace.WriteLine(" License key is runtime evaluation."); Trace.WriteLine(" Current Date: " + DateTime.Now); Trace.WriteLine(" Expiration Date: " + licenseKey.ExpirationDate); } // // Check whether the current date is beyond the expiration date. // if (DateTime.Now > licenseKey.ExpirationDate) { if (traceSwitch.TraceVerbose) { Trace.WriteLine("IsKeyValid: License is expired. ExpirationDate = " + licenseKey.ExpirationDate); } Trace.Unindent(); licenseCheckFailureReason = LicenseCheckFailureReason.EvaluationLicenseExpired; return(false); } // // Check whether the expiration date is higher than possible - this points // to someone adjusting their system calender while building a component in // an attempt to have an eval license valid for a long time. // DateTime maxExpirationDate = DateTime.Now.AddDays(licenseKey.EvaluationDays); if (licenseKey.ExpirationDate > maxExpirationDate) { if (traceSwitch.TraceVerbose) { Trace.WriteLine("IsKeyValid: License has invalid expiration date."); Trace.WriteLine(" Evaluation Days: " + licenseKey.EvaluationDays); Trace.WriteLine(" Max Expiration Date: " + maxExpirationDate); Trace.Unindent(); } licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return(false); } } // // If its past the Absolute Expiration Date, fail it even if the evaluation // period is not expired // if (licenseKey.AbsoluteExpirationDate) { if (traceSwitch.TraceVerbose) { Trace.WriteLine(" License key has absolute expiration date."); Trace.WriteLine(" Current Date: " + DateTime.Now); Trace.WriteLine(" Expiration Date: " + licenseKey.ExpirationDate); } // // Check whether the current date is beyond the expiration date. // if (DateTime.Now > licenseKey.ExpirationDate) { Trace.WriteLine("IsKeyValid: License is expired. ExpirationDate=" + licenseKey.ExpirationDate); Trace.Unindent(); licenseCheckFailureReason = LicenseCheckFailureReason.EvaluationLicenseExpired; return(false); } } // // Check to see if a 'time-bomb' has been set for the component itself. // if ( (licenseComponent.ExpirationDate.Ticks != (long)0) && (DateTime.Now > licenseComponent.ExpirationDate) ) { if (traceSwitch.TraceVerbose) { Trace.WriteLine("IsKeyValid: Component has expired. ExpirationDate=" + licenseComponent.ExpirationDate); } Trace.Unindent(); licenseCheckFailureReason = LicenseCheckFailureReason.ComponentExpired; return(false); } // // Passed all the tests, so we have a valid key // return(true); }
public LicXLicense(License l) { this.internalLicense = l; licenseKey = new LicenseKey( l.LicenseKey ); }
public LicXLicense(string licenseKeyString) { //licenseKey = new LicenseKey( licenseKeyString ); licenseKey = new LicenseKey(); internalLicense = null; }
public LicXLicense(License l) { this.internalLicense = l; licenseKey = new LicenseKey(l.LicenseKey); }
// // Returns false if it failed to generate run time LicenseKey // either b/c an exception was thrown when creating or opening a // registry subkey, or a regKey could not succesfully write to the // registry. // // Returns boolean newKeyInRegistry // private bool GenerateRunTimeLicenseKeyFromDesignTime(LicenseKey licenseKey, Type type) { Microsoft.Win32.RegistryKey regKey = null; bool newKeyInRegistry = false; // // Modify the license key to be a 'runtime evaluation' license by // changing its type, setting the expiration date, and recalculating // its hash value. The serial number stays the same. // licenseKey.Type = LicenseKeyType.RuntimeEvaluation; licenseKey.ExpirationDate = DateTime.Now.AddDays( licenseKey.EvaluationDays ); licenseKey.Hash = licenseKey.CalculateHash( licenseComponent.MasterLicensePassword ); // // Write out to the Registry. CreateSubKey returns null if it fails. // If null is returned or an exceptions occurs, catch it and return null. // try { regKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey( registryCacheName + "\\" + type.GUID.ToString() ); if ( regKey == null ) { Debug.WriteLine("Failed writing to registry."); return newKeyInRegistry; } regKey.SetValue( "License", (string) licenseKey.ToString() ); } catch( UnauthorizedAccessException e ) { Debug.WriteLine("Exception writing to registry."); Debug.WriteLine(e); return newKeyInRegistry; } catch( System.Security.SecurityException e) { Debug.WriteLine("Exception writing to registry."); Debug.WriteLine(e); return newKeyInRegistry; } if( regKey != null ) { regKey.Close(); } // // Save the new runtime license key string, as it is needed // later by GetKey(). // this.licenseKeyString = licenseKey.ToString(); newKeyInRegistry = true; return newKeyInRegistry; }
// // This routine gets the runtime license key associated with an // application. It returns true if licence runtime key was // retrieved from registry, or created and stored in registry. // This is necessary ... // private bool GetApplicationRuntimeLicenseKey(LicenseKey licenseKey, Type type) { Microsoft.Win32.RegistryKey regKey = null; bool SuccessfulWriteToRegisty = false; // // Here we check the registry to find if a runtime license exists. // If we do not find one, assume that this is the first time this // application is running. // String regKeyName = registryCacheName + "\\" + type.GUID.ToString(); try { regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey( regKeyName, true ); } catch( ArgumentException e ) { Debug.WriteLine("Exception trying to read registry."); Debug.WriteLine(e); return false; } catch( System.Security.SecurityException e) { Debug.WriteLine("Exception reading registry."); Debug.WriteLine(e); return false; } if ( regKey == null ) { // // No key found in registry, so generate one and save it. // SuccessfulWriteToRegisty = GenerateRunTimeLicenseKeyFromDesignTime(licenseKey, type); // // If there was a problem writing to the registry, // return false thereby failing the license // if(!SuccessfulWriteToRegisty) { licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return false; } try { regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey( regKeyName, true ); } catch( ArgumentException e ) { Debug.WriteLine("Exception trying to read registry."); Debug.WriteLine(e); return false; } catch( System.Security.SecurityException e) { Debug.WriteLine("Exception reading registry."); Debug.WriteLine(e); return false; } } if ( regKey != null ) { this.licenseKeyString = (string) regKey.GetValue( "License" ); if ( this.licenseKeyString == null ) { // // Someone probably deleted key manually. Fail the license. // licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; regKey.Close(); return false; } regKey.Close(); } else { licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return false; } // // this.licenseKeyString updated // return true; }
// // GetKey() is defined by LicFileLicenseProvider. It is called during // GetLicense() processing in order to populate the LicenseKey property // of the License object returned to the caller. // protected override string GetKey(Type type) { string key; LicenseKey licenseKey; if( traceSwitch.TraceVerbose ) { Trace.WriteLine( String.Format( "GetKey(type={0})", type) ); } Trace.Indent(); // // We return whichever license key string we were presented in IsKeyValid(), // unless we had been presented a 'design time evaluation' license. // In the latter case, we generate a new license key that encodes the // evaluation expiration date in it. This license key will subsequenty // be embedded in the executable being built. // key = this.licenseKeyString; // // Myk, what do we do if we catch an exception here? we should // never, because key should be valid. If we return null, will we // still get a license? In that case, we should never throw any // exceptions. // try { licenseKey = new LicenseKey( key ); } catch( ArgumentException) { licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return null; } if( traceSwitch.TraceVerbose ) { Trace.WriteLine( "GetKey: License type is " + licenseKey.Type); } if( licenseKey.Type == LicenseKeyType.DesignTimeEvaluation ) { // // Modify the license key to be a 'runtime evaluation' license by // changing its type, setting the expiration date, and recalculating // its hash value. // licenseKey.Type = LicenseKeyType.RuntimeEvaluation; licenseKey.ExpirationDate = DateTime.Now.AddDays( licenseKey.EvaluationDays ); licenseKey.Hash = licenseKey.CalculateHash( licenseComponent.MasterLicensePassword ); key = licenseKey.ToString(); } if( traceSwitch.TraceVerbose ) { Trace.WriteLine("Generated license key: " + key); } Trace.Unindent(); // // Return the new license key string to be embedded. // return key; }
// // This routine gets the runtime license key assocaited with an // application. This is necessary ... //protected LicenseKey GetApplicationRuntimeLicenseKey() //{ //} // // IsKeyValid is defined by LicFileLicenseProvider, and is called // during processing of GetLicense to determine if a given license // key string represents a valid license. // // Notice that we are only given the string and the type of the // class being licensed. We need to use the LicXLicenseComponent // that is a member of the licensed instance, so we pull that out // of the class field where it had been stored earlier. // // Note that in the case of an application licensed with an evaluation // license, we may have to go to the registry to find an appropriate // license key string. // protected override bool IsKeyValid(string licenseKeyString, Type type) { bool keyIsValid; bool RunTimKeyFound = false; LicenseKey licenseKey; if ( traceSwitch.TraceVerbose ) { Trace.WriteLine( String.Format( "IsKeyValid({0}, {1})", licenseKeyString, type )); } Trace.Indent(); // // Save LicenseKeyString for later use in GetKey. If this is a // runtime application using a design time key, it will be changed // and stored for GetKey() // this.licenseKeyString = licenseKeyString; // // Create a LicenseKey object with the given license key string. // The LicenseKey can throw an Exception for the int32.parse // method. Fail it if does, b/c something is a matter with our // license key string. // try { licenseKey = new LicenseKey(licenseKeyString); } catch( ArgumentException ) { licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return false; } // // If we are being invoked at runtime, and the license key string // we are given is of type "design time evaluation", we are dealing // with a licensed application (as opposed to a component). In // this case, the runtime license we use needs to be in the // registry. (The first time the application is run, we need to // create this registry license). // // BUGBUG: It doesn't appear that the check for // BUGBUG: "!licenseKey.AbsoluteExpirationDate is ever needed? // BUGBUG: Need to do a full test cycle to make sure... // if( ( LicenseManager.CurrentContext.UsageMode == LicenseUsageMode.Runtime ) && ( licenseKey.Type == LicenseKeyType.DesignTimeEvaluation ) && ( !licenseKey.AbsoluteExpirationDate ) ) { RunTimKeyFound = GetApplicationRuntimeLicenseKey( licenseKey, type); if(!RunTimKeyFound) { // there was a problem writing or reading from the registry, so // fail the license. licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return false; } else { // // this.licenseKeyString has changed to runtime application, so update // licenseKey // try { licenseKey = new LicenseKey(this.licenseKeyString); } catch( ArgumentException ) { licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return false; } } } #if delete /* // // Here we check the registry to find if a runtime license exists. // If we do not find one, assume that this is the first time this // application is running. // String regKeyName = registryCacheName + "\\" + type.GUID.ToString(); try { regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey( regKeyName, true ); } catch( ArgumentException e ) { Trace.WriteLine("Exception building license."); Trace.WriteLine(e); licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return false; } // BUGBUG: SecurityException ? if ( regKey == null ) { generateruntimefromdesign(); regKey = } // // Read the license key string from the registry, and use it // to create a new licenseKey object. regKey will // be null (undefined in VS) if the value doesn't exist. // if ( regKey != null ) { // BUGBUG: SecurityException ? this.licenseKeyString = (string) regKey.GetValue( "License" ); if ( this.licenseKeyString == NULL ) { // fail. } try { licenseKey = new LicenseKey(this.licenseKeyString); } catch( ArgumentException e) { licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return false; } regKey.Close(); } else { noRunTimeKeyFound = true; } // // If the license key string was not found in the registry, // it's assumed that this is the first run of an application. // Create a new runtime license key and save it // to the registry. // if( noRunTimeKeyFound ) { // // Modify the license key to be a 'runtime evaluation' license by // changing its type, setting the expiration date, and recalculating // its hash value. The serial number stays the same. // licenseKey.Type = LicenseKeyType.RuntimeEvaluation; licenseKey.ExpirationDate = DateTime.Now.AddDays( licenseKey.EvaluationDays ); licenseKey.Hash = licenseKey.CalculateHash( licenseComponent.MasterLicensePassword ); // // Save the new runtime license key string, as it is needed // later by GetKey(). // this.licenseKeyString = licenseKey.ToString(); // // Write out to the Registry. CreateSubKey returns null if it fails. // If an exceptions occurs, fail the license. // // Note that CreateSubKey may return a SecurityException, but the // compiler throws an error. // // BUGBUG: proibably just need to refer to it as System.Security.SecurityException // try { regKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey( registryCacheName + "\\" + type.GUID.ToString() ); if ( regKey == null ) { // BUGBUG: handle error. } regKey.SetValue( "License", (string) this.licenseKeyString ); } catch( UnauthorizedAccessException e ) { Trace.WriteLine("Exception writing to registry."); Trace.WriteLine(e); licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return false; } //catch( SecurityException e) //{ // Trace.WriteLine("Exception writing to registry."); // Trace.WriteLine(e); // licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; // return false; //} if( regKey != null ) { regKey.Close(); } } } // end of if licenseType is runtime using design time eval license */ #endif //delete // // Now we have ensured that we have the right key ( The .lic file // was possibly replaced with a runtime key saved in the registry). // First validate that the key is authentic. // keyIsValid = licenseKey.IsHashValid( licenseComponent.MasterLicensePassword ); if( !keyIsValid ) { if( traceSwitch.TraceVerbose ) { Trace.WriteLine("IsKeyValid: License key is not properly signed. Key=" + licenseKeyString); } Trace.Unindent(); licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return false; } // // If the licenseKey is a runtime evaluation, check if: // - expiration date is expired // - expiration period is longer than maximum allowed // - it has an absolute evaluation date, check if its // not expired // - a 'time-bomb' has been set for the component itself, // and if so, should it go boom. // // If any of these returns a expired or invalid value, declare // that the key is not valid by returning false after setting // the licenseCheckFailureReason. Otherwise return true. // if( licenseKey.Type == LicenseKeyType.RuntimeEvaluation ) { if( traceSwitch.TraceVerbose ) { Trace.WriteLine(" License key is runtime evaluation."); Trace.WriteLine(" Current Date: " + DateTime.Now); Trace.WriteLine(" Expiration Date: " + licenseKey.ExpirationDate); } // // Check whether the current date is beyond the expiration date. // if( DateTime.Now > licenseKey.ExpirationDate ) { if( traceSwitch.TraceVerbose ) { Trace.WriteLine("IsKeyValid: License is expired. ExpirationDate = " + licenseKey.ExpirationDate); } Trace.Unindent(); licenseCheckFailureReason = LicenseCheckFailureReason.EvaluationLicenseExpired; return false; } // // Check whether the expiration date is higher than possible - this points // to someone adjusting their system calender while building a component in // an attempt to have an eval license valid for a long time. // DateTime maxExpirationDate = DateTime.Now.AddDays( licenseKey.EvaluationDays ); if( licenseKey.ExpirationDate > maxExpirationDate ) { if( traceSwitch.TraceVerbose ) { Trace.WriteLine("IsKeyValid: License has invalid expiration date."); Trace.WriteLine(" Evaluation Days: " + licenseKey.EvaluationDays); Trace.WriteLine(" Max Expiration Date: " + maxExpirationDate); Trace.Unindent(); } licenseCheckFailureReason = LicenseCheckFailureReason.InvalidLicenseKey; return false; } } // // If its past the Absolute Expiration Date, fail it even if the evaluation // period is not expired // if( licenseKey.AbsoluteExpirationDate ) { if( traceSwitch.TraceVerbose ) { Trace.WriteLine(" License key has absolute expiration date."); Trace.WriteLine(" Current Date: " + DateTime.Now); Trace.WriteLine(" Expiration Date: " + licenseKey.ExpirationDate); } // // Check whether the current date is beyond the expiration date. // if( DateTime.Now > licenseKey.ExpirationDate ) { Trace.WriteLine("IsKeyValid: License is expired. ExpirationDate="+licenseKey.ExpirationDate); Trace.Unindent(); licenseCheckFailureReason = LicenseCheckFailureReason.EvaluationLicenseExpired; return false; } } // // Check to see if a 'time-bomb' has been set for the component itself. // if( ( licenseComponent.ExpirationDate.Ticks != (long)0 ) && ( DateTime.Now > licenseComponent.ExpirationDate ) ) { if( traceSwitch.TraceVerbose ) { Trace.WriteLine("IsKeyValid: Component has expired. ExpirationDate=" + licenseComponent.ExpirationDate); } Trace.Unindent(); licenseCheckFailureReason = LicenseCheckFailureReason.ComponentExpired; return false; } // // Passed all the tests, so we have a valid key // return true; }