Exemplo n.º 1
0
    def test_mismatch_keypair(self):
        keypair0 = utils.generate_key_pair()
        keypair1 = utils.generate_key_pair()

        key = RSA.importKey(keypair0["private"])
        pubkey = RSA.importKey(keypair1["public"])

        message = "The quick brown fox jumps over the lazy dog."
        sig = key.sign(message, '')
        self.assertFalse(pubkey.verify(message, sig))
Exemplo n.º 2
0
    def test_mismatch_keypair(self):
        keypair0 = utils.generate_key_pair()
        keypair1 = utils.generate_key_pair()

        key = RSA.importKey(keypair0["private"])
        pubkey = RSA.importKey(keypair1["public"])

        message = "The quick brown fox jumps over the lazy dog."
        h = SHA256.new(message)
        sig = pss.new(key).sign(h)
        with self.assertRaises(ValueError):
            pss.new(pubkey).verify(h, sig)
Exemplo n.º 3
0
    def test_mismatch_keypair(self):
        keypair0 = utils.generate_key_pair()
        keypair1 = utils.generate_key_pair()

        key = RSA.importKey(keypair0["private"])
        pubkey = RSA.importKey(keypair1["public"])

        message = "The quick brown fox jumps over the lazy dog."
        h = SHA256.new(message)
        sig = pss.new(key).sign(h)
        with self.assertRaises(ValueError):
            pss.new(pubkey).verify(h, sig)
Exemplo n.º 4
0
def main_settings(request, username):
    state = "Change Settings"

    changed = False
    initial = {
        "email": request.user.email,
        "xsede_username": request.user.xsede_username,
        "public_key": request.user.public_key,
    }
    settings_form = SettingsForm(request.POST or None, initial=initial)
    if settings_form.is_valid():
        d = dict(settings_form.cleaned_data)
        if request.user.email != d.get("email"):
            request.user.email = d.get("email")
            logger.info("User '%s' updated email to %s." %
                        (request.user.username, request.user.email))
            changed = True

        if d.get("new_ssh_keypair"):
            keys = utils.generate_key_pair(username)
            if request.user.public_key:
                try:
                    utils.update_all_ssh_keys(request.user, keys["public"])
                except Exception as e:
                    logger.warn(
                        "User '%s' tried to update ssh keys and got an error: %s"
                        % (username, str(e)))
                    # TODO add failed state
                    pass
            request.user.public_key = keys["public"]
            request.user.private_key = keys["private"]
            logger.info("User '%s' updated ssh keys." % request.user.username)
            changed = True

        if d.get("xsede_username") != request.user.xsede_username:
            request.user.xsede_username = d.get("xsede_username")
            logger.info("User '%s' updated xsede_username to %s." %
                        (request.user.username, request.user.xsede_username))
            changed = True

    if changed:
        request.user.save()
        initial = {
            "email": request.user.email,
            "xsede_username": request.user.xsede_username,
            "public_key": request.user.public_key,
        }
        settings_form = SettingsForm(initial=initial)
        state = "Settings Successfully Saved"

    c = {
        "pages": utils.PAGES,
        "page": "settings",
        "state": state,
        "form": settings_form,
    }
    return render(request, "account/main_settings.html", c)
Exemplo n.º 5
0
def main_settings(request, username):
    state = "Change Settings"

    changed = False
    initial = {
        "email": request.user.email,
        "xsede_username": request.user.xsede_username,
        "public_key": request.user.public_key,
    }
    settings_form = SettingsForm(request.POST or None, initial=initial)
    if settings_form.is_valid():
        d = dict(settings_form.cleaned_data)
        if request.user.email != d.get("email"):
            request.user.email = d.get("email")
            logger.info("User '%s' updated email to %s." %
                        (request.user.username, request.user.email))
            changed = True

        if d.get("new_ssh_keypair"):
            keys = utils.generate_key_pair(username)
            if request.user.public_key:
                try:
                    utils.update_all_ssh_keys(request.user, keys["public"])
                except Exception as e:
                    logger.warn("User '%s' tried to update ssh keys and got an error: %s" % (username, str(e)))
                    # TODO add failed state
                    pass
            request.user.public_key = keys["public"]
            request.user.private_key = keys["private"]
            logger.info("User '%s' updated ssh keys." % request.user.username)
            changed = True

        if d.get("xsede_username") != request.user.xsede_username:
            request.user.xsede_username = d.get("xsede_username")
            logger.info("User '%s' updated xsede_username to %s." %
                        (request.user.username, request.user.xsede_username))
            changed = True

    if changed:
        request.user.save()
        initial = {
            "email": request.user.email,
            "xsede_username": request.user.xsede_username,
            "public_key": request.user.public_key,
        }
        settings_form = SettingsForm(initial=initial)
        state = "Settings Successfully Saved"

    c = {
        "pages": utils.PAGES,
        "page": "settings",
        "state": state,
        "form": settings_form,
    }
    return render(request, "account/main_settings.html", c)
Exemplo n.º 6
0
    def test_key_pair(self):
        keypair = utils.generate_key_pair()

        key = RSA.importKey(keypair["private"])
        pubkey = RSA.importKey(keypair["public"])

        message = "The quick brown fox jumps over the lazy dog."
        h = SHA256.new(message)
        sig = pss.new(key).sign(h)
        try:
            pss.new(pubkey).verify(h, sig)
        except ValueError:
            self.fail()
