From 2437ab75a306bb70b8d54409effb2be85965bb7e Mon Sep 17 00:00:00 2001 From: William Grant Date: Wed, 9 Mar 2011 21:21:58 +1100 Subject: [PATCH] Extract public key parsing. --- dkim/__init__.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/dkim/__init__.py b/dkim/__init__.py index 33694ef..45592d9 100644 --- a/dkim/__init__.py +++ b/dkim/__init__.py @@ -131,6 +131,18 @@ def hash_headers(hasher, canonicalize_headers, headers, include_headers, hasher.update(":") hasher.update(x[1]) + +def parse_public_key(data): + x = asn1_parse(ASN1_Object, data) + # Not sure why the [1:] is necessary to skip a byte. + pkd = asn1_parse(ASN1_RSAPublicKey, x[0][1][1:]) + pk = { + 'modulus': pkd[0][0], + 'publicExponent': pkd[0][1], + } + return pk + + INTEGER = 0x02 BIT_STRING = 0x03 OCTET_STRING = 0x04 @@ -592,13 +604,7 @@ def verify(message, debuglog=None, dnsfunc=dnstxt): if debuglog is not None: print >>debuglog, "invalid format in _domainkey txt record" return False - x = asn1_parse(ASN1_Object, base64.b64decode(pub['p'])) - # Not sure why the [1:] is necessary to skip a byte. - pkd = asn1_parse(ASN1_RSAPublicKey, x[0][1][1:]) - pk = { - 'modulus': pkd[0][0], - 'publicExponent': pkd[0][1], - } + pk = parse_public_key(base64.b64decode(pub['p'])) modlen = len(int2str(pk['modulus'])) if debuglog is not None: print >>debuglog, "modlen:", modlen