| | 1 | | using System.Security.Cryptography; |
| | 2 | | using System.Security.Cryptography.X509Certificates; |
| | 3 | | using LGDXRobotCloud.API.Configurations; |
| | 4 | | using LGDXRobotCloud.API.Exceptions; |
| | 5 | | using LGDXRobotCloud.Data.DbContexts; |
| | 6 | | using LGDXRobotCloud.Data.Entities; |
| | 7 | | using LGDXRobotCloud.Data.Models.Business.Administration; |
| | 8 | | using LGDXRobotCloud.Utilities.Helpers; |
| | 9 | | using Microsoft.EntityFrameworkCore; |
| | 10 | | using Microsoft.Extensions.Options; |
| | 11 | |
|
| | 12 | | namespace LGDXRobotCloud.API.Services.Administration; |
| | 13 | |
|
| | 14 | | public interface IRobotCertificateService |
| | 15 | | { |
| | 16 | | Task<(IEnumerable<RobotCertificateListBusinessModel>, PaginationHelper)> GetRobotCertificatesAsync(int pageNumber, int |
| | 17 | | Task<RobotCertificateBusinessModel> GetRobotCertificateAsync(Guid robotCertificateId); |
| | 18 | | RobotCertificateIssueBusinessModel IssueRobotCertificate(Guid robotId); |
| | 19 | | Task<RobotCertificateRenewBusinessModel> RenewRobotCertificateAsync(RobotCertificateRenewRequestBusinessModel robotCer |
| | 20 | |
|
| | 21 | | RootCertificateBusinessModel? GetRootCertificate(); |
| | 22 | | } |
| | 23 | |
|
| 0 | 24 | | public class RobotCertificateService( |
| 0 | 25 | | LgdxContext context, |
| 0 | 26 | | IOptionsSnapshot<LgdxRobotCloudConfiguration> options |
| 0 | 27 | | ) : IRobotCertificateService |
| | 28 | | { |
| 0 | 29 | | private readonly LgdxContext _context = context; |
| 0 | 30 | | private readonly LgdxRobotCloudConfiguration _lgdxRobotCloudConfiguration = options.Value; |
| | 31 | |
|
| | 32 | | private record CertificateDetail |
| | 33 | | { |
| 0 | 34 | | required public string RootCertificate { get; set; } |
| 0 | 35 | | required public string RobotCertificatePrivateKey { get; set; } |
| 0 | 36 | | required public string RobotCertificatePublicKey { get; set; } |
| 0 | 37 | | required public string RobotCertificateThumbprint { get; set; } |
| 0 | 38 | | required public DateTime RobotCertificateNotBefore { get; set; } |
| 0 | 39 | | required public DateTime RobotCertificateNotAfter { get; set; } |
| | 40 | | } |
| | 41 | |
|
| | 42 | | public async Task<(IEnumerable<RobotCertificateListBusinessModel>, PaginationHelper)> GetRobotCertificatesAsync(int pa |
| 0 | 43 | | { |
| 0 | 44 | | var query = _context.RobotCertificates as IQueryable<RobotCertificate>; |
| 0 | 45 | | var itemCount = await query.CountAsync(); |
| 0 | 46 | | var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize); |
| 0 | 47 | | var robotCertificates = await query.AsNoTracking() |
| 0 | 48 | | .OrderBy(a => a.Id) |
| 0 | 49 | | .Skip(pageSize * (pageNumber - 1)) |
| 0 | 50 | | .Take(pageSize) |
| 0 | 51 | | .Select(a => new RobotCertificateListBusinessModel { |
| 0 | 52 | | Id = a.Id, |
| 0 | 53 | | Thumbprint = a.Thumbprint, |
| 0 | 54 | | ThumbprintBackup = a.ThumbprintBackup, |
| 0 | 55 | | NotBefore = a.NotBefore, |
| 0 | 56 | | NotAfter = a.NotAfter |
| 0 | 57 | | }) |
| 0 | 58 | | .ToListAsync(); |
| 0 | 59 | | return (robotCertificates, PaginationHelper); |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | public async Task<RobotCertificateBusinessModel> GetRobotCertificateAsync(Guid robotCertificateId) |
| 0 | 63 | | { |
| 0 | 64 | | return await _context.RobotCertificates.AsNoTracking() |
| 0 | 65 | | .Where(a => a.Id == robotCertificateId) |
| 0 | 66 | | .Include(a => a.Robot) |
| 0 | 67 | | .Select(a => new RobotCertificateBusinessModel { |
| 0 | 68 | | Id = a.Id, |
| 0 | 69 | | RobotId = a.Robot.Id, |
| 0 | 70 | | RobotName = a.Robot.Name, |
| 0 | 71 | | Thumbprint = a.Thumbprint, |
| 0 | 72 | | ThumbprintBackup = a.ThumbprintBackup, |
| 0 | 73 | | NotBefore = a.NotBefore, |
| 0 | 74 | | NotAfter = a.NotAfter |
| 0 | 75 | | }) |
| 0 | 76 | | .FirstOrDefaultAsync() |
| 0 | 77 | | ?? throw new LgdxNotFound404Exception(); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | private CertificateDetail GenerateRobotCertificate(Guid robotId) |
| 0 | 81 | | { |
| 0 | 82 | | X509Store store = new(StoreName.My, StoreLocation.CurrentUser); |
| 0 | 83 | | store.Open(OpenFlags.OpenExistingOnly); |
| 0 | 84 | | X509Certificate2 rootCertificate = store.Certificates.First(c => c.SerialNumber == _lgdxRobotCloudConfiguration.Root |
| | 85 | |
|
| 0 | 86 | | var certificateNotBefore = DateTime.UtcNow; |
| 0 | 87 | | var certificateNotAfter = DateTimeOffset.UtcNow.AddDays(_lgdxRobotCloudConfiguration.RobotCertificateValidDay); |
| | 88 | |
|
| 0 | 89 | | var rsa = RSA.Create(); |
| 0 | 90 | | var certificateRequest = new CertificateRequest("CN=LGDXRobot Cloud Robot Certificate for " + robotId.ToString() + " |
| 0 | 91 | | var certificate = certificateRequest.Create(rootCertificate, certificateNotBefore, certificateNotAfter, RandomNumber |
| | 92 | |
|
| 0 | 93 | | return new CertificateDetail { |
| 0 | 94 | | RootCertificate = rootCertificate.ExportCertificatePem(), |
| 0 | 95 | | RobotCertificatePrivateKey = new string(PemEncoding.Write("PRIVATE KEY", rsa.ExportPkcs8PrivateKey())), |
| 0 | 96 | | RobotCertificatePublicKey = certificate.ExportCertificatePem(), |
| 0 | 97 | | RobotCertificateThumbprint = certificate.Thumbprint, |
| 0 | 98 | | RobotCertificateNotBefore = certificateNotBefore, |
| 0 | 99 | | RobotCertificateNotAfter = certificateNotAfter.DateTime |
| 0 | 100 | | }; |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | public RobotCertificateIssueBusinessModel IssueRobotCertificate(Guid robotId) |
| 0 | 104 | | { |
| 0 | 105 | | var certificate = GenerateRobotCertificate(robotId); |
| 0 | 106 | | return new RobotCertificateIssueBusinessModel { |
| 0 | 107 | | RootCertificate = certificate.RootCertificate, |
| 0 | 108 | | RobotCertificatePrivateKey = certificate.RobotCertificatePrivateKey, |
| 0 | 109 | | RobotCertificatePublicKey = certificate.RobotCertificatePublicKey, |
| 0 | 110 | | RobotCertificateThumbprint = certificate.RobotCertificateThumbprint, |
| 0 | 111 | | RobotCertificateNotBefore = certificate.RobotCertificateNotBefore, |
| 0 | 112 | | RobotCertificateNotAfter = certificate.RobotCertificateNotAfter |
| 0 | 113 | | }; |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | public async Task<RobotCertificateRenewBusinessModel> RenewRobotCertificateAsync(RobotCertificateRenewRequestBusinessM |
| 0 | 117 | | { |
| 0 | 118 | | var certificate = _context.RobotCertificates |
| 0 | 119 | | .Where(c => c.Id == robotCertificateRenewRequestBusinessModel.CertificateId) |
| 0 | 120 | | .FirstOrDefault() |
| 0 | 121 | | ?? throw new LgdxNotFound404Exception(); |
| | 122 | |
|
| 0 | 123 | | var newCertificate = GenerateRobotCertificate(certificate.RobotId); |
| 0 | 124 | | if (robotCertificateRenewRequestBusinessModel.RevokeOldCertificate) |
| 0 | 125 | | { |
| 0 | 126 | | certificate.ThumbprintBackup = null; |
| 0 | 127 | | } |
| | 128 | | else |
| 0 | 129 | | { |
| 0 | 130 | | certificate.ThumbprintBackup = certificate.Thumbprint; |
| 0 | 131 | | } |
| 0 | 132 | | certificate.Thumbprint = newCertificate.RobotCertificateThumbprint; |
| 0 | 133 | | certificate.NotBefore = DateTime.SpecifyKind(newCertificate.RobotCertificateNotBefore, DateTimeKind.Utc); |
| 0 | 134 | | certificate.NotAfter = DateTime.SpecifyKind(newCertificate.RobotCertificateNotAfter, DateTimeKind.Utc); |
| 0 | 135 | | await _context.SaveChangesAsync(); |
| | 136 | |
|
| 0 | 137 | | var robot = await _context.Robots.AsNoTracking() |
| 0 | 138 | | .Where(r => r.Id == certificate.RobotId) |
| 0 | 139 | | .Select(r => new { |
| 0 | 140 | | r.Id, |
| 0 | 141 | | r.Name |
| 0 | 142 | | }) |
| 0 | 143 | | .FirstOrDefaultAsync() |
| 0 | 144 | | ?? throw new LgdxNotFound404Exception(); |
| | 145 | |
|
| 0 | 146 | | return new RobotCertificateRenewBusinessModel { |
| 0 | 147 | | RobotId = robot.Id, |
| 0 | 148 | | RobotName = robot.Name, |
| 0 | 149 | | RootCertificate = newCertificate.RootCertificate, |
| 0 | 150 | | RobotCertificatePrivateKey = newCertificate.RobotCertificatePrivateKey, |
| 0 | 151 | | RobotCertificatePublicKey = newCertificate.RobotCertificatePublicKey |
| 0 | 152 | | }; |
| 0 | 153 | | } |
| | 154 | |
|
| | 155 | | public RootCertificateBusinessModel? GetRootCertificate() |
| 0 | 156 | | { |
| 0 | 157 | | X509Store store = new(StoreName.My, StoreLocation.CurrentUser); |
| 0 | 158 | | store.Open(OpenFlags.OpenExistingOnly); |
| 0 | 159 | | X509Certificate2 rootCertificate = store.Certificates.First(c => c.SerialNumber == _lgdxRobotCloudConfiguration.Root |
| 0 | 160 | | return new RootCertificateBusinessModel { |
| 0 | 161 | | NotBefore = rootCertificate.NotBefore.ToUniversalTime(), |
| 0 | 162 | | NotAfter = rootCertificate.NotAfter.ToUniversalTime(), |
| 0 | 163 | | PublicKey = rootCertificate.ExportCertificatePem() |
| 0 | 164 | | }; |
| 0 | 165 | | } |
| | 166 | | } |