2022-06-29 19:46:41 -04:00
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
2017-08-09 17:01:37 -04:00
|
|
|
|
using System.Text;
|
2022-01-21 09:36:25 -05:00
|
|
|
|
using System.Text.Json;
|
2023-11-29 09:18:08 +10:00
|
|
|
|
using Bit.Core.AdminConsole.Entities;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2017-08-09 17:01:37 -04:00
|
|
|
|
using Bit.Core.Models.Business;
|
2017-08-16 23:15:09 -04:00
|
|
|
|
using Bit.Core.Repositories;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2017-08-09 17:01:37 -04:00
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2020-01-10 08:47:58 -05:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2017-08-17 17:10:34 -04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-08-09 17:01:37 -04:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Services;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-08-09 17:01:37 -04:00
|
|
|
|
public class LicensingService : ILicensingService
|
|
|
|
|
|
{
|
2019-07-10 20:05:07 -04:00
|
|
|
|
private readonly X509Certificate2 _certificate;
|
2022-03-21 18:13:00 -04:00
|
|
|
|
private readonly IGlobalSettings _globalSettings;
|
2022-08-29 14:53:16 -04:00
|
|
|
|
private readonly IUserRepository _userRepository;
|
2019-07-10 20:05:07 -04:00
|
|
|
|
private readonly IOrganizationRepository _organizationRepository;
|
|
|
|
|
|
private readonly IOrganizationUserRepository _organizationUserRepository;
|
|
|
|
|
|
private readonly IMailService _mailService;
|
2022-03-21 18:13:00 -04:00
|
|
|
|
private readonly ILogger<LicensingService> _logger;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2019-07-10 20:05:07 -04:00
|
|
|
|
private IDictionary<Guid, DateTime> _userCheckCache = new Dictionary<Guid, DateTime>();
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2022-03-21 18:13:00 -04:00
|
|
|
|
public LicensingService(
|
2017-08-16 23:15:09 -04:00
|
|
|
|
IUserRepository userRepository,
|
2019-07-10 20:05:07 -04:00
|
|
|
|
IOrganizationRepository organizationRepository,
|
2020-03-27 14:36:37 -04:00
|
|
|
|
IOrganizationUserRepository organizationUserRepository,
|
2020-05-18 16:06:34 -04:00
|
|
|
|
IMailService mailService,
|
2020-03-27 14:36:37 -04:00
|
|
|
|
IWebHostEnvironment environment,
|
2017-08-22 15:27:29 -04:00
|
|
|
|
ILogger<LicensingService> logger,
|
2020-03-27 14:36:37 -04:00
|
|
|
|
IGlobalSettings globalSettings)
|
2022-08-29 14:53:16 -04:00
|
|
|
|
{
|
|
|
|
|
|
_userRepository = userRepository;
|
2019-07-10 20:05:07 -04:00
|
|
|
|
_organizationRepository = organizationRepository;
|
|
|
|
|
|
_organizationUserRepository = organizationUserRepository;
|
|
|
|
|
|
_mailService = mailService;
|
|
|
|
|
|
_logger = logger;
|
2020-03-27 14:36:37 -04:00
|
|
|
|
_globalSettings = globalSettings;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2022-05-10 17:12:09 -04:00
|
|
|
|
var certThumbprint = environment.IsDevelopment() ?
|
2021-03-18 07:21:00 +10:00
|
|
|
|
"207E64A231E8AA32AAF68A61037C075EBEBD553F" :
|
2018-07-19 00:02:21 -04:00
|
|
|
|
"B34876439FCDA2846505B2EFBBA6C4A951313EBE";
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (_globalSettings.SelfHosted)
|
2022-08-29 14:53:16 -04:00
|
|
|
|
{
|
2022-05-10 17:12:09 -04:00
|
|
|
|
_certificate = CoreHelpers.GetEmbeddedCertificateAsync(environment.IsDevelopment() ? "licensing_dev.cer" : "licensing.cer", null)
|
2018-07-19 00:02:21 -04:00
|
|
|
|
.GetAwaiter().GetResult();
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
else if (CoreHelpers.SettingHasValue(_globalSettings.Storage?.ConnectionString) &&
|
|
|
|
|
|
CoreHelpers.SettingHasValue(_globalSettings.LicenseCertificatePassword))
|
2019-07-10 20:05:07 -04:00
|
|
|
|
{
|
2021-12-22 19:47:35 +01:00
|
|
|
|
_certificate = CoreHelpers.GetBlobCertificateAsync(globalSettings.Storage.ConnectionString, "certificates",
|
|
|
|
|
|
"licensing.pfx", _globalSettings.LicenseCertificatePassword)
|
|
|
|
|
|
.GetAwaiter().GetResult();
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-07-10 20:05:07 -04:00
|
|
|
|
_certificate = CoreHelpers.GetCertificate(certThumbprint);
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2019-07-10 20:05:07 -04:00
|
|
|
|
if (_certificate == null || !_certificate.Thumbprint.Equals(CoreHelpers.CleanCertificateThumbprint(certThumbprint),
|
|
|
|
|
|
StringComparison.InvariantCultureIgnoreCase))
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2019-07-10 20:05:07 -04:00
|
|
|
|
throw new Exception("Invalid licensing certificate.");
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-10 20:05:07 -04:00
|
|
|
|
if (_globalSettings.SelfHosted && !CoreHelpers.SettingHasValue(_globalSettings.LicenseDirectory))
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2019-07-10 20:05:07 -04:00
|
|
|
|
throw new InvalidOperationException("No license directory.");
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-10 20:05:07 -04:00
|
|
|
|
public async Task ValidateOrganizationsAsync()
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2019-07-10 20:05:07 -04:00
|
|
|
|
if (!_globalSettings.SelfHosted)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-10 20:05:07 -04:00
|
|
|
|
var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync();
|
|
|
|
|
|
_logger.LogInformation(Constants.BypassFiltersEventId, null,
|
2022-11-30 08:40:12 -05:00
|
|
|
|
"Validating licenses for {NumberOfOrganizations} organizations.", enabledOrgs.Count);
|
|
|
|
|
|
|
|
|
|
|
|
var exceptions = new List<Exception>();
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var org in enabledOrgs)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2022-11-30 08:40:12 -05:00
|
|
|
|
try
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2022-11-30 08:40:12 -05:00
|
|
|
|
var license = await ReadOrganizationLicenseAsync(org);
|
|
|
|
|
|
if (license == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
await DisableOrganizationAsync(org, null, "No license file.");
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2022-11-30 08:40:12 -05:00
|
|
|
|
var totalLicensedOrgs = enabledOrgs.Count(o => string.Equals(o.LicenseKey, license.LicenseKey));
|
|
|
|
|
|
if (totalLicensedOrgs > 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
await DisableOrganizationAsync(org, license, "Multiple organizations.");
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2019-07-10 20:05:07 -04:00
|
|
|
|
|
2022-11-30 08:40:12 -05:00
|
|
|
|
if (!license.VerifyData(org, _globalSettings))
|
|
|
|
|
|
{
|
|
|
|
|
|
await DisableOrganizationAsync(org, license, "Invalid data.");
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2017-08-09 17:01:37 -04:00
|
|
|
|
|
2022-11-30 08:40:12 -05:00
|
|
|
|
if (!license.VerifySignature(_certificate))
|
|
|
|
|
|
{
|
|
|
|
|
|
await DisableOrganizationAsync(org, license, "Invalid signature.");
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
2017-08-09 17:01:37 -04:00
|
|
|
|
{
|
2022-11-30 08:40:12 -05:00
|
|
|
|
exceptions.Add(ex);
|
2017-08-09 17:01:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-11-30 08:40:12 -05:00
|
|
|
|
|
|
|
|
|
|
if (exceptions.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new AggregateException("There were one or more exceptions while validating organizations.", exceptions);
|
|
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-04-13 09:25:54 -04:00
|
|
|
|
private async Task DisableOrganizationAsync(Organization org, ILicense license, string reason)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2020-05-19 18:22:03 -04:00
|
|
|
|
_logger.LogInformation(Constants.BypassFiltersEventId, null,
|
|
|
|
|
|
"Organization {0} ({1}) has an invalid license and is being disabled. Reason: {2}",
|
2018-04-13 09:25:54 -04:00
|
|
|
|
org.Id, org.Name, reason);
|
2017-11-06 08:12:36 -05:00
|
|
|
|
org.Enabled = false;
|
2020-05-19 18:22:03 -04:00
|
|
|
|
org.ExpirationDate = license?.Expires ?? DateTime.UtcNow;
|
2017-11-06 08:12:36 -05:00
|
|
|
|
org.RevisionDate = DateTime.UtcNow;
|
|
|
|
|
|
await _organizationRepository.ReplaceAsync(org);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2020-05-19 18:22:03 -04:00
|
|
|
|
await _mailService.SendLicenseExpiredAsync(new List<string> { org.BillingEmail }, org.Name);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2017-08-09 17:01:37 -04:00
|
|
|
|
|
2017-08-17 00:12:11 -04:00
|
|
|
|
public async Task ValidateUsersAsync()
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-08-17 00:12:11 -04:00
|
|
|
|
if (!_globalSettings.SelfHosted)
|
2017-08-09 17:01:37 -04:00
|
|
|
|
{
|
2017-08-17 00:12:11 -04:00
|
|
|
|
return;
|
2017-08-11 22:55:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-06 08:12:36 -05:00
|
|
|
|
var premiumUsers = await _userRepository.GetManyByPremiumAsync(true);
|
2018-08-15 10:54:15 -04:00
|
|
|
|
_logger.LogInformation(Constants.BypassFiltersEventId, null,
|
|
|
|
|
|
"Validating premium for {0} users.", premiumUsers.Count);
|
2017-08-17 17:10:34 -04:00
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var user in premiumUsers)
|
2017-11-06 08:12:36 -05:00
|
|
|
|
{
|
2018-04-13 09:25:54 -04:00
|
|
|
|
await ProcessUserValidationAsync(user);
|
2017-08-09 17:01:37 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2017-08-09 17:01:37 -04:00
|
|
|
|
|
2018-04-13 09:25:54 -04:00
|
|
|
|
public async Task<bool> ValidateUserPremiumAsync(User user)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-04-13 09:25:54 -04:00
|
|
|
|
if (!_globalSettings.SelfHosted)
|
2017-11-06 08:12:36 -05:00
|
|
|
|
{
|
2020-05-19 18:22:03 -04:00
|
|
|
|
return user.Premium;
|
2017-11-06 08:12:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-22 15:27:29 -04:00
|
|
|
|
if (!user.Premium)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-17 00:12:11 -04:00
|
|
|
|
// Only check once per day
|
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
|
if (_userCheckCache.ContainsKey(user.Id))
|
2017-08-09 17:01:37 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
var lastCheck = _userCheckCache[user.Id];
|
|
|
|
|
|
if (lastCheck < now && now - lastCheck < TimeSpan.FromDays(1))
|
2017-08-11 22:55:25 -04:00
|
|
|
|
{
|
|
|
|
|
|
return user.Premium;
|
|
|
|
|
|
}
|
2022-08-29 15:53:48 -04:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-10-11 15:07:22 -04:00
|
|
|
|
_userCheckCache[user.Id] = now;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_userCheckCache.Add(user.Id, now);
|
2017-08-11 17:06:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-11 22:55:25 -04:00
|
|
|
|
_logger.LogInformation(Constants.BypassFiltersEventId, null,
|
2018-10-11 15:07:22 -04:00
|
|
|
|
"Validating premium license for user {0}({1}).", user.Id, user.Email);
|
|
|
|
|
|
return await ProcessUserValidationAsync(user);
|
2017-08-11 22:55:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<bool> ProcessUserValidationAsync(User user)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-10-11 15:07:22 -04:00
|
|
|
|
var license = ReadUserLicense(user);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (license == null)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-10-11 15:07:22 -04:00
|
|
|
|
await DisablePremiumAsync(user, null, "No license file.");
|
2017-08-11 22:55:25 -04:00
|
|
|
|
return false;
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
2017-08-11 22:55:25 -04:00
|
|
|
|
|
2017-08-09 17:01:37 -04:00
|
|
|
|
if (!license.VerifyData(user))
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2017-08-09 17:01:37 -04:00
|
|
|
|
await DisablePremiumAsync(user, license, "Invalid data.");
|
2020-05-19 18:22:03 -04:00
|
|
|
|
return false;
|
2017-08-09 17:01:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!license.VerifySignature(_certificate))
|
2022-08-29 14:53:16 -04:00
|
|
|
|
{
|
2022-01-21 09:36:25 -05:00
|
|
|
|
await DisablePremiumAsync(user, license, "Invalid signature.");
|
|
|
|
|
|
return false;
|
2017-08-09 17:01:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-10 17:12:09 -04:00
|
|
|
|
private async Task DisablePremiumAsync(User user, ILicense license, string reason)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2022-05-10 17:12:09 -04:00
|
|
|
|
_logger.LogInformation(Constants.BypassFiltersEventId, null,
|
|
|
|
|
|
"User {0}({1}) has an invalid license and premium is being disabled. Reason: {2}",
|
|
|
|
|
|
user.Id, user.Email, reason);
|
2022-08-29 14:53:16 -04:00
|
|
|
|
|
2022-05-10 17:12:09 -04:00
|
|
|
|
user.Premium = false;
|
|
|
|
|
|
user.PremiumExpirationDate = license?.Expires ?? DateTime.UtcNow;
|
2020-05-19 18:22:03 -04:00
|
|
|
|
user.RevisionDate = DateTime.UtcNow;
|
2022-05-10 17:12:09 -04:00
|
|
|
|
await _userRepository.ReplaceAsync(user);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-08-09 17:01:37 -04:00
|
|
|
|
await _mailService.SendLicenseExpiredAsync(new List<string> { user.Email });
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-10 17:12:09 -04:00
|
|
|
|
public bool VerifyLicense(ILicense license)
|
2022-08-29 14:53:16 -04:00
|
|
|
|
{
|
2022-05-10 17:12:09 -04:00
|
|
|
|
return license.VerifySignature(_certificate);
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-10 17:12:09 -04:00
|
|
|
|
public byte[] SignLicense(ILicense license)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2022-05-10 17:12:09 -04:00
|
|
|
|
if (_globalSettings.SelfHosted || !_certificate.HasPrivateKey)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2022-05-10 17:12:09 -04:00
|
|
|
|
throw new InvalidOperationException("Cannot sign licenses.");
|
2017-08-09 17:01:37 -04:00
|
|
|
|
}
|
2022-08-29 14:53:16 -04:00
|
|
|
|
|
2022-05-10 17:12:09 -04:00
|
|
|
|
return license.Sign(_certificate);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-10 17:12:09 -04:00
|
|
|
|
private UserLicense ReadUserLicense(User user)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2022-05-10 17:12:09 -04:00
|
|
|
|
var filePath = $"{_globalSettings.LicenseDirectory}/user/{user.Id}.json";
|
|
|
|
|
|
if (!File.Exists(filePath))
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-10 17:12:09 -04:00
|
|
|
|
var data = File.ReadAllText(filePath, Encoding.UTF8);
|
|
|
|
|
|
return JsonSerializer.Deserialize<UserLicense>(data);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-10 17:12:09 -04:00
|
|
|
|
public Task<OrganizationLicense> ReadOrganizationLicenseAsync(Organization organization) =>
|
|
|
|
|
|
ReadOrganizationLicenseAsync(organization.Id);
|
|
|
|
|
|
public async Task<OrganizationLicense> ReadOrganizationLicenseAsync(Guid organizationId)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2022-05-10 17:12:09 -04:00
|
|
|
|
var filePath = Path.Combine(_globalSettings.LicenseDirectory, "organization", $"{organizationId}.json");
|
|
|
|
|
|
if (!File.Exists(filePath))
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-08-09 17:01:37 -04:00
|
|
|
|
return null;
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2022-05-10 17:12:09 -04:00
|
|
|
|
using var fs = File.OpenRead(filePath);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
return await JsonSerializer.DeserializeAsync<OrganizationLicense>(fs);
|
2017-08-09 17:01:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|