tests: Encode/decode base64 operations for python3

Signed-off-by: Masashi Honma <masashi.honma@gmail.com>
This commit is contained in:
Masashi Honma 2019-02-03 18:33:00 +02:00 committed by Jouni Malinen
parent 5c9ba3412d
commit c4e333fa43
2 changed files with 13 additions and 13 deletions

View File

@ -2795,7 +2795,7 @@ def upnp_soap_action(conn, path, action, include_soap_action=True,
act = ET.SubElement(body, "{%s}%s" % (wpsns, action)) act = ET.SubElement(body, "{%s}%s" % (wpsns, action))
if newmsg: if newmsg:
msg = ET.SubElement(act, "NewMessage") msg = ET.SubElement(act, "NewMessage")
msg.text = base64.b64encode(newmsg) msg.text = base64.b64encode(newmsg.encode()).decode()
if neweventtype: if neweventtype:
msg = ET.SubElement(act, "NewWLANEventType") msg = ET.SubElement(act, "NewWLANEventType")
msg.text = neweventtype msg.text = neweventtype
@ -4204,7 +4204,7 @@ def send_wlanevent(url, uuid, data, no_response=False):
<e:property><STAStatus>1</STAStatus></e:property> <e:property><STAStatus>1</STAStatus></e:property>
<e:property><APStatus>1</APStatus></e:property> <e:property><APStatus>1</APStatus></e:property>
<e:property><WLANEvent>''' <e:property><WLANEvent>'''
payload += base64.b64encode(data) payload += base64.b64encode(data).decode()
payload += '</WLANEvent></e:property></e:propertyset>' payload += '</WLANEvent></e:property></e:propertyset>'
headers = { "Content-type": 'text/xml; charset="utf-8"', headers = { "Content-type": 'text/xml; charset="utf-8"',
"Server": "Unspecified, UPnP/1.0, Unspecified", "Server": "Unspecified, UPnP/1.0, Unspecified",
@ -5978,7 +5978,7 @@ def test_ap_wps_set_selected_registrar_proto(dev, apdev):
(200, "104a000110") ] (200, "104a000110") ]
for status,test in tests: for status,test in tests:
tlvs = binascii.unhexlify(test) tlvs = binascii.unhexlify(test)
newmsg = base64.b64encode(tlvs) newmsg = base64.b64encode(tlvs).decode()
msg = '<?xml version="1.0"?>\n' msg = '<?xml version="1.0"?>\n'
msg += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' msg += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
msg += '<s:Body>' msg += '<s:Body>'

View File

@ -57,7 +57,7 @@ def test_dpp_qr_code_parsing(dev, apdev):
"DPP:;;", "DPP:;;",
"DPP:C:1/2;M:;K;;", "DPP:C:1/2;M:;K;;",
"DPP:I:;M:01020304050;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;", "DPP:I:;M:01020304050;K:MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIgADURzxmttZoIRIPWGoQMV00XHWCAQIhXruVWOz0NjlkIA=;;",
"DPP:K:" + base64.b64encode(b"hello") + ";;", "DPP:K:" + base64.b64encode(b"hello").decode() + ";;",
"DPP:K:MEkwEwYHKoZIzj0CAQYIKoZIzj0DAQEDMgAEXiJuIWt1Q/CPCkuULechh37UsXPmbUANOeN5U9sOQROE4o/NEFeFEejROHYwwehF;;", "DPP:K:MEkwEwYHKoZIzj0CAQYIKoZIzj0DAQEDMgAEXiJuIWt1Q/CPCkuULechh37UsXPmbUANOeN5U9sOQROE4o/NEFeFEejROHYwwehF;;",
"DPP:K:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANNZaZA4T/kRDjnmpI1ACOJhAuTIIEk2KFOpS6XPpGF+EVr/ao3XemkE0/nzXmGaLzLqTUCJknSdxTnVPeWfCVsCAwEAAQ==;;", "DPP:K:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANNZaZA4T/kRDjnmpI1ACOJhAuTIIEk2KFOpS6XPpGF+EVr/ao3XemkE0/nzXmGaLzLqTUCJknSdxTnVPeWfCVsCAwEAAQ==;;",
"DPP:K:MIIBCjCB0wYHKoZIzj0CATCBxwIBATAkBgcqhkjOPQEBAhkA/////////////////////v//////////MEsEGP////////////////////7//////////AQYZCEFGeWcgOcPp+mrciQwSf643uzBRrmxAxUAMEWub8hCL2TtV5Uo04Eg6uEhltUEMQQYjagOsDCQ9ny/IOtDoYgA9P8K/YL/EBIHGSuV/8jaeGMQEe1rJM3Vc/l3oR55SBECGQD///////////////+Z3vg2FGvJsbTSKDECAQEDMgAEXiJuIWt1Q/CPCkuULechh37UsXPmbUANOeN5U9sOQROE4o/NEFeFEejROHYwwehF;;", "DPP:K:MIIBCjCB0wYHKoZIzj0CATCBxwIBATAkBgcqhkjOPQEBAhkA/////////////////////v//////////MEsEGP////////////////////7//////////AQYZCEFGeWcgOcPp+mrciQwSf643uzBRrmxAxUAMEWub8hCL2TtV5Uo04Eg6uEhltUEMQQYjagOsDCQ9ny/IOtDoYgA9P8K/YL/EBIHGSuV/8jaeGMQEe1rJM3Vc/l3oR55SBECGQD///////////////+Z3vg2FGvJsbTSKDECAQEDMgAEXiJuIWt1Q/CPCkuULechh37UsXPmbUANOeN5U9sOQROE4o/NEFeFEejROHYwwehF;;",
@ -1290,8 +1290,8 @@ def build_conf_obj(kty="EC", crv="P-256",
elif not no_signed_connector: elif not no_signed_connector:
payload = '{"groups":[{"groupId":"*","netRole":"sta"}],"netAccessKey":{"kty":"EC","crv":"P-256","x":"aTF4JEGIPKSZ0Xv9zdCMjm-tn5XpMsYIVZ9wySAz1gI","y":"QGcHWA_6rbU9XDXAztoX-M5Q3suTnMaqEhULtn7SSXw"}}' payload = '{"groups":[{"groupId":"*","netRole":"sta"}],"netAccessKey":{"kty":"EC","crv":"P-256","x":"aTF4JEGIPKSZ0Xv9zdCMjm-tn5XpMsYIVZ9wySAz1gI","y":"QGcHWA_6rbU9XDXAztoX-M5Q3suTnMaqEhULtn7SSXw"}}'
sign = "_sm6YswxMf6hJLVTyYoU1uYUeY2VVkUNjrzjSiEhY42StD_RWowStEE-9CRsdCvLmsTptZ72_g40vTFwdId20A" sign = "_sm6YswxMf6hJLVTyYoU1uYUeY2VVkUNjrzjSiEhY42StD_RWowStEE-9CRsdCvLmsTptZ72_g40vTFwdId20A"
conn = base64.urlsafe_b64encode(prot_hdr).rstrip('=') + '.' conn = base64.urlsafe_b64encode(prot_hdr.encode()).decode().rstrip('=') + '.'
conn += base64.urlsafe_b64encode(payload).rstrip('=') + '.' conn += base64.urlsafe_b64encode(payload.encode()).decode().rstrip('=') + '.'
conn += sign conn += sign
conf += '"signedConnector":"%s",' % conn conf += '"signedConnector":"%s",' % conn
@ -1556,7 +1556,7 @@ def ecdsa_sign(pkey, message, alg="sha256"):
raise Exception("Extra data at the end of ECDSA signature") raise Exception("Extra data at the end of ECDSA signature")
raw_sign = r + s raw_sign = r + s
return base64.urlsafe_b64encode(raw_sign).rstrip('=') return base64.urlsafe_b64encode(raw_sign).decode().rstrip('=')
p256_priv_key = """-----BEGIN EC PRIVATE KEY----- p256_priv_key = """-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIBVQij9ah629f1pu3tarDQGQvrzHgAkgYd1jHGiLxNajoAoGCCqGSM49 MHcCAQEEIBVQij9ah629f1pu3tarDQGQvrzHgAkgYd1jHGiLxNajoAoGCCqGSM49
@ -1572,11 +1572,11 @@ def run_dpp_config_connector(dev, apdev, expiry=None, payload=None,
raise HwsimSkip("OpenSSL python method not available") raise HwsimSkip("OpenSSL python method not available")
pkey = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, pkey = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM,
p256_priv_key) p256_priv_key)
x = base64.urlsafe_b64encode(p256_pub_key_x).rstrip('=') x = base64.urlsafe_b64encode(p256_pub_key_x).decode().rstrip('=')
y = base64.urlsafe_b64encode(p256_pub_key_y).rstrip('=') y = base64.urlsafe_b64encode(p256_pub_key_y).decode().rstrip('=')
pubkey = '\04' + p256_pub_key_x + p256_pub_key_y pubkey = b'\x04' + p256_pub_key_x + p256_pub_key_y
kid = base64.urlsafe_b64encode(hashlib.sha256(pubkey).digest()).rstrip('=') kid = base64.urlsafe_b64encode(hashlib.sha256(pubkey).digest()).decode().rstrip('=')
prot_hdr = '{"typ":"dppCon","kid":"%s","alg":"ES256"}' % kid prot_hdr = '{"typ":"dppCon","kid":"%s","alg":"ES256"}' % kid
@ -1585,8 +1585,8 @@ def run_dpp_config_connector(dev, apdev, expiry=None, payload=None,
if expiry: if expiry:
payload += ',"expiry":"%s"' % expiry payload += ',"expiry":"%s"' % expiry
payload += '}' payload += '}'
conn = base64.urlsafe_b64encode(prot_hdr).rstrip('=') + '.' conn = base64.urlsafe_b64encode(prot_hdr.encode()).decode().rstrip('=') + '.'
conn += base64.urlsafe_b64encode(payload).rstrip('=') conn += base64.urlsafe_b64encode(payload.encode()).decode().rstrip('=')
sign = ecdsa_sign(pkey, conn) sign = ecdsa_sign(pkey, conn)
conn += '.' + sign conn += '.' + sign
run_dpp_config_error(dev, apdev, run_dpp_config_error(dev, apdev,