Changeset 05022dc in trunk


Ignore:
Timestamp:
2010-07-11T21:37:21Z (15 years ago)
Author:
david-sarah <david-sarah@…>
Branches:
master
Children:
7528974
Parents:
c0e7d84
Message:

Move EncryptedTemporaryFile? from SFTP frontend to allmydata.util.fileutil, and make the FTP frontend also use it (fixing #1083).

Location:
src/allmydata
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/allmydata/frontends/ftpd.py

    rc0e7d84 r05022dc  
    11
    2 import tempfile
    32from zope.interface import implements
    43from twisted.application import service, strports
     
    1110     NoSuchChildError
    1211from allmydata.immutable.upload import FileHandle
     12from allmydata.util.fileutil import EncryptedTemporaryFile
    1313
    1414class ReadFile:
     
    2828        # we write the data to a temporary file, since Tahoe can't do
    2929        # streaming upload yet.
    30         self.f = tempfile.TemporaryFile()
     30        self.f = EncryptedTemporaryFile()
    3131        return None
    3232
  • TabularUnified src/allmydata/frontends/sftpd.py

    rc0e7d84 r05022dc  
    11
    2 import os, tempfile, heapq, binascii, traceback, array, stat, struct
     2import heapq, traceback, array, stat, struct
    33from types import NoneType
    44from stat import S_IFREG, S_IFDIR
     
    3333from allmydata.immutable.upload import FileHandle
    3434from allmydata.dirnode import update_metadata
    35 
    36 from pycryptopp.cipher.aes import AES
     35from allmydata.util.fileutil import EncryptedTemporaryFile
    3736
    3837noisy = True
     
    287286
    288287    return None
    289 
    290 
    291 class EncryptedTemporaryFile(PrefixingLogMixin):
    292     # not implemented: next, readline, readlines, xreadlines, writelines
    293 
    294     def __init__(self):
    295         PrefixingLogMixin.__init__(self, facility="tahoe.sftp")
    296         self.file = tempfile.TemporaryFile()
    297         self.key = os.urandom(16)  # AES-128
    298 
    299     def _crypt(self, offset, data):
    300         # TODO: use random-access AES (pycryptopp ticket #18)
    301         offset_big = offset // 16
    302         offset_small = offset % 16
    303         iv = binascii.unhexlify("%032x" % offset_big)
    304         cipher = AES(self.key, iv=iv)
    305         cipher.process("\x00"*offset_small)
    306         return cipher.process(data)
    307 
    308     def close(self):
    309         self.file.close()
    310 
    311     def flush(self):
    312         self.file.flush()
    313 
    314     def seek(self, offset, whence=0):  # 0 = SEEK_SET
    315         if noisy: self.log(".seek(%r, %r)" % (offset, whence), level=NOISY)
    316         self.file.seek(offset, whence)
    317 
    318     def tell(self):
    319         offset = self.file.tell()
    320         if noisy: self.log(".tell() = %r" % (offset,), level=NOISY)
    321         return offset
    322 
    323     def read(self, size=-1):
    324         if noisy: self.log(".read(%r)" % (size,), level=NOISY)
    325         index = self.file.tell()
    326         ciphertext = self.file.read(size)
    327         plaintext = self._crypt(index, ciphertext)
    328         return plaintext
    329 
    330     def write(self, plaintext):
    331         if noisy: self.log(".write(<data of length %r>)" % (len(plaintext),), level=NOISY)
    332         index = self.file.tell()
    333         ciphertext = self._crypt(index, plaintext)
    334         self.file.write(ciphertext)
    335 
    336     def truncate(self, newsize):
    337         if noisy: self.log(".truncate(%r)" % (newsize,), level=NOISY)
    338         self.file.truncate(newsize)
    339288
    340289
  • TabularUnified src/allmydata/util/fileutil.py

    rc0e7d84 r05022dc  
    33"""
    44
    5 import sys, exceptions, os, stat, tempfile, time
     5import sys, exceptions, os, stat, tempfile, time, binascii
    66
    77from twisted.python import log
     8
     9from pycryptopp.cipher.aes import AES
     10
    811
    912def rename(src, dst, tries=4, basedelay=0.1):
     
    113116            rm_dir(self.name)
    114117
     118class EncryptedTemporaryFile:
     119    # not implemented: next, readline, readlines, xreadlines, writelines
     120
     121    def __init__(self):
     122        self.file = tempfile.TemporaryFile()
     123        self.key = os.urandom(16)  # AES-128
     124
     125    def _crypt(self, offset, data):
     126        offset_big = offset // 16
     127        offset_small = offset % 16
     128        iv = binascii.unhexlify("%032x" % offset_big)
     129        cipher = AES(self.key, iv=iv)
     130        cipher.process("\x00"*offset_small)
     131        return cipher.process(data)
     132
     133    def close(self):
     134        self.file.close()
     135
     136    def flush(self):
     137        self.file.flush()
     138
     139    def seek(self, offset, whence=0):  # 0 = SEEK_SET
     140        self.file.seek(offset, whence)
     141
     142    def tell(self):
     143        offset = self.file.tell()
     144        return offset
     145
     146    def read(self, size=-1):
     147        index = self.file.tell()
     148        ciphertext = self.file.read(size)
     149        plaintext = self._crypt(index, ciphertext)
     150        return plaintext
     151
     152    def write(self, plaintext):
     153        index = self.file.tell()
     154        ciphertext = self._crypt(index, plaintext)
     155        self.file.write(ciphertext)
     156
     157    def truncate(self, newsize):
     158        self.file.truncate(newsize)
     159
     160
    115161def make_dirs(dirname, mode=0777):
    116162    """
Note: See TracChangeset for help on using the changeset viewer.