gnuk/tool/gpg_agent.py

129 lines
3.9 KiB
Python
Raw Permalink Normal View History

2013-02-14 02:09:06 +00:00
"""
gpg_agent.py - a library to connect gpg-agent
2015-08-03 10:22:02 +00:00
Copyright (C) 2013, 2015 Free Software Initiative of Japan
2013-02-14 02:09:06 +00:00
Author: NIIBE Yutaka <gniibe@fsij.org>
This file is a part of Gnuk, a GnuPG USB Token implementation.
Gnuk is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gnuk is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
2013-01-20 09:01:24 +00:00
import platform, os, socket
IS_WINDOWS=(platform.system() == 'Windows')
BUFLEN=1024
class gpg_agent(object):
def __init__(self):
if IS_WINDOWS:
home = os.getenv("HOME")
2013-01-21 03:09:33 +00:00
if not home:
home = os.getenv("APPDATA")
2013-01-20 09:01:24 +00:00
comm_port = os.path.join(home, "gnupg", "S.gpg-agent")
#
f = open(comm_port, "rb", 0)
2015-07-28 05:45:18 +00:00
infostr = f.read().decode('UTF-8')
2013-01-20 09:01:24 +00:00
f.close()
#
info = infostr.split('\n', 1)
port = int(info[0])
nonce = info[1]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", port))
s.send(nonce)
else:
infostr = os.getenv("GPG_AGENT_INFO")
info = infostr.split(':', 2)
path = info[0]
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(path)
self.sock = s
2015-07-28 05:45:18 +00:00
self.buf_remained = b""
2013-01-20 11:40:19 +00:00
self.response = None
2013-01-20 09:01:24 +00:00
def read_line(self):
2015-07-28 05:45:18 +00:00
line = b""
if self.buf_remained != b"":
2013-01-20 09:01:24 +00:00
chunk = self.buf_remained
else:
chunk = self.sock.recv(BUFLEN)
while True:
2015-07-28 05:45:18 +00:00
pos = chunk.find(b'\n')
2013-01-20 09:01:24 +00:00
if pos >= 0:
self.buf_remained = chunk[pos+1:]
line = line + chunk[0:pos]
return line
else:
line = line + chunk
chunk = self.sock.recv(BUFLEN)
2013-01-20 11:40:19 +00:00
def get_response(self):
2013-01-20 12:17:43 +00:00
r = self.response
2015-07-28 05:45:18 +00:00
result = b""
2013-01-20 12:17:43 +00:00
while True:
2015-07-28 05:45:18 +00:00
i = r.find(b'%')
2013-01-20 12:17:43 +00:00
if i < 0:
result += r
break
2015-07-28 05:45:18 +00:00
hex_str = r[i+1:i+3].decode('UTF-8')
2013-01-20 12:17:43 +00:00
result += r[0:i]
2015-07-28 05:45:18 +00:00
if bytes == str:
result += chr(int(hex_str,16))
else:
result += bytes.fromhex(hex_str)
2013-01-20 12:17:43 +00:00
r = r[i+3:]
return result
2013-01-20 11:40:19 +00:00
2013-01-20 09:01:24 +00:00
def send_command(self, cmd):
2015-07-28 05:45:18 +00:00
self.sock.send(cmd.encode('UTF-8'))
self.response = b""
2013-01-20 09:01:24 +00:00
while True:
while True:
l = self.read_line()
2015-07-28 05:45:18 +00:00
if l[0] != b'#'[0]:
2013-01-20 09:01:24 +00:00
break
2015-07-28 05:45:18 +00:00
if l[0] == b'D'[0]:
2013-01-20 11:40:19 +00:00
self.response += l[2:]
2015-07-28 05:45:18 +00:00
elif l[0:2] == b'OK':
2013-01-20 11:40:19 +00:00
return True
2015-07-28 05:45:18 +00:00
elif l[0:3] == b'ERR':
2013-01-20 09:01:24 +00:00
return False
else: # XXX: S, INQUIRE, END
return False
def close(self):
2015-07-28 05:45:18 +00:00
self.sock.send(b'BYE\n')
2013-01-20 09:01:24 +00:00
bye = self.read_line()
self.sock.close()
return bye # "OK closing connection"
# Test
2013-01-20 12:17:43 +00:00
if __name__ == '__main__':
g = gpg_agent()
2015-07-28 05:45:18 +00:00
print(g.read_line().decode('UTF-8'))
print(g.send_command("KEYINFO --list --data\n"))
kl_str = g.get_response().decode('UTF-8')
2013-01-20 12:17:43 +00:00
kl_str = kl_str[0:-1]
kl = kl_str.split('\n')
import re
kl_o3 = [kg for kg in kl if re.search("OPENPGP\\.3", kg)]
2015-07-28 05:45:18 +00:00
print(kl_o3)
2013-01-20 12:17:43 +00:00
kg = kl_o3[0].split(' ')[0]
2015-07-28 05:45:18 +00:00
print(g.send_command("READKEY %s\n" % kg))
2013-01-20 12:17:43 +00:00
r = g.get_response()
import binascii
2015-07-28 05:45:18 +00:00
print(binascii.hexlify(r).decode('UTF-8'))
print(g.close().decode('UTF-8'))