示例#1
0
        public static tbl_PrivateKey CreatePrivKey(IConfiguration conf, IUnitOfWork uow, tbl_User user,
                                                   SshHostKeyAlgorithm keyAlgo, int privKeySize, string privKeyPass, SignatureHashAlgorithm sigAlgo, string comment)
        {
            var callPath   = $"{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}";
            var privId     = Guid.NewGuid();
            var pubId      = Guid.NewGuid();
            var privStream = new MemoryStream();
            var pubStream  = new MemoryStream();
            var keyPair    = SshPrivateKey.Generate(keyAlgo, privKeySize);

            keyPair.Save(privStream, privKeyPass, SshPrivateKeyFormat.Pkcs8);
            keyPair.SavePublicKey(pubStream, SshPublicKeyFormat.Pkcs8);

            var privKey = uow.PrivateKeys.Create(
                new tbl_PrivateKey
            {
                Id          = privId,
                PublicKeyId = pubId,
                IdentityId  = user.IdentityId,
                KeyValue    = Encoding.ASCII.GetString(privStream.ToArray()),
                KeyAlgo     = keyPair.KeyAlgorithm.ToString(),
                KeyPass     = AES.EncryptString(privKeyPass, conf["Databases:AuroraSecret"]),
                KeyFormat   = SshPrivateKeyFormat.Pkcs8.ToString(),
                Enabled     = true,
                Deletable   = true,
                Created     = DateTime.Now,
                LastUpdated = null,
            });

            Log.Information($"'{callPath}' '{user.IdentityAlias}' private key algo {keyPair.KeyAlgorithm} sig {keyPair.Fingerprint.ToString(sigAlgo, false)}" +
                            $"{Environment.NewLine}{privKey.KeyValue}");

            var pubKey = uow.PublicKeys.Create(
                new tbl_PublicKey
            {
                Id           = pubId,
                PrivateKeyId = privId,
                IdentityId   = user.IdentityId,
                KeyValue     = Encoding.ASCII.GetString(pubStream.ToArray()),
                KeyAlgo      = keyPair.KeyAlgorithm.ToString(),
                KeyFormat    = SshPublicKeyFormat.Pkcs8.ToString(),
                SigValue     = keyPair.Fingerprint.ToString(sigAlgo, false),
                SigAlgo      = sigAlgo.ToString(),
                Comment      = comment,
                Enabled      = true,
                Deletable    = true,
                Created      = DateTime.Now,
                LastUpdated  = null,
            });

            Log.Information($"'{callPath}' '{user.IdentityAlias}' public key algo {keyPair.KeyAlgorithm} sig {keyPair.Fingerprint.ToString(sigAlgo, false)}" +
                            $"{Environment.NewLine}{pubKey.KeyValue}");

            uow.Commit();

            return(privKey);
        }
示例#2
0
        public static tbl_PublicKey ImportPubKey(IUnitOfWork uow, tbl_User user,
                                                 SignatureHashAlgorithm sigAlgo, string hostname, FileInfo inputFile)
        {
            var callPath = $"{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}";

            tbl_PublicKey pubKeyEntity = null;
            var           pubKey       = new SshPublicKey(inputFile.FullName);
            var           pubKeyStream = new MemoryStream();

            pubKey.SavePublicKey(pubKeyStream, SshPublicKeyFormat.Pkcs8);

            var pubKeyValue = Encoding.ASCII.GetString(pubKeyStream.ToArray());
            var pubKeyFound = uow.PublicKeys.Get(QueryExpressionFactory.GetQueryExpression <tbl_PublicKey>()
                                                 .Where(x => x.Id == user.IdentityId && x.KeyValue == pubKeyValue).ToLambda());

            if (pubKeyFound == null)
            {
                pubKeyEntity = uow.PublicKeys.Create(
                    new tbl_PublicKey
                {
                    Id          = Guid.NewGuid(),
                    IdentityId  = user.IdentityId,
                    KeyValue    = pubKeyValue,
                    KeyAlgo     = pubKey.KeyAlgorithm.ToString(),
                    KeyFormat   = SshPublicKeyFormat.Pkcs8.ToString(),
                    SigValue    = pubKey.Fingerprint.ToString(sigAlgo, false),
                    SigAlgo     = sigAlgo.ToString(),
                    Comment     = hostname,
                    Enabled     = true,
                    Deletable   = true,
                    Created     = DateTime.Now,
                    LastUpdated = null,
                });

                Log.Information($"'{callPath}' '{user.IdentityAlias}' public key algo {pubKey.KeyAlgorithm} sig {pubKey.Fingerprint.ToString(sigAlgo, false)}" +
                                $"{Environment.NewLine}{pubKeyValue}");
            }
            else
            {
                Log.Warning($"'{callPath}' '{user.IdentityAlias}' skip public key algo {pubKey.KeyAlgorithm} sig {pubKey.Fingerprint.ToString(sigAlgo, false)}" +
                            $"{Environment.NewLine}{pubKeyValue}");
            }

            uow.Commit();

            return(pubKeyEntity);
        }
