2017-08-09 17:01:37 -04:00
|
|
|
|
using Bit.Core.Models.Business;
|
|
|
|
|
|
using Bit.Core.Models.Table;
|
2017-08-16 23:15:09 -04:00
|
|
|
|
using Bit.Core.Repositories;
|
2017-08-09 17:01:37 -04:00
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
|
|
using System.Text;
|
2017-08-16 17:08:20 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2017-08-09 17:01:37 -04:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Services
|
|
|
|
|
|
{
|
2017-08-11 22:55:25 -04:00
|
|
|
|
public class RsaLicensingService : ILicensingService
|
2017-08-09 17:01:37 -04:00
|
|
|
|
{
|
|
|
|
|
|
private readonly X509Certificate2 _certificate;
|
|
|
|
|
|
private readonly GlobalSettings _globalSettings;
|
2017-08-16 17:08:20 -04:00
|
|
|
|
private IDictionary<Guid, DateTime> _userCheckCache = new Dictionary<Guid, DateTime>();
|
|
|
|
|
|
private IDictionary<Guid, DateTime> _organizationCheckCache = new Dictionary<Guid, DateTime>();
|
2017-08-16 23:15:09 -04:00
|
|
|
|
private readonly IUserRepository _userRepository;
|
2017-08-09 17:01:37 -04:00
|
|
|
|
|
2017-08-11 22:55:25 -04:00
|
|
|
|
public RsaLicensingService(
|
2017-08-16 23:15:09 -04:00
|
|
|
|
IUserRepository userRepository,
|
2017-08-09 17:01:37 -04:00
|
|
|
|
IHostingEnvironment environment,
|
|
|
|
|
|
GlobalSettings globalSettings)
|
|
|
|
|
|
{
|
2017-08-16 23:15:09 -04:00
|
|
|
|
_userRepository = userRepository;
|
2017-08-16 17:08:20 -04:00
|
|
|
|
|
2017-08-11 22:55:25 -04:00
|
|
|
|
var certThumbprint = "207e64a231e8aa32aaf68a61037c075ebebd553f";
|
2017-08-09 17:01:37 -04:00
|
|
|
|
_globalSettings = globalSettings;
|
2017-08-11 22:55:25 -04:00
|
|
|
|
_certificate = !_globalSettings.SelfHosted ? CoreHelpers.GetCertificate(certThumbprint)
|
|
|
|
|
|
: CoreHelpers.GetEmbeddedCertificate("licensing.cer", null);
|
|
|
|
|
|
if(_certificate == null || !_certificate.Thumbprint.Equals(CoreHelpers.CleanCertificateThumbprint(certThumbprint),
|
|
|
|
|
|
StringComparison.InvariantCultureIgnoreCase))
|
2017-08-09 17:01:37 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new Exception("Invalid licensing certificate.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!CoreHelpers.SettingHasValue(_globalSettings.LicenseDirectory))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException("No license directory.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool VerifyOrganizationPlan(Organization organization)
|
|
|
|
|
|
{
|
2017-08-11 22:55:25 -04:00
|
|
|
|
if(!_globalSettings.SelfHosted)
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!organization.SelfHost)
|
2017-08-09 17:01:37 -04:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var license = ReadOrganiztionLicense(organization);
|
|
|
|
|
|
return license != null && license.VerifyData(organization) && license.VerifySignature(_certificate);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-16 17:08:20 -04:00
|
|
|
|
public async Task<bool> VerifyUserPremiumAsync(User user)
|
2017-08-09 17:01:37 -04:00
|
|
|
|
{
|
2017-08-11 22:55:25 -04:00
|
|
|
|
if(!_globalSettings.SelfHosted)
|
|
|
|
|
|
{
|
|
|
|
|
|
return user.Premium;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-09 17:01:37 -04:00
|
|
|
|
if(!user.Premium)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-16 17:08:20 -04:00
|
|
|
|
// Only check once per day
|
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
|
if(_userCheckCache.ContainsKey(user.Id))
|
|
|
|
|
|
{
|
|
|
|
|
|
var lastCheck = _userCheckCache[user.Id];
|
2017-08-16 23:45:40 -04:00
|
|
|
|
if(lastCheck < now && now - lastCheck < TimeSpan.FromDays(1))
|
2017-08-16 17:08:20 -04:00
|
|
|
|
{
|
|
|
|
|
|
return user.Premium;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_userCheckCache[user.Id] = now;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_userCheckCache.Add(user.Id, now);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-09 17:01:37 -04:00
|
|
|
|
var license = ReadUserLicense(user);
|
2017-08-16 17:08:20 -04:00
|
|
|
|
var licensedForPremium = license != null && license.VerifyData(user) && license.VerifySignature(_certificate);
|
|
|
|
|
|
if(!licensedForPremium)
|
|
|
|
|
|
{
|
2017-08-16 23:15:09 -04:00
|
|
|
|
user.Premium = false;
|
|
|
|
|
|
user.PremiumExpirationDate = license.Expires;
|
|
|
|
|
|
user.RevisionDate = DateTime.UtcNow;
|
|
|
|
|
|
await _userRepository.ReplaceAsync(user);
|
2017-08-16 17:08:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return licensedForPremium;
|
2017-08-09 17:01:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-11 17:06:31 -04:00
|
|
|
|
public bool VerifyLicense(ILicense license)
|
|
|
|
|
|
{
|
|
|
|
|
|
return license.VerifySignature(_certificate);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-11 22:55:25 -04:00
|
|
|
|
public byte[] SignLicense(ILicense license)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(_globalSettings.SelfHosted || !_certificate.HasPrivateKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException("Cannot sign licenses.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return license.Sign(_certificate);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-09 17:01:37 -04:00
|
|
|
|
private UserLicense ReadUserLicense(User user)
|
|
|
|
|
|
{
|
2017-08-11 22:55:25 -04:00
|
|
|
|
var filePath = $"{_globalSettings.LicenseDirectory}/user/{user.Id}.json";
|
2017-08-09 17:01:37 -04:00
|
|
|
|
if(!File.Exists(filePath))
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var data = File.ReadAllText(filePath, Encoding.UTF8);
|
2017-08-16 17:08:20 -04:00
|
|
|
|
return JsonConvert.DeserializeObject<UserLicense>(data);
|
2017-08-09 17:01:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private OrganizationLicense ReadOrganiztionLicense(Organization organization)
|
|
|
|
|
|
{
|
2017-08-11 22:55:25 -04:00
|
|
|
|
var filePath = $"{_globalSettings.LicenseDirectory}/organization/{organization.Id}.json";
|
2017-08-09 17:01:37 -04:00
|
|
|
|
if(!File.Exists(filePath))
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var data = File.ReadAllText(filePath, Encoding.UTF8);
|
2017-08-16 17:08:20 -04:00
|
|
|
|
return JsonConvert.DeserializeObject<OrganizationLicense>(data);
|
2017-08-09 17:01:37 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|