diff --git a/Logic/Security/Authentication.cs b/Logic/Security/Authentication.cs index 68a2dd05..5c244d08 100644 --- a/Logic/Security/Authentication.cs +++ b/Logic/Security/Authentication.cs @@ -81,13 +81,13 @@ namespace Swarmops.Logic.Security bool goodCredentials = false; - if (candidate.PasswordHash == GeneratePasswordHash(candidate, password)) + if (CheckPassword(candidate, password)) { goodCredentials = true; } - else + else if (PilotInstallationIds.IsPilot(PilotInstallationIds.PiratePartySE)) { - // If the most recent password hash mechanism fails, try legacy hashes + // If the most recent password hash mechanism fails, try legacy hashes IF on pilot installation string[] legacyHashes = GenerateLegacyPasswordHashes(candidate, password); @@ -153,23 +153,41 @@ namespace Swarmops.Logic.Security /// The person to generate the hash for. /// The password to hash. /// A hash on the format "C8 5B A2 19 B5..." (29 characters). - internal static string GeneratePasswordHash (Person person, string password) + internal static string GenerateNewPasswordHash (Person person, string password) { + if (person == null) + { + throw new ArgumentNullException("person"); + } + if (person.PersonId == 0) { throw new ArgumentOutOfRangeException("PersonId on the supplied person cannot be 0"); } - return GeneratePasswordHash(person.PersonId, password); + return GenerateNewPasswordHash(person.PersonId, password); } - internal static string GeneratePasswordHash (int personId, string password) + internal static string GenerateNewPasswordHash (int personId, string password) { - return SHA1.Hash(password + personId + "Pirate"); - // return BCrypt.HashPassword(password + personId.ToString(CultureInfo.InvariantCulture), BCrypt.GenerateSalt(12)); WTF doesn't work + string salt = BCrypt.GenerateSalt(12); // Longer than standard (10) on purpose. Should be good through 2020 or so. + string passwordHash = BCrypt.HashPassword(personId.ToString(CultureInfo.InvariantCulture) + password, salt); + + return passwordHash; } + internal static bool CheckPassword(Person person, string password) + { + if (!person.PasswordHash.StartsWith("$2a$12$")) + { + return false; // not a BCrypt hash with work factor 12; fall back to legacy or fail. Caller's problem + } + + return BCrypt.CheckPassword(person.Identity.ToString(CultureInfo.InvariantCulture) + password, + person.PasswordHash); + } + /// /// Generates password hashes for legacy passwords in system (that are no longer generated). /// @@ -294,7 +312,7 @@ namespace Swarmops.Logic.Security foreach (Person p in candidatePeople) { - string encodedPasswordTicket = GeneratePasswordHash(p, passwordTicket); + string encodedPasswordTicket = GenerateNewPasswordHash(p, passwordTicket); p.ResetPasswordTicket = encodedPasswordTicket + ";" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } @@ -356,7 +374,7 @@ namespace Swarmops.Logic.Security { string passwordTicket = CreateRandomPassword(EmailVerificationTicketLength); - string encodedPasswordTicket = GeneratePasswordHash(p.Identity, passwordTicket); + string encodedPasswordTicket = SHA1.Hash(passwordTicket); p.ResetPasswordTicket = encodedPasswordTicket + ";" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string mailbody = ""; @@ -379,7 +397,6 @@ namespace Swarmops.Logic.Security throw new VerificationTicketLengthException("Wrong length of code, should be " + EmailVerificationTicketLength.ToString() + " characters."); } - string encodedPasswordTicket = GeneratePasswordHash(pers, token); string storedTicket = pers.ResetPasswordTicket; string[] storedParts = storedTicket.Split(new char[] {';'}); if (storedParts.Length < 2) @@ -395,7 +412,7 @@ namespace Swarmops.Logic.Security throw new VerificationTicketTooOldException( "Verification code too old, it must be used within 10 hours."); } - else if (encodedPasswordTicket.ToLower().Replace(" ", "") == storedParts[0].ToLower().Replace(" ", "")) + else if (SHA1.Hash(token) == storedParts[0]) { // Yes, proceed to next step } @@ -415,7 +432,7 @@ namespace Swarmops.Logic.Security internal static bool ValidatePassword (Person person, string oldpassword) { - if (person.PasswordHash == GeneratePasswordHash(person, oldpassword)) + if (CheckPassword(person, oldpassword)) { return true; } @@ -488,7 +505,7 @@ namespace Swarmops.Logic.Security string encodedPasswordTicket = - GeneratePasswordHash(p, p.Identity.ToString()).Replace(" ", "").Substring(0, 4) + + SHA1.Hash(p.Identity.ToString(CultureInfo.InvariantCulture)).Replace(" ", "").Substring(0, 4) + p.Identity.ToString(); mailbody = App_LocalResources.Authentication.RequestActivistSignoff_Mail_Preamble; @@ -513,7 +530,7 @@ namespace Swarmops.Logic.Security string encodedPasswordTicket = - GeneratePasswordHash(p, p.Identity.ToString()).Replace(" ", "").Substring(0, 4) + + GenerateNewPasswordHash(p, p.Identity.ToString()).Replace(" ", "").Substring(0, 4) + p.Identity.ToString(); links += "\r\n\r\n"; links += "#" + p.PersonId.ToString(); @@ -541,7 +558,7 @@ namespace Swarmops.Logic.Security public static void ValidateRequestActivistSignoffProcess (Person p, string code) { string encodedPasswordTicket = - GeneratePasswordHash(p, p.Identity.ToString()).Replace(" ", "").Substring(0, 4) + p.Identity.ToString(); + SHA1.Hash(p.Identity.ToString(CultureInfo.InvariantCulture)).Replace(" ", "").Substring(0, 4) + p.Identity.ToString(); if (code != encodedPasswordTicket) { throw new VerificationTicketWrongException("No such code exists."); diff --git a/Logic/Security/BCrypt.cs b/Logic/Security/BCrypt.cs index 155ebf57..50067a61 100644 --- a/Logic/Security/BCrypt.cs +++ b/Logic/Security/BCrypt.cs @@ -494,24 +494,27 @@ namespace Swarmops.Logic.Security uint i, n, l = block[offset], r = block[offset + 1]; l ^= this.p[0]; - for (i = 0; i <= BLOWFISH_NUM_ROUNDS - 2; ) + unchecked // Added as per http://bcrypt.codeplex.com/SourceControl/network/forks/mammo/UncheckedFix/changeset/96b7f8314be4 to fix overflow exception { - // Feistel substitution on left word - n = this.s[(l >> 24) & 0xff]; - n += this.s[0x100 | ((l >> 16) & 0xff)]; - n ^= this.s[0x200 | ((l >> 8) & 0xff)]; - n += this.s[0x300 | (l & 0xff)]; - r ^= n ^ this.p[++i]; + for (i = 0; i <= BLOWFISH_NUM_ROUNDS - 2;) + { + // Feistel substitution on left word + n = this.s[(l >> 24) & 0xff]; + n += this.s[0x100 | ((l >> 16) & 0xff)]; + n ^= this.s[0x200 | ((l >> 8) & 0xff)]; + n += this.s[0x300 | (l & 0xff)]; + r ^= n ^ this.p[++i]; - // Feistel substitution on right word - n = this.s[(r >> 24) & 0xff]; - n += this.s[0x100 | ((r >> 16) & 0xff)]; - n ^= this.s[0x200 | ((r >> 8) & 0xff)]; - n += this.s[0x300 | (r & 0xff)]; - l ^= n ^ this.p[++i]; + // Feistel substitution on right word + n = this.s[(r >> 24) & 0xff]; + n += this.s[0x100 | ((r >> 16) & 0xff)]; + n ^= this.s[0x200 | ((r >> 8) & 0xff)]; + n += this.s[0x300 | (r & 0xff)]; + l ^= n ^ this.p[++i]; + } + block[offset] = r ^ this.p[BLOWFISH_NUM_ROUNDS + 1]; + block[offset + 1] = l; } - block[offset] = r ^ this.p[BLOWFISH_NUM_ROUNDS + 1]; - block[offset + 1] = l; } /// diff --git a/Logic/Swarm/Person.cs b/Logic/Swarm/Person.cs index dacd7940..0db60a82 100644 --- a/Logic/Swarm/Person.cs +++ b/Logic/Swarm/Person.cs @@ -539,7 +539,7 @@ namespace Swarmops.Logic.Swarm if (password.Length > 0) { - newPerson.PasswordHash = Authentication.GeneratePasswordHash(personId, password); + newPerson.PasswordHash = Authentication.GenerateNewPasswordHash(personId, password); } else { @@ -644,7 +644,7 @@ namespace Swarmops.Logic.Swarm public virtual void SetPassword (string newPassword) { - string hash = Authentication.GeneratePasswordHash(Identity, newPassword); + string hash = Authentication.GenerateNewPasswordHash(Identity, newPassword); SwarmDb.GetDatabaseForWriting().SetPersonPasswordHash(Identity, hash); base.PasswordHash = hash; } diff --git a/Site5/Pages/v5/Init/Default.aspx.cs b/Site5/Pages/v5/Init/Default.aspx.cs index c00b31c6..d8411ecb 100644 --- a/Site5/Pages/v5/Init/Default.aspx.cs +++ b/Site5/Pages/v5/Init/Default.aspx.cs @@ -284,6 +284,8 @@ public partial class Pages_v5_Init_Default : System.Web.UI.Page _initProgress = 7; _initMessage = "Creating all countries on local server..."; Thread.Sleep(100); + int count = 0; + int total = countries.Length; // Create all countries in our own database @@ -294,6 +296,9 @@ public partial class Pages_v5_Init_Default : System.Web.UI.Page country.Culture, rootGeographyId, 5, string.Empty); + + count++; + _initMessage = String.Format("Creating all countries on local server... ({0}%)", count*100/total); } _initProgress = 10; @@ -336,8 +341,11 @@ public partial class Pages_v5_Init_Default : System.Web.UI.Page SwarmDb.GetDatabaseForWriting().SetCountryGeographyId(countryIdTranslation[countryCode], countryRootGeographyId); + count = 0; + total = InitDatabaseThreadCountGeographyChildren (geography.Children); + InitDatabaseThreadCreateGeographyChildren(geography.Children, countryRootGeographyId, - ref geographyIdTranslation); + ref geographyIdTranslation, countryCode, ref count, total); _initProgress = 10 + (int) (countryCount*initStepPerCountry + initStepPerCountry/3); _initMessage = "Retrieving cities for " + countryCode + "..."; @@ -456,6 +464,10 @@ public partial class Pages_v5_Init_Default : System.Web.UI.Page Persistence.Key["LastGeographyUpdateId"] = "0"; + // Set an installation ID + + Persistence.Key["SwarmopsInstallationId"] = Guid.NewGuid().ToString(); + // Create initial currencies (European) Currency.Create("EUR", "Euros", "€"); @@ -463,7 +475,7 @@ public partial class Pages_v5_Init_Default : System.Web.UI.Page Currency.Create("CAD", "Canadian Dollars", "CA$"); Currency.Create("SEK", "Swedish Krona", string.Empty); Currency.Create("NOK", "Norwegian Krona", string.Empty); - Currency.Create("DKK", "Danisk Krona", string.Empty); + Currency.Create("DKK", "Danish Krona", string.Empty); Currency.Create("ISK", "Icelandic Krona", string.Empty); Currency.Create("CHF", "Swiss Franc", string.Empty); Currency.Create("GBP", "Pounds Sterling", "£"); @@ -481,17 +493,39 @@ public partial class Pages_v5_Init_Default : System.Web.UI.Page Thread.Sleep(1000); // give some time for static var to stick and web interface to react before killing thread } + private static int InitDatabaseThreadCountGeographyChildren(Swarmops.Site.Automation.Geography[] children) + { + int count = 0; + + foreach (Swarmops.Site.Automation.Geography child in children) + { + count++; + count += InitDatabaseThreadCountGeographyChildren(child.Children); + } + + return count; + } + private static void InitDatabaseThreadCreateGeographyChildren(Swarmops.Site.Automation.Geography[] children, int parentGeographyId, - ref Dictionary geographyIdTranslation) + ref Dictionary geographyIdTranslation, string countryCode, ref int count, int total) { + count++; + foreach (Swarmops.Site.Automation.Geography geography in children) { int newGeographyId = SwarmDb.GetDatabaseForWriting().CreateGeography(geography.Name, parentGeographyId); geographyIdTranslation[geography.GeographyId] = newGeographyId; - InitDatabaseThreadCreateGeographyChildren(geography.Children, newGeographyId, ref geographyIdTranslation); + InitDatabaseThreadCreateGeographyChildren(geography.Children, newGeographyId, ref geographyIdTranslation, countryCode, ref count, total); } + + if (total != 0) + { + _initMessage = String.Format("Setting up geography for {0}... ({1}%)", countryCode, count*100/total); + // shouldn't be here but wtf + } + Thread.Sleep(50); }