示例#3
0
        /*
         * openssh uses base64 and special formatting for public keys like with "authorized_keys"
         * https://man.openbsd.org/ssh-keygen
         */
        public static ICollection <tbl_PublicKey> ImportPubKeyBase64(IUnitOfWork uow, tbl_User user,
                                                                     SignatureHashAlgorithm sigAlgo, FileInfo inputFile)
        {
            var callPath    = $"{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}";
            var pubKeyLines = File.ReadAllLines(inputFile.FullName);
            var pubKeys     = new List <tbl_PublicKey>();

            foreach (var line in pubKeyLines)
            {
                var base64 = line.Split(' ');

                switch (base64[0])
                {
                case "ssh-dsa":
                    break;

                case "ssh-rsa":
                    break;

                //case "ecdsa-sha2-nistp256":
                //	break;

                //case "ecdsa-sha2-nistp384":
                //	break;

                //case "ecdsa-sha2-nistp521":
                //	break;

                //case "ssh-ed25519":
                //	break;

                default:
                {
                    Log.Warning($"'{callPath}' '{user.IdentityAlias}' algorithm {base64[0]} not supported");
                    continue;
                }
                }

                var pubKey       = new SshPublicKey(Convert.FromBase64String(base64[1]));
                var pubKeyStream = new MemoryStream();
                pubKey.SavePublicKey(pubKeyStream, SshPublicKeyFormat.Pkcs8);

                var pubKeyValue = Encoding.ASCII.GetString(pubKeyStream.ToArray());

                if (!uow.PublicKeys.Get(QueryExpressionFactory.GetQueryExpression <tbl_PublicKey>()
                                        .Where(x => x.Id == user.IdentityId && x.KeyValue == pubKeyValue).ToLambda()).Any())
                {
                    var newPubKey = uow.PublicKeys.Create(
                        new tbl_PublicKey
                    {
                        Id          = Guid.NewGuid(),
                        IdentityId  = user.IdentityId,
                        KeyValue    = pubKeyValue,
                        KeyAlgo     = pubKey.KeyAlgorithm.ToString(),
                        KeyFormat   = SshPublicKeyFormat.Pkcs8.ToString(),
                        SigValue    = pubKey.Fingerprint.ToString(sigAlgo, false),
                        SigAlgo     = sigAlgo.ToString(),
                        Comment     = base64[2],
                        Enabled     = true,
                        Deletable   = true,
                        Created     = DateTime.Now,
                        LastUpdated = null,
                    });

                    pubKeys.Add(newPubKey);

                    Log.Information($"'{callPath}' '{user.IdentityAlias}' public key algo {pubKey.KeyAlgorithm} sig {pubKey.Fingerprint.ToString(sigAlgo, false)}" +
                                    $"{Environment.NewLine}{pubKeyValue}");
                }
                else
                {
                    Log.Warning($"'{callPath}' '{user.IdentityAlias}' skip public key algo {pubKey.KeyAlgorithm} sig {pubKey.Fingerprint.ToString(sigAlgo, false)}" +
                                $"{Environment.NewLine}{pubKeyValue}");
                }
            }

            uow.Commit();

            return(pubKeys);
        }
