Debian Codegraph @

ansible_collections/community/crypto/plugins/module_utils/_crypto/_obj2txt.py

raw
1 # This code is part of Ansible, but is an independent component.
2 # This particular file snippet, and this file snippet only, is licensed under the
3 # Apache 2.0 License. Modules you write using this snippet, which is embedded
4 # dynamically by Ansible, still belong to the author of the module, and may assign
5 # their own license to the complete work.
6
7 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
8 # Do not use this from other collections or standalone plugins/modules!
9
10 # This excerpt is dual licensed under the terms of the Apache License, Version
11 # 2.0, and the BSD License. See the LICENSE file at
12 # https://github.com/pyca/cryptography/blob/master/LICENSE for complete details.
13 #
14 # The Apache 2.0 license has been included as LICENSES/Apache-2.0.txt in this collection.
15 # The BSD License license has been included as LICENSES/BSD-3-Clause.txt in this collection.
16 # SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
17 #
18 # Adapted from cryptography's hazmat/backends/openssl/decode_asn1.py
19 #
20 # Copyright (c) 2015, 2016 Paul Kehrer (@reaperhulk)
21 # Copyright (c) 2017 Fraser Tweedale (@frasertweedale)
22
23 # Relevant commits from cryptography project (https://github.com/pyca/cryptography):
24 # pyca/cryptography@719d536dd691e84e208534798f2eb4f82aaa2e07
25 # pyca/cryptography@5ab6d6a5c05572bd1c75f05baf264a2d0001894a
26 # pyca/cryptography@2e776e20eb60378e0af9b7439000d0e80da7c7e3
27 # pyca/cryptography@fb309ed24647d1be9e319b61b1f2aa8ebb87b90b
28 # pyca/cryptography@2917e460993c475c72d7146c50dc3bbc2414280d
29 # pyca/cryptography@3057f91ea9a05fb593825006d87a391286a4d828
30 # pyca/cryptography@d607dd7e5bc5c08854ec0c9baff70ba4a35be36f
31
32 from __future__ import annotations
33
34 import typing as t
35
36 # WARNING: this function no longer works with cryptography 35.0.0 and newer!
37 # It must **ONLY** be used in compatibility code for older
38 # cryptography versions!
39
40
41 def obj2txt(openssl_lib: t.Any, openssl_ffi: t.Any, obj: t.Any) -> str:
42 # Set to 80 on the recommendation of
43 # https://www.openssl.org/docs/crypto/OBJ_nid2ln.html#return_values
44 #
45 # But OIDs longer than this occur in real life (e.g. Active
46 # Directory makes some very long OIDs). So we need to detect
47 # and properly handle the case where the default buffer is not
48 # big enough.
49 #
50 buf_len = 80
51 buf = openssl_ffi.new("char[]", buf_len)
52
53 # 'res' is the number of bytes that *would* be written if the
54 # buffer is large enough. If 'res' > buf_len - 1, we need to
55 # alloc a big-enough buffer and go again.
56 res = openssl_lib.OBJ_obj2txt(buf, buf_len, obj, 1)
57 if res > buf_len - 1: # account for terminating null byte
58 buf_len = res + 1
59 buf = openssl_ffi.new("char[]", buf_len)
60 res = openssl_lib.OBJ_obj2txt(buf, buf_len, obj, 1)
61 bytes_str: bytes = openssl_ffi.buffer(buf, res)[:]
62 return bytes_str.decode()
63
64
65 __all__ = ("obj2txt",)