All Projects → ojarva → python-sshpubkeys

ojarva / python-sshpubkeys

Licence: BSD-3-Clause license
OpenSSH public key parser for Python

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to python-sshpubkeys

finspec-spec
Multi-protocol, machine-readable specifications for financial services
Stars: ✭ 18 (-78.82%)
Mutual labels:  validator
yii2-at-least-validator
Makes one or more attributes mandatory inside a set of attributes.
Stars: ✭ 28 (-67.06%)
Mutual labels:  validator
valite
🔥 Concurrently execute your validators in a simple, practical and light validator engine.
Stars: ✭ 20 (-76.47%)
Mutual labels:  validator
garn-validator
Create validations with ease
Stars: ✭ 42 (-50.59%)
Mutual labels:  validator
validation
Developer experience focused validator.
Stars: ✭ 15 (-82.35%)
Mutual labels:  validator
fake-numbers
Generate fake, valid numbers. Check if a number is valid. Support a lot of different numbers: Credit card, EAN, ISBN, RTN, VIN, etc.
Stars: ✭ 51 (-40%)
Mutual labels:  validator
guice-validator
Guice javax.validation method validation integration
Stars: ✭ 35 (-58.82%)
Mutual labels:  validator
utf8-validator
UTF-8 Validator
Stars: ✭ 18 (-78.82%)
Mutual labels:  validator
ATGValidator
iOS validation framework with form validation support
Stars: ✭ 51 (-40%)
Mutual labels:  validator
Hammer
Simple, reliable FHIR validator
Stars: ✭ 27 (-68.24%)
Mutual labels:  validator
gulp-html
Gulp plugin for HTML validation, using the official Nu Html Checker (v.Nu)
Stars: ✭ 70 (-17.65%)
Mutual labels:  validator
kontrolio
Simple standalone data validation library inspired by Laravel and Symfony
Stars: ✭ 51 (-40%)
Mutual labels:  validator
hey-validator
Data validator
Stars: ✭ 14 (-83.53%)
Mutual labels:  validator
cron-validate
A cron-expression validator for TypeScript/JavaScript projects.
Stars: ✭ 40 (-52.94%)
Mutual labels:  validator
simple-validator
Simple Validator is an awesome and easy to use validator for php
Stars: ✭ 73 (-14.12%)
Mutual labels:  validator
ngx-translate-lint
Simple CLI tools for check `ngx-translate` keys
Stars: ✭ 25 (-70.59%)
Mutual labels:  validator
FilterInputJs
Tiny and Powerful Library for limit an entry (text box,input) as number,string or more...
Stars: ✭ 37 (-56.47%)
Mutual labels:  validator
vvalidator
VValidator - Go validator library.
Stars: ✭ 26 (-69.41%)
Mutual labels:  validator
national-code
Simple implementation of Iranian national code validation
Stars: ✭ 31 (-63.53%)
Mutual labels:  validator
exploit-CVE-2016-6515
OpenSSH remote DOS exploit and vulnerable container
Stars: ✭ 53 (-37.65%)
Mutual labels:  openssh

OpenSSH Public Key Parser for Python

Major changes between versions 2 and 3

  • Dropped support for Python 2.6 and 3.3
  • Even in loose mode, DSA keys must be 1024, 2048, or 3072 bits (earlier this was looser)
  • The interface (API) is exactly the same

Usage

Native implementation for validating OpenSSH public keys.

Currently ssh-rsa, ssh-dss (DSA), ssh-ed25519 and ecdsa keys with NIST curves are supported.

Installation:

pip install sshpubkeys

or clone the repository and use

python setup.py install

Usage:

import sys
from sshpubkeys import SSHKey

ssh = SSHKey("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAYQCxO38tKAJXIs9ivPxt7AY"
      "dfybgtAR1ow3Qkb9GPQ6wkFHQqcFDe6faKCxH6iDRteo4D8L8B"
      "xwzN42uZSB0nfmjkIxFTcEU3mFSXEbWByg78aoddMrAAjatyrh"
      "H1pON6P0= ojarva@ojar-laptop", strict=True)
try:
    ssh.parse()
except InvalidKeyError as err:
    print("Invalid key:", err)
    sys.exit(1)
except NotImplementedError as err:
    print("Invalid key type:", err)
    sys.exit(1)

print(ssh.bits)  # 768
print(ssh.hash_md5())  # 56:84:1e:90:08:3b:60:c7:29:70:5f:5e:25:a6:3b:86
print(ssh.hash_sha256())  # SHA256:xk3IEJIdIoR9MmSRXTP98rjDdZocmXJje/28ohMQEwM
print(ssh.hash_sha512())  # SHA512:1C3lNBhjpDVQe39hnyy+xvlZYU3IPwzqK1rVneGavy6O3/ebjEQSFvmeWoyMTplIanmUK1hmr9nA8Skmj516HA
print(ssh.comment)  # ojar@ojar-laptop
print(ssh.options_raw)  # None (string of optional options at the beginning of public key)
print(ssh.options)  # None (options as a dictionary, parsed and validated)

Parsing of authorized_keys files:

import os
from sshpubkeys import AuthorizedKeysFile
f = open(os.environ["HOME"] + "/.ssh/authorized_keys", "r")
key_file = AuthorizedKeysFile(f, strict=False)

for key in key_file.keys:
    print(key.key_type, key.bits, key.hash_sha512())

Options

Set options in constructor as a keywords (i.e., SSHKey(None, strict=False))

  • strict: defaults to True. Disallows keys OpenSSH's ssh-keygen refuses to create. For instance, this includes DSA keys where length != 1024 bits and RSA keys shorter than 1024-bit. If set to False, tries to allow all keys OpenSSH accepts, including highly insecure 1-bit DSA keys.
  • skip_option_parsing: if set to True, options string is not parsed (ssh.options_raw is populated, but ssh.options is not).
  • disallow_options: if set to True, options are not allowed and it will raise an InvalidOptionsError.

Exceptions

  • NotImplementedError if invalid ecdsa curve or unknown key type is encountered.
  • InvalidKeyError if any other error is encountered:
    • TooShortKeyError if key is too short (<768 bits for RSA, <1024 for DSA, <256 for ED25519)
    • TooLongKeyError if key is too long (>16384 for RSA, >1024 for DSA, >256 for ED25519)
    • InvalidTypeError if key type ("ssh-rsa" in above example) does not match to what is included in base64 encoded data.
    • MalformedDataError if decoding and extracting the data fails.
    • InvalidOptionsError if options string is invalid.
      • InvalidOptionNameError if option name contains invalid characters.
        • UnknownOptionNameError if option name is not recognized.
      • MissingMandatoryOptionValueError if option needs to have parameter, but it is absent.

Tests

See "tests/" folder for unit tests. Use

python setup.py test

or

python3 setup.py test

to run test suite. If you have keys that are not parsed properly, or malformed keys that raise incorrect exception, please send your public key to [email protected], and I'll include it. Alternatively, create a new issue or make a pull request in github.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].