<s> import sys <EOL> import socket <EOL> import logging <EOL> from optparse import OptionParser <EOL> import subprocess <EOL> from tls import * <EOL> def make_hello ( ) : <EOL> status_extension = StatusRequestExtension . create ( ) <EOL> hello = ClientHelloMessage . create ( TLSRecord . TLS1_0 , <EOL> '<STR_LIT>' , <EOL> [ TLS_RSA_WITH_RC4_128_MD5 , <EOL> TLS_RSA_WITH_RC4_128_SHA , <EOL> TLS_RSA_WITH_3DES_EDE_CBC_SHA , <EOL> TLS_RSA_WITH_AES_128_CBC_SHA , <EOL> TLS_RSA_WITH_AES_256_CBC_SHA , <EOL> TLS_RSA_WITH_AES_128_CBC_SHA256 , <EOL> TLS_RSA_WITH_AES_256_CBC_SHA256 ] , <EOL> extensions = [ status_extension <EOL> ] ) <EOL> record = TLSRecord . create ( content_type = TLSRecord . Handshake , <EOL> version = TLSRecord . TLS1_0 , <EOL> message = hello . bytes ) <EOL> return record . bytes <EOL> def print_ocsp ( ocsp ) : <EOL> p = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> stdout = subprocess . PIPE , <EOL> stdin = subprocess . PIPE ) <EOL> p . stdin . write ( ocsp ) <EOL> p . stdin . close ( ) <EOL> print p . stdout . read ( ) <EOL> def stapling ( f ) : <EOL> print '<STR_LIT>' <EOL> f . write ( make_hello ( ) ) <EOL> print '<STR_LIT>' <EOL> while True : <EOL> record = read_tls_record ( f ) <EOL> if record . content_type ( ) == TLSRecord . Handshake : <EOL> message = HandshakeMessage . from_bytes ( record . message ( ) ) <EOL> if message . message_type ( ) == HandshakeMessage . ServerHelloDone : <EOL> print '<STR_LIT>' <EOL> break <EOL> elif message . message_type ( ) == HandshakeMessage . CertificateStatus : <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' , message . status_type ( ) <EOL> print_ocsp ( message . response ( ) ) <EOL> break <EOL> elif record . content_type ( ) == TLSRecord . Alert : <EOL> alert = AlertMessage . from_bytes ( record . message ( ) ) <EOL> print alert <EOL> if alert . alert_level ( ) == AlertMessage . Fatal : <EOL> raise IOError ( '<STR_LIT>' ) <EOL> else : <EOL> print '<STR_LIT>' % ( record . content_type ( ) ) <EOL> def main ( ) : <EOL> options = OptionParser ( usage = '<STR_LIT>' , <EOL> description = '<STR_LIT>' ) <EOL> options . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> type = '<STR_LIT:int>' , default = <NUM_LIT> , <EOL> help = '<STR_LIT>' ) <EOL> options . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' ) <EOL> opts , args = options . parse_args ( ) <EOL> if len ( args ) < <NUM_LIT:1> : <EOL> options . print_help ( ) <EOL> return <EOL> if opts . debug : <EOL> logging . basicConfig ( level = logging . DEBUG ) <EOL> s = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> print '<STR_LIT>' <EOL> s . connect ( ( args [ <NUM_LIT:0> ] , opts . port ) ) <EOL> f = s . makefile ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> f = LoggedFile ( f ) <EOL> stapling ( f ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
