Ejemplo n.º 1
0
        public async Task <IActionResult> GenerateKeyPair([FromServices] IRsaKeyPairService rsaKeyPairService,
                                                          [FromServices] KeyPairAppService keyPairAppService, int?keyStrength = 2048)
        {
            if (!keyStrength.HasValue)
            {
                return(RedirectToAction(nameof(GenerateKeyPair), new { keyStrength = 2048 }));
            }

            var keyPair = rsaKeyPairService.GenerateRsaPrivateAndPublicKeyPair(keyStrength.Value);

            Regex regex         = new Regex(@"s/\x00//g;");
            var   keyPairEntity = new KeyPair()
            {
                AccountSysid = this.UserSession.UserSysId,
                TenantId     = this.UserSession.TenantId.Value,
                PrivateKey   = regex.Replace(CertificateUtil.ExportToByte(keyPair.Private).ConvertToBase64String(), string.Empty),
                PublicKey    = regex.Replace(CertificateUtil.ExportToByte(keyPair.Public).ConvertToBase64String(), string.Empty)
            };

            await keyPairAppService.InsertAsync(keyPairEntity);

            await _agileLabsDbContext.SaveChangesAsync();

            ViewBag.PrivateKey = CertificateUtil.ExportToByte(keyPair.Private).ConvertToUTF8String().Replace(Environment.NewLine, "<br/>");
            ViewBag.PublicKey  = CertificateUtil.ExportToByte(keyPair.Public).ConvertToUTF8String().Replace(Environment.NewLine, "<br/>");

            return(View());
        }
        public async Task <IActionResult> Create([FromServices] IDeviceCertificateService deviceCertificateService,
                                                 [FromServices] IRsaKeyPairService rsaKeyPairService,
                                                 [FromServices] KeyPairAppService keyPairAppService,
                                                 [Bind] CertificateSignRequestCreateViewModel model,
                                                 [Bind] Guid?keyPairSysid = null)
        {
            if (ModelState.IsValid)
            {
                var x509NameList = BUildNameList(model);

                if (!x509NameList.Any())
                {
                    throw new Exception("X509 Name can't be empty");
                }

                AsymmetricCipherKeyPair keyPair = null;
                if (keyPairSysid.HasValue)
                {
                    var keyPairEntity = tenantContext.KeyPairs.SingleOrDefault(x => x.Sysid == keyPairSysid.Value);
                    keyPair = CertificateUtil.ReadPrivateKey(keyPairEntity.PrivateKey.ConvertBase64ToUTF8());
                }
                else
                {
                    var keyPairEntity = keyPairAppService.GenerateKeyPair(out keyPair, model.CommonName);
                    keyPairSysid = keyPairEntity.Sysid;
                    await keyPairAppService.InsertAsync(keyPairEntity);
                }

                var x509Name = string.Join(", ", x509NameList);
                var sans     = new List <string> {
                    model.CommonName
                };
                var csr = deviceCertificateService.CreateCertificateSignRequest(keyPair.Public, x509Name, sans, keyPair.Private);

                var verifyResult = csr.Verify(keyPair.Public);

                var publicKey = csr.GetPublicKey();

                tenantContext.DeviceCertificates.Add(new DeviceCertificate
                {
                    Name         = model.CommonName,
                    SignRequest  = CertificateUtil.ExportToByte(csr).ConvertToBase64String(),
                    AccountSysid = this.UserSession.UserSysId,
                    KeyPairSysid = keyPairSysid.Value
                });
                await tenantContext.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> ImpartAKeyPair([Bind] string privateKey, [FromServices] KeyPairAppService keyPairAppService)
        {
            var keyPair = CertificateUtil.ReadPrivateKey(privateKey);

            var keyPairEntity = new KeyPair()
            {
                Name         = Guid.NewGuid().ToString(),
                AccountSysid = UserSession.UserSysId,
                PrivateKey   = CertificateUtil.ExportToByte(keyPair.Private).ConvertToBase64String(),
                PublicKey    = CertificateUtil.ExportToByte(keyPair.Public).ConvertToBase64String()
            };

            await keyPairAppService.InsertAsync(keyPairEntity);

            await _agileLabsDbContext.SaveChangesAsync();

            ViewBag.RawPrivateKey = privateKey.Replace(Environment.NewLine, "<br/>");
            ViewBag.PrivateKey    = CertificateUtil.ExportToByte(keyPair.Private).ConvertToUTF8String().Replace(Environment.NewLine, "<br/>");
            ViewBag.PublicKey     = CertificateUtil.ExportToByte(keyPair.Public).ConvertToUTF8String().Replace(Environment.NewLine, "<br/>");
            await Task.CompletedTask;

            return(View());
        }