Skip to content

Commit

Permalink
Added Service.get_fingerprint()
Browse files Browse the repository at this point in the history
  • Loading branch information
von committed Dec 11, 2011
1 parent e00e1b6 commit 83def16
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Perspectives/Service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
"""Classes for representing application services"""

import re
import ssl

from Fingerprint import Fingerprint

class Service:
"""Representation of a application service"""

Expand All @@ -14,6 +19,18 @@ def __init__(self, hostname, port, type=None):
def __str__(self):
return "%s:%s,%s" % (self.hostname, self.port, self.type)

# ssl.get_server_certificate() doesn't terminate the PEM correctly,
# it is missing a newline before the END line. This re detects that.
bad_cert_end_re = re.compile("(\S)(-----END CERTIFICATE-----)$",
re.MULTILINE)

def get_fingerprint(self):
"""Return the Service's certificate fingerprint"""
cert_pem = ssl.get_server_certificate((self.hostname, self.port))
# Fix missing newline before END line
cert_pem = self.bad_cert_end_re.sub(r"\1\n\2", cert_pem)
return Fingerprint.from_certificate_PEM(cert_pem)

class ServiceType:
"""Constants for service types"""
# Determined experimentally. I don't know where these are documented.
Expand Down

0 comments on commit 83def16

Please sign in to comment.