Пример #1
0
 def _onAddContactClicked( self ) :
     k = RSAKey()
     try :
         k.fromPEM_PublicKey( self.pemPublicKey )
     except RSAError :
         self._showError( 'Invalid public key.' )
         self.ui.publicKey.setFocus()
         self.ui.publicKey.selectAll()
         return
     if not isValidUserName(self.contactName) :
         self._showError( 'Only lowercase alphabets(a-z), ' +
                 'digits(0-9), and underscore(\'_\') are allowed ' +
                 'in the contact name.' )
         self.ui.contactName.setFocus()
         self.ui.contactName.selectAll()
         return
     contact = self.profile.getContactByPublicKey( k )
     if contact :
         self._showError( 'This public key is already present in ' +
                 'your contact list as \'%s\'.' % contact.name )
         self.ui.publicKey.setFocus()
         self.ui.publicKey.selectAll()
         return
     contact = self.profile.getContactByName( self.contactName )
     if contact :
         self._showError( 'This name is already present in your ' +
                 'contact list.\nPlease choose a different name.' )
         self.ui.contactName.setFocus()
         self.ui.contactName.selectAll()
         return
     contact = Contact( k, self.contactName )
     self.addContactCallback( contact )
     self.accept()
Пример #2
0
 def _doCreateKey(self):
     self.ui.stack.setCurrentWidget(self.ui.progressPage)
     self.ui.msgLabel.setText('Creating RSA Key...')
     self.repaint()
     self.rsaKey = RSAKey()
     self.rsaKey.generate(bits=2048)
     self._doRegisterKey()
Пример #3
0
 def _onAddContactClicked(self):
     k = RSAKey()
     try:
         k.fromPEM_PublicKey(self.pemPublicKey)
     except RSAError:
         self._showError('Invalid public key.')
         self.ui.publicKey.setFocus()
         self.ui.publicKey.selectAll()
         return
     if not isValidUserName(self.contactName):
         self._showError('Only lowercase alphabets(a-z), ' +
                         'digits(0-9), and underscore(\'_\') are allowed ' +
                         'in the contact name.')
         self.ui.contactName.setFocus()
         self.ui.contactName.selectAll()
         return
     contact = self.profile.getContactByPublicKey(k)
     if contact:
         self._showError('This public key is already present in ' +
                         'your contact list as \'%s\'.' % contact.name)
         self.ui.publicKey.setFocus()
         self.ui.publicKey.selectAll()
         return
     contact = self.profile.getContactByName(self.contactName)
     if contact:
         self._showError('This name is already present in your ' +
                         'contact list.\nPlease choose a different name.')
         self.ui.contactName.setFocus()
         self.ui.contactName.selectAll()
         return
     contact = Contact(k, self.contactName)
     self.addContactCallback(contact)
     self.accept()
Пример #4
0
def getPublicKey( name, addr ) :
    s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
    s.connect( addr )
    s.send( 'getpubkey %s\n' % name )
    line = ''
    while 1 :
        data = s.recv( 1 )
        line += data
        if data == '\n' : break
    s.close()
    result,pubKeyData = line.split()
    assert result == 'OK'
    publicKey = RSAKey()
    publicKey.fromDER_PublicKey( pubKeyData.decode('hex') )
    return publicKey
Пример #5
0
 def _doCreateKey( self ) :
     self.ui.stack.setCurrentWidget( self.ui.progressPage )
     self.ui.msgLabel.setText( 'Creating RSA Key...' )
     self.repaint()
     self.rsaKey = RSAKey()
     self.rsaKey.generate( bits=2048 )
     self._doRegisterKey()
Пример #6
0
def verifySignature( publicKey, data, updateLevel, signature ) :
    payload = encode( ('DHT-DATA',data,updateLevel) )
    if type(publicKey) is str :
        k = RSAKey()
        try :
            k.fromDER_PublicKey( publicKey )
        except RSAError :
            return False
    else :
        k = publicKey
    try :
        digest = Digest(digestType).digest( payload )
        k.verify( signature, digest, digestType )
        return True
    except RSAError :
        return False
Пример #7
0
 def _doConnectPubKey( self, words ) :
     if len(words) != 2 :
         self._writeError( 'Malformed request' )
         return
     hexPubKey, service = words
     if not self.session.isOnline() :
         self._writeError( 'Not online' )
         return
     try :
         pubKeyData = hexDecode( hexPubKey )
         pubKey = RSAKey()
         pubKey.fromDER_PublicKey( pubKeyData )
     except (HexDecodeError,RSAError) :
         self._writeError( 'Malformed publickey' )
         return
     self._connectInternal( pubKey, service )
