summaryrefslogtreecommitdiff
path: root/nx_secure/src/nx_secure_x509_pkcs1_rsa_private_key_parse.c
blob: 868ac74c21cd7ebf3cadd022627fd03b8565123f (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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Secure Component                                                 */
/**                                                                       */
/**    X.509 Digital Certificates                                         */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SECURE_SOURCE_CODE

#include "nx_secure_x509.h"

static VOID _nx_secure_asn1_parse_unsigned_integer(const UCHAR *data, ULONG length,
                                                   const UCHAR **target, USHORT *target_length);

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_secure_x509_pkcs1_rsa_private_key_parse         PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function parses a DER-encoded PKCS#1-formatted private RSA key */
/*    for use with X509 certificates.                                     */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    buffer                                Pointer data to be parsed     */
/*    length                                Length of data in buffer      */
/*    bytes_processed                       Return bytes processed        */
/*    rsa_key                               Return RSA key structure      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_secure_asn1_parse_unsigned_integer                              */
/*                                          Parse unsigned interger       */
/*    _nx_secure_x509_asn1_tlv_block_parse  Parse ASN.1 block             */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_secure_x509_certificate_initialize                              */
/*                                          Initialize certificate        */
/*                                                                        */
/**************************************************************************/
UINT _nx_secure_x509_pkcs1_rsa_private_key_parse(const UCHAR *buffer, UINT length,
                                                 UINT *bytes_processed,
                                                 NX_SECURE_RSA_PRIVATE_KEY *rsa_key)
{
USHORT       tlv_type;
USHORT       tlv_type_class;
ULONG        tlv_length;
const UCHAR *tlv_data;
ULONG        header_length;
ULONG        seq_length;
UINT         status;
USHORT       version;


    /* Parse an ASN.1 DER-encoded PKCS#1 formatted RSA private key file. */

    /* From RFC 3447, PKCS #1.                                                 */
    /* RSAPrivateKey ::= SEQUENCE {                                            */
    /*     version           Version,                                          */
    /*     modulus           INTEGER,  -- n                                    */
    /*     publicExponent    INTEGER,  -- e                                    */
    /*     privateExponent   INTEGER,  -- d                                    */
    /*     prime1            INTEGER,  -- p                                    */
    /*     prime2            INTEGER,  -- q                                    */
    /*     exponent1         INTEGER,  -- d mod (p-1)                          */
    /*     exponent2         INTEGER,  -- d mod (q-1)                          */
    /*     coefficient       INTEGER,  -- (inverse of q) mod p                 */
    /*     otherPrimeInfos   OtherPrimeInfos OPTIONAL                          */
    /* }                                                                       */
    /*                                                                         */
    /* Version ::= INTEGER { two-prime(0), multi(1) }                          */
    /*               (CONSTRAINED BY                                           */
    /*               {-- version must be multi if otherPrimeInfos present --}) */
    /*                                                                         */
    /*                                                                         */
    /* OtherPrimeInfos ::= SEQUENCE SIZE(1..MAX) OF OtherPrimeInfo             */
    /*                                                                         */
    /* OtherPrimeInfo ::= SEQUENCE {                                           */
    /*     prime             INTEGER,  -- ri                                   */
    /*     exponent          INTEGER,  -- di                                   */
    /*     coefficient       INTEGER   -- ti                                   */
    /* }                                                                       */

    /*  Parse a TLV block and get information to continue parsing. */
    status = _nx_secure_x509_asn1_tlv_block_parse(buffer, (ULONG *)&length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length);

    /*  Make sure we parsed the block alright. */
    if (status != 0)
    {
        return(status);
    }

    if (tlv_type != NX_SECURE_ASN_TAG_SEQUENCE || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL)
    {
        return(NX_SECURE_X509_INVALID_CERTIFICATE_SEQUENCE);
    }

    *bytes_processed = header_length;
    seq_length = tlv_length;

    /* First item in the RSA key sequence is a version field. */
    status = _nx_secure_x509_asn1_tlv_block_parse(tlv_data, &seq_length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length);

    /*  Make sure we parsed the block alright. */
    if (status != 0)
    {
        return(status);
    }

    if (tlv_type != NX_SECURE_ASN_TAG_INTEGER || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL)
    {
        return(NX_SECURE_PKCS1_INVALID_PRIVATE_KEY);
    }

    /* Update byte count. */
    *bytes_processed += (header_length + tlv_length);

    /* Version has 2 values: two-prime(0) and multi(1). It is multi only if the "OtherPrimeInfo" field is present. */
    version = tlv_data[0];

    if (version != 0x00 && version != 0x01)
    {
        return(NX_SECURE_PKCS1_INVALID_PRIVATE_KEY);
    }

    /* Advance our working pointer past the last field. */
    tlv_data = &tlv_data[tlv_length];


    /* Parse our next field, the modulus. */
    status = _nx_secure_x509_asn1_tlv_block_parse(tlv_data, &seq_length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length);

    /*  Make sure we parsed the block alright. */
    if (status != 0)
    {
        return(status);
    }

    if (tlv_type != NX_SECURE_ASN_TAG_INTEGER || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL)
    {
        return(NX_SECURE_PKCS1_INVALID_PRIVATE_KEY);
    }

    /* Update byte count. */
    *bytes_processed += (header_length + tlv_length);

    /* The modulus is an integer, so no padding bytes are needed (as with a BITSTRING). */
    if (rsa_key != NULL)
    {
        _nx_secure_asn1_parse_unsigned_integer(tlv_data, tlv_length, &rsa_key -> nx_secure_rsa_public_modulus,
                                               &rsa_key -> nx_secure_rsa_public_modulus_length);
    }

    /* Advance our working pointer past the last field. */
    tlv_data = &tlv_data[tlv_length];

    /* Parse our next field, the public exponent. */
    status = _nx_secure_x509_asn1_tlv_block_parse(tlv_data, &seq_length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length);

    /*  Make sure we parsed the block alright. */
    if (status != 0)
    {
        return(status);
    }

    if (tlv_type != NX_SECURE_ASN_TAG_INTEGER || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL)
    {
        return(NX_SECURE_PKCS1_INVALID_PRIVATE_KEY);
    }

    /* Update byte count. */
    *bytes_processed += (header_length + tlv_length);

    /* The exponent is an integer, so no padding bytes are needed (as with a BITSTRING). */
    if (rsa_key != NULL)
    {
        _nx_secure_asn1_parse_unsigned_integer(tlv_data, tlv_length, &rsa_key -> nx_secure_rsa_public_exponent,
                                               &rsa_key -> nx_secure_rsa_public_exponent_length);
    }

    /* Advance our working pointer past the last field. */
    tlv_data = &tlv_data[tlv_length];

    /* Parse our next field, the private exponent. */
    status = _nx_secure_x509_asn1_tlv_block_parse(tlv_data, &seq_length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length);

    /*  Make sure we parsed the block alright. */
    if (status != 0)
    {
        return(status);
    }

    if (tlv_type != NX_SECURE_ASN_TAG_INTEGER || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL)
    {
        return(NX_SECURE_PKCS1_INVALID_PRIVATE_KEY);
    }

    /* Update byte count. */
    *bytes_processed += (header_length + tlv_length);

    /* The value is an integer, so no padding bytes are needed (as with a BITSTRING). */
    if (rsa_key != NULL)
    {
        _nx_secure_asn1_parse_unsigned_integer(tlv_data, tlv_length, &rsa_key -> nx_secure_rsa_private_exponent,
                                               &rsa_key -> nx_secure_rsa_private_exponent_length);
    }

    /* Advance our working pointer past the last field. */
    tlv_data = &tlv_data[tlv_length];

    /* Parse our next field, the private prime P. */
    status = _nx_secure_x509_asn1_tlv_block_parse(tlv_data, &seq_length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length);

    /*  Make sure we parsed the block alright. */
    if (status != 0)
    {
        return(status);
    }

    if (tlv_type != NX_SECURE_ASN_TAG_INTEGER || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL)
    {
        return(NX_SECURE_PKCS1_INVALID_PRIVATE_KEY);
    }

    /* Update byte count. */
    *bytes_processed += (header_length + tlv_length);

    /* The modulus is an integer, so no padding bytes are needed (as with a BITSTRING). */
    if (rsa_key != NULL)
    {
        _nx_secure_asn1_parse_unsigned_integer(tlv_data, tlv_length, &rsa_key -> nx_secure_rsa_private_prime_p,
                                               &rsa_key -> nx_secure_rsa_private_prime_p_length);
    }

    /* Advance our working pointer past the last field. */
    tlv_data = &tlv_data[tlv_length];

    /* Parse our next field, the private prime Q. */
    status = _nx_secure_x509_asn1_tlv_block_parse(tlv_data, &seq_length, &tlv_type, &tlv_type_class, &tlv_length, &tlv_data, &header_length);

    /*  Make sure we parsed the block alright. */
    if (status != 0)
    {
        return(status);
    }

    if (tlv_type != NX_SECURE_ASN_TAG_INTEGER || tlv_type_class != NX_SECURE_ASN_TAG_CLASS_UNIVERSAL)
    {
        return(NX_SECURE_PKCS1_INVALID_PRIVATE_KEY);
    }

    /* Update byte count. */
    *bytes_processed += (header_length + tlv_length);

    /* The value is an integer, so no padding bytes are needed (as with a BITSTRING). */
    if (rsa_key != NULL)
    {
        _nx_secure_asn1_parse_unsigned_integer(tlv_data, tlv_length, &rsa_key -> nx_secure_rsa_private_prime_q,
                                               &rsa_key -> nx_secure_rsa_private_prime_q_length);
    }

    return(NX_SECURE_X509_SUCCESS);
}

/* This is a helper function to parse DER-encoded signed integers found in the RSA key file.
   It converts the signed integer into an unsigned character block for use in RSA calculations.
   DER-encoded integers are in 2's-complement format and thus can be negative. All
   RSA keys are unsigned integers so we need to compensate for the extra padding.
   If the first byte is 0x00, check the top bit of the next byte. If that bit
   is set, then discard the 0x00 byte. */
/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_secure_asn1_parse_unsigned_integer              PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Timothy Stapko, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function parses an ASN.1 DER-encoded unsigned integer.         */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    data                                  Pointer data to be parsed     */
/*    length                                Length of data in buffer      */
/*    target                                Return target processed       */
/*    target_length                         Return length of target       */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_secure_x509_pkcs1_rsa_private_key_parse                         */
/*                                          Parse RSA private key field   */
/*                                                                        */
/**************************************************************************/
static VOID _nx_secure_asn1_parse_unsigned_integer(const UCHAR *data, ULONG length,
                                                   const UCHAR **target, USHORT *target_length)
{
UINT offset;

    /* If there is only one byte, don't do anything. */
    if (length <= 1)
    {
        return;
    }

    /* Default is not to skip the first byte. */
    offset = 0;

    if (data[0] == 0x00 && ((data[1] & 0x80) != 0))
    {
        /* Discard the first byte when copying to the target. */
        offset = 1;
    }

    /* Finally set the data pointer and length based on our offset. */
    *target = &data[offset];
    *target_length = (USHORT)(length - offset);
}