summaryrefslogtreecommitdiff
path: root/python/samba/tests/samba_tool/user_generate_csr.py
blob: 7623c1eb9db21ffd43ecb19d59760df3ee5806b0 (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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Unix SMB/CIFS implementation.
#
# Tests for `samba-tool user generate-csr`
#
# Copyright (C) Catalyst.Net Ltd 2025
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import os
from typing import Optional

from samba.domain.models import Computer, User
from samba.tests.samba_tool.base import SambaToolCmdTest

from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.x509.oid import NameOID


szOID_NTDS_CA_SECURITY_EXT = x509.ObjectIdentifier("1.3.6.1.4.1.311.25.2")


HOST = "ldap://{DC_SERVER}".format(**os.environ)
CREDS = "-U{DC_USERNAME}%{DC_PASSWORD}".format(**os.environ)


class SambaToolUserGenerateCsrTest(SambaToolCmdTest):
    cmd = "user"
    model = User
    user = "alice"

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.samdb = cls.getSamDB("-H", HOST, CREDS)

    def test_create_private_key_pem(self):
        self._test_create_private_key(encoding=serialization.Encoding.PEM)

    def test_create_private_key_der(self):
        self._test_create_private_key(encoding=serialization.Encoding.DER)

    def test_create_private_key_password(self):
        self._test_create_private_key(password="pass1234")

    def _test_create_private_key(
        self,
        *,
        encoding: serialization.Encoding = serialization.Encoding.PEM,
        password: Optional[str] = None,
    ):
        private_key = rsa.generate_private_key(
            public_exponent=65537, key_size=2048, backend=default_backend()
        )

        if password is not None:
            encryption_algorithm = serialization.BestAvailableEncryption(
                password.encode("utf-8")
            )
        else:
            encryption_algorithm = serialization.NoEncryption()

        private_key_bytes = private_key.private_bytes(
            encoding=encoding,
            format=serialization.PrivateFormat.PKCS8,
            encryption_algorithm=encryption_algorithm,
        )

        user = self.model.find(self.samdb, self.user)
        account_name = user.account_name
        if user.user_principal_name is not None:
            account_upn = user.user_principal_name
        else:
            realm = self.samdb.domain_dns_name()
            account_upn = f"{account_name}@{realm.lower()}"

        subject_name = x509.Name([
            # Note that the subject name is used in certificate mappings
            x509.NameAttribute(NameOID.COUNTRY_NAME, "US"),
            x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "SambaState"),
            x509.NameAttribute(NameOID.ORGANIZATION_NAME, "SambaSelfTesting"),
            x509.NameAttribute(NameOID.ORGANIZATIONAL_UNIT_NAME, "Users"),
            x509.NameAttribute(NameOID.COMMON_NAME, account_upn),
        ])
        subject_name_str = subject_name.rfc4514_string()

        with self.mktemp() as private_key_filename, self.mktemp() as csr_filename:
            with open(private_key_filename, "wb") as private_key_file:
                _ = private_key_file.write(private_key_bytes)

            cmd = [
                self.cmd,
                "generate-csr",
                "-H",
                HOST,
                CREDS,
                self.user,
                subject_name_str,
                private_key_filename,
                csr_filename,
            ]
            if password is not None:
                cmd.append("--private-key-pass")
                cmd.append(password)

            result, out, err = self.runcmd(*cmd)
            self.assertCmdSuccess(result, out, err)

            with open(csr_filename, "rb") as csr_file:
                csr_bytes = csr_file.read()

            csr = x509.load_pem_x509_csr(csr_bytes)

            self.assertEqual(subject_name, csr.subject)

            try:
                sid_extension = csr.extensions.get_extension_for_oid(
                    szOID_NTDS_CA_SECURITY_EXT,
                )
            except x509.ExtensionNotFound:
                self.fail("expected to find SID extension")

            # We don’t check the whole ASN.1 structure is correct, just that it
            # contains the encoded SID.
            encoded_sid = user.object_sid.encode("utf-8")
            self.assertIn(encoded_sid, sid_extension.value.value)


class SambaToolComputerGenerateCsrTest(SambaToolUserGenerateCsrTest):
    cmd = "computer"
    model = Computer
    user = "ADDC"


if __name__ == "__main__":
    global_asn1_print = False
    global_hexdump = False
    import unittest

    unittest.main()