예제 #1
0
def new_connection():
    print request.get_json()
    device_name = request.json.get('device_name')
    if device_name is None:
        abort(400) #missing arguments
    cert = Certificate.query.filter_by(device_name = device_name)
    if cert is None:
        abort(400) # not registered device

    #Return device_id and server_pub_key encrypted by Device PublicKey

    #use PublicKey to encrypt ServerPublicKey to reponse
    e = cert.export_key()
    server_public_key = app.config['PUBLIC_KEY'].exportKey()
    randomS = '{0:b}'.format(Random.getrandbits(128))
    Session['RandomS'] = randomS
    m = jsonify({'device_id': str(cert.get_id()),'public-key': server_public_key,'randomS': randomS})
    c = e.encrypt(m)
    
    #calculate hash
    h = SHA256.new()
    h.update(m)
    h = h.hexdigest()
    return jsonify({'c': c, 'h': h})