Пример #8
0
 def run(self, name, local, remote, cache_key):
     local_storage = get_storage_class(local)()
     remote_storage = get_storage_class(remote)()
     #encrypt
     if settings.ENCRYPT_UPLOADED_FILES:
         key = RSAKey()
         # Read in a public key
         fd = open(settings.CRYPTO_KEYS_PUBLIC, "rb")
         public_key = fd.read()
         fd.close()
         # import this public key
         key.fromPEM_PublicKey(public_key)            
         encrypt_file(key, local_storage.open(name),\
             local_storage.open(name, 'w'))
         remote_storage.save(name, local_storage.open(name))
     else:
         remote_storage.save(name, local_storage.open(name))
     cache.set(cache_key, True)
     return True
Пример #9
0
def loadProfile(entry, password):
    ps = profileSettings()
    userName = ps.getData(entry + '/Name')
    keyId = ps.getData(entry + '/KeyID')
    encKey = ps.getData(entry + '/PrivateKey')
    rsaKey = RSAKey()
    try:
        rsaKey.fromPEM_PrivateKey(encKey, password)
    except RSAError:
        return None
    profile = Profile(rsaKey, userName, keyId, entry)
    contactsData = ps.getData(entry + '/ContactList', '')
    for line in contactsData.split('\n'):
        line = line.strip()
        if not line: continue
        name, hexKey = line.split(':')
        assert isValidUserName(name)
        pubKey = RSAKey()
        pubKey.fromDER_PublicKey(hexDecode(hexKey))
        contact = Contact(pubKey, name)
        profile.addContact(contact)
    return profile
Пример #10
0
def verifySignature(publicKey, data, updateLevel, signature):
    payload = encode(('DHT-DATA', data, updateLevel))
    if type(publicKey) is str:
        k = RSAKey()
        try:
            k.fromDER_PublicKey(publicKey)
        except RSAError:
            return False
    else:
        k = publicKey
    try:
        digest = Digest(digestType).digest(payload)
        k.verify(signature, digest, digestType)
        return True
    except RSAError:
        return False
Пример #11
0
class CreateKeyDialog( QDialog ) :
    def __init__( self, parent, reactor ) :
        QDialog.__init__( self, parent )
        self.ui = Ui_CreateKeyDialog()
        self.ui.setupUi( self )
        self.reactor = reactor
        self.registerOp = None
        self.ui.stack.setCurrentWidget( self.ui.inputPage )

    def _showError( self, msg ) :
        QMessageBox.critical( self, 'Error', msg )
    
    @pyqtSignature('')
    def on_createKeyButton_clicked( self ) :
        password = unicode(self.ui.password.text()).encode('utf8')
        if not password :
            self._showError( 'Please enter a password.' )
            self.ui.password.setFocus()
            self.ui.password.selectAll()
            return
        password2 = unicode(self.ui.password2.text()).encode('utf8')
        if password != password2 :
            self._showError( 'Re-entered password does not match.' )
            self.ui.password2.setFocus()
            self.ui.password2.selectAll()
            return
        userName = unicode(self.ui.userName.text()).encode('utf8')
        if not userName :
            self._showError( 'Please enter a username.' )
            self.ui.userName.setFocus()
            self.ui.userName.selectAll()
            return
        if not isValidUserName(userName) :
            self._showError( 'Only lowercase alphabets(a-z), ' +
                    'digits(0-9), and underscore(\'_\') are allowed ' +
                    'in the username.' )
            self.ui.userName.setFocus()
            self.ui.userName.selectAll()
            return
        self.userName = userName
        self.password = password
        self._doCreateKey()

    def _doCreateKey( self ) :
        self.ui.stack.setCurrentWidget( self.ui.progressPage )
        self.ui.msgLabel.setText( 'Creating RSA Key...' )
        self.repaint()
        self.rsaKey = RSAKey()
        self.rsaKey.generate( bits=2048 )
        self._doRegisterKey()

    def _registerKey( self, callback=None ) :
        data = 'username:%s' % self.userName
        digestType = DigestType( 'SHA1' )
        digest = Digest(digestType).digest( data )
        signature = self.rsaKey.sign( digest, digestType )

        form = dict( username=self.userName,
                public_key=self.rsaKey.toDER_PublicKey(),
                signature=signature )
        postData = urllib.urlencode( form )

        request = HttpRequest( self.reactor )
        def onResponse( returnCode, data ) :
            if returnCode != 200 :
                op.notify( -1 )
                return
            try :
                keyId = int(data)
                op.notify( keyId )
            except ValueError :
                op.notify( -1 )
        httpOp = request.post( 'http://cspace.in/addkey', postData, onResponse )
        op = AsyncOp( callback, httpOp.cancel )
        return op

    def _doRegisterKey( self ) :
        self.ui.stack.setCurrentWidget( self.ui.progressPage )
        self.ui.msgLabel.setText( 'Registering Public Key...' )
        self.registerOp = self._registerKey( self._onRegister )

    def _onRegister( self, keyId ) :
        self.registerOp = None
        if keyId < 0 :
            self.ui.stack.setCurrentWidget( self.ui.errorPage )
            return
        self.keyId = str(keyId)
        self.accept()

    @pyqtSignature('')
    def on_tryAgainButton_clicked( self ) :
        self._doRegisterKey()

    def done( self, r ) :
        QDialog.done( self, r )
        self.close()

    def closeEvent( self, ev ) :
        if self.registerOp :
            self.registerOp.cancel()
            self.registerOp = None
        delaygc( self )
        QDialog.closeEvent( self, ev )
