|
| 1 | +# This file is part of ssh-python. |
| 2 | +# Copyright (C) 2018 Panos Kittenis |
| 3 | +# |
| 4 | +# This library is free software; you can redistribute it and/or |
| 5 | +# modify it under the terms of the GNU Lesser General Public |
| 6 | +# License as published by the Free Software Foundation, version 2.1. |
| 7 | +# |
| 8 | +# This library is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | +# Lesser General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU Lesser General Public |
| 14 | +# License along with this library; if not, write to the Free Software |
| 15 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-130 |
| 16 | + |
| 17 | +from libc.string cimport const_char |
| 18 | + |
| 19 | +from keytypes cimport from_keytype, KeyType |
| 20 | +from utils cimport to_str, to_bytes |
| 21 | + |
| 22 | +from .exceptions import KeyExportError, KeyImportError, KeyGenerationError |
| 23 | + |
| 24 | +cimport c_ssh |
| 25 | + |
| 26 | + |
| 27 | +cdef class SSHKey: |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + cdef SSHKey from_ptr(c_ssh.ssh_key key): |
| 31 | + cdef SSHKey _key = SSHKey.__new__(SSHKey) |
| 32 | + _key._key = key |
| 33 | + return _key |
| 34 | + |
| 35 | + def __cinit__(self): |
| 36 | + self._key = c_ssh.ssh_key_new() |
| 37 | + if self._key is NULL: |
| 38 | + raise MemoryError |
| 39 | + |
| 40 | + def __dealloc__(self): |
| 41 | + if self._key is not NULL: |
| 42 | + c_ssh.ssh_key_free(self._key) |
| 43 | + self._key = NULL |
| 44 | + |
| 45 | + def is_private(self): |
| 46 | + cdef bint rc |
| 47 | + with nogil: |
| 48 | + rc = c_ssh.ssh_key_is_private(self._key) |
| 49 | + return bool(rc) |
| 50 | + |
| 51 | + def is_public(self): |
| 52 | + cdef bint rc |
| 53 | + with nogil: |
| 54 | + rc = c_ssh.ssh_key_is_public(self._key) |
| 55 | + return bool(rc) |
| 56 | + |
| 57 | + def __eq__(self, SSHKey other): |
| 58 | + cdef bint is_private |
| 59 | + cdef bint equal |
| 60 | + with nogil: |
| 61 | + is_private = c_ssh.ssh_key_is_private(self._key) |
| 62 | + equal = c_ssh.ssh_key_cmp( |
| 63 | + self._key, other._key, c_ssh.ssh_keycmp_e.SSH_KEY_CMP_PRIVATE) \ |
| 64 | + if is_private else \ |
| 65 | + c_ssh.ssh_key_cmp( |
| 66 | + self._key, other._key, |
| 67 | + c_ssh.ssh_keycmp_e.SSH_KEY_CMP_PUBLIC) |
| 68 | + return bool(equal) |
| 69 | + |
| 70 | + def key_type(self): |
| 71 | + cdef c_ssh.ssh_keytypes_e _type |
| 72 | + with nogil: |
| 73 | + _type = c_ssh.ssh_key_type(self._key) |
| 74 | + return from_keytype(_type) |
| 75 | + |
| 76 | + def ecdsa_name(self): |
| 77 | + cdef const_char *c_name |
| 78 | + cdef bytes b_name |
| 79 | + with nogil: |
| 80 | + c_name = c_ssh.ssh_pki_key_ecdsa_name(self._key) |
| 81 | + b_name = c_name |
| 82 | + return to_str(b_name) |
| 83 | + |
| 84 | + def export_privkey_file(self, filepath, passphrase=None): |
| 85 | + cdef bytes b_passphrase |
| 86 | + cdef bytes b_filepath = to_bytes(filepath) |
| 87 | + cdef const_char *c_passphrase = NULL |
| 88 | + cdef const_char *c_filepath = b_filepath |
| 89 | + cdef int rc |
| 90 | + if passphrase is not None: |
| 91 | + b_passphrase = to_bytes(passphrase) |
| 92 | + c_passphrase = b_passphrase |
| 93 | + with nogil: |
| 94 | + rc = c_ssh.ssh_pki_export_privkey_file( |
| 95 | + self._key, c_passphrase, NULL, NULL, c_filepath) |
| 96 | + if rc != c_ssh.SSH_OK: |
| 97 | + raise KeyExportError(c_ssh.ssh_get_error(self._key)) |
| 98 | + |
| 99 | + def export_privkey_to_pubkey(self): |
| 100 | + cdef SSHKey pub_key |
| 101 | + cdef c_ssh.ssh_key _pub_key |
| 102 | + cdef int rc |
| 103 | + with nogil: |
| 104 | + rc = c_ssh.ssh_pki_export_privkey_to_pubkey(self._key, &_pub_key) |
| 105 | + if rc != c_ssh.SSH_OK: |
| 106 | + raise KeyExportError(c_ssh.ssh_get_error(self._key)) |
| 107 | + pub_key = SSHKey.from_ptr(_pub_key) |
| 108 | + return pub_key |
| 109 | + |
| 110 | + def export_pubkey_base64(self): |
| 111 | + cdef char *_key |
| 112 | + cdef int rc |
| 113 | + cdef bytes b_key |
| 114 | + cdef size_t key_len |
| 115 | + with nogil: |
| 116 | + rc = c_ssh.ssh_pki_export_pubkey_base64(self._key, &_key) |
| 117 | + if rc != c_ssh.SSH_OK: |
| 118 | + with gil: |
| 119 | + raise KeyExportError(c_ssh.ssh_get_error(self._key)) |
| 120 | + b_key = _key |
| 121 | + c_ssh.ssh_string_free_char(_key) |
| 122 | + return b_key |
| 123 | + |
| 124 | + |
| 125 | +def generate(KeyType key_type, int bits): |
| 126 | + cdef SSHKey key |
| 127 | + cdef c_ssh.ssh_key _key |
| 128 | + cdef int rc |
| 129 | + with nogil: |
| 130 | + rc = c_ssh.ssh_pki_generate(key_type._type, bits, &_key) |
| 131 | + if rc != c_ssh.SSH_OK: |
| 132 | + raise KeyGenerationError(c_ssh.ssh_get_error(_key)) |
| 133 | + key = SSHKey.from_ptr(_key) |
| 134 | + return key |
| 135 | + |
| 136 | + |
| 137 | +def import_privkey_base64(bytes b64_key, passphrase=None): |
| 138 | + cdef const_char *c_key = b64_key |
| 139 | + cdef bytes b_passphrase |
| 140 | + cdef const_char *c_passphrase = NULL |
| 141 | + cdef int rc |
| 142 | + cdef SSHKey key |
| 143 | + cdef c_ssh.ssh_key _key |
| 144 | + if passphrase is not None: |
| 145 | + b_passphrase = to_bytes(passphrase) |
| 146 | + c_passphrase = b_passphrase |
| 147 | + with nogil: |
| 148 | + rc = c_ssh.ssh_pki_import_privkey_base64( |
| 149 | + c_key, c_passphrase, NULL, NULL, &_key) |
| 150 | + if rc != c_ssh.SSH_OK: |
| 151 | + raise KeyImportError(c_ssh.ssh_get_error(_key)) |
| 152 | + key = SSHKey.from_ptr(_key) |
| 153 | + return key |
| 154 | + |
| 155 | + |
| 156 | +def import_privkey_file(filepath, passphrase=None): |
| 157 | + cdef bytes b_passphrase |
| 158 | + cdef bytes b_filepath = to_bytes(filepath) |
| 159 | + cdef const_char *c_passphrase = NULL |
| 160 | + cdef const_char *c_filepath = b_filepath |
| 161 | + cdef int rc |
| 162 | + cdef SSHKey key |
| 163 | + cdef c_ssh.ssh_key _key |
| 164 | + if passphrase is not None: |
| 165 | + b_passphrase = to_bytes(passphrase) |
| 166 | + c_passphrase = b_passphrase |
| 167 | + with nogil: |
| 168 | + rc = c_ssh.ssh_pki_import_privkey_file( |
| 169 | + c_filepath, c_passphrase, NULL, NULL, &_key) |
| 170 | + if rc != c_ssh.SSH_OK: |
| 171 | + raise KeyExportError(c_ssh.ssh_get_error(_key)) |
| 172 | + key = SSHKey.from_ptr(_key) |
| 173 | + return key |
| 174 | + |
| 175 | + |
| 176 | +def import_pubkey_base64(bytes b64_key, KeyType key_type): |
| 177 | + cdef const_char *c_key = b64_key |
| 178 | + cdef int rc |
| 179 | + cdef SSHKey key |
| 180 | + cdef c_ssh.ssh_key _key |
| 181 | + with nogil: |
| 182 | + rc = c_ssh.ssh_pki_import_pubkey_base64( |
| 183 | + c_key, key_type._type, &_key) |
| 184 | + if rc != c_ssh.SSH_OK: |
| 185 | + raise KeyImportError(c_ssh.ssh_get_error(_key)) |
| 186 | + key = SSHKey.from_ptr(_key) |
| 187 | + return key |
| 188 | + |
| 189 | + |
| 190 | +def import_pubkey_file(filepath): |
| 191 | + cdef bytes b_filepath = to_bytes(filepath) |
| 192 | + cdef const_char *c_filepath = b_filepath |
| 193 | + cdef int rc |
| 194 | + cdef SSHKey key |
| 195 | + cdef c_ssh.ssh_key _key |
| 196 | + with nogil: |
| 197 | + rc = c_ssh.ssh_pki_import_pubkey_file( |
| 198 | + c_filepath, &_key) |
| 199 | + if rc != c_ssh.SSH_OK: |
| 200 | + raise KeyExportError(c_ssh.ssh_get_error(_key)) |
| 201 | + key = SSHKey.from_ptr(_key) |
| 202 | + return key |
0 commit comments