1 # Copyright (c) 1999-2001 Jason Gunthorpe <jgg@debian.org>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 # - gpgm with a status FD being fed keymaterial and other interesting
19 # things does nothing.. If it could ID the keys and stuff over the
20 # status-fd I could decide what to do with them. I would also like it
21 # to report which key it selected for encryption (also if there
22 # were multi-matches..) Being able to detect a key-revoke cert would be
24 # - I would like to be able to fetch the comment and version fields from the
25 # packets so I can tell if a signature is made by pgp2 to enable the
26 # pgp2 encrypting mode.
28 import string, mimetools, multifile, sys, StringIO, os, tempfile, re;
29 import rfc822, time, fcntl, FCNTL, anydbm
33 # "--load-extension","rsa",
34 GPGBasicOptions = ["--no-options","--batch",
35 "--no-default-keyring","--always-trust"];
37 GPGSigOptions = ["--output","-"];
38 GPGSearchOptions = ["--dry-run","--with-colons","--fingerprint"];
39 GPGEncryptOptions = ["--output","-","--quiet","--always-trust",\
40 "--armor","--encrypt"];
41 GPGEncryptPGP2Options = ["--set-filename","","--rfc1991",\
42 "--load-extension","idea",\
43 "--cipher-algo","idea"] + GPGEncryptOptions;
45 # Replay cutoff times in seconds
46 CleanCutOff = 7*24*60*60;
47 AgeCutOff = 4*24*60*60;
48 FutureCutOff = 3*24*60*60;
50 # Set the keyrings, the input is a list of keyrings
51 def SetKeyrings(Rings):
53 GPGKeyRings.append("--keyring");
54 GPGKeyRings.append(x);
56 # GetClearSig takes an un-seekable email message stream (mimetools.Message)
57 # and returns a standard PGP '---BEGIN PGP SIGNED MESSAGE---' bounded
59 # If this is fed to gpg/pgp it will verify the signature and spit out the
60 # signed text component. Email headers and PGP mime (RFC 2015) is understood
61 # but no effort is made to cull any information outside the PGP boundaries
62 # Please note that in the event of a mime decode the mime headers will be
63 # present in the signature text! The return result is a tuple, the first
64 # element is the text itself the second is a mime flag indicating if the
65 # result should be mime processed after sig checking.
67 # Paranoid will check the message text to make sure that all the plaintext is
68 # in fact signed (bounded by a PGP packet)
69 def GetClearSig(Msg,Paranoid = 0):
71 # See if this is a MIME encoded multipart signed message
72 if Msg.gettype() == "multipart/signed":
73 Boundary = Msg.getparam("boundary");
75 raise Error, "multipart/* without a boundary parameter";
77 # Create the multipart handler. Regrettably their implementation
79 SkMessage = StringIO.StringIO();
80 SkMessage.write(Msg.fp.read());
82 mf = multifile.MultiFile(SkMessage)
83 mf.push(Msg.getparam("boundary"));
85 # Check the first bit of the message..
91 if len(string.strip(x)) != 0:
92 raise Error,"Unsigned text in message (at start)";
95 # Get the first part of the multipart message
97 raise Error, "Invalid pgp/mime encoding [no section]";
99 # Get the part as a safe seekable stream
100 Signed = StringIO.StringIO();
101 Signed.write(mf.read());
102 InnerMsg = mimetools.Message(Signed);
104 # Make sure it is the right type
105 if InnerMsg.gettype() != "text/plain":
106 raise Error, "Invalid pgp/mime encoding [wrong plaintext type]";
108 # Get the next part of the multipart message
110 raise Error, "Invalid pgp/mime encoding [no section]";
111 InnerMsg = mimetools.Message(mf);
112 if InnerMsg.gettype() != "application/pgp-signature":
113 raise Error, "Invalid pgp/mime encoding [wrong signature type]";
114 Signature = string.joinfields(mf.readlines(),'');
116 # Check the last bit of the message..
123 if len(string.strip(x)) != 0:
124 raise Error,"Unsigned text in message (at end)";
127 # Append the PGP boundary header and the signature text to re-form the
128 # original signed block [needs to convert to \r\n]
129 Output = "-----BEGIN PGP SIGNED MESSAGE-----\r\n";
130 # Semi-evil hack to get the proper hash type inserted in the message
131 if Msg.getparam('micalg') != None:
132 Output = Output + "Hash: MD5,SHA1,%s\r\n"%(string.upper(Msg.getparam('micalg')[4:]));
133 Output = Output + "\r\n";
134 Output = Output + string.replace(Signed.getvalue(),"\n-","\n- -") + Signature;
138 # Just return the message body
139 return (string.joinfields(Msg.fp.readlines(),''),0);
143 for x in Msg.fp.readlines():
145 Tmp = string.strip(x);
149 # Leading up to the signature
151 if Tmp == "-----BEGIN PGP SIGNED MESSAGE-----":
154 raise Error,"Unsigned text in message (at start)";
157 # In the signature plain text
159 if Tmp == "-----BEGIN PGP SIGNATURE-----":
165 if Tmp == "-----END PGP SIGNATURE-----":
171 raise Error,"Unsigned text in message (at end)";
174 # This opens GPG in 'write filter' mode. It takes Message and sends it
175 # to GPGs standard input, pipes the standard output to a temp file along
176 # with the status FD. The two tempfiles are passed to GPG by fd and are
177 # accessible from the filesystem for only a short period. Message may be
178 # None in which case GPGs stdin is closed directly after forking. This
179 # is best used for sig checking and encryption.
180 # The return result is a tuple (Exit,StatusFD,OutputFD), both fds are
181 # fully rewound and readable.
182 def GPGWriteFilter(Program,Options,Message):
183 # Make sure the tmp files we open are unreadable, there is a short race
184 # between when the temp file is opened and unlinked that some one else
185 # could open it or hard link it. This is not important however as no
186 # Secure data is fed through the temp files.
187 OldMask = os.umask(0777);
189 Output = tempfile.TemporaryFile("w+b");
190 GPGText = tempfile.TemporaryFile("w+b");
192 InPipe = [InPipe[0],InPipe[1]];
197 # Fork off GPG in a horrible way, we redirect most of its FDs
198 # Input comes from a pipe and its two outputs are spooled to unlinked
199 # temp files (ie private)
203 os.dup2(InPipe[0],0);
205 os.dup2(Output.fileno(),1);
206 os.dup2(os.open("/dev/null",os.O_WRONLY),2);
207 os.dup2(GPGText.fileno(),3);
209 Args = [Program,"--status-fd","3"] + GPGBasicOptions + GPGKeyRings + Options
210 os.execvp(Program,Args);
214 # Get rid of the other end of the pipe
221 os.write(InPipe[1],Message);
227 # Wait for GPG to finish
228 Exit = os.waitpid(Child,0);
230 # Create the result including the new readable file descriptors
231 Result = (Exit,os.fdopen(os.dup(GPGText.fileno()),"r"), \
232 os.fdopen(os.dup(Output.fileno()),"r"));
247 # This takes a text passage, a destination and a flag indicating the
248 # compatibility to use and returns an encrypted message to the recipient.
249 # It is best if the recipient is specified using the hex key fingerprint
250 # of the target, ie 0x64BE1319CCF6D393BF87FF9358A6D4EE
251 def GPGEncrypt(Message,To,PGP2):
252 # Encrypt using the PGP5 block encoding and with the PGP5 option set.
253 # This will handle either RSA or DSA/DH asymetric keys.
254 # In PGP2 compatible mode IDEA and rfc1991 encoding are used so that
255 # PGP2 can read the result. RSA keys do not need PGP2 to be set, as GPG
256 # can read a message encrypted with blowfish and RSA.
260 Res = GPGWriteFilter(GPGPath,["-r",To]+GPGEncryptOptions,Message);
263 Text = Res[2].read();
270 # We have to call gpg with a filename or it will create a packet that
271 # PGP2 cannot understand.
272 TmpName = tempfile.mktemp();
275 MsgFile = open(TmpName,"wc");
276 MsgFile.write(Message);
278 Res = GPGWriteFilter(GPGPath,["-r",To]+GPGEncryptPGP2Options+[TmpName],None);
281 Text = Res[2].read();
292 # Checks the signature of a standard PGP message, like that returned by
293 # GetClearSig. It returns a large tuple of the form:
294 # (Why,(SigId,Date,KeyFinger),(KeyID,KeyFinger,Owner,Length,PGP2),Text);
296 # Why = None if checking was OK otherwise an error string.
297 # SigID+Date represent something suitable for use in a replay cache. The
298 # date is returned as the number of seconds since the UTC epoch.
299 # The keyID is also in this tuple for easy use of the replay
301 # KeyID, KeyFinger and Owner represent the Key used to sign this message
302 # PGP2 indicates if the message was created using PGP 2.x
303 # Text is the full byte-for-byte signed text in a string
304 def GPGCheckSig(Message):
307 Res = GPGWriteFilter(GPGPath,GPGSigOptions,Message);
310 # Parse the GPG answer
321 # Grab and split up line
322 Line = Strm.readline();
325 Split = re.split("[ \n]",Line);
326 if Split[0] != "[GNUPG:]":
329 # We only process the first occurance of any tag.
330 if TagMap.has_key(Split[1]):
332 TagMap[Split[1]] = None;
334 # Good signature response
335 if Split[1] == "GOODSIG":
336 # Just in case GPG returned a bad signal before this (bug?)
340 Owner = string.join(Split[3:],' ');
342 # Bad signature response
343 if Split[1] == "BADSIG":
346 Why = "Verification of signature failed";
348 # Bad signature response
349 if Split[1] == "ERRSIG":
353 Why = "GPG error, ERRSIG status tag is invalid";
354 elif Split[7] == '9':
355 Why = "Unable to verify signature, signing key missing.";
356 elif Split[7] == '4':
357 Why = "Unable to verify signature, unknown packet format/key type";
359 Why = "Unable to verify signature, unknown reason";
361 if Split[1] == "NO_PUBKEY":
363 Why = "Unable to verify signature, signing key missing.";
366 if Split[1] == "SIGEXPIRED":
368 Why = "Signature has expired";
371 if Split[1] == "KEYREVOKED":
373 Why = "Signing key has been revoked";
376 if Split[1] == "NODATA" or Split[1] == "BADARMOR":
378 Why = "The packet was corrupted or contained no data";
381 if Split[1] == "SIG_ID":
383 Date = long(Split[4]);
385 # ValidSig has the key finger print
386 if Split[1] == "VALIDSIG":
387 KeyFinger = Split[2];
389 # Reopen the stream as a readable stream
390 Text = Res[2].read();
392 # A gpg failure is an automatic bad signature
393 if Exit[1] != 0 and Why == None:
395 Why = "GPG execution failed " + str(Exit[0]);
397 if GoodSig == 0 and (Why == None or len(Why) == 0):
398 Why = "Checking Failed";
400 # Try to decide if this message was sent using PGP2
402 if (re.search("-----[\n\r][\n\r]?Version: 2\\.",Message) != None):
405 return (Why,(SigId,Date,KeyFinger),(KeyID,KeyFinger,Owner,0,PGP2Message),Text);
411 # Search for keys given a search pattern. The pattern is passed directly
412 # to GPG for processing. The result is a list of tuples of the form:
413 # (KeyID,KeyFinger,Owner,Length)
414 # Which is similar to the key identification tuple output by GPGChecksig
415 def GPGKeySearch(SearchCriteria):
416 Args = [GPGPath] + GPGBasicOptions + GPGKeyRings + GPGSearchOptions + \
417 [SearchCriteria," 2> /dev/null"]
424 Strm = os.popen(string.join(Args," "),"r");
427 # Grab and split up line
428 Line = Strm.readline();
431 Split = string.split(Line,":");
433 # Store some of the key fields
434 if Split[0] == 'pub':
437 Length = int(Split[2]);
440 if Split[0] == 'fpr':
441 if Hits.has_key(Split[9]):
443 Hits[Split[9]] = None;
444 Result.append( (KeyID,Split[9],Owner,Length) );
450 # Print the available key information in a format similar to GPG's output
451 # We do not know the values of all the feilds so they are just replaced
453 def GPGPrintKeyInfo(Ident):
454 print "pub %u?/%s ??-??-?? %s" % (Ident[3],Ident[0][-8:],Ident[2]);
455 print " key fingerprint = 0x%s" % (Ident[1]);
457 # Perform a substition of template
458 def TemplateSubst(Map,Template):
460 Template = string.replace(Template,x,Map[x]);
463 # The replay class uses a python DB (BSD db if avail) to implement
464 # protection against replay. Replay is an attacker capturing the
465 # plain text signed message and sending it back to the victim at some
466 # later date. Each signature has a unique signature ID (and signing
467 # Key Fingerprint) as well as a timestamp. The first stage of replay
468 # protection is to ensure that the timestamp is reasonable, in particular
469 # not to far ahead or too far behind the current system time. The next
470 # step is to look up the signature + key fingerprint in the replay database
471 # and determine if it has been recived. The database is cleaned out
472 # periodically and old signatures are discarded. By using a timestamp the
473 # database size is bounded to being within the range of the allowed times
474 # plus a little fuzz. The cache is serialized with a flocked lock file
476 def __init__(self,Database):
477 self.Lock = open(Database + ".lock","w",0600);
478 fcntl.flock(self.Lock.fileno(),FCNTL.LOCK_EX);
479 self.DB = anydbm.open(Database,"c",0600);
480 self.CleanCutOff = CleanCutOff;
481 self.AgeCutOff = AgeCutOff;
482 self.FutureCutOff = FutureCutOff;
484 # Close the cache and lock
491 # Clean out any old signatures
493 CutOff = time.time() - self.CleanCutOff;
494 for x in self.DB.keys():
495 if int(self.DB[x]) <= CutOff:
498 # Check a signature. 'sig' is a 3 tuple that has the sigId, date and
501 if Sig[0] == None or Sig[1] == None or Sig[2] == None:
502 return "Invalid signature";
503 if int(Sig[1]) > time.time() + self.FutureCutOff:
504 return "Signature has a time too far in the future";
505 if self.DB.has_key(Sig[0] + '-' + Sig[2]):
506 return "Signature has already been received";
507 if int(Sig[1]) < time.time() - self.AgeCutOff:
508 return "Signature has passed the age cut off ";
509 # + str(int(Sig[1])) + ',' + str(time.time()) + "," + str(Sig);
512 # Add a signature, the sig is the same as is given to Check
514 if Sig[0] == None or Sig[1] == None:
515 raise RuntimeError,"Invalid signature";
516 if Sig[1] < time.time() - self.CleanCutOff:
518 Key = Sig[0] + '-' + Sig[2]
519 if self.DB.has_key(Key):
520 if int(self.DB[Key]) < Sig[1]:
521 self.DB[Key] = str(int(Sig[1]));
523 self.DB[Key] = str(int(Sig[1]));