aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/aws-crt-cpp/source/io/Pkcs11.cpp
blob: 6e71287387ddccc9dc34b35a4321e54206bb9867 (plain) (blame)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include <aws/crt/io/Pkcs11.h>

#include <aws/io/logging.h>
#include <aws/io/pkcs11.h>

namespace Aws
{
    namespace Crt
    {
        namespace Io
        {
            std::shared_ptr<Pkcs11Lib> Pkcs11Lib::Create(const String &filename, Allocator *allocator)
            {
                return Create(filename, InitializeFinalizeBehavior::Default, allocator);
            }

            std::shared_ptr<Pkcs11Lib> Pkcs11Lib::Create(
                const String &filename,
                InitializeFinalizeBehavior initializeFinalizeBehavior,
                Allocator *allocator)
            {
                aws_pkcs11_lib_options options;
                AWS_ZERO_STRUCT(options);

                if (!filename.empty())
                {
                    options.filename = ByteCursorFromString(filename);
                }

                switch (initializeFinalizeBehavior)
                {
                    case InitializeFinalizeBehavior::Default:
                        options.initialize_finalize_behavior = AWS_PKCS11_LIB_DEFAULT_BEHAVIOR;
                        break;
                    case InitializeFinalizeBehavior::Omit:
                        options.initialize_finalize_behavior = AWS_PKCS11_LIB_OMIT_INITIALIZE;
                        break;
                    case InitializeFinalizeBehavior::Strict:
                        options.initialize_finalize_behavior = AWS_PKCS11_LIB_STRICT_INITIALIZE_FINALIZE;
                        break;
                    default:
                        AWS_LOGF_ERROR(
                            AWS_LS_IO_PKCS11,
                            "Cannot create Pkcs11Lib. Invalid InitializeFinalizeBehavior %d",
                            (int)initializeFinalizeBehavior);
                        aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
                        return nullptr;
                }

                struct aws_pkcs11_lib *impl = aws_pkcs11_lib_new(allocator, &options);
                if (impl == nullptr)
                {
                    return nullptr;
                }

                return MakeShared<Pkcs11Lib>(allocator, *impl);
            }

            Pkcs11Lib::Pkcs11Lib(aws_pkcs11_lib &impl) : impl(&impl) {}

            Pkcs11Lib::~Pkcs11Lib() { aws_pkcs11_lib_release(impl); }

        } // namespace Io
    }     // namespace Crt
} // namespace Aws