summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore/SecretString.cpp
blob: 398aa963007ee86aebd3e87d4fcd3180cc7b1b74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
 * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibCore/SecretString.h>
#include <string.h>

namespace Core {

SecretString SecretString::take_ownership(char*& cstring, size_t length)
{
    auto buffer = ByteBuffer::copy(cstring, length);
    VERIFY(buffer.has_value());

    explicit_bzero(cstring, length);
    free(cstring);

    return SecretString(buffer.release_value());
}

SecretString SecretString::take_ownership(ByteBuffer&& buffer)
{
    return SecretString(move(buffer));
}

SecretString::SecretString(ByteBuffer&& buffer)
    : m_secure_buffer(move(buffer))
{
}

SecretString::~SecretString()
{
    if (!m_secure_buffer.is_empty()) {
        // Note: We use explicit_bzero to avoid the zeroing from being optimized out by the compiler,
        // which is possible if memset was to be used here.
        explicit_bzero(m_secure_buffer.data(), m_secure_buffer.capacity());
    }
}

}