Exemplo n.º 7
0
    def test_key_pair(self):
        keypair = utils.generate_key_pair()

        key = RSA.importKey(keypair["private"])
        pubkey = RSA.importKey(keypair["public"])

        message = "The quick brown fox jumps over the lazy dog."
        h = SHA256.new(message)
        sig = pss.new(key).sign(h)
        try:
            pss.new(pubkey).verify(h, sig)
        except ValueError:
            self.fail()
Exemplo n.º 8
0
def add_public_ssh_key(cont):

    # Generate a private/public key pair if there's not one already in use
    public_key = get_fab_public_key()
    if not public_key:
        private, public_key = generate_key_pair()
        env.key = private

    #write password to file
    tar_data = io.BytesIO()
    tarinfo = tarfile.TarInfo(name='.ssh/authorized_keys')
    tarinfo.size = len(public_key)
    tarinfo.mtime = time.time()
    with tarfile.TarFile(fileobj=tar_data, mode='w') as tar:
        tar.addfile(tarinfo, io.BytesIO(public_key))

    tar_data.seek(0)
    cont.put_archive(path='/root/', data=tar_data)
Exemplo n.º 9
0
    def handle_add_computer(self, packet1_data, sender):
        ''' Handle Add Computer request. 

            Communicate with the SENDER and authenticate him. If
            successfull, add SENDER to the list of admin computers.

            @type packet1_data: Packet
            @param packet1_data: First packet of the communication.
            @type sender: utils.IPv4Address
            @param sender: Sender IP address.
        '''
        # 1. packet
        packet1 = add_computer.parse_packet_1(packet1_data)

        # 2. packet
        g = 0x17  # generator
        (public_key, private_key) = utils.generate_key_pair(g)
        packet2 = add_computer.make_packet_2(g, public_key)
        self.network.write(packet2, sender)

        # 3. packet
        packet3_data = self.network.read_from(sender)
        (admin, auth_data) = add_computer.parse_packet_3(packet3_data, private_key)

        # Decrypt username/password
        plaintext = utils.decrypt(admin.get_shared_key(), auth_data)
        username = utils.null_terminated(plaintext[0:32])
        password = utils.null_terminated(plaintext[64:96])

        # 4. packet
        if self.check_user(username, password):
            allow_access = True
            self.admin_list.add_admin(sender, admin)
        else:
            allow_access = False

        packet4 = add_computer.make_packet_4(allow_access, 
                self.config.get_listen_addr(),
                self.config.get_mac_addr())
        self.network.write(packet4, sender)
Exemplo n.º 10
0
def register_user(request):
    if request.user.is_authenticated():
        return redirect(HOME_URL)

    state = "Please register"

    reg_form = RegistrationForm(request.POST or None)
    pass_form = SetPasswordForm(request.user, request.POST or None)
    temp1 = reg_form.is_valid()     # prevent short circuit
    temp2 = pass_form.is_valid()    # prevent short circuit
    if temp1 and temp2:
        d = dict(
            reg_form.cleaned_data.items() + pass_form.cleaned_data.items()
        )

        new_user = get_user_model().objects.create_user(d["username"],
                                                        d["email"],
                                                        d["new_password1"])
        new_user.is_active = False

        activation_key = utils.generate_key(d["username"])
        new_user.activation_key = activation_key
        keypair = utils.generate_key_pair(d["username"])
        new_user.public_key = keypair["public"]
        new_user.private_key = keypair["private"]

        new_user.save()
        logger.info("New user '%s' registered." % new_user.username)
        c = {
            "key": activation_key,
        }
        return render(request, "account/post_register.html", c)

    c = {
        "state": state,
        "reg_form": reg_form,
        "pass_form": pass_form,
    }
    return render(request, "account/register.html", c)
Exemplo n.º 11
0
def register_user(request):
    if request.user.is_authenticated():
        return redirect(HOME_URL)

    state = "Please register"

    reg_form = RegistrationForm(request.POST or None)
    pass_form = SetPasswordForm(request.user, request.POST or None)
    temp1 = reg_form.is_valid()  # prevent short circuit
    temp2 = pass_form.is_valid()  # prevent short circuit
    if temp1 and temp2:
        d = dict(reg_form.cleaned_data.items() +
                 pass_form.cleaned_data.items())

        new_user = get_user_model().objects.create_user(
            d["username"], d["email"], d["new_password1"])
        new_user.is_active = False

        activation_key = utils.generate_key(d["username"])
        new_user.activation_key = activation_key
        keypair = utils.generate_key_pair(d["username"])
        new_user.public_key = keypair["public"]
        new_user.private_key = keypair["private"]

        new_user.save()
        logger.info("New user '%s' registered." % new_user.username)
        c = {
            "key": activation_key,
        }
        return render(request, "account/post_register.html", c)

    c = {
        "state": state,
        "reg_form": reg_form,
        "pass_form": pass_form,
    }
    return render(request, "account/register.html", c)
Exemplo n.º 12
0
 def test_generate_key_pair(self):
     self.assertIsNotNone(utils.generate_key_pair())
     self.assertIsNotNone(utils.generate_key_pair("username"))
Exemplo n.º 13
0
 def __init__(self, passphrase):
     (self.public_key, self.private_key) = generate_key_pair(passphrase)
Exemplo n.º 14
0
 def test_generate_key_pair(self):
     self.assertIsNotNone(utils.generate_key_pair())
     self.assertIsNotNone(utils.generate_key_pair("username"))