2010-07-27 07:02:44 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
|
|
|
## src/common/jingle_xtls.py
|
|
|
|
##
|
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 3 only.
|
|
|
|
##
|
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
## You should have received a copy of the GNU General Public License
|
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
import logging
|
2010-07-27 15:29:12 +02:00
|
|
|
import os
|
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
import nbxmpp
|
2010-08-11 08:46:53 +02:00
|
|
|
from common import gajim
|
2017-02-08 03:49:33 +01:00
|
|
|
|
2010-07-27 07:02:44 +02:00
|
|
|
log = logging.getLogger('gajim.c.jingle_xtls')
|
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
|
2010-07-27 07:02:44 +02:00
|
|
|
PYOPENSSL_PRESENT = False
|
|
|
|
|
2013-12-04 18:43:28 +01:00
|
|
|
# key-exchange id -> [callback, args], accept that session once key-exchange completes
|
|
|
|
pending_contents = {}
|
2010-08-10 18:50:14 +02:00
|
|
|
|
2013-12-04 18:43:28 +01:00
|
|
|
def key_exchange_pend(id_, cb, args):
|
|
|
|
# args is a list
|
|
|
|
pending_contents[id_] = [cb, args]
|
2010-08-26 10:36:58 +02:00
|
|
|
|
2010-08-26 11:09:35 +02:00
|
|
|
def approve_pending_content(id_):
|
2013-12-04 18:43:28 +01:00
|
|
|
cb = pending_contents[id_][0]
|
|
|
|
args = pending_contents[id_][1]
|
|
|
|
cb(*args)
|
2010-08-10 18:50:14 +02:00
|
|
|
|
2010-07-27 07:02:44 +02:00
|
|
|
try:
|
2012-04-22 19:48:36 +02:00
|
|
|
import OpenSSL.SSL
|
2010-07-27 07:02:44 +02:00
|
|
|
PYOPENSSL_PRESENT = True
|
|
|
|
except ImportError:
|
|
|
|
log.info("PyOpenSSL not available")
|
|
|
|
|
2010-08-06 15:57:13 +02:00
|
|
|
if PYOPENSSL_PRESENT:
|
2017-02-07 20:56:26 +01:00
|
|
|
from OpenSSL import SSL, crypto
|
2010-08-26 10:36:58 +02:00
|
|
|
TYPE_RSA = crypto.TYPE_RSA
|
|
|
|
TYPE_DSA = crypto.TYPE_DSA
|
2010-08-06 15:57:13 +02:00
|
|
|
|
2010-08-08 15:25:29 +02:00
|
|
|
SELF_SIGNED_CERTIFICATE = 'localcert'
|
2013-11-12 21:10:22 +01:00
|
|
|
DH_PARAMS = 'dh_params.pem'
|
|
|
|
DEFAULT_DH_PARAMS = 'dh4096.pem'
|
2010-08-26 10:36:58 +02:00
|
|
|
|
2010-07-27 07:02:44 +02:00
|
|
|
def default_callback(connection, certificate, error_num, depth, return_code):
|
2017-02-08 03:49:33 +01:00
|
|
|
log.info("certificate: %s", certificate)
|
2010-07-27 07:02:44 +02:00
|
|
|
return return_code
|
|
|
|
|
2013-12-04 18:43:28 +01:00
|
|
|
def load_cert_file(cert_path, cert_store=None):
|
2010-08-08 15:25:29 +02:00
|
|
|
"""
|
2012-12-09 21:37:51 +01:00
|
|
|
This is almost identical to the one in nbxmpp.tls_nb
|
2010-08-08 15:25:29 +02:00
|
|
|
"""
|
|
|
|
if not os.path.isfile(cert_path):
|
2013-12-04 18:43:28 +01:00
|
|
|
return None
|
2010-08-08 15:25:29 +02:00
|
|
|
try:
|
|
|
|
f = open(cert_path)
|
2013-01-01 23:18:36 +01:00
|
|
|
except IOError as e:
|
2017-02-08 03:49:33 +01:00
|
|
|
log.warning('Unable to open certificate file %s: %s', cert_path,
|
|
|
|
str(e))
|
2013-12-04 18:43:28 +01:00
|
|
|
return None
|
2010-08-08 15:25:29 +02:00
|
|
|
lines = f.readlines()
|
|
|
|
i = 0
|
|
|
|
begin = -1
|
|
|
|
for line in lines:
|
|
|
|
if 'BEGIN CERTIFICATE' in line:
|
|
|
|
begin = i
|
|
|
|
elif 'END CERTIFICATE' in line and begin > -1:
|
|
|
|
cert = ''.join(lines[begin:i+2])
|
|
|
|
try:
|
|
|
|
x509cert = OpenSSL.crypto.load_certificate(
|
2012-08-22 12:21:45 +02:00
|
|
|
OpenSSL.crypto.FILETYPE_PEM, cert)
|
2013-12-04 18:43:28 +01:00
|
|
|
if cert_store:
|
|
|
|
cert_store.add_cert(x509cert)
|
2015-07-18 17:27:50 +02:00
|
|
|
f.close()
|
2013-12-04 18:43:28 +01:00
|
|
|
return x509cert
|
2013-01-01 23:18:36 +01:00
|
|
|
except OpenSSL.crypto.Error as exception_obj:
|
2017-02-08 03:49:33 +01:00
|
|
|
log.warning('Unable to load a certificate from file %s: %s',
|
|
|
|
cert_path, exception_obj.args[0][0][2])
|
2010-08-08 15:25:29 +02:00
|
|
|
except:
|
|
|
|
log.warning('Unknown error while loading certificate from file '
|
2017-02-08 03:49:33 +01:00
|
|
|
'%s', cert_path)
|
2010-08-08 15:25:29 +02:00
|
|
|
begin = -1
|
|
|
|
i += 1
|
2013-01-05 09:54:17 +01:00
|
|
|
f.close()
|
2010-08-08 15:25:29 +02:00
|
|
|
|
2013-12-04 18:43:28 +01:00
|
|
|
def get_context(fingerprint, verify_cb=None, remote_jid=None):
|
2010-07-27 07:02:44 +02:00
|
|
|
"""
|
|
|
|
constructs and returns the context objects
|
|
|
|
"""
|
2013-11-05 11:21:56 +01:00
|
|
|
ctx = SSL.Context(SSL.SSLv23_METHOD)
|
2014-01-29 14:12:10 +01:00
|
|
|
flags = (SSL.OP_NO_SSLv2 | SSL.OP_NO_SSLv3 | SSL.OP_SINGLE_DH_USE \
|
|
|
|
| SSL.OP_NO_TICKET)
|
2013-11-05 11:21:56 +01:00
|
|
|
ctx.set_options(flags)
|
2013-12-23 23:26:54 +01:00
|
|
|
ctx.set_cipher_list('HIGH:!aNULL:!3DES')
|
2010-07-29 15:40:40 +02:00
|
|
|
|
2010-07-27 15:29:12 +02:00
|
|
|
if fingerprint == 'server': # for testing purposes only
|
2012-08-22 12:21:45 +02:00
|
|
|
ctx.set_verify(SSL.VERIFY_NONE|SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
|
2017-02-08 03:49:33 +01:00
|
|
|
verify_cb or default_callback)
|
2010-07-29 15:40:40 +02:00
|
|
|
elif fingerprint == 'client':
|
|
|
|
ctx.set_verify(SSL.VERIFY_PEER, verify_cb or default_callback)
|
2010-08-26 10:36:58 +02:00
|
|
|
|
2010-08-11 08:46:53 +02:00
|
|
|
cert_name = os.path.join(gajim.MY_CERT_DIR, SELF_SIGNED_CERTIFICATE)
|
2015-11-16 22:58:18 +01:00
|
|
|
ctx.use_privatekey_file((cert_name + '.pkey').encode('utf-8'))
|
|
|
|
ctx.use_certificate_file((cert_name + '.cert').encode('utf-8'))
|
2013-11-12 21:10:22 +01:00
|
|
|
|
|
|
|
# Try to load Diffie-Hellman parameters.
|
|
|
|
# First try user DH parameters, if this fails load the default DH parameters
|
|
|
|
dh_params_name = os.path.join(gajim.MY_CERT_DIR, DH_PARAMS)
|
|
|
|
try:
|
|
|
|
with open(dh_params_name, "r") as dh_params_file:
|
2016-11-05 15:58:34 +01:00
|
|
|
ctx.load_tmp_dh(dh_params_name.encode('utf-8'))
|
2015-07-18 17:27:50 +02:00
|
|
|
except FileNotFoundError as err:
|
|
|
|
default_dh_params_name = os.path.join(gajim.DATA_DIR,
|
2017-02-08 03:49:33 +01:00
|
|
|
'other', DEFAULT_DH_PARAMS)
|
2013-11-12 21:10:22 +01:00
|
|
|
try:
|
|
|
|
with open(default_dh_params_name, "r") as default_dh_params_file:
|
2015-11-16 22:58:18 +01:00
|
|
|
ctx.load_tmp_dh(default_dh_params_name.encode('utf-8'))
|
2015-07-18 17:27:50 +02:00
|
|
|
except FileNotFoundError as err:
|
2017-02-08 03:49:33 +01:00
|
|
|
log.error('Unable to load default DH parameter file: %s, %s',
|
|
|
|
default_dh_params_name, err)
|
2013-11-12 21:10:22 +01:00
|
|
|
raise
|
|
|
|
|
2013-12-04 18:43:28 +01:00
|
|
|
if remote_jid:
|
|
|
|
store = ctx.get_cert_store()
|
|
|
|
path = os.path.join(os.path.expanduser(gajim.MY_PEER_CERTS_PATH),
|
2017-02-08 03:49:33 +01:00
|
|
|
remote_jid) + '.cert'
|
2013-12-04 18:43:28 +01:00
|
|
|
if os.path.exists(path):
|
|
|
|
load_cert_file(path, cert_store=store)
|
2017-02-08 03:49:33 +01:00
|
|
|
log.debug('certificate file %s loaded fingerprint %s',
|
|
|
|
path, fingerprint)
|
2010-07-27 07:02:44 +02:00
|
|
|
return ctx
|
|
|
|
|
2013-12-04 18:43:28 +01:00
|
|
|
def read_cert(certpath):
|
2010-08-10 14:34:46 +02:00
|
|
|
certificate = ''
|
2013-01-05 10:07:35 +01:00
|
|
|
with open(certpath, 'r') as certfile:
|
|
|
|
for line in certfile.readlines():
|
|
|
|
if not line.startswith('-'):
|
|
|
|
certificate += line
|
2013-12-04 18:43:28 +01:00
|
|
|
return certificate
|
|
|
|
|
|
|
|
def send_cert(con, jid_from, sid):
|
|
|
|
certpath = os.path.join(gajim.MY_CERT_DIR, SELF_SIGNED_CERTIFICATE) + \
|
|
|
|
'.cert'
|
|
|
|
certificate = read_cert(certpath)
|
2017-02-07 21:56:47 +01:00
|
|
|
iq = nbxmpp.Iq('result', to=jid_from)
|
2010-08-10 14:34:46 +02:00
|
|
|
iq.setAttr('id', sid)
|
2010-08-26 10:36:58 +02:00
|
|
|
|
2010-08-10 14:34:46 +02:00
|
|
|
pubkey = iq.setTag('pubkeys')
|
2012-12-09 21:37:51 +01:00
|
|
|
pubkey.setNamespace(nbxmpp.NS_PUBKEY_PUBKEY)
|
2010-08-26 10:36:58 +02:00
|
|
|
|
2010-08-10 14:34:46 +02:00
|
|
|
keyinfo = pubkey.setTag('keyinfo')
|
|
|
|
name = keyinfo.setTag('name')
|
|
|
|
name.setData('CertificateHash')
|
|
|
|
cert = keyinfo.setTag('x509cert')
|
|
|
|
cert.setData(certificate)
|
2010-08-26 10:36:58 +02:00
|
|
|
|
2010-08-10 14:34:46 +02:00
|
|
|
con.send(iq)
|
|
|
|
|
|
|
|
def handle_new_cert(con, obj, jid_from):
|
|
|
|
jid = gajim.get_jid_without_resource(jid_from)
|
2010-08-11 08:46:53 +02:00
|
|
|
certpath = os.path.join(os.path.expanduser(gajim.MY_PEER_CERTS_PATH), jid)
|
2010-08-10 14:34:46 +02:00
|
|
|
certpath += '.cert'
|
|
|
|
|
2010-08-26 11:09:35 +02:00
|
|
|
id_ = obj.getAttr('id')
|
2010-08-26 10:36:58 +02:00
|
|
|
|
2010-08-10 14:34:46 +02:00
|
|
|
x509cert = obj.getTag('pubkeys').getTag('keyinfo').getTag('x509cert')
|
2010-08-26 10:36:58 +02:00
|
|
|
|
2010-08-10 14:34:46 +02:00
|
|
|
cert = x509cert.getData()
|
2010-08-26 10:36:58 +02:00
|
|
|
|
|
|
|
f = open(certpath, 'w')
|
2010-08-10 14:34:46 +02:00
|
|
|
f.write('-----BEGIN CERTIFICATE-----\n')
|
|
|
|
f.write(cert)
|
|
|
|
f.write('-----END CERTIFICATE-----\n')
|
2013-12-04 18:43:28 +01:00
|
|
|
f.close()
|
2010-08-26 10:36:58 +02:00
|
|
|
|
2010-08-26 11:09:35 +02:00
|
|
|
approve_pending_content(id_)
|
2010-08-26 10:36:58 +02:00
|
|
|
|
2013-12-04 18:43:28 +01:00
|
|
|
def check_cert(jid, fingerprint):
|
|
|
|
certpath = os.path.join(os.path.expanduser(gajim.MY_PEER_CERTS_PATH), jid)
|
|
|
|
certpath += '.cert'
|
|
|
|
if os.path.exists(certpath):
|
|
|
|
cert = load_cert_file(certpath)
|
|
|
|
if cert:
|
2014-02-16 17:41:31 +01:00
|
|
|
try:
|
2015-07-18 18:10:55 +02:00
|
|
|
digest_algo = cert.get_signature_algorithm().decode('utf-8').\
|
|
|
|
split('With')[0]
|
2014-04-08 22:36:19 +02:00
|
|
|
except AttributeError as e:
|
2014-02-16 17:41:31 +01:00
|
|
|
# Old py-OpenSSL is missing get_signature_algorithm
|
|
|
|
digest_algo = "sha256"
|
2013-12-04 18:43:28 +01:00
|
|
|
if cert.digest(digest_algo) == fingerprint:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2010-08-10 14:34:46 +02:00
|
|
|
def send_cert_request(con, to_jid):
|
2012-12-09 21:37:51 +01:00
|
|
|
iq = nbxmpp.Iq('get', to=to_jid)
|
2010-08-26 11:09:35 +02:00
|
|
|
id_ = con.connection.getAnID()
|
|
|
|
iq.setAttr('id', id_)
|
2010-08-10 14:34:46 +02:00
|
|
|
pubkey = iq.setTag('pubkeys')
|
2012-12-09 21:37:51 +01:00
|
|
|
pubkey.setNamespace(nbxmpp.NS_PUBKEY_PUBKEY)
|
2010-08-10 14:34:46 +02:00
|
|
|
con.connection.send(iq)
|
2013-01-01 21:06:16 +01:00
|
|
|
return str(id_)
|
2010-08-10 14:34:46 +02:00
|
|
|
|
2010-08-06 15:57:13 +02:00
|
|
|
# the following code is partly due to pyopenssl examples
|
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
def createKeyPair(type_, bits):
|
2010-08-06 15:57:13 +02:00
|
|
|
"""
|
|
|
|
Create a public/private key pair.
|
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
Arguments: type_ - Key type, must be one of TYPE_RSA and TYPE_DSA
|
2010-08-06 15:57:13 +02:00
|
|
|
bits - Number of bits to use in the key
|
|
|
|
Returns: The public/private key pair in a PKey object
|
|
|
|
"""
|
|
|
|
pkey = crypto.PKey()
|
2017-02-08 03:49:33 +01:00
|
|
|
pkey.generate_key(type_, bits)
|
2010-08-06 15:57:13 +02:00
|
|
|
return pkey
|
|
|
|
|
2013-12-04 18:43:28 +01:00
|
|
|
def createCertRequest(pkey, digest="sha256", **name):
|
2010-08-06 15:57:13 +02:00
|
|
|
"""
|
|
|
|
Create a certificate request.
|
|
|
|
|
|
|
|
Arguments: pkey - The key to associate with the request
|
2013-12-04 18:43:28 +01:00
|
|
|
digest - Digestion method to use for signing, default is sha256
|
2010-08-06 15:57:13 +02:00
|
|
|
**name - The name of the subject of the request, possible
|
|
|
|
arguments are:
|
|
|
|
C - Country name
|
|
|
|
ST - State or province name
|
|
|
|
L - Locality name
|
|
|
|
O - Organization name
|
|
|
|
OU - Organizational unit name
|
|
|
|
CN - Common name
|
|
|
|
emailAddress - E-mail address
|
|
|
|
Returns: The certificate request in an X509Req object
|
|
|
|
"""
|
|
|
|
req = crypto.X509Req()
|
|
|
|
subj = req.get_subject()
|
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
for (key, value) in name.items():
|
2010-08-06 15:57:13 +02:00
|
|
|
setattr(subj, key, value)
|
|
|
|
|
|
|
|
req.set_pubkey(pkey)
|
|
|
|
req.sign(pkey, digest)
|
|
|
|
return req
|
|
|
|
|
2014-04-08 22:36:19 +02:00
|
|
|
def createCertificate(req, issuerCert, issuerKey, serial, notBefore, notAfter, digest="sha256"):
|
2010-08-06 15:57:13 +02:00
|
|
|
"""
|
|
|
|
Generate a certificate given a certificate request.
|
|
|
|
|
|
|
|
Arguments: req - Certificate reqeust to use
|
|
|
|
issuerCert - The certificate of the issuer
|
|
|
|
issuerKey - The private key of the issuer
|
|
|
|
serial - Serial number for the certificate
|
|
|
|
notBefore - Timestamp (relative to now) when the certificate
|
|
|
|
starts being valid
|
|
|
|
notAfter - Timestamp (relative to now) when the certificate
|
|
|
|
stops being valid
|
2013-12-04 18:43:28 +01:00
|
|
|
digest - Digest method to use for signing, default is sha256
|
2010-08-06 15:57:13 +02:00
|
|
|
Returns: The signed certificate in an X509 object
|
|
|
|
"""
|
|
|
|
cert = crypto.X509()
|
|
|
|
cert.set_serial_number(serial)
|
|
|
|
cert.gmtime_adj_notBefore(notBefore)
|
|
|
|
cert.gmtime_adj_notAfter(notAfter)
|
|
|
|
cert.set_issuer(issuerCert.get_subject())
|
|
|
|
cert.set_subject(req.get_subject())
|
|
|
|
cert.set_pubkey(req.get_pubkey())
|
|
|
|
cert.sign(issuerKey, digest)
|
|
|
|
return cert
|
|
|
|
|
|
|
|
def make_certs(filepath, CN):
|
|
|
|
"""
|
|
|
|
make self signed certificates
|
2012-08-22 12:21:45 +02:00
|
|
|
filepath : absolute path of certificate file, will be appended the '.pkey'
|
|
|
|
and '.cert' extensions
|
2010-08-06 15:57:13 +02:00
|
|
|
CN : common name
|
|
|
|
"""
|
2013-11-05 11:21:56 +01:00
|
|
|
key = createKeyPair(TYPE_RSA, 4096)
|
2010-08-06 15:57:13 +02:00
|
|
|
req = createCertRequest(key, CN=CN)
|
2013-01-02 13:54:02 +01:00
|
|
|
cert = createCertificate(req, req, key, 0, 0, 60*60*24*365*5) # five years
|
2013-03-05 15:58:20 +01:00
|
|
|
with open(filepath + '.pkey', 'wb') as f:
|
2013-12-30 21:19:15 +01:00
|
|
|
os.chmod(filepath + '.pkey', 0o600)
|
2013-03-05 15:58:20 +01:00
|
|
|
f.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, key))
|
|
|
|
with open(filepath + '.cert', 'wb') as f:
|
2014-04-08 22:47:43 +02:00
|
|
|
f.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert))
|
2010-08-06 15:57:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
make_certs('./selfcert', 'gajim')
|