File: pkcs11_lib.c

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (45 lines) | stat: -rw-r--r-- 1,355 bytes parent folder | download | duplicates (3)
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
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include "io.h"

#include <aws/io/pkcs11.h>

static const char *s_capsule_name = "aws_pkcs11_lib";

static void s_pkcs11_lib_capsule_destructor(PyObject *capsule) {
    struct aws_pkcs11_lib *pkcs11_lib = PyCapsule_GetPointer(capsule, s_capsule_name);
    aws_pkcs11_lib_release(pkcs11_lib);
}

struct aws_pkcs11_lib *aws_py_get_pkcs11_lib(PyObject *pkcs11_lib) {
    return aws_py_get_binding(pkcs11_lib, s_capsule_name, "Pkcs11Lib");
}

PyObject *aws_py_pkcs11_lib_new(PyObject *self, PyObject *args) {
    (void)self;

    struct aws_byte_cursor filename;
    int behavior;
    if (!PyArg_ParseTuple(args, "s#i", &filename.ptr, &filename.len, &behavior)) {
        return NULL;
    }

    struct aws_pkcs11_lib_options options = {
        .filename = filename,
        .initialize_finalize_behavior = behavior,
    };
    struct aws_pkcs11_lib *pkcs11_lib = aws_pkcs11_lib_new(aws_py_get_allocator(), &options);
    if (pkcs11_lib == NULL) {
        return PyErr_AwsLastError();
    }

    PyObject *capsule = PyCapsule_New(pkcs11_lib, s_capsule_name, s_pkcs11_lib_capsule_destructor);
    if (capsule == NULL) {
        aws_pkcs11_lib_release(pkcs11_lib); /* cleanup due to error */
        return NULL;
    }

    return capsule;
}