<s> from __future__ import with_statement <EOL> import base64 <EOL> import logging <EOL> import os <EOL> import struct <EOL> from Crypto . Hash import SHA <EOL> from Crypto . PublicKey import RSA <EOL> from Crypto . Signature import PKCS1_v1_5 <EOL> from google . appengine . ext import webapp <EOL> from google . appengine . ext . webapp import util <EOL> import registration <EOL> class PostRegistration ( webapp . RequestHandler ) : <EOL> def post ( self ) : <EOL> minlen = <NUM_LIT:4> + <NUM_LIT:4> + <NUM_LIT:20> + <NUM_LIT:4> + <NUM_LIT:1> + <NUM_LIT:4> + <NUM_LIT:1> <EOL> STR_VERSERVER = '<STR_LIT>' <EOL> INT_VERCLIENT = <NUM_LIT> <EOL> STR_VERCLIENT = '<STR_LIT>' <EOL> if not os . environ . has_key ( '<STR_LIT>' ) : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> if not os . environ . has_key ( '<STR_LIT>' ) : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> HTTPS = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> CURRENT_VERSION_ID = os . environ . get ( '<STR_LIT>' , STR_VERSERVER ) <EOL> if HTTPS . __str__ ( ) != '<STR_LIT>' : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> self . response . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> data = self . request . body <EOL> size = str . __len__ ( data ) <EOL> if size < minlen : <EOL> logging . debug ( "<STR_LIT>" % data ) <EOL> logging . debug ( "<STR_LIT>" % size ) <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> client = ( struct . unpack ( "<STR_LIT>" , data [ <NUM_LIT:0> : <NUM_LIT:4> ] ) ) [ <NUM_LIT:0> ] <EOL> if client < INT_VERCLIENT : <EOL> self . resp_simple ( <NUM_LIT:0> , ( '<STR_LIT>' % STR_VERCLIENT ) ) <EOL> return <EOL> server = int ( CURRENT_VERSION_ID [ <NUM_LIT:0> : <NUM_LIT:8> ] , <NUM_LIT:16> ) <EOL> submissionAuth = None <EOL> submissionType = <NUM_LIT:1> <EOL> pos = <NUM_LIT:4> <EOL> lenkeyid = ( struct . unpack ( "<STR_LIT>" , data [ pos : ( pos + <NUM_LIT:4> ) ] ) ) [ <NUM_LIT:0> ] <EOL> pos = pos + <NUM_LIT:4> <EOL> keyId = data [ pos : ( pos + lenkeyid ) ] <EOL> pos = pos + lenkeyid <EOL> lensubtok = ( struct . unpack ( "<STR_LIT>" , data [ pos : ( pos + <NUM_LIT:4> ) ] ) ) [ <NUM_LIT:0> ] <EOL> pos = pos + <NUM_LIT:4> <EOL> submissionToken = str ( data [ pos : ( pos + lensubtok ) ] ) <EOL> pos = pos + lensubtok <EOL> if lensubtok >= <NUM_LIT:32> : <EOL> submissionAuth = submissionToken <EOL> submissionType = <NUM_LIT:1> <EOL> lenregid = ( struct . unpack ( "<STR_LIT>" , data [ pos : ( pos + <NUM_LIT:4> ) ] ) ) [ <NUM_LIT:0> ] <EOL> pos = pos + <NUM_LIT:4> <EOL> registrationId = str ( data [ pos : ( pos + lenregid ) ] ) <EOL> pos = pos + lenregid <EOL> devtype = ( struct . unpack ( "<STR_LIT>" , data [ pos : ( pos + <NUM_LIT:4> ) ] ) ) [ <NUM_LIT:0> ] <EOL> pos = pos + <NUM_LIT:4> <EOL> if size > pos : <EOL> lennonce = ( struct . unpack ( "<STR_LIT>" , data [ pos : ( pos + <NUM_LIT:4> ) ] ) ) [ <NUM_LIT:0> ] <EOL> pos = pos + <NUM_LIT:4> <EOL> nonce = str ( data [ pos : ( pos + lennonce ) ] ) <EOL> pos = pos + lennonce <EOL> lenpubkey = ( struct . unpack ( "<STR_LIT>" , data [ pos : ( pos + <NUM_LIT:4> ) ] ) ) [ <NUM_LIT:0> ] <EOL> pos = pos + <NUM_LIT:4> <EOL> pubkey = str ( data [ pos : ( pos + lenpubkey ) ] ) <EOL> pos = pos + lenpubkey <EOL> plain_pos = pos <EOL> sig_len = ( struct . unpack ( "<STR_LIT>" , data [ pos : ( pos + <NUM_LIT:4> ) ] ) ) [ <NUM_LIT:0> ] <EOL> pos = pos + <NUM_LIT:4> <EOL> sig = data [ pos : ( pos + sig_len ) ] <EOL> pos = pos + sig_len <EOL> if lenpubkey > <NUM_LIT:0> : <EOL> rsa_key = RSA . importKey ( base64 . decodestring ( pubkey ) ) <EOL> h = SHA . new ( ) <EOL> h . update ( data [ : plain_pos ] ) <EOL> verifier = PKCS1_v1_5 . new ( rsa_key ) <EOL> if verifier . verify ( h , sig ) : <EOL> submissionAuth = pubkey <EOL> submissionType = <NUM_LIT:2> <EOL> logging . debug ( '<STR_LIT>' ) <EOL> else : <EOL> logging . error ( '<STR_LIT>' ) <EOL> return <EOL> query = registration . Registration . all ( ) . order ( '<STR_LIT>' ) <EOL> query . filter ( '<STR_LIT>' , keyId ) <EOL> num = query . count ( ) <EOL> if num >= <NUM_LIT:1> : <EOL> reg_old = query . get ( ) <EOL> updateOld = False <EOL> if submissionType > reg_old . submission_type : <EOL> updateOld = True <EOL> elif submissionAuth == reg_old . submission_token : <EOL> updateOld = True <EOL> if updateOld : <EOL> if registrationId == reg_old . registration_id : <EOL> reg_old . active = True <EOL> reg_old . submission_token = submissionAuth <EOL> reg_old . submission_type = submissionType <EOL> reg_old . put ( ) <EOL> key = reg_old . key ( ) <EOL> if not key . has_id_or_name ( ) : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> else : <EOL> reg_new = registration . Registration ( key_id = keyId , submission_token = submissionAuth , submission_type = submissionType , registration_id = registrationId , notify_type = devtype , client_ver = client ) <EOL> reg_new . put ( ) <EOL> key = reg_new . key ( ) <EOL> if not key . has_id_or_name ( ) : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> else : <EOL> logging . info ( '<STR_LIT>' % ( reg_old . submission_token [ <NUM_LIT:0> : <NUM_LIT:30> ] , submissionAuth [ <NUM_LIT:0> : <NUM_LIT:30> ] ) ) <EOL> else : <EOL> reg_new = registration . Registration ( key_id = keyId , submission_token = submissionAuth , submission_type = submissionType , registration_id = registrationId , notify_type = devtype , client_ver = client ) <EOL> reg_new . put ( ) <EOL> key = reg_new . key ( ) <EOL> if not key . has_id_or_name ( ) : <EOL> self . resp_simple ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> return <EOL> self . response . out . write ( '<STR_LIT:%s>' % struct . pack ( '<STR_LIT>' , server ) ) <EOL> def resp_simple ( self , code , msg ) : <EOL> self . response . out . write ( '<STR_LIT>' % ( struct . pack ( '<STR_LIT>' , code ) , msg ) ) <EOL> if code == <NUM_LIT:0> : <EOL> logging . error ( msg ) <EOL> def main ( ) : <EOL> STR_VERSERVER = '<STR_LIT>' <EOL> CURRENT_VERSION_ID = os . environ . get ( '<STR_LIT>' , STR_VERSERVER ) <EOL> isProd = CURRENT_VERSION_ID [ <NUM_LIT:8> : <NUM_LIT:9> ] == '<STR_LIT:p>' <EOL> if isProd : <EOL> logging . getLogger ( ) . setLevel ( logging . INFO ) <EOL> else : <EOL> logging . getLogger ( ) . setLevel ( logging . DEBUG ) <EOL> application = webapp . WSGIApplication ( [ ( '<STR_LIT>' , PostRegistration ) ] , <EOL> debug = True ) <EOL> util . run_wsgi_app ( application ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
