;ò ˆç‹Gc@sÖdZdkZdkZdkZdkZdkZdkZdkZdkZdk Tdk Z dZ dZ dZ dZdZdZd Zd Zeid ƒZgad Zd efd„ƒYZdefd„ƒYZd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z!d„Z"d„Z#d„Z$dfd„ƒYZ%dfd„ƒYZ&d fd!„ƒYZ'd"fd#„ƒYZ(d$d%„Z)d&fd'„ƒYZ*d(fd)„ƒYZ+d*fd+„ƒYZ,d,„Z-dS(-s¡ TrustedPickle.py contains a variety of classes and functions used to sign pickle files with private keys. These pickle files contain not only UNENCRYPTED data, they also contain signatures and trust relationships which allow a program to decide whether the data should be trusted BEFORE unpickling. Although a two-part keying system is used to sign the data, the data itself is intentionally unencrypted. This is done for three important reasons: 1. Certain governments have created laws which control the use of strong encryption. Although this program uses a similar technology to verify who authored a file, the data is not encrypted, so that this technology may be exported freely. 2. This project is not "about" data encryption, it is for creating freely- transferrable files whose authorship can be validated before use. 3. By leaving the data unencrypted, the end user may retain control over whether the data is used or not. The user can be informed that the file was not created by a trusted party (such as the original program's author) and given the chance to accept or reject the file before it is unpickled. Using TrustedPickle.py has one other big advantage over blindly using regular pickle files. The Python maintainers stress that it may be possible to maliciously construct a pickle file so that it executes arbitrary code. By only allowing your program to use data signed by yourself or one of your trusted agents, you can reduce the risk of a third party circumventing your program's security measures by creating such data files. Constants available in this module: TRUSTED: indicates data signed with a trusted key TRANSFERRED: indicates data with a trust relationship to a trusted key UNKNOWN: indicates no trust relationship links data to a trusted key REVOKED: indicates data signed with a revoked key Custom exceptions thrown by this module: FileFormatError: unexpected file end or invalid file signature encountered MismatchedKeys: thrown when mismatched public and private keys are used Common classes and functions in this module: PrivateKey: required to sign a file or establish a trust relationship PublicKey: includes a signed name and e-mail address, identifying the author Signature: shows a programmers faith in a document TrustRelationship: includes a truster, a trustee, and a transferrable flag PublicKeyFile: file holding public keys and trust relationships TPickle: signed data file NewKeys(): interactively create a public and private key pair ModuleObject: module wrapper Low-level functions in this module you should not need to call: Hash(): used in signing a string N(s*iiiisTrPZsTrPUsTrPvsTrPbs (\.[^\.]+)$i sFileFormatErrorcBstZRS(N(s__name__s __module__(((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysFileFormatErrortssMismatchedKeyscBstZRS(N(s__name__s __module__(((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysMismatchedKeysvscCsP||jo||f\}}nx"|o|||f\}}q&W|SdS(s0Returns the greatest common denomenator of a & bN(sasb(sasb((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysGCDys cCs^|df\}}xA|o9|d@o|||}n|||}|dL}qW|SdS(sReturns (m ** Key) mod niN(smsisjsKeysn(smsKeysnsisj((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysSolves cCsddddf\}}}}xd|o\||}||||f\}}||||f\}}||||f\}}q!W|SdS(sJReturns Key, such that ((Key * e) mod phi) = 1. NOTE: Key may be negative!llN(sx1sKeysy1sy2sphisesq(sesphisy2sqsKeysy1sx1((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys ExtEuclidˆs cCs4tid|dƒ}t||d|ƒdjSdS(s>Uses Fermat's little theorem to test if n is PROBABLY a prime.iiN(srandomsrandintsnsasSolve(snsa((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys TestPrime’scCsLxEtdƒD]7}tit|ƒttiddd>ƒƒt|tdd>Btdd?B}dd>tddd>d@d>Btdd ?B}tddd >d@d >td d >Btd d ?B}|||fSdS(sEReturns three random numbers for p, q, and Key from GenSeed5x52 bits.li@ii ii(iiiidii0iN(s GenSeed5x52spsqsKey(spsqsKey((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysPickNumsžs  ".2cCsLd}x2t|ƒD]$}|t|d@ƒ7}|dL}qW|i|ƒdS(Nsiÿi(sSsrangesBytessischrsNsFileswrite(sFilesNsBytessisS((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysWriteN±s  cCss|i|ƒ}t|ƒ|jo t‚nd}x6t|ƒD](}|t t ||ƒƒ|d>>O}q?W|SdS(Nli( sFilesreadsBytessSslensFileFormatErrorsRetValsrangesislongsord(sFilesBytessisSsRetVal((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysReadN¸s  &cCs't|t|ƒdƒ|i|ƒdS(Ni(sWriteNsFileslensStrswrite(sFilesStr((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysWriteSÀscCsCt|dƒ}|i|ƒ}t|ƒ|jo t‚n|SdS(Ni(sReadNsFilesLsreadsRetValslensFileFormatError(sFilesLsRetVal((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysReadSÄs  cCs tti|ƒiƒdƒSdS(s1Returns a 128 bit digest of the string as a long.iN(slongsmd5sStrings hexdigest(sString((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysHashÊss PrivateKeycBsPtZdZedƒZded„Zdd„Zd„Zd„Z d„Z RS(sêHolds a private key, used in signing documents and trust relationships. To create a private key, instantiate the object with a filename (and optionally a password to keep the key secure) and then pass it into the GenKeys() function, below. GenKeys() takes care of writing the key to disk. To read in an existing key, instantiate with filename (and password, if one was used in its creation), and call the Read() function. Call the Test() function to verify that the password used was correct. If incorrect, use SetPassword(), Read(), and Test() to try again. The password on an existing private key may be changed by calling SetPassword() and Write(). Make sure the key was decoded correctly by calling Test() BEFORE changing the password!s4Test is a test string used to validate private keys.scCs+||t|ƒf\|_|_|_dS(s Constructor.N(sKeysFilenamesHashsPasswordsself(sselfsFilenamesPasswordsKey((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys__init__àscCst|ƒ|_dS(sFSet/change a password on the private key file to make it more private.N(sHashsPasswordsself(sselfsPassword((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys SetPasswordäscCsOt|idƒ}z+|itƒt||i|iAdƒWd|i ƒXdS(sWrite key to file.swbiN( sopensselfsFilenamesFswrites MAGIC_PRIVsWriteNsKeysPasswordsclose(sselfsF((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysWriteès cCsat|idƒ}z=|idƒtjo t‚nt|dƒ|iA|_ Wd|i ƒXdS(s$Read and decode the key from a file.srbiiN( sopensselfsFilenamesFsreads MAGIC_PRIVsFileFormatErrorsReadNsPasswordsKeysclose(sselfsF((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysReadñs cCs>t|i|i|iƒ}|it||i|iƒjSdS(sRTest the private key against the public key to make sure it was decoded correctly.N(sSolvesselfsTestHashsKeysPublicsnsEncode(sselfsPublicsEncode((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysTestús( s__name__s __module__s__doc__sHashsTestHashsNones__init__s SetPasswordsWritesReadsTest(((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys PrivateKeyÎs    s PublicKeycBs>tZdZd„Zd„Zd„ZeeƒZd„ZRS(sÇPublicKey is a user's public key, name, and address, signed with their private key. Before trusting the name or address portion of this class, be sure to test the signature with the Test() function.cCs|||||f\|_|_|_|_t|tƒo5t|i|iƒ}t ||i|iƒ|_ n ||_ dS(s Constructor.N( snsKeysNamesAddresssselfs isinstancesPrivVals PrivateKeysHashsHsSolves Validator(sselfsnsKeysNamesAddresssPrivValsH((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys__init__s *cCs]t||idƒt||idƒt||iƒt||iƒt||idƒdS(sWrites PublicKey to the file.iiN( sWriteNsFilesselfsnsKeysWriteSsNamesAddresss Validator(sselfsFile((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysWrites cCsAtt|dƒt|dƒt|ƒt|ƒt|dƒƒSdS(s6Static method: Returns a PublicKey read from the file.iiN(s PublicKeysReadNsFilesReadS(sFile((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysReads-cCs9t|i|iƒ}|t|i|i|iƒjSdS(sVTest the Name and Address against the public key to make sure it has not been altered.N( sHashsselfsNamesAddresssHsSolves ValidatorsKeysn(sselfsH((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysTests(s__name__s __module__s__doc__s__init__sWritesReads staticmethodsTest(((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys PublicKeys    s SignaturecBsPtZdZd„Zd„Zd„Zd„Zd„ZeeƒZd„Z RS(s†Signature is a user's vouchure for the data's integrity. Before trusting this signature, be sure to test it with the Test() function.cGs5t|ƒdjo|i|Œn|i|ŒdS(s Constructors.iN(slensArgssselfs_Loaded_s_New_(sselfsArgs((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys__init__'scCs||f\|_|_dS(N(sSigners Validatorsself(sselfsSigners Validator((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys_Loaded_-scCsX|i|iƒƒ o t‚n|i|_tt |ƒ|i |iƒi ƒ|_ dS(N(sPrivatesTests PublicFiles MyPublicKeysMismatchedKeyssOwnersselfsSignersSolvesHashsDatasKeysns Validator(sselfsDatas PublicFilesPrivate((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys_New_/s  cCs*t||idƒt||idƒdS(sWrites PublicKey to the file.iN(sWriteNsFilesselfsSigners Validator(sselfsFile((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysWrite5scCs#tt|dƒt|dƒƒSdS(s6Static method: Returns a Signature read from the file.iN(s SignaturesReadNsFile(sFile((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysRead:scCs)t|ƒt|i|i|iƒjSdS(sTest the signature.N(sHashsDatasSolvesselfs ValidatorsPublicsKeysn(sselfsDatasPublic((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysTest?s( s__name__s __module__s__doc__s__init__s_Loaded_s_New_sWritesReads staticmethodsTest(((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys Signature#s       sTrustRelationshipcBsStZdZd„Zd„Zdd„Zd„Zd„Zd„Ze eƒZRS(sTrustRelationship tracks a truster who trusts a trustee. The Transferrable flag (Y/N) indicates whether the truster trusts the trustee enough to chain on their own trust relationships. Before trusting the listed relationship, be sure to test the signature with the Test() function.cGs5t|ƒdjo|i|Œn|i|ŒdS(s Constructors.iN(slensArgssselfs_Loaded_s_New_(sselfsArgs((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys__init__IscCs=|||f\|_|_|_||f\|_|_dS(N(sTrustersTrusteesnsselfs Transferrables Validator(sselfsTrustersTrusteesns Transferrables Validator((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys_Loaded_Os!sNcCs´|i|i|if\|_|_|_||_tt|iƒ|ƒ}t |t ƒot ||i|iƒ|_ n ||_ |t |i |i|iƒjo t‚ndS(N(sTrustersKeysTrusteesnsselfs TransferrablesHashshexsHs isinstancesPrivVals PrivateKeysSolves ValidatorsMismatchedKeys(sselfsTrustersTrusteesPrivVals TransferrablesH((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys_New_Rs*  "cCs?tt|iƒ|iƒ}|t|i|i|i ƒjSdS(slTest the trustee and transferred flag against the truster's public key to make sure it has not been altered.N( sHashshexsselfsTrustees TransferrablesHsSolves ValidatorsTrustersn(sselfsH((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysTest]scCs`t||idƒt||idƒt||idƒ|i|iƒt||idƒdS(s%Writes TrustRelationship to the file.iiN( sWriteNsFilesselfsTrustersTrusteesnswrites Transferrables Validator(sselfsFile((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysWritecs cCsqt|dƒt|dƒt|dƒf\}}}|idƒt|dƒf\}}t|||||ƒSdS(s>Static method: Returns a TrustRelationship read from the file.iiiN( sReadNsFilesTrustersTrusteesnsreads Transferrables ValidatorsTrustRelationship(sFilesTrusters Validators TransferrablesnsTrustee((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysReadks3$( s__name__s __module__s__doc__s__init__s_Loaded_s_New_sTestsWritesReads staticmethod(((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysTrustRelationshipCs      scCs¼xwtootƒ\} } } x7to/t| ƒoPntƒtƒ\} } } qWx7to/t| ƒoPntƒtƒ\} } } qYW| | } | d| d}xZtoR| djo | |jot | |ƒdjoPntƒtƒ\} } } q¯Wt | |ƒ}|djo||7}nt tid| ƒƒ}tt|| | ƒ|| ƒ|joPqqWt|||ƒ}|iƒt| | |||ƒ}||fSdS(sKGenerates a private and public key. Saves private. Set GenSeed5x52 first!iiN(sTruesPickNumsspsqsPubKeys TestPrimesReseedsxsnsphisGCDs ExtEuclidsPrivKeyslongsrandomsrandintsMsSolves PrivateKeysPrivateFilenamesPrivatePasswordsPrivatesWrites PublicKeysNamesAddresssPublic(sPrivateFilenamesNamesAddresssPrivatePasswordsPublicsPrivKeysphisMsPrivatesnsqspsxsPubKey((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysGenKeysrsD   0 %  s PublicKeyFilecBsVtZdZed„Zd„Zd„Zd„Zed„Z dd„Z d„Z RS( sƒThe PublicKeyFile class encapsulates the disk file which maintains a user's public key and all their pertinent trust relationships.cCsf|tf\|_|_hhf\|_|_|o+|ih|i|©scstˆi|ƒS(N(slensselfsRevokedsx(sx(sself(sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys­sN(sopensselfsFilenamesFswrites MAGIC_PUBsWriteNsOwnerslensKeyssisWritessumsmapsTrustedsjsRevokedsclose(sselfsFsisj((sselfsP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysWrite¡s.  (  ( $cCs„t|idƒ}z`|idƒtjo t‚nt|dƒ|_h|_ x<t t|dƒƒD]%}t i |ƒ}||i |iZsN(sopensselfsFilenamesFsCompressswrites MAGIC_ZIPsWriteSszlibscompresssDatas MAGIC_UNZIPsWriteNslensKeyssisWrites SignaturesssumsmapsTrustedsjsclose(sselfsjsFsi((sselfsP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysWriteJs0     ( $cCs ||_dS(N(sCompresssself(sselfsCompress((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysSetCompressionasc scti|ƒti|ƒf\}}tit‡d†ˆiƒƒ}||}||@} | ot t ‡d†| ƒfSntiƒ|i ƒf\}}xo|D]g}ˆii|ƒoNxKˆi|D]8}ˆi||} | iƒo|i| iƒqÉqÉWq¢q¢W||B}}x¼|o´tiƒ}x—|D]}ˆii|ƒovxsˆi|D]`}ˆi||} | idjo | i|jo)| iƒ oq_n|i| iƒq_q_Wq8q8W||O}qWt||@|ƒ} | ott ‡d†| ƒfSnt||@ƒ} | ott ‡d†| ƒfSntt ‡d†|ƒfSdS(s@Test to see if the document has been signed by someone we trust.cs+ˆi|iˆiˆiˆi|iƒS(N(sselfs SignaturessxsTestsDatasKeyssSigner(sx(sself(sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pyskscs ˆi|S(N(sselfsKeyssx(sx(sself(sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysossYN(ssetssSetsTrustedsRevokedsfiltersselfs SignaturessAllSigssSigssRetValsTRUSTEDsmapscopysFoundsTrusterssishas_keysjsTsTestsaddsTrusters Transferrableslists TRANSFERREDsREVOKEDsUNKNOWN( sselfsTrustedsRevokedsisjsAllSigssSigssTrusterssFoundsRetValsT((sselfsP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysTestSignaturesdsN$      cCs<t|ƒtjot|ƒ}nti|dƒ|_dS(s(Construct the pickle from a data object.iÿÿÿÿN(stypesDatas ModuleTypes ModuleObjectscPicklesdumpssself(sselfsData((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysPicklescCs:ti|iƒ}t|tƒo|iƒ}n|SdS(s/Deconstruct the pickle data into a data object.N(scPicklesloadssselfsDatas isinstances ModuleObjectsImport(sselfsData((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysUnpickle’s c Cs?t|i||ƒ|i|i<|iƒ|i|i<|t joùt i ƒt i |igƒf\}}x±|D]©}|ii|ƒox|i|D]z}|i||}|i|iƒ|ii|ihƒ|i|i<||i|i|i<|i|i|i|i '"&  "*( s__name__s __module__s__doc__s__init__sReadsWritesTruesSetCompressionsTestSignaturessPicklesUnpicklesFalsesSign(((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysTPickle#s     )  cCs¦tiƒttiddd>dƒƒgatdƒ}tiƒtittiddd>dƒƒƒtdƒ}tiƒtittiddd>dƒƒƒtdƒ}tiƒtittiddd>dƒƒƒxNt oFt i dƒ}|d joPnt i d ƒ}||joPqçqçWtiƒtittiddd>dƒƒƒtd ||||ƒ\}}td ||ƒ}|iƒd S(s7Interactively generate a new key pair. DO NOT AUTOMATE!ili4is Your name: sYour e-mail address: s,Key pair filename base (___.pub & ___.prv): s!Private key password (blank OK): ss$Repeat password (for verification): s%s.prvs%s.pubN(srandomsseedslongsrandints GenSeed5x52s raw_inputsNamesappendsAddresssBasesTruesgetpasssPass1sPass2sGenKeyssPrivatesPublics PublicKeyFilesKeyFilesWrite(sNamesPrivatesPass1sBasesPass2sAddresssPublicsKeyFile((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pysNewKeys¹s4 #  '  '  '   '(.s__doc__scPicklesgetpasssglobsmd5sossrandomsressetsstypesszlibsTRUSTEDs TRANSFERREDsUNKNOWNsREVOKEDs MAGIC_ZIPs MAGIC_UNZIPs MAGIC_PRIVs MAGIC_PUBscompiles EXT_SEARCHs GenSeed5x52s CompressLevels ExceptionsFileFormatErrorsMismatchedKeyssGCDsSolves ExtEuclids TestPrimesReseedsPickNumssWriteNsReadNsWriteSsReadSsHashs PrivateKeys PublicKeys SignaturesTrustRelationshipsGenKeyss PublicKeyFiles ModuleObjectsTPicklesNewKeys()sTrustRelationshipsUNKNOWNsrandoms TRANSFERREDsgetpasss EXT_SEARCHsReseedsGCDs MAGIC_UNZIPsHashs MAGIC_PRIVs PublicKeyFilesWriteSsresWriteNsMismatchedKeyssTRUSTEDs MAGIC_PUBscPicklesREVOKEDsNewKeyssTPicklesglobs CompressLevelsReadNs TestPrimesReadSsFileFormatErrors Signatures PrivateKeysmd5sGenKeysszlibsPickNumss MAGIC_ZIPs PublicKeys ExtEuclidssetssSolvesoss ModuleObject((sP/mnt/gmirror/ports/security/py-trustedpickle/work/trustedpickle/TrustedPickle.pys?UsV                  2# / #t–