@@ -561,43 +561,40 @@ def utils_cert_download(_, args):
561561 :param _: ignore/unused
562562 :param args: Parsed arguments
563563 """
564- import ssl
565564 from urllib .parse import urlparse
566565 import socket
566+ from OpenSSL import SSL , crypto
567567 import traceback
568568
569569 file = sys .stdout
570570 hostname = 'unset'
571571 port = 'unkown'
572+ if args .output :
573+ file = open (args .output , 'w' )
574+ parsed_url = urlparse (args .hostname )
575+ hostname = parsed_url .hostname or args .hostname # Use the parse hostname, or it None use the supplied one
576+ port = int (parsed_url .port or args .port ) # Use the parsed port, if not use the supplied one (default 443)
577+ certs = []
572578 try :
573- if args .output :
574- file = open (args .output , 'w' )
575- parsed_url = urlparse (args .hostname )
576- hostname = parsed_url .hostname or args .hostname # Use the parse hostname, or it None use the supplied one
577- port = int (parsed_url .port or args .port ) # Use the parsed port, if not use the supplied one (default 443)
578- conn = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
579- context = ssl .SSLContext (ssl .PROTOCOL_SSLv23 )
580- sock = context .wrap_socket (conn , server_hostname = hostname )
581- if not args .quiet or args .debug :
582- print_stderr (f'Attempting to download PEM certificate from { hostname } :{ port } ...' )
583579 if args .debug :
584- print_stderr ('Connecting to host...' )
585- sock .connect ((hostname , port ))
586- if args .debug :
587- print_stderr ('Getting peer cert...' )
588- peer_cert = sock .getpeercert (True )
589- if not peer_cert :
590- print_stderr (f'Error: Failed to download peer certificate data from { hostname } :{ port } ' )
591- exit (1 )
592- if args .debug :
593- print_stderr ('Converting DER to PEM...' )
594- cert_data = ssl .DER_cert_to_PEM_cert (peer_cert )
595- if not cert_data or cert_data == '' :
596- print_stderr (f'Error: Failed to convert certificate data to PEM from { hostname } :{ port } ' )
597- exit (1 )
598- else :
599- print (cert_data .strip (), file = file ) # Print the downloaded PEM certificate
600- except Exception as e :
580+ print_stderr (f'Connecting to { hostname } on { port } ...' )
581+ conn = SSL .Connection (SSL .Context (SSL .TLSv1_2_METHOD ), socket .socket ())
582+ conn .connect ((hostname , port ))
583+ conn .do_handshake ()
584+ certs = conn .get_peer_cert_chain ()
585+ for index , cert in enumerate (certs ):
586+ cert_components = dict (cert .get_subject ().get_components ())
587+ if (sys .version_info [0 ] >= 3 ):
588+ cn = cert_components .get (b'CN' )
589+ else :
590+ cn = cert_components .get ('CN' )
591+ if not args .quiet :
592+ print_stderr (f'Centificate { index } - CN: { cn } ' )
593+ if (sys .version_info [0 ] >= 3 ):
594+ print ((crypto .dump_certificate (crypto .FILETYPE_PEM , cert ).decode ('utf-8' )).strip (), file = file ) # Print the downloaded PEM certificate
595+ else :
596+ print ((crypto .dump_certificate (crypto .FILETYPE_PEM , cert )).strip (), file = file )
597+ except SSL .Error as e :
601598 print_stderr (f'ERROR: Exception ({ e .__class__ .__name__ } ) Downloading certificate from { hostname } :{ port } - { e } .' )
602599 if args .debug :
603600 traceback .print_exc ()
0 commit comments