def unwrap(p8_private_key, passphrase=None):
 if passphrase:
  passphrase = tobytes(passphrase)
  found = False
  try:
   p8_private_key = PBES1.decrypt(p8_private_key, passphrase)
   found = True
  except PbesError as e:
   error_str = ('PBES1[%s]' % str(e))
  except ValueError:
   error_str = 'PBES1[Invalid]'
  if (not found):
   try:
    p8_private_key = PBES2.decrypt(p8_private_key, passphrase)
    found = True
   except PbesError as e:
    error_str += (',PBES2[%s]' % str(e))
   except ValueError:
    error_str += ',PBES2[Invalid]'
  if (not found):
   raise ValueError(('Error decoding PKCS#8 (%s)' % error_str))
 pk_info = DerSequence().decode(p8_private_key, nr_elements=(2, 3, 4))
 if ((len(pk_info) == 2) and (not passphrase)):
  raise ValueError('Not a valid clear PKCS#8 structure (maybe it is encrypted?)')
 if (pk_info[0] != 0):
  raise ValueError('Not a valid PrivateKeyInfo SEQUENCE')
 algo = DerSequence().decode(pk_info[1], nr_elements=(1, 2))
 algo_oid = DerObjectId().decode(algo[0]).value
 if (len(algo) == 1):
  algo_params = None
 else:
  try:
   DerNull().decode(algo[1])
   algo_params = None
  except:
   algo_params = algo[1]
 private_key = DerOctetString().decode(pk_info[2]).payload
 return (algo_oid, private_key, algo_params)

def unwrap ( p8_private_key , passphrase = None ) : if passphrase : passphrase = tobytes ( passphrase ) found = False try : p8_private_key = PBES1 . decrypt ( p8_private_key , passphrase ) found = True except PbesError as e : error_str = ( 'PBES1[%s]' % str ( e ) ) except ValueError : error_str = 'PBES1[Invalid]' if ( not found ) : try : p8_private_key = PBES2 . decrypt ( p8_private_key , passphrase ) found = True except PbesError as e : error_str += ( ',PBES2[%s]' % str ( e ) ) except ValueError : error_str += ',PBES2[Invalid]' if ( not found ) : raise ValueError ( ( 'Error decoding PKCS#8 (%s)' % error_str ) ) pk_info = DerSequence ( ) . decode ( p8_private_key , nr_elements = ( 2 , 3 , 4 ) ) if ( ( len ( pk_info ) == 2 ) and ( not passphrase ) ) : raise ValueError ( 'Not a valid clear PKCS#8 structure (maybe it is encrypted?)' ) if ( pk_info [ 0 ] != 0 ) : raise ValueError ( 'Not a valid PrivateKeyInfo SEQUENCE' ) algo = DerSequence ( ) . decode ( pk_info [ 1 ] , nr_elements = ( 1 , 2 ) ) algo_oid = DerObjectId ( ) . decode ( algo [ 0 ] ) . value if ( len ( algo ) == 1 ) : algo_params = None else : try : DerNull ( ) . decode ( algo [ 1 ] ) algo_params = None except : algo_params = algo [ 1 ] private_key = DerOctetString ( ) . decode ( pk_info [ 2 ] ) . payload return ( algo_oid , private_key , algo_params )
unwrap --> type://host/path .