예제 #1
0
파일: UserHelper.cs 프로젝트: bhbk/fm3na7zy
        public static bool ValidatePubKey(ICollection <tbl_PublicKey> userKeys, SshPublicKey loginKey)
        {
            var loginStream = new MemoryStream();

            loginKey.SavePublicKey(loginStream, SshPublicKeyFormat.Pkcs8);

            var loginValue = Encoding.ASCII.GetString(loginStream.ToArray());

            foreach (var userKey in userKeys)
            {
                var pubKeyBytes = Encoding.ASCII.GetBytes(userKey.KeyValue);
                var pubKeyInfo  = new PublicKeyInfo();
                pubKeyInfo.Load(new MemoryStream(pubKeyBytes));

                var pubStream = new MemoryStream();
                var pubKey    = new SshPublicKey(pubKeyInfo);
                pubKey.SavePublicKey(pubStream, SshPublicKeyFormat.Pkcs8);

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

                if (loginValue == pubKeyValue)
                {
                    return(true);
                }
            }

            return(false);
        }
예제 #2
0
파일: KeyHelper.cs 프로젝트: bhbk/fm3na7zy
        /*
         * openssh uses base64 and special formatting for public keys like with "authorized_keys"
         * https://man.openbsd.org/ssh-keygen
         */
        public static StringBuilder ExportPubKeyBase64(tbl_User user, ICollection <tbl_PublicKey> keys)
        {
            var callPath = $"{MethodBase.GetCurrentMethod().DeclaringType.Name}.{MethodBase.GetCurrentMethod().Name}";
            var sb       = new StringBuilder();

            foreach (var key in keys)
            {
                var pubBytes   = Encoding.ASCII.GetBytes(key.KeyValue);
                var pubKeyInfo = new PublicKeyInfo();
                pubKeyInfo.Load(new MemoryStream(pubBytes));

                var pubStream = new MemoryStream();
                var pubKey    = new SshPublicKey(pubKeyInfo);
                pubKey.SavePublicKey(pubStream, SshPublicKeyFormat.Pkcs8);

                var algo = string.Empty;

                switch (pubKey.KeyAlgorithm)
                {
                case SshHostKeyAlgorithm.DSS:
                    algo = "ssh-dsa";
                    break;

                case SshHostKeyAlgorithm.RSA:
                    algo = "ssh-rsa";
                    break;

                //case SshHostKeyAlgorithm.ECDsaNistP256:
                //	algo = "ecdsa-sha2-nistp256";
                //	break;

                //case SshHostKeyAlgorithm.ECDsaNistP384:
                //	algo = "ecdsa-sha2-nistp384";
                //	break;

                //case SshHostKeyAlgorithm.ECDsaNistP521:
                //	algo = "ecdsa-sha2-nistp521";
                //	break;

                //case SshHostKeyAlgorithm.ED25519:
                //	algo = "ssh-ed25519";
                //	break;

                default:
                {
                    Log.Warning($"'{callPath}' '{user.IdentityAlias}' algorithm {pubKey.KeyAlgorithm} not supported");
                    continue;
                }
                }

                sb.AppendLine($"{algo} {Convert.ToBase64String(pubKey.GetPublicKey())} {key.Comment}");
            }

            return(sb);
        }
예제 #3
0
파일: KeyHelper.cs 프로젝트: bhbk/fm3na7zy
        public static byte[] ExportPubKey(tbl_PublicKey key, SshPublicKeyFormat pubKeyFormat)
        {
            var pubBytes   = Encoding.ASCII.GetBytes(key.KeyValue);
            var pubKeyInfo = new PublicKeyInfo();

            pubKeyInfo.Load(new MemoryStream(pubBytes));

            var pubStream = new MemoryStream();
            var pubKey    = new SshPublicKey(pubKeyInfo);

            pubKey.SavePublicKey(pubStream, pubKeyFormat);

            return(pubStream.ToArray());
        }