예제 #1
0
 def verify(self, data, sig):
     """ returns of the good and bad signatures"""
     sigfile = datafile = None
     try:
         # create temporary files
         fd, sigfile = pycompat.mkstemp(prefix=b"hg-gpg-", suffix=b".sig")
         fp = os.fdopen(fd, r'wb')
         fp.write(sig)
         fp.close()
         fd, datafile = pycompat.mkstemp(prefix=b"hg-gpg-", suffix=b".txt")
         fp = os.fdopen(fd, r'wb')
         fp.write(data)
         fp.close()
         gpgcmd = (
             b"%s --logger-fd 1 --status-fd 1 --verify \"%s\" \"%s\"" % (
                 self.path,
                 sigfile,
                 datafile,
             ))
         ret = procutil.filter(b"", gpgcmd)
     finally:
         for f in (sigfile, datafile):
             try:
                 if f:
                     os.unlink(f)
             except OSError:
                 pass
     keys = []
     key, fingerprint = None, None
     for l in ret.splitlines():
         # see DETAILS in the gnupg documentation
         # filter the logger output
         if not l.startswith(b"[GNUPG:]"):
             continue
         l = l[9:]
         if l.startswith(b"VALIDSIG"):
             # fingerprint of the primary key
             fingerprint = l.split()[10]
         elif l.startswith(b"ERRSIG"):
             key = l.split(b" ", 3)[:2]
             key.append(b"")
             fingerprint = None
         elif (l.startswith(b"GOODSIG") or l.startswith(b"EXPSIG")
               or l.startswith(b"EXPKEYSIG") or l.startswith(b"BADSIG")):
             if key is not None:
                 keys.append(key + [fingerprint])
             key = l.split(b" ", 2)
             fingerprint = None
     if key is not None:
         keys.append(key + [fingerprint])
     return keys
예제 #2
0
 def sign(self, data):
     gpgcmd = b"%s --sign --detach-sign%s" % (self.path, self.key)
     return procutil.filter(data, gpgcmd)