Welcome to mirror list, hosted at ThFree Co, Russian Federation.

secrets.py « src - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a72603d31cb574e0dd0c135cdd659e84f5e25e8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding:utf-8 -*-
## 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/>.
##

from common.configpaths import gajimpaths

import Crypto
from common import crypto
from common import exceptions

import os
import pickle

secrets_filename = gajimpaths['SECRETS_FILE']
secrets_cache = None

class Secrets():
    def __init__(self, filename):
        self.filename = filename
        self.srs = {}
        self.pubkeys = {}
        self.privkeys = {}

    def cancel(self):
        raise exceptions.Cancelled

    def save(self):
        f = open(secrets_filename, 'w')
        pickle.dump(self, f)
        f.close()

    def retained_secrets(self, account, bare_jid):
        try:
            return self.srs[account][bare_jid]
        except KeyError:
            return []

    # 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] = {}

        if not jid in self.srs[account]:
            self.srs[account][jid] = []

        self.srs[account][jid].append((secret, verified))

        self.save()

    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]

    # has the user verified this retained secret?
    def srs_verified(self, account, jid, srs):
        return self.find_srs(account, jid, srs)[1]

    def replace_srs(self, account, jid, old_secret, new_secret, verified):
        our_secrets = self.srs[account][jid]

        idx = our_secrets.index(self.find_srs(account, jid, old_secret))

        our_secrets[idx] = (new_secret, verified)

        self.save()

    # the public key associated with 'account'
    def my_pubkey(self, account):
        try:
            pk = self.privkeys[account]
        except KeyError:
            pk = Crypto.PublicKey.RSA.generate(2048, crypto.random_bytes)

            self.privkeys[account] = pk
            self.save()

        return pk

def load_secrets(filename):
    global Secrets
    f = open(filename, 'r')

    try:
        secrets = pickle.load(f)
    except (KeyError, EOFError):
        f.close()
        secrets = Secrets(filename)
    except (AttributeError, TypeError):
        class Secrets(object, Secrets):
            pass
        try:
            f.seek(0)
            secrets = pickle.load(f)
        except (KeyError, EOFError):
            f.close()
            secrets = Secrets(filename)

    return secrets

def secrets():
    global secrets_cache

    if secrets_cache:
        return secrets_cache

    if os.path.exists(secrets_filename):
        secrets_cache = load_secrets(secrets_filename)
    else:
        secrets_cache = Secrets(secrets_filename)

    return secrets_cache