net/data/ocsp_unittest/annotate_test_data.py
def GenerateCommentForBlock(block_name, block_data):
  """Returns a string describing |block_data|. The format of |block_data| is
  inferred from |block_name|, and is pretty-printed accordingly. For
  instance CERTIFICATE is understood to be an X.509 certificate and pretty
  printed using OpenSSL's x509 command. If there is no specific pretty-printer
  for the data type, it is annotated using "openssl asn1parse"."""

  # Try to pretty printing as X.509 certificate.
  if "CERTIFICATE" in block_name:
    p = subprocess.Popen(["openssl", "x509", "-text", "-noout",
                          "-inform", "DER"],
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)
    stdout_data, stderr_data = p.communicate(block_data)

    # If pretty printing succeeded, return it.
    if p.returncode == 0:
      stdout_data = stdout_data.strip()
      return '$ openssl x509 -text < [%s]\n%s' % (block_name, stdout_data)

  # Try pretty printing as OCSP Response.
  if block_name == "OCSP RESPONSE":
    tmp_file_path = "tmp_ocsp.der"
    WriteStringToFile(block_data, tmp_file_path)
    p = subprocess.Popen(["openssl", "ocsp", "-noverify", "-resp_text",
                          "-respin", tmp_file_path],
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)
    stdout_data, stderr_data = p.communicate(block_data)
    os.remove(tmp_file_path)

    # If pretty printing succeeded, return it.
    if p.returncode == 0:
      stdout_data = stdout_data.strip()
      # May contain embedded CERTIFICATE pem blocks. Escape these since
      # CERTIFICATE already has meanining in the test file.
      stdout_data = stdout_data.replace("-----", "~~~~~")
      return '$ openssl ocsp -resp_text -respin <([%s])\n%s' % (block_name,
                                                                stdout_data)
    print 'Error pretty printing OCSP response:\n',stderr_data

  # Otherwise try pretty printing using asn1parse.

  p = subprocess.Popen(['openssl', 'asn1parse', '-i', '-inform', 'DER'],
                       stdout=subprocess.PIPE, stdin=subprocess.PIPE,
                       stderr=subprocess.PIPE)
  stdout_data, stderr_data = p.communicate(input=block_data)
  generated_comment = '$ openssl asn1parse -i < [%s]\n%s' % (block_name,
                                                             stdout_data)

  # The OCTET STRING encoded BasicOCSPResponse is also parsed out using
  #'openssl asn1parse'.
  if block_name == 'OCSP RESPONSE':
    if '[HEX DUMP]:' in generated_comment:
      (generated_comment, response) = generated_comment.split('[HEX DUMP]:', 1)
      response = response.replace('\n', '')
      if len(response) % 2 != 0:
        response = '0' + response
      response = GenerateCommentForBlock('INNER', response.decode('hex'))
      response = response.split('\n', 1)[1]
      response = response.replace(': ', ':      ')
      generated_comment += '\n%s' % (response)
  return generated_comment.strip('\n')
Returns a string describing |block_data|. The format of |block_data| is
  inferred from |block_name|, and is pretty-printed accordingly. For
  instance CERTIFICATE is understood to be an X.509 certificate and pretty
  printed using OpenSSL's x509 command. If there is no specific pretty-printer
  for the data type, it is annotated using "openssl asn1parse".
