summaryrefslogtreecommitdiff
path: root/src/leap/crypto/certs.py
blob: aa1fc9e99b030a273a58a733a38133b200eafe35 (plain)
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
import ctypes
import socket

import gnutls.connection
import gnutls.library


def get_https_cert_fingerprint(domain):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    cred = gnutls.connection.X509Credentials()

    session = gnutls.connection.ClientSession(sock, cred)
    session.connect((domain, 443))
    session.handshake()
    cert = session.peer_certificate

    _buffer = ctypes.create_string_buffer(20)
    buffer_length = ctypes.c_size_t(20)

    gnutls.library.functions.gnutls_x509_crt_get_fingerprint(
        cert._c_object, gnutls.library.constants.GNUTLS_DIG_SHA1,  # 3
        ctypes.byref(_buffer), ctypes.byref(buffer_length))

    # deinit
    #server_cert._X509Certificate__deinit(server_cert._c_object)
    # needed? is segfaulting

    fpr = ctypes.string_at(_buffer, buffer_length.value)
    hex_fpr = u":".join(u"%02X" % ord(char) for char in fpr)

    return hex_fpr