def make_payment_request(outputs, memo, time, expires, key_path, cert_path): pd = pb2.PaymentDetails() for script, amount in outputs: pd.outputs.add(amount=amount, script=script) pd.time = time pd.expires = expires pd.memo = memo pr = pb2.PaymentRequest() pr.serialized_payment_details = pd.SerializeToString() pr.signature = '' pr = pb2.PaymentRequest() pr.serialized_payment_details = pd.SerializeToString() pr.signature = '' if key_path and cert_path: import tlslite with open(key_path, 'r') as f: rsakey = tlslite.utils.python_rsakey.Python_RSAKey.parsePEM( f.read()) with open(cert_path, 'r') as f: chain = tlslite.X509CertChain() chain.parsePemList(f.read()) certificates = pb2.X509Certificates() certificates.certificate.extend( map(lambda x: str(x.bytes), chain.x509List)) pr.pki_type = 'x509+sha256' pr.pki_data = certificates.SerializeToString() msgBytes = bytearray(pr.SerializeToString()) hashBytes = bytearray(hashlib.sha256(msgBytes).digest()) sig = rsakey.sign(x509.PREFIX_RSA_SHA256 + hashBytes) pr.signature = bytes(sig) return pr.SerializeToString()
def read_chain(self): "cert chain is all in one file, in LEAF -> ROOT order" import tlslite delim = '-----END CERTIFICATE-----\n' data = open(self.cert_path).read() certs = data.split(delim) chain = [] for cert in certs: if cert: x = tlslite.X509() x.parse(cert + delim) chain.append(x) self.chain = tlslite.X509CertChain(chain)
def __init__(self, certFile, fakeHost, host='127.0.0.1', port=4443): HTTPServer.__init__(self, (host, port), HttpHandler) self.host = host self.port = port self.url = 'https://' + host + '/' self.fakeUrl = 'https://' + fakeHost + '/' self.apk = None self.result = None with open(certFile) as f: cert = f.read() self.certChain = tlslite.X509CertChain() self.certChain.parsePemList(cert) self.privateKey = tlslite.parsePEMKey(cert, private=True)
def sign_request_with_x509(pr, key_path, cert_path): import tlslite with open(key_path, 'r') as f: rsakey = tlslite.utils.python_rsakey.Python_RSAKey.parsePEM(f.read()) with open(cert_path, 'r') as f: chain = tlslite.X509CertChain() chain.parsePemList(f.read()) certificates = pb2.X509Certificates() certificates.certificate.extend(map(lambda x: str(x.bytes), chain.x509List)) pr.pki_type = 'x509+sha256' pr.pki_data = certificates.SerializeToString() msgBytes = bytearray(pr.SerializeToString()) hashBytes = bytearray(hashlib.sha256(msgBytes).digest()) sig = rsakey.sign(x509.PREFIX_RSA_SHA256 + hashBytes) pr.signature = bytes(sig)
import tlslite from electrum.transaction import Transaction from electrum import paymentrequest from electrum import paymentrequest_pb2 as pb2 chain_file = 'mychain.pem' cert_file = 'mycert.pem' amount = 1000000 address = "18U5kpCAU4s8weFF8Ps5n8HAfpdUjDVF64" memo = "blah" out_file = "payreq" with open(chain_file, 'r') as f: chain = tlslite.X509CertChain() chain.parsePemList(f.read()) certificates = pb2.X509Certificates() certificates.certificate.extend(map(lambda x: str(x.bytes), chain.x509List)) with open(cert_file, 'r') as f: rsakey = tlslite.utils.python_rsakey.Python_RSAKey.parsePEM(f.read()) script = Transaction.pay_script('address', address).decode('hex') pr_string = paymentrequest.make_payment_request(amount, script, memo, rsakey) with open(out_file, 'wb') as f: f.write(pr_string)
if __name__ == "__main__": signal.signal(signal.SIGINT, signal_handler) arg = parse_args() handle_args(arg) # Determine the type of certificate and parse the file into an X509CertChain object x509 = tlslite.X509() if os.path.splitext(arg.cert)[1].lower() == ".pem": with open(arg.cert, "r") as s: x509.parse(s.read()) else: with open(arg.cert, "rb") as b: x509.parseBinary(b.read()) certChain = tlslite.X509CertChain([x509]) # Parse the private key file with open(arg.key, "r") as s: privateKey = tlslite.parsePEMKey(s.read(), private=True) # Listen for client connections sock = socket.socket() sock.bind(("", arg.port)) sock.listen() sock_list.append(sock) while True: with lib.spinner.Spinner(" Listening on port {}".format(arg.port)): client, addr = sock.accept() sock_list.append(client)