Returns a string describing |block_data|. The format of |block_data| is
inferred from |block_name|, and is pretty-printed accordingly. For
instance CERTIFICATE is understood to be an X.509 certificate and pretty
printed using OpenSSL's x509 command. If there is no specific pretty-printer
for the data type, it is annotated using "openssl asn1parse".
Returns a string describing |block_data| . The format of |block_data| is inferred from |block_name| and is pretty - printed accordingly . For instance CERTIFICATE is understood to be an X . 509 certificate and pretty printed using OpenSSL ' s x509 command . If there is no specific pretty - printer for the data type it is annotated using " openssl asn1parse " .
def GenerateCommentForBlock(block_name, block_data):
  if "CERTIFICATE" in block_name:
    p = subprocess.Popen(["openssl", "x509", "-text", "-noout",
                          "-inform", "DER"],
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)
    stdout_data, stderr_data = p.communicate(block_data)
    if p.returncode == 0:
      stdout_data = stdout_data.strip()
      return '$ openssl x509 -text < [%s]\n%s' % (block_name, stdout_data)
  if block_name == "OCSP RESPONSE":
    tmp_file_path = "tmp_ocsp.der"
    WriteStringToFile(block_data, tmp_file_path)
    p = subprocess.Popen(["openssl", "ocsp", "-noverify", "-resp_text",
                          "-respin", tmp_file_path],
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)
    stdout_data, stderr_data = p.communicate(block_data)
    os.remove(tmp_file_path)
    if p.returncode == 0:
      stdout_data = stdout_data.strip()
      stdout_data = stdout_data.replace("-----", "~~~~~")
      return '$ openssl ocsp -resp_text -respin <([%s])\n%s' % (block_name,
                                                                stdout_data)
    print 'Error pretty printing OCSP response:\n',stderr_data
  p = subprocess.Popen(['openssl', 'asn1parse', '-i', '-inform', 'DER'],
                       stdout=subprocess.PIPE, stdin=subprocess.PIPE,
                       stderr=subprocess.PIPE)
  stdout_data, stderr_data = p.communicate(input=block_data)
  generated_comment = '$ openssl asn1parse -i < [%s]\n%s' % (block_name,
                                                             stdout_data)
  if block_name == 'OCSP RESPONSE':
    if '[HEX DUMP]:' in generated_comment:
      (generated_comment, response) = generated_comment.split('[HEX DUMP]:', 1)
      response = response.replace('\n', '')
      if len(response) % 2 != 0:
        response = '0' + response
      response = GenerateCommentForBlock('INNER', response.decode('hex'))
      response = response.split('\n', 1)[1]
      response = response.replace(': ', ':      ')
      generated_comment += '\n%s' % (response)
  return generated_comment.strip('\n')
def GenerateCommentForBlock ( block_name , block_data ) : if "CERTIFICATE" in block_name : p = subprocess . Popen ( [ "openssl" , "x509" , "-text" , "-noout" , "-inform" , "DER" ] , stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) stdout_data , stderr_data = p . communicate ( block_data ) if p . returncode == 0 : stdout_data = stdout_data . strip ( ) return '$ openssl x509 -text < [%s]\n%s' % ( block_name , stdout_data ) if block_name == "OCSP RESPONSE" : tmp_file_path = "tmp_ocsp.der" WriteStringToFile ( block_data , tmp_file_path ) p = subprocess . Popen ( [ "openssl" , "ocsp" , "-noverify" , "-resp_text" , "-respin" , tmp_file_path ] , stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) stdout_data , stderr_data = p . communicate ( block_data ) os . remove ( tmp_file_path ) if p . returncode == 0 : stdout_data = stdout_data . strip ( ) stdout_data = stdout_data . replace ( "-----" , "~~~~~" ) return '$ openssl ocsp -resp_text -respin <([%s])\n%s' % ( block_name , stdout_data ) print 'Error pretty printing OCSP response:\n' , stderr_data p = subprocess . Popen ( [ 'openssl' , 'asn1parse' , '-i' , '-inform' , 'DER' ] , stdout = subprocess . PIPE , stdin = subprocess . PIPE , stderr = subprocess . PIPE ) stdout_data , stderr_data = p . communicate ( input = block_data ) generated_comment = '$ openssl asn1parse -i < [%s]\n%s' % ( block_name , stdout_data ) if block_name == 'OCSP RESPONSE' : if '[HEX DUMP]:' in generated_comment : ( generated_comment , response ) = generated_comment . split ( '[HEX DUMP]:' , 1 ) response = response . replace ( '\n' , '' ) if len ( response ) % 2 != 0 : response = '0' + response response = GenerateCommentForBlock ( 'INNER' , response . decode ( 'hex' ) ) response = response . split ( '\n' , 1 ) [ 1 ] response = response . replace ( ': ' , ':      ' ) generated_comment += '\n%s' % ( response ) return generated_comment . strip ( '\n' )
Returns a string describing |block_data|.
Returns a string describing |block_data| .
"""Returns a string describing |block_data|. The format of |block_data| is
  inferred from |block_name|, and is pretty-printed accordingly. For
  instance CERTIFICATE is understood to be an X.509 certificate and pretty
  printed using OpenSSL's x509 command. If there is no specific pretty-printer
  for the data type, it is annotated using "openssl asn1parse".""" # Try to pretty printing as X.509 certificate. # If pretty printing succeeded, return it. # Try pretty printing as OCSP Response. # If pretty printing succeeded, return it. # May contain embedded CERTIFICATE pem blocks. Escape these since # CERTIFICATE already has meanining in the test file. # Otherwise try pretty printing using asn1parse. # The OCTET STRING encoded BasicOCSPResponse is also parsed out using #'openssl asn1parse'.
block_name
block_data