File: common.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (321 lines) | stat: -rw-r--r-- 9,620 bytes parent folder | download
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from base64 import b32encode, b64decode, b64encode
from math import ceil
from os import makedirs, urandom
from os.path import exists

CERT_NAME = "sdk_test_certificate"
CERT_ENDING = "-cert.pem"
KEY_ENDING = "-key.pem"


def create_random_name(prefix="dps_sdk_test", length=24):
    if len(prefix) > length:
        raise ValueError(
            "The length of the prefix must not be longer than random name length"
        )

    padding_size = length - len(prefix)
    if padding_size < 4:
        raise ValueError(
            "The randomized part of the name is shorter than 4, which may not be able to offer enough "
            "randomness"
        )

    random_bytes = urandom(int(ceil(float(padding_size) / 8) * 5))
    random_padding = b32encode(random_bytes)[:padding_size]

    return str(prefix + random_padding.decode().lower())


def generate_attestation(
    attestation_type,
    endorsement_key=None,
    primary_key=None,
    secondary_key=None,
    primary_cert=None,
    secondary_cert=None,
    signing_certs=False,
):
    attestation = {"type": attestation_type}
    if attestation_type == "tpm":
        attestation["tpm"] = {"endorsementKey": endorsement_key}
    if attestation_type == "symmetricKey":
        attestation["symmetricKey"] = {
            "primaryKey": primary_key,
            "secondaryKey": secondary_key,
        }
    if attestation_type == "x509":
        if primary_cert:
            primary_cert = {"certificate": primary_cert}
        if secondary_cert:
            secondary_cert = {"certificate": secondary_cert}
        certs = (
            {"primary": primary_cert, "secondary": secondary_cert}
            if (primary_cert or secondary_cert)
            else None
        )
        attestation["x509"] = (
            {"signingCertificates": certs}
            if signing_certs
            else {"clientCertificates": certs}
        )
    return attestation


def generate_enrollment(
    id=None,
    attestation_type=None,
    capabilities=None,
    endorsement_key=None,
    primary_cert=None,
    secondary_cert=None,
    device_id=None,
    iot_hub_host_name=None,
    initial_twin_properties=None,
    provisioning_status=None,
    reprovision_policy=None,
    primary_key=None,
    secondary_key=None,
    allocation_policy=None,
    iot_hubs=None,
    webhook_url=None,
    api_version=None,
    optional_device_information=None,
):
    attestation = generate_attestation(
        attestation_type=attestation_type,
        endorsement_key=endorsement_key,
        primary_key=primary_key,
        secondary_key=secondary_key,
        primary_cert=primary_cert,
        secondary_cert=secondary_cert,
    )
    custom_allocation = (
        {"webhookUrl": webhook_url, "apiVersion": api_version}
        if allocation_policy == "custom"
        else None
    )
    enrollment = {
        "registrationId": id or create_random_name(),
        "attestation": attestation,
        "allocationPolicy": allocation_policy,
        "capabilities": capabilities,
        "reprovisionPolicy": reprovision_policy,
        "customAllocationDefinition": custom_allocation,
        "provisioningStatus": provisioning_status,
        "deviceId": device_id,
        "optionalDeviceInformation": optional_device_information,
        "iotHubs": iot_hubs,
        "iotHubHostName": iot_hub_host_name,
        "initialTwin": initial_twin_properties,
    }
    return enrollment


def generate_enrollment_group(
    id=None,
    attestation_type=None,
    capabilities=None,
    endorsement_key=None,
    primary_cert=None,
    secondary_cert=None,
    iot_hub_host_name=None,
    initial_twin_properties=None,
    provisioning_status=None,
    reprovision_policy=None,
    primary_key=None,
    secondary_key=None,
    allocation_policy=None,
    iot_hubs=None,
    webhook_url=None,
    api_version=None,
):
    attestation = generate_attestation(
        attestation_type=attestation_type,
        endorsement_key=endorsement_key,
        primary_key=primary_key,
        secondary_key=secondary_key,
        primary_cert=primary_cert,
        secondary_cert=secondary_cert,
        signing_certs=True,
    )
    custom_allocation = (
        {"webhookUrl": webhook_url, "apiVersion": api_version}
        if allocation_policy == "custom"
        else None
    )

    return {
        "enrollmentGroupId": id or create_random_name(),
        "attestation": attestation,
        "allocationPolicy": allocation_policy,
        "capabilities": capabilities,
        "provisioningStatus": provisioning_status,
        "reprovisionPolicy": reprovision_policy,
        "iotHubs": iot_hubs,
        "iotHubHostName": iot_hub_host_name,
        "initialTwin": initial_twin_properties,
        "customAllocationDefinition": custom_allocation,
    }