示例#4
0
        public static tbl_PrivateKey ImportPrivKey(IConfiguration conf, IUnitOfWork uow, tbl_User user,
                                                   string privKeyPass, SignatureHashAlgorithm sigAlgo, string comment, FileInfo inputFile)
        {
            var callPath = $"{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}";

            tbl_PrivateKey pubKey     = null;
            var            keyPair    = new SshPrivateKey(inputFile.FullName, privKeyPass);
            var            privId     = Guid.NewGuid();
            var            pubId      = Guid.NewGuid();
            var            privStream = new MemoryStream();
            var            pubStream  = new MemoryStream();

            keyPair.Save(privStream, privKeyPass, SshPrivateKeyFormat.Pkcs8);
            keyPair.SavePublicKey(pubStream, SshPublicKeyFormat.Pkcs8);

            var privKeyValue = Encoding.ASCII.GetString(privStream.ToArray());
            var pubKeyValue  = Encoding.ASCII.GetString(pubStream.ToArray());

            var privKeyFound = uow.PrivateKeys.Get(QueryExpressionFactory.GetQueryExpression <tbl_PrivateKey>()
                                                   .Where(x => x.Id == user.IdentityId && x.KeyValue == privKeyValue).ToLambda())
                               .SingleOrDefault();

            var pubKeyFound = uow.PublicKeys.Get(QueryExpressionFactory.GetQueryExpression <tbl_PublicKey>()
                                                 .Where(x => x.Id == user.IdentityId && x.KeyValue == pubKeyValue).ToLambda())
                              .SingleOrDefault();

            if (privKeyFound == null &&
                pubKeyFound == null)
            {
                uow.PrivateKeys.Create(
                    new tbl_PrivateKey
                {
                    Id          = privId,
                    PublicKeyId = pubId,
                    IdentityId  = user.IdentityId,
                    KeyValue    = privKeyValue,
                    KeyAlgo     = keyPair.KeyAlgorithm.ToString(),
                    KeyPass     = AES.EncryptString(privKeyPass, conf["Databases:AuroraSecret"]),
                    KeyFormat   = SshPrivateKeyFormat.Pkcs8.ToString(),
                    Enabled     = true,
                    Deletable   = true,
                    Created     = DateTime.Now,
                    LastUpdated = null,
                });

                Log.Information($"'{callPath}' '{user.IdentityAlias}' private key algo {keyPair.KeyAlgorithm} sig {keyPair.Fingerprint.ToString(sigAlgo, false)}" +
                                $"{Environment.NewLine}{privKeyValue}");

                uow.PublicKeys.Create(
                    new tbl_PublicKey
                {
                    Id           = pubId,
                    PrivateKeyId = privId,
                    IdentityId   = user.IdentityId,
                    KeyValue     = pubKeyValue,
                    KeyAlgo      = keyPair.KeyAlgorithm.ToString(),
                    KeyFormat    = SshPublicKeyFormat.Pkcs8.ToString(),
                    SigValue     = keyPair.Fingerprint.ToString(sigAlgo, false),
                    SigAlgo      = sigAlgo.ToString(),
                    Comment      = comment,
                    Enabled      = true,
                    Deletable    = true,
                    Created      = DateTime.Now,
                    LastUpdated  = null,
                });

                Log.Information($"'{callPath}' '{user.IdentityAlias}' public key algo {keyPair.KeyAlgorithm} sig {keyPair.Fingerprint.ToString(sigAlgo, false)}" +
                                $"{Environment.NewLine}{pubKeyValue}");
            }
            else if (privKeyFound != null &&
                     pubKeyFound == null)
            {
                uow.PublicKeys.Create(
                    new tbl_PublicKey
                {
                    Id           = pubId,
                    PrivateKeyId = privKeyFound.Id,
                    IdentityId   = user.IdentityId,
                    KeyValue     = pubKeyValue,
                    KeyAlgo      = keyPair.KeyAlgorithm.ToString(),
                    KeyFormat    = SshPublicKeyFormat.Pkcs8.ToString(),
                    SigValue     = keyPair.Fingerprint.ToString(sigAlgo, false),
                    SigAlgo      = sigAlgo.ToString(),
                    Comment      = comment,
                    Enabled      = true,
                    Deletable    = true,
                    Created      = DateTime.Now,
                    LastUpdated  = null,
                });

                Log.Information($"'{callPath}' '{user.IdentityAlias}' public key algo {keyPair.KeyAlgorithm} sig {keyPair.Fingerprint.ToString(sigAlgo, false)}" +
                                $"{Environment.NewLine}{pubKeyValue}");
            }
            else if (privKeyFound == null &&
                     pubKeyFound != null)
            {
                uow.PrivateKeys.Create(
                    new tbl_PrivateKey
                {
                    Id          = privId,
                    PublicKeyId = pubKeyFound.Id,
                    IdentityId  = user.IdentityId,
                    KeyValue    = privKeyValue,
                    KeyAlgo     = keyPair.Fingerprint.ToString(sigAlgo, false).ToString(),
                    KeyPass     = AES.EncryptString(privKeyPass, conf["Databases:AuroraSecret"]),
                    Enabled     = true,
                    Deletable   = true,
                    Created     = DateTime.Now,
                    LastUpdated = null,
                });

                Log.Information($"'{callPath}' '{user.IdentityAlias}' private key algo {keyPair.KeyAlgorithm} sig {keyPair.Fingerprint.ToString(sigAlgo, false)}" +
                                $"{Environment.NewLine}{privKeyValue}");
            }

            uow.Commit();

            return(pubKey);
        }