Пример #12
0
def getProfile():
    key = RSAKey()
    key.fromPEM_PrivateKey(file('ks.key').read())
    profile = UserProfile(key, 'ks')
    return profile
Пример #13
0
class CreateKeyDialog(QDialog):
    def __init__(self, parent, reactor):
        QDialog.__init__(self, parent)
        self.ui = Ui_CreateKeyDialog()
        self.ui.setupUi(self)
        self.reactor = reactor
        self.registerOp = None
        self.ui.stack.setCurrentWidget(self.ui.inputPage)

    def _showError(self, msg):
        QMessageBox.critical(self, 'Error', msg)

    @pyqtSignature('')
    def on_createKeyButton_clicked(self):
        password = unicode(self.ui.password.text()).encode('utf8')
        if not password:
            self._showError('Please enter a password.')
            self.ui.password.setFocus()
            self.ui.password.selectAll()
            return
        password2 = unicode(self.ui.password2.text()).encode('utf8')
        if password != password2:
            self._showError('Re-entered password does not match.')
            self.ui.password2.setFocus()
            self.ui.password2.selectAll()
            return
        userName = unicode(self.ui.userName.text()).encode('utf8')
        if not userName:
            self._showError('Please enter a username.')
            self.ui.userName.setFocus()
            self.ui.userName.selectAll()
            return
        if not isValidUserName(userName):
            self._showError('Only lowercase alphabets(a-z), ' +
                            'digits(0-9), and underscore(\'_\') are allowed ' +
                            'in the username.')
            self.ui.userName.setFocus()
            self.ui.userName.selectAll()
            return
        self.userName = userName
        self.password = password
        self._doCreateKey()

    def _doCreateKey(self):
        self.ui.stack.setCurrentWidget(self.ui.progressPage)
        self.ui.msgLabel.setText('Creating RSA Key...')
        self.repaint()
        self.rsaKey = RSAKey()
        self.rsaKey.generate(bits=2048)
        self._doRegisterKey()

    def _registerKey(self, callback=None):
        data = 'username:%s' % self.userName
        digestType = DigestType('SHA1')
        digest = Digest(digestType).digest(data)
        signature = self.rsaKey.sign(digest, digestType)

        form = dict(username=self.userName,
                    public_key=self.rsaKey.toDER_PublicKey(),
                    signature=signature)
        postData = urllib.urlencode(form)

        request = HttpRequest(self.reactor)

        def onResponse(returnCode, data):
            if returnCode != 200:
                op.notify(-1)
                return
            try:
                keyId = int(data)
                op.notify(keyId)
            except ValueError:
                op.notify(-1)

        httpOp = request.post('http://cspace.in/addkey', postData, onResponse)
        op = AsyncOp(callback, httpOp.cancel)
        return op

    def _doRegisterKey(self):
        self.ui.stack.setCurrentWidget(self.ui.progressPage)
        self.ui.msgLabel.setText('Registering Public Key...')
        self.registerOp = self._registerKey(self._onRegister)

    def _onRegister(self, keyId):
        self.registerOp = None
        if keyId < 0:
            self.ui.stack.setCurrentWidget(self.ui.errorPage)
            return
        self.keyId = str(keyId)
        self.accept()

    @pyqtSignature('')
    def on_tryAgainButton_clicked(self):
        self._doRegisterKey()

    def done(self, r):
        QDialog.done(self, r)
        self.close()

    def closeEvent(self, ev):
        if self.registerOp:
            self.registerOp.cancel()
            self.registerOp = None
        delaygc(self)
        QDialog.closeEvent(self, ev)
Пример #14
0
def getProfile() :
    key = RSAKey()
    key.fromPEM_PrivateKey( file('ks.key').read() )
    profile = UserProfile( key, 'ks' )
    return profile