def generate_key(byte_length=32):
    """
    Generate cryptographically secure device key.
    """
    import secrets

    token_bytes = secrets.token_bytes(byte_length)
    return b64encode(token_bytes).decode("utf8")


def create_test_cert(
    subject=CERT_NAME,
):
    return create_self_signed_certificate(
        subject=subject,
        valid_days=1,
    )


def create_self_signed_certificate(
    subject: str,
    valid_days: int = 365,
    key_size: int = 2048,
):
    """
    Function used to create a basic self-signed certificate with no extensions.

    Args:
        subject (str): Certificate common name; host name or wildcard.
        valid_days (int): number of days certificate is valid for; used to calculate
            certificate expiry.
        cert_putput_dir (str): string value of output directory.
        cert_only (bool): generate certificate only; no private key or thumbprint.
        file_prefix (str): Certificate file name if it needs to be different from the subject.
        sha_version (int): The SHA version to use for generating the thumbprint. For
            IoT Hub, SHA1 is currently used. For DPS, SHA256 has to be used.

    Returns:
        result (dict): dict with certificate value, private key and thumbprint.
    """
    import datetime

    from cryptography import x509
    from cryptography.hazmat.primitives import hashes, serialization
    from cryptography.hazmat.primitives.asymmetric import rsa
    from cryptography.x509.oid import NameOID

    # create a key pair
    key = rsa.generate_private_key(public_exponent=65537, key_size=key_size)
    serial = x509.random_serial_number()
    # create a self-signed cert
    subject_name = x509.Name(
        [
            x509.NameAttribute(NameOID.COMMON_NAME, subject),
        ]
    )
    cert = (
        x509.CertificateBuilder()
        .subject_name(subject_name)
        .issuer_name(subject_name)
        .public_key(key.public_key())
        .serial_number(serial)
        .not_valid_before(datetime.datetime.utcnow())
        .not_valid_after(
            datetime.datetime.utcnow() + datetime.timedelta(days=valid_days)
        )
    )

    # sign
    cert = cert.sign(key, hashes.SHA256())

    # private key
    key_dump = key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.TraditionalOpenSSL,
        encryption_algorithm=serialization.NoEncryption(),
    ).decode("utf-8")

    # certificate string
    cert_dump = cert.public_bytes(serialization.Encoding.PEM).decode("utf-8")

    hash = hashes.SHA256()

    # thumbprint
    thumbprint = cert.fingerprint(hash).hex().upper()

    result = {
        "certificate": cert_dump.rstrip(),
        "privateKey": key_dump,
        "thumbprint": thumbprint,
    }

    return result


def write_content_to_file(
    content,
    destination: str,
    file_name: str,
    overwrite: bool = False,
):
    from pathlib import PurePath

    dest_path = PurePath(destination)
    file_path = dest_path.joinpath(file_name)

    if exists(file_path) and not overwrite:
        raise f"File already exists at path: {file_path}"
    if overwrite and destination:
        makedirs(destination, exist_ok=True)
    write_content = bytes(content, "utf-8") if isinstance(content, str) else content
    with open(file_path, "wb") as f:
        f.write(write_content)


def open_certificate(certificate_path: str) -> str:
    """
    Opens certificate file (as read binary) from the file system and
    returns the value read.

    Args:
        certificate_path (str): the path the the certificate file.

    Returns:
        certificate (str): returns utf-8 encoded value from certificate file.
    """
    certificate = ""
    with open(certificate_path, "rb") as cert_file:
        certificate = cert_file.read()
        try:
            certificate = certificate.decode("utf-8")
        except UnicodeError:
            certificate = b64encode(certificate).decode("utf-8")
    # Remove trailing white space from the certificate content
    return certificate.rstrip()


def sign_string(key, string_to_sign, key_is_base64=True):
    """
    Compute device SAS key
    Args:
        primary_key: Primary group SAS token to compute device keys
        registration_id: Registration ID is alphanumeric, lowercase, and may contain hyphens.
    Returns:
        device key
    """
    import hashlib
    import hmac

    if key_is_base64:
        key = b64decode(key)
    else:
        if isinstance(key, str):
            key = key.encode("utf-8")
    if isinstance(string_to_sign, str):
        string_to_sign = string_to_sign.encode("utf-8")
    signed_hmac_sha256 = hmac.HMAC(key, string_to_sign, hashlib.sha256)
    digest = signed_hmac_sha256.digest()
    encoded_digest = b64encode(digest)
    return encoded_digest