diff options
author | Rob Austein <sra@hactrn.net> | 2007-08-11 23:36:01 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2007-08-11 23:36:01 +0000 |
commit | 8f9cb3188faa6b620e55196a06ca7039852f9f7e (patch) | |
tree | 37a23dc45cc9f61bfcf8fc4ef08d7ad0822c057b /pow/POW-0.7/lib/pkix.py | |
parent | 0785654778704722a2c09442073a40845e419a46 (diff) |
Whack crypto driver into form as a proper set of virtual classes.
svn path=/pow/POW-0.7/lib/pkix.py; revision=865
Diffstat (limited to 'pow/POW-0.7/lib/pkix.py')
-rwxr-xr-x | pow/POW-0.7/lib/pkix.py | 116 |
1 files changed, 68 insertions, 48 deletions
diff --git a/pow/POW-0.7/lib/pkix.py b/pow/POW-0.7/lib/pkix.py index 996afb8e..ca13fe0c 100755 --- a/pow/POW-0.7/lib/pkix.py +++ b/pow/POW-0.7/lib/pkix.py @@ -49,46 +49,47 @@ _fragments = [] def _docset(): return _der._docset() + _fragments +#---------- crypto driver ----------# + class CryptoDriverDigest(object): """Driver representation of a digest. - This implementation is specific to the POW driver. + This is a virtual class. You will have to subtype it. """ def __init__(self, type): """Initialize a digest object.""" - self.digest = POW.Digest(type) + raise NotImplementedError def update(self, input): """Feed data into a digest object.""" - self.digest.update(input) + raise NotImplementedError def finalize(self): """Get result of a digest operation.""" - return self.digest.digest() + raise NotImplementedError class CryptoDriverRSA(object): """Driver representation of an RSA key. - This implementation is specific to the POW driver. + This is a virtual class. You will have to subtype it. """ def __init__(self, rsa, digestType): """Initialize an RSA object.""" - self.rsa = rsa - self.type = digestType + raise NotImplementedError def getDER(self): """Get DER representation of an RSA key.""" - return self.rsa.derWrite(POW.RSA_PUBLIC_KEY) + raise NotImplementedError def sign(self, digest): """Sign a digest with an RSA key.""" - return self.rsa.sign(digest, self.type) + raise NotImplementedError def verify(self, signature, digest): """Verify the signature of a digest with an RSA key.""" - return self.rsa.verify(signature, digest, self.type) + raise NotImplementedError class CryptoDriver(object): """Dispatcher for crypto calls. @@ -96,32 +97,14 @@ class CryptoDriver(object): This module has very minimal dependencies on crypto code, as it's almost entirely about ASN.1 encoding and decoding. Rather than wiring in the handful of crypto calls, we dispatch them through - this driver. The default driver uses POW, but so long as you - implement the same interface, you can replace it with any crypto - package you like. - """ + this driver. The default driver uses POW, but you can replace it + with any crypto package you like. - def __init__(self): - """Initialize the driver. + This is a virtual class. You will have to subtype it. + """ - Among other tasks, driver initialization is where we import the - crypto package we're using (not much point otherwise, as we'd - always import the default crypto package immediately). - - This implementation is specific to the POW driver. - """ - import POW - self.driver2OID = { - POW.MD2_DIGEST : (1, 2, 840, 113549, 1, 1, 2), # md2WithRSAEncryption - POW.MD5_DIGEST : (1, 2, 840, 113549, 1, 1, 4), # md5WithRSAEncryption - POW.SHA_DIGEST : (1, 3, 14, 3, 2, 15), # shaWithRSAEncryption - POW.SHA1_DIGEST : (1, 2, 840, 113549, 1, 1, 5), # sha1withRSAEncryption - POW.RIPEMD160_DIGEST : (1, 2, 840, 113549, 1, 1, 6), # ripemd160WithRSAEncryption - POW.SHA256_DIGEST : (1, 2, 840, 113549, 1, 1, 11), # sha256WithRSAEncryption - POW.SHA512_DIGEST : (1, 2, 840, 113549, 1, 1, 13) } # sha512WithRSAEncryption - self.OID2driver = {} - for k,v in self.POWtoOID.iteritems(): - self.OID2driver[v] = k + DigestDriver = None + RSADriver = None def getOID(self, digestType): """Convert a digest identifier into an OID. @@ -130,8 +113,6 @@ class CryptoDriver(object): OID and just return it. If the identifier is in the driver identifier mapping table, we use that to return an OID. Otherwise, we try mapping it via the name-to-OID database. - - This implementation might be reusable by other drivers. """ if isinstance(digestType, tuple): return digestType @@ -140,29 +121,66 @@ class CryptoDriver(object): return obj2oid(digestType) def digest(self, oid): - """Instantiate and initialize a driver digest object. + """Instantiate and initialize a driver digest object.""" + assert isinstance(self.DigestDriver, CryptoDriverDigest) + return self.DigestDriver(self.OID2driver[oid]) - This implementation might be reusable by other drivers. - """ + def rsa(self, key, oid): + """Instantiate and initialize a driver RSA object.""" + assert isinstance(self.RSADriver, CryptoDriverRSA) + return self.RSADriver(key, self.OID2driver[oid]) + +class POWCryptoDriverDigest(CryptoDriverDigest): + """Driver representation of a digest for POW.""" + def __init__(self, type): + self.digest = POW.Digest(type) + def update(self, input): + self.digest.update(input) + def finalize(self): + return self.digest.digest() - return CryptoDriverDigest(self.OID2driver[oid]) +class POWCryptoDriverRSA(CryptoDriverRSA): + """Driver representation of an RSA key for POW.""" + def __init__(self, rsa, digestType): + self.rsa = rsa + self.type = digestType + def getDER(self): + return self.rsa.derWrite(POW.RSA_PUBLIC_KEY) + def sign(self, digest): + return self.rsa.sign(digest, self.type) + def verify(self, signature, digest): + return self.rsa.verify(signature, digest, self.type) - def rsa(self, key, oid): - """Instantiate and initialize a driver RSA object. +class POWCryptoDriver(object): + """Dispatcher for crypto calls using POW package.""" - This implementation might be reusable by other drivers. - """ - return CryptoDriverRSA(key, self.OID2driver[oid]) + DigestDriver = POWCryptoDriverDigest + RSADriver = POWCryptoDriverRSA + + def __init__(self): + """Initialize the POW driver.""" + import POW + self.driver2OID = { + POW.MD2_DIGEST : (1, 2, 840, 113549, 1, 1, 2), # md2WithRSAEncryption + POW.MD5_DIGEST : (1, 2, 840, 113549, 1, 1, 4), # md5WithRSAEncryption + POW.SHA_DIGEST : (1, 3, 14, 3, 2, 15), # shaWithRSAEncryption + POW.SHA1_DIGEST : (1, 2, 840, 113549, 1, 1, 5), # sha1withRSAEncryption + POW.RIPEMD160_DIGEST : (1, 2, 840, 113549, 1, 1, 6), # ripemd160WithRSAEncryption + POW.SHA256_DIGEST : (1, 2, 840, 113549, 1, 1, 11), # sha256WithRSAEncryption + POW.SHA512_DIGEST : (1, 2, 840, 113549, 1, 1, 13) } # sha512WithRSAEncryption + self.OID2driver = {} + for k,v in self.POWtoOID.iteritems(): + self.OID2driver[v] = k _cryptoDriver = None # Don't touch this directly def setCryptoDriver(driver): """Set crypto driver. - The driver should be an instance or subtype of CryptoDriver. + The driver should be a subtype of CryptoDriver. """ assert isinstance(driver, CryptoDriver) - _cryptoDriver = driver + _cryptoDriver = driver() def getCryptoDriver(): """Return the currently selected CryptoDriver instance. @@ -170,9 +188,11 @@ def getCryptoDriver(): If no driver has been selected, instantiate the default POW driver. """ if _cryptoDriver is None: - _cryptoDriver = CryptoDriver() + setCryptoDriver(POWCryptoDriver) return _cryptoDriver +#---------- crypto driver ----------# + def _addFragment(frag): global _fragments _fragments.append(frag) |