Skip to content

Commit 2192510

Browse files
committed
added new, updated dane encryption example
1 parent a68cc55 commit 2192510

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

examples/dane_encrypt.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
# an example of using getdns to pull out a TLSA record,
3+
# extract a certificate, extract the public key, and then
4+
# encrypt some text
5+
#
6+
# requires the following Python modules:
7+
# getdns
8+
# m2crypto
9+
#
10+
11+
12+
import getdns
13+
import M2Crypto as m2
14+
from M2Crypto import RSA
15+
import sys
16+
17+
18+
19+
#
20+
# I commented out the "return None" because this is demo code and you
21+
# should be able to play with it. But, in deployed applications you
22+
# MUST check that dnssec_status is GETDNS_DNSSEC_SECURE
23+
#
24+
25+
def get_first_secure_response(results):
26+
replies_tree = results.replies_tree
27+
if (not replies_tree) or (not len(replies_tree)) or (not replies_tree[0]['answer']) or (not len(replies_tree[0]['answer'])):
28+
print 'empty answer list'
29+
return None
30+
else:
31+
reply = replies_tree[0]
32+
if reply['dnssec_status'] != getdns.GETDNS_DNSSEC_SECURE:
33+
print 'insecure reply'
34+
# return None
35+
answer = replies_tree[0]['answer']
36+
record = [ x for x in answer if x['type'] is getdns.GETDNS_RRTYPE_TLSA ]
37+
if len(record) == 0:
38+
print 'no answers of type TLSA'
39+
return None
40+
return record[0]
41+
42+
def main():
43+
tls_name = '77fa5113ab6a532ce2e6901f3bd3351c0db5845e0b1b5fb09907808d._smimecert.getdnsapi.org'
44+
45+
if len(sys.argv) == 2:
46+
tls_name = sys.argv[1]
47+
c = getdns.Context()
48+
extensions = { 'dnssec_return_status' : getdns.GETDNS_EXTENSION_TRUE }
49+
results = c.general(tls_name, request_type=getdns.RRTYPE_TLSA, extensions=extensions)
50+
if results.replies_full['status'] != getdns.GETDNS_RESPSTATUS_GOOD:
51+
print 'query status is {0}'.format(results.status)
52+
sys.exit(1)
53+
else:
54+
record = get_first_secure_response(results)
55+
cert = record['rdata']['certificate_association_data']
56+
try:
57+
x509 = m2.X509.load_cert_der_string(cert)
58+
rsakey = x509.get_pubkey().get_rsa()
59+
encrypted = rsakey.public_encrypt("A chunk of text", RSA.pkcs1_oaep_padding)
60+
print encrypted.encode('base64')
61+
except:
62+
print 'Error: ', sys.exc_info()[0]
63+
sys.exit(1)
64+
65+
if __name__ == '__main__':
66+
main()

0 commit comments

Comments
 (0)