-
Notifications
You must be signed in to change notification settings - Fork 1
/
show_chain.py
executable file
·65 lines (51 loc) · 1.93 KB
/
show_chain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
# This file is part of the Perspectives Notary Server
#
# Copyright (C) 2011 Ondrej Mikle, CZ.NIC Labs
#
# This program 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, version 3 of the License.
#
# This program 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/>.
# Simple viewer for certificate chains. Given observation id in ee_certs table,
# prints out whole cert chain for that cert piping it through openssl.
import sys
import time
from datetime import datetime
import shlex
import subprocess
import hashlib
import db
import config
import notary_common
if len(sys.argv) < 3:
print >> sys.stderr, "show_chain.py <notary.config> <ee_cert_id_from_db>"
sys.exit(1)
config.config_initialize(sys.argv[1])
db.db_initialize(config.Config)
ee_cert_id = int(sys.argv[2])
cursor = db.Db.cursor()
sql = "SELECT certificate FROM ee_certs WHERE id=%s LIMIT 1"
cursor.execute(sql, (ee_cert_id,))
row = cursor.fetchone()
db.Db.commit()
if not row:
sys.exit("No certificate by that id")
ee_cert = str(row['certificate'])
ca_certs = notary_common.get_ca_chain(ee_cert_id)
#cmd = shlex.split("openssl x509 -inform der -noout -text")
cmd = shlex.split("openssl x509 -inform der -text")
for cert in [ee_cert] + ca_certs:
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(cout, cerr) = p.communicate(cert)
print cout
print "** SHA1:", hashlib.sha1(cert).hexdigest(), "MD5:", hashlib.md5(cert).hexdigest(), "SHA256:", hashlib.sha256(cert).hexdigest()
print "-"*20
p.wait()