/usr/share/pyshared/keyczar/readers.py is in python-keyczar 0.6~b.061709+svn502-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 130 131 132 133 | #!/usr/bin/python2.4
#
# Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
A Reader supports reading metadata and key info for key sets.
@author: arkajit.dey@gmail.com (Arkajit Dey)
"""
import os
import errors
import keydata
import keys
import util
class Reader(object):
"""Interface providing supported methods (no implementation)."""
def GetMetadata(self):
"""
Return the KeyMetadata for the key set being read.
@return: JSON string representation of KeyMetadata object
@rtype: string
@raise KeyczarError: if unable to read metadata (e.g. IOError)
"""
def GetKey(self, version_number):
"""
Return the key corresponding to the given version.
@param version_number: the version number of the desired key
@type version_number: integer
@return: JSON string representation of a Key object
@rtype: string
@raise KeyczarError: if unable to read key info (e.g. IOError)
"""
class FileReader(Reader):
"""Reader that reads key data from files."""
def __init__(self, location):
self._location = location
def GetMetadata(self):
return util.ReadFile(os.path.join(self._location, "meta"))
def GetKey(self, version_number):
return util.ReadFile(os.path.join(self._location, str(version_number)))
class EncryptedReader(Reader):
"""Reader that reads encrypted key data from files."""
def __init__(self, reader, crypter):
self._reader = reader
self._crypter = crypter
def GetMetadata(self):
return self._reader.GetMetadata()
def GetKey(self, version_number):
return self._crypter.Decrypt(self._reader.GetKey(version_number))
class MockReader(Reader):
"""Mock reader used for testing Keyczart."""
def __init__(self, name, purpose, type, encrypted=False):
self.kmd = keydata.KeyMetadata(name, purpose, type, encrypted)
self.pubkmd = None
self.keys = {}
self.pubkeys = {}
@property
def numkeys(self):
return len(self.keys)
def GetMetadata(self):
return str(self.kmd)
def GetKey(self, version_number):
try:
return str(self.keys[version_number])
except KeyError:
raise errors.KeyczarError("Unrecognized Version Number")
def GetStatus(self, version_number):
return self.kmd.GetVersion(version_number).status
def SetKey(self, version_number, key):
self.keys[version_number] = key
def SetPubKey(self, version_number, key):
self.pubkeys[version_number] = key
def AddKey(self, version_number, status, size=None):
"""Utility method for testing."""
key = keys.GenKey(self.kmd.type, size)
self.keys[version_number] = key
return self.kmd.AddVersion(keydata.KeyVersion(version_number, status,
False))
def RemoveKey(self, version_number):
"""Mocks out deleting revoked key files."""
self.keys.pop(version_number)
def ExistsVersion(self, version_number):
return version_number in self.keys
def HasPubKey(self, version_number):
priv = self.keys[version_number]
pub = self.pubkeys[version_number]
return priv.public_key == pub
def GetKeySize(self, version_number):
return self.keys[version_number].size
|