2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/secrets.py
|
|
|
|
##
|
|
|
|
## Copyright (C) 2007-2008 Brendan Taylor <whateley AT gmail.com>
|
|
|
|
## Copyright (C) 2008 Jonathan Schleifer <js-gajim AT webkeks.org>
|
|
|
|
##
|
|
|
|
## 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/>.
|
|
|
|
##
|
|
|
|
|
2007-09-29 22:51:01 +02:00
|
|
|
from common.configpaths import gajimpaths
|
|
|
|
|
2008-12-02 16:10:31 +01:00
|
|
|
import Crypto
|
2007-09-29 22:51:01 +02:00
|
|
|
from common import crypto
|
|
|
|
from common import exceptions
|
|
|
|
|
|
|
|
import os
|
|
|
|
import pickle
|
|
|
|
|
|
|
|
secrets_filename = gajimpaths['SECRETS_FILE']
|
|
|
|
secrets_cache = None
|
|
|
|
|
|
|
|
class Secrets:
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, filename):
|
|
|
|
self.filename = filename
|
|
|
|
self.srs = {}
|
|
|
|
self.pubkeys = {}
|
|
|
|
self.privkeys = {}
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def cancel(self):
|
|
|
|
raise exceptions.Cancelled
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def save(self):
|
|
|
|
f = open(secrets_filename, 'w')
|
|
|
|
pickle.dump(self, f)
|
|
|
|
f.close()
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def retained_secrets(self, account, bare_jid):
|
|
|
|
try:
|
|
|
|
return self.srs[account][bare_jid]
|
|
|
|
except KeyError:
|
|
|
|
return []
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# retained secrets are stored as a tuple of the secret and whether the user
|
|
|
|
# has verified it
|
|
|
|
def save_new_srs(self, account, jid, secret, verified):
|
|
|
|
if not account in self.srs:
|
|
|
|
self.srs[account] = {}
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
if not jid in self.srs[account]:
|
|
|
|
self.srs[account][jid] = []
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.srs[account][jid].append((secret, verified))
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.save()
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def find_srs(self, account, jid, srs):
|
|
|
|
our_secrets = self.srs[account][jid]
|
|
|
|
return [(x, y) for x, y in our_secrets if x == srs][0]
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# has the user verified this retained secret?
|
|
|
|
def srs_verified(self, account, jid, srs):
|
|
|
|
return self.find_srs(account, jid, srs)[1]
|
2008-06-29 06:39:29 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def replace_srs(self, account, jid, old_secret, new_secret, verified):
|
|
|
|
our_secrets = self.srs[account][jid]
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
idx = our_secrets.index(self.find_srs(account, jid, old_secret))
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
our_secrets[idx] = (new_secret, verified)
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.save()
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# the public key associated with 'account'
|
|
|
|
def my_pubkey(self, account):
|
|
|
|
try:
|
|
|
|
pk = self.privkeys[account]
|
|
|
|
except KeyError:
|
|
|
|
pk = Crypto.PublicKey.RSA.generate(384, crypto.random_bytes)
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.privkeys[account] = pk
|
|
|
|
self.save()
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
return pk
|
2007-09-29 22:51:01 +02:00
|
|
|
|
|
|
|
def load_secrets(filename):
|
2010-02-08 15:08:40 +01:00
|
|
|
f = open(filename, 'r')
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
|
|
|
secrets = pickle.load(f)
|
|
|
|
except KeyError:
|
|
|
|
f.close()
|
|
|
|
secrets = Secrets(filename)
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
return secrets
|
2007-09-29 22:51:01 +02:00
|
|
|
|
|
|
|
def secrets():
|
2010-02-08 15:08:40 +01:00
|
|
|
global secrets_cache
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
if secrets_cache:
|
|
|
|
return secrets_cache
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
if os.path.exists(secrets_filename):
|
|
|
|
secrets_cache = load_secrets(secrets_filename)
|
|
|
|
else:
|
|
|
|
secrets_cache = Secrets(secrets_filename)
|
2007-09-29 22:51:01 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
return secrets_cache
|