Coverage Report

Created: 2025-06-23 06:10

/src/wolfssl/src/x509_str.c
Line
Count
Source (jump to first uncovered line)
1
/* x509_str.c
2
 *
3
 * Copyright (C) 2006-2025 wolfSSL Inc.
4
 *
5
 * This file is part of wolfSSL.
6
 *
7
 * wolfSSL is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * wolfSSL is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20
 */
21
22
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
23
24
#if !defined(WOLFSSL_X509_STORE_INCLUDED)
25
    #ifndef WOLFSSL_IGNORE_FILE_WARN
26
        #warning x509_str.c does not need to be compiled separately from ssl.c
27
    #endif
28
#else
29
30
#ifndef WOLFCRYPT_ONLY
31
32
#ifndef NO_CERTS
33
34
#ifdef OPENSSL_EXTRA
35
static int X509StoreGetIssuerEx(WOLFSSL_X509 **issuer,
36
                            WOLFSSL_STACK *certs, WOLFSSL_X509 *x);
37
static int X509StoreAddCa(WOLFSSL_X509_STORE* store,
38
                                          WOLFSSL_X509* x509, int type);
39
#endif
40
41
/* Based on OpenSSL default max depth */
42
#ifndef WOLFSSL_X509_STORE_DEFAULT_MAX_DEPTH
43
#define WOLFSSL_X509_STORE_DEFAULT_MAX_DEPTH 100
44
#endif
45
46
/******************************************************************************
47
 * START OF X509_STORE_CTX APIs
48
 *****************************************************************************/
49
50
/* This API is necessary outside of OPENSSL_EXTRA because it is used in
51
 * SetupStoreCtxCallback */
52
WOLFSSL_X509_STORE_CTX* wolfSSL_X509_STORE_CTX_new_ex(void* heap)
53
0
{
54
0
    WOLFSSL_X509_STORE_CTX* ctx;
55
0
    WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_new_ex");
56
57
0
    ctx = (WOLFSSL_X509_STORE_CTX*)XMALLOC(sizeof(WOLFSSL_X509_STORE_CTX), heap,
58
0
                                    DYNAMIC_TYPE_X509_CTX);
59
0
    if (ctx != NULL) {
60
0
        XMEMSET(ctx, 0, sizeof(WOLFSSL_X509_STORE_CTX));
61
0
        ctx->heap = heap;
62
#ifdef OPENSSL_EXTRA
63
        if ((ctx->owned = wolfSSL_sk_X509_new_null()) == NULL) {
64
            XFREE(ctx, heap, DYNAMIC_TYPE_X509_CTX);
65
            ctx = NULL;
66
        }
67
        if (ctx != NULL &&
68
            wolfSSL_X509_STORE_CTX_init(ctx, NULL, NULL, NULL) !=
69
                WOLFSSL_SUCCESS) {
70
            wolfSSL_X509_STORE_CTX_free(ctx);
71
            ctx = NULL;
72
        }
73
#endif
74
0
    }
75
76
0
    return ctx;
77
0
}
78
79
/* This API is necessary outside of OPENSSL_EXTRA because it is used in
80
 * SetupStoreCtxCallback */
81
/* free's extra data */
82
void wolfSSL_X509_STORE_CTX_free(WOLFSSL_X509_STORE_CTX* ctx)
83
0
{
84
0
    WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_free");
85
0
    if (ctx != NULL) {
86
#ifdef HAVE_EX_DATA_CLEANUP_HOOKS
87
        wolfSSL_CRYPTO_cleanup_ex_data(&ctx->ex_data);
88
#endif
89
90
#ifdef OPENSSL_EXTRA
91
        XFREE(ctx->param, ctx->heap, DYNAMIC_TYPE_OPENSSL);
92
        ctx->param = NULL;
93
94
        if (ctx->chain != NULL) {
95
            wolfSSL_sk_X509_free(ctx->chain);
96
        }
97
        if (ctx->owned != NULL) {
98
            wolfSSL_sk_X509_pop_free(ctx->owned, NULL);
99
        }
100
101
        if (ctx->current_issuer != NULL) {
102
            wolfSSL_X509_free(ctx->current_issuer);
103
            ctx->current_issuer = NULL;
104
        }
105
#endif
106
107
0
        XFREE(ctx, ctx->heap, DYNAMIC_TYPE_X509_CTX);
108
0
    }
109
0
}
110
111
#ifdef OPENSSL_EXTRA
112
113
#if defined(SESSION_CERTS) || defined(WOLFSSL_SIGNER_DER_CERT)
114
115
/**
116
 * Find the issuing cert of the input cert. On a self-signed cert this
117
 * function will return an error.
118
 * @param issuer The issuer x509 struct is returned here
119
 * @param cm     The cert manager that is queried for the issuer
120
 * @param x      This cert's issuer will be queried in cm
121
 * @return       WOLFSSL_SUCCESS on success
122
 *               WOLFSSL_FAILURE on error
123
 */
124
static int x509GetIssuerFromCM(WOLFSSL_X509 **issuer, WOLFSSL_CERT_MANAGER* cm,
125
        WOLFSSL_X509 *x)
126
{
127
    Signer* ca = NULL;
128
#ifdef WOLFSSL_SMALL_STACK
129
    DecodedCert* cert = NULL;
130
#else
131
    DecodedCert  cert[1];
132
#endif
133
134
    if (cm == NULL || x == NULL || x->derCert == NULL) {
135
        WOLFSSL_MSG("No cert DER buffer or NULL cm. Defining "
136
                    "WOLFSSL_SIGNER_DER_CERT could solve the issue");
137
        return WOLFSSL_FAILURE;
138
    }
139
140
#ifdef WOLFSSL_SMALL_STACK
141
    cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, DYNAMIC_TYPE_DCERT);
142
    if (cert == NULL)
143
        return WOLFSSL_FAILURE;
144
#endif
145
146
    /* Use existing CA retrieval APIs that use DecodedCert. */
147
    InitDecodedCert(cert, x->derCert->buffer, x->derCert->length, cm->heap);
148
    if (ParseCertRelative(cert, CERT_TYPE, 0, NULL, NULL) == 0
149
            && !cert->selfSigned) {
150
    #ifndef NO_SKID
151
        if (cert->extAuthKeyIdSet)
152
            ca = GetCA(cm, cert->extAuthKeyId);
153
        if (ca == NULL)
154
            ca = GetCAByName(cm, cert->issuerHash);
155
    #else /* NO_SKID */
156
        ca = GetCA(cm, cert->issuerHash);
157
    #endif /* NO SKID */
158
    }
159
    FreeDecodedCert(cert);
160
#ifdef WOLFSSL_SMALL_STACK
161
    XFREE(cert, NULL, DYNAMIC_TYPE_DCERT);
162
#endif
163
164
    if (ca == NULL)
165
        return WOLFSSL_FAILURE;
166
167
#ifdef WOLFSSL_SIGNER_DER_CERT
168
    /* populate issuer with Signer DER */
169
    if (wolfSSL_X509_d2i_ex(issuer, ca->derCert->buffer,
170
            ca->derCert->length, cm->heap) == NULL)
171
        return WOLFSSL_FAILURE;
172
#else
173
    /* Create an empty certificate as CA doesn't have a certificate. */
174
    *issuer = (WOLFSSL_X509 *)XMALLOC(sizeof(WOLFSSL_X509), 0,
175
        DYNAMIC_TYPE_OPENSSL);
176
    if (*issuer == NULL)
177
        return WOLFSSL_FAILURE;
178
179
    InitX509((*issuer), 1, NULL);
180
#endif
181
182
    return WOLFSSL_SUCCESS;
183
}
184
#endif /* SESSION_CERTS || WOLFSSL_SIGNER_DER_CERT */
185
186
WOLFSSL_X509_STORE_CTX* wolfSSL_X509_STORE_CTX_new(void)
187
{
188
    WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_new");
189
    return wolfSSL_X509_STORE_CTX_new_ex(NULL);
190
}
191
192
int wolfSSL_X509_STORE_CTX_init(WOLFSSL_X509_STORE_CTX* ctx,
193
     WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509,
194
     WOLF_STACK_OF(WOLFSSL_X509)* sk)
195
{
196
    WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_init");
197
198
    if (ctx != NULL) {
199
        ctx->store = store;
200
        #ifndef WOLFSSL_X509_STORE_CERTS
201
        ctx->current_cert = x509;
202
        #else
203
        if(x509 != NULL){
204
            ctx->current_cert = wolfSSL_X509_d2i_ex(NULL,
205
                    x509->derCert->buffer,
206
                    x509->derCert->length,
207
                    x509->heap);
208
            if(ctx->current_cert == NULL)
209
                return WOLFSSL_FAILURE;
210
        } else
211
            ctx->current_cert = NULL;
212
        #endif
213
214
        ctx->ctxIntermediates = sk;
215
        if (ctx->chain != NULL) {
216
            wolfSSL_sk_X509_free(ctx->chain);
217
            ctx->chain = NULL;
218
        }
219
#ifdef SESSION_CERTS
220
        ctx->sesChain = NULL;
221
#endif
222
        ctx->domain = NULL;
223
#ifdef HAVE_EX_DATA
224
        XMEMSET(&ctx->ex_data, 0, sizeof(ctx->ex_data));
225
#endif
226
        ctx->userCtx = NULL;
227
        ctx->error = 0;
228
        ctx->error_depth = 0;
229
        ctx->discardSessionCerts = 0;
230
231
        if (ctx->param == NULL) {
232
            ctx->param = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
233
                           sizeof(WOLFSSL_X509_VERIFY_PARAM),
234
                           ctx->heap, DYNAMIC_TYPE_OPENSSL);
235
            if (ctx->param == NULL){
236
                WOLFSSL_MSG("wolfSSL_X509_STORE_CTX_init failed");
237
                return WOLFSSL_FAILURE;
238
            }
239
            XMEMSET(ctx->param, 0, sizeof(*ctx->param));
240
        }
241
242
        return WOLFSSL_SUCCESS;
243
    }
244
    return WOLFSSL_FAILURE;
245
}
246
247
/* Its recommended to use a full free -> init cycle of all the objects
248
 * because wolfSSL_X509_STORE_CTX_init may modify the store too which doesn't
249
 * get reset here. */
250
void wolfSSL_X509_STORE_CTX_cleanup(WOLFSSL_X509_STORE_CTX* ctx)
251
{
252
    if (ctx != NULL) {
253
254
        XFREE(ctx->param, ctx->heap, DYNAMIC_TYPE_OPENSSL);
255
        ctx->param = NULL;
256
257
        wolfSSL_X509_STORE_CTX_init(ctx, NULL, NULL, NULL);
258
    }
259
}
260
261
262
void wolfSSL_X509_STORE_CTX_trusted_stack(WOLFSSL_X509_STORE_CTX *ctx,
263
                                          WOLF_STACK_OF(WOLFSSL_X509) *sk)
264
{
265
    if (ctx != NULL) {
266
        ctx->setTrustedSk = sk;
267
    }
268
}
269
270
271
/* Returns corresponding X509 error from internal ASN error <e> */
272
int GetX509Error(int e)
273
{
274
    switch (e) {
275
        case WC_NO_ERR_TRACE(ASN_BEFORE_DATE_E):
276
            return WOLFSSL_X509_V_ERR_CERT_NOT_YET_VALID;
277
        case WC_NO_ERR_TRACE(ASN_AFTER_DATE_E):
278
            return WOLFSSL_X509_V_ERR_CERT_HAS_EXPIRED;
279
        case WC_NO_ERR_TRACE(ASN_NO_SIGNER_E):
280
            /* get issuer error if no CA found locally */
281
            return WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
282
        case WC_NO_ERR_TRACE(ASN_SELF_SIGNED_E):
283
            return WOLFSSL_X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
284
        case WC_NO_ERR_TRACE(ASN_PATHLEN_INV_E):
285
        case WC_NO_ERR_TRACE(ASN_PATHLEN_SIZE_E):
286
            return WOLFSSL_X509_V_ERR_PATH_LENGTH_EXCEEDED;
287
        case WC_NO_ERR_TRACE(ASN_SIG_OID_E):
288
        case WC_NO_ERR_TRACE(ASN_SIG_CONFIRM_E):
289
        case WC_NO_ERR_TRACE(ASN_SIG_HASH_E):
290
        case WC_NO_ERR_TRACE(ASN_SIG_KEY_E):
291
            return WOLFSSL_X509_V_ERR_CERT_SIGNATURE_FAILURE;
292
        /* We can't disambiguate if its the before or after date that caused
293
         * the error. Assume expired. */
294
        case WC_NO_ERR_TRACE(CRL_CERT_DATE_ERR):
295
            return WOLFSSL_X509_V_ERR_CRL_HAS_EXPIRED;
296
        case WC_NO_ERR_TRACE(CRL_CERT_REVOKED):
297
            return WOLFSSL_X509_V_ERR_CERT_REVOKED;
298
        case WC_NO_ERR_TRACE(CRL_MISSING):
299
            return WOLFSSL_X509_V_ERR_UNABLE_TO_GET_CRL;
300
        case 0:
301
        case 1:
302
            return 0;
303
        default:
304
#ifdef HAVE_WOLFSSL_MSG_EX
305
            WOLFSSL_MSG_EX("Error not configured or implemented yet: %d", e);
306
#else
307
            WOLFSSL_MSG("Error not configured or implemented yet");
308
#endif
309
            return e;
310
    }
311
}
312
313
static void SetupStoreCtxError_ex(WOLFSSL_X509_STORE_CTX* ctx, int ret,
314
                                                                    int depth)
315
{
316
    int error = GetX509Error(ret);
317
318
    wolfSSL_X509_STORE_CTX_set_error(ctx, error);
319
    wolfSSL_X509_STORE_CTX_set_error_depth(ctx, depth);
320
}
321
322
static void SetupStoreCtxError(WOLFSSL_X509_STORE_CTX* ctx, int ret)
323
{
324
    int depth = 0;
325
326
    /* Set error depth */
327
    if (ctx->chain)
328
        depth = (int)ctx->chain->num;
329
330
    SetupStoreCtxError_ex(ctx, ret, depth);
331
}
332
333
static int X509StoreVerifyCert(WOLFSSL_X509_STORE_CTX* ctx)
334
{
335
    int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE);
336
    WOLFSSL_ENTER("X509StoreVerifyCert");
337
338
    if (ctx->current_cert != NULL && ctx->current_cert->derCert != NULL) {
339
        ret = wolfSSL_CertManagerVerifyBuffer(ctx->store->cm,
340
                    ctx->current_cert->derCert->buffer,
341
                    ctx->current_cert->derCert->length,
342
                    WOLFSSL_FILETYPE_ASN1);
343
        SetupStoreCtxError(ctx, ret);
344
    #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
345
        if (ctx->store->verify_cb)
346
            ret = ctx->store->verify_cb(ret >= 0 ? 1 : 0, ctx) == 1 ?
347
                                                        WOLFSSL_SUCCESS : ret;
348
    #endif
349
350
    #ifndef NO_ASN_TIME
351
        if (ret != WC_NO_ERR_TRACE(ASN_BEFORE_DATE_E) &&
352
            ret != WC_NO_ERR_TRACE(ASN_AFTER_DATE_E)) {
353
            /* wolfSSL_CertManagerVerifyBuffer only returns ASN_AFTER_DATE_E or
354
             * ASN_BEFORE_DATE_E if there are no additional errors found in the
355
             * cert. Therefore, check if the cert is expired or not yet valid
356
             * in order to return the correct expected error. */
357
            byte *afterDate = ctx->current_cert->notAfter.data;
358
            byte *beforeDate = ctx->current_cert->notBefore.data;
359
360
            if (XVALIDATE_DATE(afterDate,
361
                    (byte)ctx->current_cert->notAfter.type, ASN_AFTER) < 1) {
362
                ret = ASN_AFTER_DATE_E;
363
            }
364
            else if (XVALIDATE_DATE(beforeDate,
365
                    (byte)ctx->current_cert->notBefore.type, ASN_BEFORE) < 1) {
366
                ret = ASN_BEFORE_DATE_E;
367
            }
368
            SetupStoreCtxError(ctx, ret);
369
        #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
370
            if (ctx->store->verify_cb)
371
                ret = ctx->store->verify_cb(ret >= 0 ? 1 : 0,
372
                                            ctx) == 1 ? WOLFSSL_SUCCESS : -1;
373
        #endif
374
        }
375
    #endif
376
    }
377
378
    return ret;
379
}
380
381
static int addAllButSelfSigned(WOLF_STACK_OF(WOLFSSL_X509)*to,
382
                               WOLF_STACK_OF(WOLFSSL_X509)*from, int *numAdded)
383
{
384
    int ret = WOLFSSL_SUCCESS;
385
    int i = 0;
386
    int cnt = 0;
387
    WOLFSSL_X509 *x = NULL;
388
389
    for (i = 0; i < wolfSSL_sk_X509_num(from); i++) {
390
        x = wolfSSL_sk_X509_value(from, i);
391
        if (wolfSSL_X509_NAME_cmp(&x->issuer, &x->subject) != 0) {
392
            if (wolfSSL_sk_X509_push(to, x) <= 0) {
393
                ret = WOLFSSL_FAILURE;
394
                goto exit;
395
            }
396
            cnt++;
397
        }
398
    }
399
400
exit:
401
    if (numAdded != NULL) {
402
        *numAdded = cnt;
403
    }
404
    return ret;
405
}
406
407
/* Verifies certificate chain using WOLFSSL_X509_STORE_CTX
408
 * returns 1 on success or <= 0 on failure.
409
 */
410
int wolfSSL_X509_verify_cert(WOLFSSL_X509_STORE_CTX* ctx)
411
{
412
    int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE);
413
    int done = 0;
414
    int added = 0;
415
    int i = 0;
416
    int numInterAdd = 0;
417
    int depth = 0;
418
    WOLFSSL_X509 *issuer = NULL;
419
    WOLFSSL_X509 *orig = NULL;
420
    WOLF_STACK_OF(WOLFSSL_X509)* certs = NULL;
421
    WOLF_STACK_OF(WOLFSSL_X509)* certsToUse = NULL;
422
    WOLFSSL_ENTER("wolfSSL_X509_verify_cert");
423
424
    if (ctx == NULL || ctx->store == NULL || ctx->store->cm == NULL
425
         || ctx->current_cert == NULL || ctx->current_cert->derCert == NULL) {
426
        return WOLFSSL_FATAL_ERROR;
427
    }
428
429
    certs = ctx->store->certs;
430
    if (ctx->setTrustedSk != NULL) {
431
        certs = ctx->setTrustedSk;
432
    }
433
434
    if (certs == NULL &&
435
        wolfSSL_sk_X509_num(ctx->ctxIntermediates) > 0) {
436
        certsToUse = wolfSSL_sk_X509_new_null();
437
        ret = addAllButSelfSigned(certsToUse, ctx->ctxIntermediates, NULL);
438
    }
439
    else {
440
        /* Add the intermediates provided on init to the list of untrusted
441
         * intermediates to be used */
442
        ret = addAllButSelfSigned(certs, ctx->ctxIntermediates, &numInterAdd);
443
    }
444
    if (ret != WOLFSSL_SUCCESS) {
445
        goto exit;
446
    }
447
448
    if (ctx->chain != NULL) {
449
        wolfSSL_sk_X509_free(ctx->chain);
450
    }
451
    ctx->chain = wolfSSL_sk_X509_new_null();
452
453
    if (ctx->depth > 0) {
454
        depth = ctx->depth + 1;
455
    }
456
    else {
457
        depth = WOLFSSL_X509_STORE_DEFAULT_MAX_DEPTH + 1;
458
    }
459
460
    orig = ctx->current_cert;
461
    while(done == 0 && depth > 0) {
462
        issuer = NULL;
463
464
        /* Try to find an untrusted issuer first */
465
        ret = X509StoreGetIssuerEx(&issuer, certs,
466
                                               ctx->current_cert);
467
        if (ret == WOLFSSL_SUCCESS) {
468
            if (ctx->current_cert == issuer) {
469
                wolfSSL_sk_X509_push(ctx->chain, ctx->current_cert);
470
                break;
471
            }
472
473
            /* We found our issuer in the non-trusted cert list, add it
474
             * to the CM and verify the current cert against it */
475
        #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
476
            /* OpenSSL doesn't allow the cert as CA if it is not CA:TRUE for
477
             * intermediate certs.
478
             */
479
            if (!issuer->isCa) {
480
                /* error depth is current depth + 1 */
481
                SetupStoreCtxError_ex(ctx, X509_V_ERR_INVALID_CA,
482
                                (ctx->chain) ? (int)(ctx->chain->num + 1) : 1);
483
                if (ctx->store->verify_cb) {
484
                    ret = ctx->store->verify_cb(0, ctx);
485
                    if (ret != WOLFSSL_SUCCESS) {
486
                        goto exit;
487
                    }
488
                }
489
            } else {
490
        #endif
491
            ret = X509StoreAddCa(ctx->store, issuer,
492
                                            WOLFSSL_TEMP_CA);
493
            if (ret != WOLFSSL_SUCCESS) {
494
                goto exit;
495
            }
496
            added = 1;
497
            ret = X509StoreVerifyCert(ctx);
498
            if (ret != WOLFSSL_SUCCESS) {
499
                goto exit;
500
            }
501
            /* Add it to the current chain and look at the issuer cert next */
502
            wolfSSL_sk_X509_push(ctx->chain, ctx->current_cert);
503
        #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
504
            }
505
        #endif
506
            ctx->current_cert = issuer;
507
        }
508
        else if (ret == WC_NO_ERR_TRACE(WOLFSSL_FAILURE)) {
509
            /* Could not find in untrusted list, only place left is
510
             * a trusted CA in the CM */
511
            ret = X509StoreVerifyCert(ctx);
512
            if (ret != WOLFSSL_SUCCESS) {
513
                if (((ctx->flags & WOLFSSL_PARTIAL_CHAIN) ||
514
                     (ctx->store->param->flags & WOLFSSL_PARTIAL_CHAIN)) &&
515
                    (added == 1)) {
516
                    wolfSSL_sk_X509_push(ctx->chain, ctx->current_cert);
517
                    ret = WOLFSSL_SUCCESS;
518
                }
519
                goto exit;
520
            }
521
522
            /* Cert verified, finish building the chain */
523
            wolfSSL_sk_X509_push(ctx->chain, ctx->current_cert);
524
            issuer = NULL;
525
    #ifdef WOLFSSL_SIGNER_DER_CERT
526
            x509GetIssuerFromCM(&issuer, ctx->store->cm, ctx->current_cert);
527
            if (issuer != NULL && ctx->owned != NULL) {
528
                wolfSSL_sk_X509_push(ctx->owned, issuer);
529
            }
530
    #else
531
            if (ctx->setTrustedSk == NULL) {
532
                X509StoreGetIssuerEx(&issuer,
533
                    ctx->store->trusted, ctx->current_cert);
534
            }
535
            else {
536
                X509StoreGetIssuerEx(&issuer,
537
                    ctx->setTrustedSk, ctx->current_cert);
538
            }
539
    #endif
540
            if (issuer != NULL) {
541
                wolfSSL_sk_X509_push(ctx->chain, issuer);
542
            }
543
544
            done = 1;
545
        }
546
        else {
547
            goto exit;
548
        }
549
550
        depth--;
551
    }
552
553
exit:
554
    /* Remove additional intermediates from init from the store */
555
    if (ctx != NULL && numInterAdd > 0) {
556
        for (i = 0; i < numInterAdd; i++) {
557
            wolfSSL_sk_X509_pop(ctx->store->certs);
558
        }
559
    }
560
    /* Remove intermediates that were added to CM */
561
    if (ctx != NULL) {
562
        if (ctx->store != NULL) {
563
            if (added == 1) {
564
                wolfSSL_CertManagerUnloadTempIntermediateCerts(ctx->store->cm);
565
            }
566
        }
567
        if (orig != NULL) {
568
            ctx->current_cert = orig;
569
        }
570
    }
571
    if (certsToUse != NULL) {
572
        wolfSSL_sk_X509_free(certsToUse);
573
    }
574
575
    return ret == WOLFSSL_SUCCESS ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
576
}
577
578
#endif /* OPENSSL_EXTRA */
579
580
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
581
    WOLFSSL_X509* wolfSSL_X509_STORE_CTX_get_current_cert(
582
                                                WOLFSSL_X509_STORE_CTX* ctx)
583
    {
584
        WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_current_cert");
585
        if (ctx)
586
            return ctx->current_cert;
587
        return NULL;
588
    }
589
590
591
    int wolfSSL_X509_STORE_CTX_get_error(WOLFSSL_X509_STORE_CTX* ctx)
592
    {
593
        WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_error");
594
        if (ctx != NULL)
595
            return ctx->error;
596
        return 0;
597
    }
598
599
600
    int wolfSSL_X509_STORE_CTX_get_error_depth(WOLFSSL_X509_STORE_CTX* ctx)
601
    {
602
        WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_error_depth");
603
        if(ctx)
604
            return ctx->error_depth;
605
        return WOLFSSL_FATAL_ERROR;
606
    }
607
608
/* get X509_STORE_CTX ex_data, max idx is MAX_EX_DATA */
609
void* wolfSSL_X509_STORE_CTX_get_ex_data(WOLFSSL_X509_STORE_CTX* ctx, int idx)
610
{
611
    WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_ex_data");
612
#ifdef HAVE_EX_DATA
613
    if (ctx != NULL) {
614
        return wolfSSL_CRYPTO_get_ex_data(&ctx->ex_data, idx);
615
    }
616
#else
617
    (void)ctx;
618
    (void)idx;
619
#endif
620
    return NULL;
621
}
622
#endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
623
624
#ifdef OPENSSL_EXTRA
625
    void wolfSSL_X509_STORE_CTX_set_verify_cb(WOLFSSL_X509_STORE_CTX *ctx,
626
                                  WOLFSSL_X509_STORE_CTX_verify_cb verify_cb)
627
    {
628
        WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_verify_cb");
629
        if(ctx == NULL)
630
            return;
631
        ctx->verify_cb = verify_cb;
632
    }
633
634
/* Gets pointer to X509_STORE that was used to create context.
635
 *
636
 * Return valid pointer on success, NULL if ctx was NULL or not initialized
637
 */
638
WOLFSSL_X509_STORE* wolfSSL_X509_STORE_CTX_get0_store(
639
        WOLFSSL_X509_STORE_CTX* ctx)
640
{
641
    WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get0_store");
642
643
    if (ctx == NULL)
644
        return NULL;
645
646
    return ctx->store;
647
}
648
649
WOLFSSL_X509* wolfSSL_X509_STORE_CTX_get0_cert(WOLFSSL_X509_STORE_CTX* ctx)
650
{
651
    if (ctx == NULL)
652
        return NULL;
653
654
    return ctx->current_cert;
655
}
656
657
void wolfSSL_X509_STORE_CTX_set_time(WOLFSSL_X509_STORE_CTX* ctx,
658
                                    unsigned long flags,
659
                                    time_t t)
660
{
661
    (void)flags;
662
663
    if (ctx == NULL || ctx->param == NULL)
664
        return;
665
666
    ctx->param->check_time = t;
667
    ctx->param->flags |= WOLFSSL_USE_CHECK_TIME;
668
}
669
670
#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
671
#ifndef NO_WOLFSSL_STUB
672
int wolfSSL_X509_STORE_CTX_set_purpose(WOLFSSL_X509_STORE_CTX *ctx,
673
                                       int purpose)
674
{
675
    (void)ctx;
676
    (void)purpose;
677
    WOLFSSL_STUB("wolfSSL_X509_STORE_CTX_set_purpose (not implemented)");
678
    return 0;
679
}
680
#endif /* !NO_WOLFSSL_STUB */
681
682
#endif /* WOLFSSL_QT || OPENSSL_ALL */
683
#endif /* OPENSSL_EXTRA */
684
685
#ifdef OPENSSL_EXTRA
686
687
void wolfSSL_X509_STORE_CTX_set_flags(WOLFSSL_X509_STORE_CTX *ctx,
688
        unsigned long flags)
689
{
690
    if ((ctx != NULL) && (flags & WOLFSSL_PARTIAL_CHAIN)){
691
        ctx->flags |= WOLFSSL_PARTIAL_CHAIN;
692
    }
693
}
694
695
/* set X509_STORE_CTX ex_data, max idx is MAX_EX_DATA. Return WOLFSSL_SUCCESS
696
 * on success, WOLFSSL_FAILURE on error. */
697
int wolfSSL_X509_STORE_CTX_set_ex_data(WOLFSSL_X509_STORE_CTX* ctx, int idx,
698
                                       void *data)
699
{
700
    WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_ex_data");
701
#ifdef HAVE_EX_DATA
702
    if (ctx != NULL)
703
    {
704
        return wolfSSL_CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
705
    }
706
#else
707
    (void)ctx;
708
    (void)idx;
709
    (void)data;
710
#endif
711
    return WOLFSSL_FAILURE;
712
}
713
714
#ifdef HAVE_EX_DATA_CLEANUP_HOOKS
715
/* set X509_STORE_CTX ex_data, max idx is MAX_EX_DATA. Return WOLFSSL_SUCCESS
716
 * on success, WOLFSSL_FAILURE on error. */
717
int wolfSSL_X509_STORE_CTX_set_ex_data_with_cleanup(
718
    WOLFSSL_X509_STORE_CTX* ctx,
719
    int idx,
720
    void *data,
721
    wolfSSL_ex_data_cleanup_routine_t cleanup_routine)
722
{
723
    WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_ex_data_with_cleanup");
724
    if (ctx != NULL)
725
    {
726
        return wolfSSL_CRYPTO_set_ex_data_with_cleanup(&ctx->ex_data, idx,
727
                                                        data, cleanup_routine);
728
    }
729
    return WOLFSSL_FAILURE;
730
}
731
#endif /* HAVE_EX_DATA_CLEANUP_HOOKS */
732
733
#if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL)
734
void wolfSSL_X509_STORE_CTX_set_depth(WOLFSSL_X509_STORE_CTX* ctx, int depth)
735
{
736
    WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_depth");
737
    if (ctx)
738
        ctx->depth = depth;
739
}
740
#endif
741
742
WOLFSSL_X509* wolfSSL_X509_STORE_CTX_get0_current_issuer(
743
        WOLFSSL_X509_STORE_CTX* ctx)
744
{
745
    WOLFSSL_STACK* node;
746
    WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get0_current_issuer");
747
748
    if (ctx == NULL)
749
        return NULL;
750
751
    /* get0 only checks currently built chain */
752
    if (ctx->chain != NULL) {
753
        for (node = ctx->chain; node != NULL; node = node->next) {
754
            if (wolfSSL_X509_check_issued(node->data.x509,
755
                                          ctx->current_cert) ==
756
                                                WOLFSSL_X509_V_OK) {
757
                return node->data.x509;
758
            }
759
        }
760
    }
761
762
    return NULL;
763
}
764
765
/* Set an error stat in the X509 STORE CTX
766
 *
767
 */
768
void wolfSSL_X509_STORE_CTX_set_error(WOLFSSL_X509_STORE_CTX* ctx, int er)
769
{
770
    WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_error");
771
772
    if (ctx != NULL) {
773
        ctx->error = er;
774
    }
775
}
776
777
/* Set the error depth in the X509 STORE CTX */
778
void wolfSSL_X509_STORE_CTX_set_error_depth(WOLFSSL_X509_STORE_CTX* ctx,
779
                                                                    int depth)
780
{
781
    WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_error_depth");
782
783
    if (ctx != NULL) {
784
        ctx->error_depth = depth;
785
    }
786
}
787
788
WOLFSSL_STACK* wolfSSL_X509_STORE_CTX_get_chain(WOLFSSL_X509_STORE_CTX* ctx)
789
{
790
    WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_chain");
791
792
    if (ctx == NULL) {
793
        return NULL;
794
    }
795
796
#ifdef SESSION_CERTS
797
    /* if chain is null but sesChain is available then populate stack */
798
    if (ctx->chain == NULL && ctx->sesChain != NULL) {
799
        int i;
800
        int error = 0;
801
        WOLFSSL_X509_CHAIN* c = ctx->sesChain;
802
        WOLFSSL_STACK*     sk = wolfSSL_sk_new_node(ctx->heap);
803
804
        if (sk == NULL)
805
            return NULL;
806
807
        for (i = 0; i < c->count; i++) {
808
            WOLFSSL_X509* x509 = wolfSSL_get_chain_X509(c, i);
809
810
            if (x509 == NULL) {
811
                WOLFSSL_MSG("Unable to get x509 from chain");
812
                error = 1;
813
                break;
814
            }
815
816
            if (wolfSSL_sk_X509_push(sk, x509) <= 0) {
817
                WOLFSSL_MSG("Unable to load x509 into stack");
818
                wolfSSL_X509_free(x509);
819
                x509 = NULL;
820
                error = 1;
821
                break;
822
            }
823
        }
824
825
#if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
826
    defined(OPENSSL_EXTRA)
827
        /* add CA used to verify top of chain to the list */
828
        if (!error && c->count > 0) {
829
            WOLFSSL_X509* x509 = wolfSSL_get_chain_X509(c, c->count - 1);
830
            WOLFSSL_X509* issuer = NULL;
831
            if (x509 != NULL) {
832
                if (wolfSSL_X509_STORE_CTX_get1_issuer(&issuer, ctx, x509)
833
                        == WOLFSSL_SUCCESS) {
834
                    /* check that the certificate being looked up is not self
835
                     * signed and that a issuer was found */
836
                    if (issuer != NULL && wolfSSL_X509_NAME_cmp(&x509->issuer,
837
                                &x509->subject) != 0) {
838
                        if (wolfSSL_sk_X509_push(sk, issuer) <= 0) {
839
                            WOLFSSL_MSG("Unable to load CA x509 into stack");
840
                            error = 1;
841
                            wolfSSL_X509_free(issuer);
842
                            issuer = NULL;
843
                        }
844
                    }
845
                    else {
846
                        WOLFSSL_MSG("Certificate is self signed");
847
                        wolfSSL_X509_free(issuer);
848
                        issuer = NULL;
849
                    }
850
                }
851
                else {
852
                    WOLFSSL_MSG("Could not find CA for certificate");
853
                }
854
            }
855
            wolfSSL_X509_free(x509);
856
            x509 = NULL;
857
        }
858
#endif
859
        if (error) {
860
            wolfSSL_sk_X509_pop_free(sk, NULL);
861
            return NULL;
862
        }
863
        ctx->chain = sk;
864
    }
865
#endif /* SESSION_CERTS */
866
867
    return ctx->chain;
868
}
869
870
/* like X509_STORE_CTX_get_chain(), but return a copy with data reference
871
   counts increased */
872
WOLFSSL_STACK* wolfSSL_X509_STORE_CTX_get1_chain(WOLFSSL_X509_STORE_CTX* ctx)
873
{
874
    WOLFSSL_STACK* ref;
875
876
    if (ctx == NULL) {
877
        return NULL;
878
    }
879
880
    /* get chain in ctx */
881
    ref = wolfSSL_X509_STORE_CTX_get_chain(ctx);
882
    if (ref == NULL) {
883
        return ref;
884
    }
885
886
    /* create duplicate of ctx chain */
887
    return wolfSSL_sk_dup(ref);
888
}
889
890
#ifndef NO_WOLFSSL_STUB
891
WOLFSSL_X509_STORE_CTX *wolfSSL_X509_STORE_CTX_get0_parent_ctx(
892
                                                   WOLFSSL_X509_STORE_CTX *ctx)
893
{
894
    (void)ctx;
895
    WOLFSSL_STUB("wolfSSL_X509_STORE_CTX_get0_parent_ctx");
896
    return NULL;
897
}
898
899
int wolfSSL_X509_STORE_get_by_subject(WOLFSSL_X509_STORE_CTX* ctx, int idx,
900
                            WOLFSSL_X509_NAME* name, WOLFSSL_X509_OBJECT* obj)
901
{
902
    (void)ctx;
903
    (void)idx;
904
    (void)name;
905
    (void)obj;
906
    WOLFSSL_STUB("X509_STORE_get_by_subject");
907
    return 0;
908
}
909
#endif
910
911
WOLFSSL_X509_VERIFY_PARAM *wolfSSL_X509_STORE_CTX_get0_param(
912
        WOLFSSL_X509_STORE_CTX *ctx)
913
{
914
    if (ctx == NULL)
915
        return NULL;
916
917
    return ctx->param;
918
}
919
920
#endif /* OPENSSL_EXTRA */
921
922
#if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
923
#if defined(WOLFSSL_SIGNER_DER_CERT)
924
WOLF_STACK_OF(WOLFSSL_X509)* wolfSSL_X509_STORE_get1_certs(
925
    WOLFSSL_X509_STORE_CTX* ctx, WOLFSSL_X509_NAME* name)
926
{
927
    WOLF_STACK_OF(WOLFSSL_X509)* ret = NULL;
928
    int err = 0;
929
    WOLFSSL_X509_STORE* store = NULL;
930
    WOLFSSL_STACK* sk = NULL;
931
    WOLFSSL_STACK* certToFilter = NULL;
932
    WOLFSSL_X509_NAME* certToFilterName = NULL;
933
    WOLF_STACK_OF(WOLFSSL_X509)* filteredCerts = NULL;
934
    WOLFSSL_X509* filteredCert = NULL;
935
936
    WOLFSSL_ENTER("wolfSSL_X509_STORE_get1_certs");
937
938
    if (name == NULL) {
939
        err = 1;
940
    }
941
942
    if (err == 0) {
943
        store = wolfSSL_X509_STORE_CTX_get0_store(ctx);
944
        if (store == NULL) {
945
            err = 1;
946
        }
947
    }
948
949
    if (err == 0) {
950
        filteredCerts = wolfSSL_sk_X509_new_null();
951
        if (filteredCerts == NULL) {
952
            err = 1;
953
        }
954
    }
955
956
    if (err == 0) {
957
        sk = wolfSSL_CertManagerGetCerts(store->cm);
958
        if (sk == NULL) {
959
            err = 1;
960
        }
961
    }
962
963
    if (err == 0) {
964
        certToFilter = sk;
965
        while (certToFilter != NULL) {
966
            certToFilterName = wolfSSL_X509_get_subject_name(
967
                                    certToFilter->data.x509);
968
            if (certToFilterName != NULL) {
969
                if (wolfSSL_X509_NAME_cmp(certToFilterName, name) == 0) {
970
                    filteredCert = wolfSSL_X509_dup(certToFilter->data.x509);
971
                    if (filteredCert == NULL ||
972
                            wolfSSL_sk_X509_push(filteredCerts, filteredCert)
973
                                <= 0) {
974
                        err = 1;
975
                        wolfSSL_X509_free(filteredCert);
976
                        filteredCert = NULL;
977
                        break;
978
                    }
979
                }
980
            }
981
            certToFilter = certToFilter->next;
982
        }
983
    }
984
985
    if (err == 1) {
986
        if (filteredCerts != NULL) {
987
            wolfSSL_sk_X509_pop_free(filteredCerts, NULL);
988
        }
989
        ret = NULL;
990
    }
991
    else {
992
        ret = filteredCerts;
993
    }
994
995
    if (sk != NULL) {
996
        wolfSSL_sk_X509_pop_free(sk, NULL);
997
    }
998
999
    return ret;
1000
}
1001
#endif /* WOLFSSL_SIGNER_DER_CERT */
1002
1003
#endif /* OPENSSL_EXTRA && !NO_FILESYSTEM */
1004
1005
#if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
1006
    defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
1007
int wolfSSL_X509_STORE_CTX_get1_issuer(WOLFSSL_X509 **issuer,
1008
    WOLFSSL_X509_STORE_CTX *ctx, WOLFSSL_X509 *x)
1009
{
1010
    int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE);
1011
    WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get1_issuer");
1012
1013
    if (issuer == NULL || ctx == NULL || x == NULL)
1014
        return WOLFSSL_FATAL_ERROR;
1015
1016
    ret = X509StoreGetIssuerEx(issuer, ctx->store->certs, x);
1017
    if ((ret == WOLFSSL_SUCCESS) && (*issuer != NULL)) {
1018
        return wolfSSL_X509_up_ref(*issuer);
1019
    }
1020
1021
#ifdef WOLFSSL_SIGNER_DER_CERT
1022
    ret = x509GetIssuerFromCM(issuer, ctx->store->cm, x);
1023
#else
1024
    ret = X509StoreGetIssuerEx(issuer, ctx->store->trusted, x);
1025
    if ((ret == WOLFSSL_SUCCESS) && (*issuer != NULL)) {
1026
        return wolfSSL_X509_up_ref(*issuer);
1027
    }
1028
#endif
1029
1030
    return ret;
1031
}
1032
#endif /* WOLFSSL_NGINX || WOLFSSL_HAPROXY || OPENSSL_EXTRA || OPENSSL_ALL */
1033
1034
#ifdef OPENSSL_EXTRA
1035
1036
static int X509StoreGetIssuerEx(WOLFSSL_X509 **issuer,
1037
                            WOLFSSL_STACK * certs, WOLFSSL_X509 *x)
1038
{
1039
    int i;
1040
1041
    if (issuer == NULL || x == NULL)
1042
        return WOLFSSL_FATAL_ERROR;
1043
1044
    if (certs != NULL) {
1045
        for (i = 0; i < wolfSSL_sk_X509_num(certs); i++) {
1046
            if (wolfSSL_X509_check_issued(
1047
                    wolfSSL_sk_X509_value(certs, i), x) ==
1048
                    WOLFSSL_X509_V_OK) {
1049
                *issuer = wolfSSL_sk_X509_value(certs, i);
1050
                return WOLFSSL_SUCCESS;
1051
            }
1052
        }
1053
    }
1054
1055
    return WOLFSSL_FAILURE;
1056
}
1057
1058
#endif
1059
1060
/******************************************************************************
1061
 * END OF X509_STORE_CTX APIs
1062
 *****************************************************************************/
1063
1064
/******************************************************************************
1065
 * START OF X509_STORE APIs
1066
 *****************************************************************************/
1067
1068
#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
1069
    defined(WOLFSSL_WPAS_SMALL)
1070
WOLFSSL_X509_STORE* wolfSSL_X509_STORE_new(void)
1071
{
1072
    int ret;
1073
    WOLFSSL_X509_STORE* store = NULL;
1074
    WOLFSSL_ENTER("wolfSSL_X509_STORE_new");
1075
1076
    if ((store = (WOLFSSL_X509_STORE*)XMALLOC(sizeof(WOLFSSL_X509_STORE), NULL,
1077
                                    DYNAMIC_TYPE_X509_STORE)) == NULL)
1078
        goto err_exit;
1079
1080
    XMEMSET(store, 0, sizeof(WOLFSSL_X509_STORE));
1081
    store->isDynamic = 1;
1082
1083
    wolfSSL_RefInit(&store->ref, &ret);
1084
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
1085
    if (ret != 0)
1086
        goto err_exit;
1087
#else
1088
    (void)ret;
1089
#endif
1090
1091
    if ((store->cm = wolfSSL_CertManagerNew()) == NULL)
1092
        goto err_exit;
1093
1094
#ifdef OPENSSL_EXTRA
1095
    if ((store->certs = wolfSSL_sk_X509_new_null()) == NULL)
1096
        goto err_exit;
1097
1098
    if ((store->owned = wolfSSL_sk_X509_new_null()) == NULL)
1099
        goto err_exit;
1100
1101
    if ((store->trusted = wolfSSL_sk_X509_new_null()) == NULL)
1102
        goto err_exit;
1103
#endif
1104
1105
#ifdef HAVE_CRL
1106
    store->crl = store->cm->crl;
1107
#endif
1108
1109
    store->numAdded = 0;
1110
1111
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
1112
1113
    /* Link store's new Certificate Manager to self by default */
1114
    store->cm->x509_store_p = store;
1115
1116
    if ((store->param = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
1117
                           sizeof(WOLFSSL_X509_VERIFY_PARAM),
1118
                           NULL, DYNAMIC_TYPE_OPENSSL)) == NULL) {
1119
        goto err_exit;
1120
    }
1121
    XMEMSET(store->param, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
1122
    if ((store->lookup.dirs = (WOLFSSL_BY_DIR*)XMALLOC(sizeof(WOLFSSL_BY_DIR),
1123
                           NULL, DYNAMIC_TYPE_OPENSSL)) == NULL) {
1124
        WOLFSSL_MSG("store->lookup.dir memory allocation error");
1125
        goto err_exit;
1126
    }
1127
    XMEMSET(store->lookup.dirs, 0, sizeof(WOLFSSL_BY_DIR));
1128
    if (wc_InitMutex(&store->lookup.dirs->lock) != 0) {
1129
            WOLFSSL_MSG("Bad mutex init");
1130
            goto err_exit;
1131
    }
1132
#endif
1133
1134
    return store;
1135
1136
err_exit:
1137
    if (store == NULL)
1138
        return NULL;
1139
1140
    wolfSSL_X509_STORE_free(store);
1141
1142
    return NULL;
1143
}
1144
1145
#ifdef OPENSSL_ALL
1146
static void X509StoreFreeObjList(WOLFSSL_X509_STORE* store,
1147
                  WOLF_STACK_OF(WOLFSSL_X509_OBJECT)* objs)
1148
{
1149
    int i;
1150
    WOLFSSL_X509_OBJECT *obj = NULL;
1151
    int cnt = store->numAdded;
1152
1153
    /* -1 here because it is later used as an index value into the object stack.
1154
     * With there being the chance that the only object in the stack is one from
1155
     * the numAdded to the store >= is used when comparing to 0. */
1156
    i = wolfSSL_sk_X509_OBJECT_num(objs) - 1;
1157
    while (cnt > 0 && i >= 0) {
1158
        /* The inner X509 is owned by somebody else, NULL out the reference */
1159
        obj = (WOLFSSL_X509_OBJECT *)wolfSSL_sk_X509_OBJECT_value(objs, i);
1160
        if (obj != NULL) {
1161
            obj->type = (WOLFSSL_X509_LOOKUP_TYPE)0;
1162
            obj->data.ptr = NULL;
1163
        }
1164
        cnt--;
1165
        i--;
1166
    }
1167
1168
    wolfSSL_sk_X509_OBJECT_pop_free(objs, NULL);
1169
}
1170
#endif
1171
1172
void wolfSSL_X509_STORE_free(WOLFSSL_X509_STORE* store)
1173
{
1174
    int doFree = 0;
1175
    if (store != NULL && store->isDynamic) {
1176
        int ret;
1177
        wolfSSL_RefDec(&store->ref, &doFree, &ret);
1178
    #ifdef WOLFSSL_REFCNT_ERROR_RETURN
1179
        if (ret != 0) {
1180
            WOLFSSL_MSG("Couldn't lock store mutex");
1181
        }
1182
    #else
1183
        (void)ret;
1184
    #endif
1185
1186
        if (doFree) {
1187
#ifdef HAVE_EX_DATA_CLEANUP_HOOKS
1188
            wolfSSL_CRYPTO_cleanup_ex_data(&store->ex_data);
1189
#endif
1190
            if (store->cm != NULL) {
1191
                wolfSSL_CertManagerFree(store->cm);
1192
                store->cm = NULL;
1193
            }
1194
#if defined(OPENSSL_EXTRA)
1195
            if (store->certs != NULL) {
1196
                wolfSSL_sk_X509_pop_free(store->certs, NULL);
1197
                store->certs = NULL;
1198
            }
1199
            if (store->owned != NULL) {
1200
                wolfSSL_sk_X509_pop_free(store->owned, NULL);
1201
                store->owned = NULL;
1202
            }
1203
            if (store->trusted != NULL) {
1204
                wolfSSL_sk_X509_pop_free(store->trusted, NULL);
1205
                store->trusted = NULL;
1206
            }
1207
#endif
1208
#ifdef OPENSSL_ALL
1209
            if (store->objs != NULL) {
1210
                X509StoreFreeObjList(store, store->objs);
1211
            }
1212
#endif
1213
#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
1214
            XFREE(store->param, NULL, DYNAMIC_TYPE_OPENSSL);
1215
            store->param = NULL;
1216
1217
            if (store->lookup.dirs != NULL) {
1218
#if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
1219
                if (store->lookup.dirs->dir_entry) {
1220
                    wolfSSL_sk_BY_DIR_entry_free(
1221
                        store->lookup.dirs->dir_entry);
1222
                }
1223
#endif
1224
                wc_FreeMutex(&store->lookup.dirs->lock);
1225
                XFREE(store->lookup.dirs, NULL, DYNAMIC_TYPE_OPENSSL);
1226
                store->lookup.dirs = NULL;
1227
            }
1228
#endif
1229
            wolfSSL_RefFree(&store->ref);
1230
            XFREE(store, NULL, DYNAMIC_TYPE_X509_STORE);
1231
        }
1232
    }
1233
}
1234
1235
/**
1236
 * Get ex_data in WOLFSSL_STORE at given index
1237
 * @param store a pointer to WOLFSSL_X509_STORE structure
1238
 * @param idx   Index of ex_data to get data from
1239
 * @return void pointer to ex_data on success or NULL on failure
1240
 */
1241
void* wolfSSL_X509_STORE_get_ex_data(WOLFSSL_X509_STORE* store, int idx)
1242
{
1243
    WOLFSSL_ENTER("wolfSSL_X509_STORE_get_ex_data");
1244
#ifdef HAVE_EX_DATA
1245
    if (store != NULL && idx < MAX_EX_DATA && idx >= 0) {
1246
        return wolfSSL_CRYPTO_get_ex_data(&store->ex_data, idx);
1247
    }
1248
#else
1249
    (void)store;
1250
    (void)idx;
1251
#endif
1252
    return NULL;
1253
}
1254
1255
int wolfSSL_X509_STORE_up_ref(WOLFSSL_X509_STORE* store)
1256
{
1257
    if (store) {
1258
        int ret;
1259
        wolfSSL_RefInc(&store->ref, &ret);
1260
    #ifdef WOLFSSL_REFCNT_ERROR_RETURN
1261
        if (ret != 0) {
1262
            WOLFSSL_MSG("Failed to lock store mutex");
1263
            return WOLFSSL_FAILURE;
1264
        }
1265
    #else
1266
        (void)ret;
1267
    #endif
1268
1269
        return WOLFSSL_SUCCESS;
1270
    }
1271
1272
    return WOLFSSL_FAILURE;
1273
}
1274
1275
/**
1276
 * Set ex_data for WOLFSSL_STORE
1277
 * @param store a pointer to WOLFSSL_X509_STORE structure
1278
 * @param idx   Index of ex data to set
1279
 * @param data  Data to set in ex data
1280
 * @return WOLFSSL_SUCCESS on success or WOLFSSL_FAILURE on failure
1281
 */
1282
int wolfSSL_X509_STORE_set_ex_data(WOLFSSL_X509_STORE* store, int idx,
1283
                                                                void *data)
1284
{
1285
    WOLFSSL_ENTER("wolfSSL_X509_STORE_set_ex_data");
1286
#ifdef HAVE_EX_DATA
1287
    if (store != NULL && idx < MAX_EX_DATA) {
1288
        return wolfSSL_CRYPTO_set_ex_data(&store->ex_data, idx, data);
1289
    }
1290
#else
1291
    (void)store;
1292
    (void)idx;
1293
    (void)data;
1294
#endif
1295
    return WOLFSSL_FAILURE;
1296
}
1297
1298
#ifdef HAVE_EX_DATA_CLEANUP_HOOKS
1299
/**
1300
 * Set ex_data for WOLFSSL_STORE
1301
 * @param store a pointer to WOLFSSL_X509_STORE structure
1302
 * @param idx   Index of ex data to set
1303
 * @param data  Data to set in ex data
1304
 * @return WOLFSSL_SUCCESS on success or WOLFSSL_FAILURE on failure
1305
 */
1306
int wolfSSL_X509_STORE_set_ex_data_with_cleanup(
1307
    WOLFSSL_X509_STORE* store,
1308
    int idx,
1309
    void *data,
1310
    wolfSSL_ex_data_cleanup_routine_t cleanup_routine)
1311
{
1312
    WOLFSSL_ENTER("wolfSSL_X509_STORE_set_ex_data_with_cleanup");
1313
    if (store != NULL && idx < MAX_EX_DATA) {
1314
        return wolfSSL_CRYPTO_set_ex_data_with_cleanup(&store->ex_data, idx,
1315
                                                       data, cleanup_routine);
1316
    }
1317
    return WOLFSSL_FAILURE;
1318
}
1319
1320
#endif /* HAVE_EX_DATA_CLEANUP_HOOKS */
1321
1322
#endif /* OPENSSL_EXTRA || HAVE_WEBSERVER || WOLFSSL_WPAS_SMALL */
1323
1324
#ifdef OPENSSL_EXTRA
1325
1326
#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
1327
void wolfSSL_X509_STORE_set_verify_cb(WOLFSSL_X509_STORE *st,
1328
        WOLFSSL_X509_STORE_CTX_verify_cb verify_cb)
1329
{
1330
    WOLFSSL_ENTER("wolfSSL_X509_STORE_set_verify_cb");
1331
    if (st != NULL) {
1332
        st->verify_cb = verify_cb;
1333
    }
1334
}
1335
1336
void wolfSSL_X509_STORE_set_get_crl(WOLFSSL_X509_STORE *st,
1337
        WOLFSSL_X509_STORE_CTX_get_crl_cb get_cb)
1338
{
1339
    WOLFSSL_ENTER("wolfSSL_X509_STORE_set_get_crl");
1340
    if (st != NULL) {
1341
        st->get_crl_cb = get_cb;
1342
    }
1343
}
1344
1345
#ifndef NO_WOLFSSL_STUB
1346
void wolfSSL_X509_STORE_set_check_crl(WOLFSSL_X509_STORE *st,
1347
        WOLFSSL_X509_STORE_CTX_check_crl_cb check_crl)
1348
{
1349
    (void)st;
1350
    (void)check_crl;
1351
    WOLFSSL_STUB("wolfSSL_X509_STORE_set_check_crl (not implemented)");
1352
}
1353
#endif
1354
#endif /* WOLFSSL_QT || OPENSSL_ALL */
1355
1356
WOLFSSL_X509_LOOKUP* wolfSSL_X509_STORE_add_lookup(WOLFSSL_X509_STORE* store,
1357
                                               WOLFSSL_X509_LOOKUP_METHOD* m)
1358
{
1359
    WOLFSSL_ENTER("wolfSSL_X509_STORE_add_lookup");
1360
    if (store == NULL || m == NULL)
1361
        return NULL;
1362
1363
    /* Make sure the lookup has a back reference to the store. */
1364
    store->lookup.store = store;
1365
    /* store a type to know which method wants to be used for */
1366
    store->lookup.type = m->type;
1367
    return &store->lookup;
1368
}
1369
1370
static int X509StoreAddCa(WOLFSSL_X509_STORE* store,
1371
                                          WOLFSSL_X509* x509, int type)
1372
{
1373
    int result = WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR);
1374
    DerBuffer* derCert = NULL;
1375
1376
    WOLFSSL_ENTER("X509StoreAddCa");
1377
    if (store != NULL && x509 != NULL && x509->derCert != NULL) {
1378
        result = AllocDer(&derCert, x509->derCert->length,
1379
            x509->derCert->type, NULL);
1380
        if (result == 0) {
1381
            /* AddCA() frees the buffer. */
1382
            XMEMCPY(derCert->buffer,
1383
                            x509->derCert->buffer, x509->derCert->length);
1384
            result = AddCA(store->cm, &derCert, type, VERIFY);
1385
        }
1386
    }
1387
1388
    return result;
1389
}
1390
1391
1392
int wolfSSL_X509_STORE_add_cert(WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509)
1393
{
1394
    int result = WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR);
1395
1396
    WOLFSSL_ENTER("wolfSSL_X509_STORE_add_cert");
1397
    if (store != NULL && store->cm != NULL && x509 != NULL
1398
                                                && x509->derCert != NULL) {
1399
        /* Mimic the openssl behavior, must be self signed to be considered
1400
         * trusted, addCA() internals will do additional checks for
1401
         * CA=TRUE */
1402
        if (wolfSSL_X509_NAME_cmp(&x509->issuer, &x509->subject) == 0) {
1403
            result = X509StoreAddCa(store, x509, WOLFSSL_USER_CA);
1404
            if (result == WOLFSSL_SUCCESS && store->trusted != NULL) {
1405
                result = wolfSSL_X509_up_ref(x509);
1406
                if (result == WOLFSSL_SUCCESS) {
1407
                    result = wolfSSL_sk_X509_push(store->trusted, x509);
1408
                    if (result > 0) {
1409
                        result = WOLFSSL_SUCCESS;
1410
                    }
1411
                    else {
1412
                        result = WOLFSSL_FATAL_ERROR;
1413
                        wolfSSL_X509_free(x509);
1414
                        x509 = NULL;
1415
                    }
1416
                }
1417
            }
1418
        }
1419
        else {
1420
            if (store->certs != NULL) {
1421
                result = wolfSSL_X509_up_ref(x509);
1422
                if (result == WOLFSSL_SUCCESS) {
1423
                    result = wolfSSL_sk_X509_push(store->certs, x509);
1424
                    if (result > 0) {
1425
                        result = WOLFSSL_SUCCESS;
1426
                    }
1427
                    else {
1428
                        result = WOLFSSL_FATAL_ERROR;
1429
                        wolfSSL_X509_free(x509);
1430
                        x509 = NULL;
1431
                    }
1432
                }
1433
            }
1434
            else {
1435
                /* If store->certs is NULL, this is an X509_STORE managed by an
1436
                 * SSL_CTX, preserve behavior and always add as USER_CA */
1437
                result = X509StoreAddCa(
1438
                            store, x509, WOLFSSL_USER_CA);
1439
            }
1440
        }
1441
    }
1442
1443
    WOLFSSL_LEAVE("wolfSSL_X509_STORE_add_cert", result);
1444
1445
    if (result != WOLFSSL_SUCCESS) {
1446
        result = WOLFSSL_FATAL_ERROR;
1447
    }
1448
1449
    return result;
1450
}
1451
1452
int wolfSSL_X509_STORE_set_flags(WOLFSSL_X509_STORE* store, unsigned long flag)
1453
{
1454
    int ret = WOLFSSL_SUCCESS;
1455
1456
    WOLFSSL_ENTER("wolfSSL_X509_STORE_set_flags");
1457
1458
    if (store == NULL)
1459
        return WOLFSSL_FAILURE;
1460
1461
    if ((flag & WOLFSSL_CRL_CHECKALL) || (flag & WOLFSSL_CRL_CHECK)) {
1462
        ret = wolfSSL_CertManagerEnableCRL(store->cm, (int)flag);
1463
    }
1464
#if defined(OPENSSL_COMPATIBLE_DEFAULTS)
1465
    else if (flag == 0) {
1466
        ret = wolfSSL_CertManagerDisableCRL(store->cm);
1467
    }
1468
#endif
1469
    if (flag & WOLFSSL_PARTIAL_CHAIN) {
1470
        store->param->flags |= WOLFSSL_PARTIAL_CHAIN;
1471
    }
1472
    return ret;
1473
}
1474
1475
int X509StoreLoadCertBuffer(WOLFSSL_X509_STORE *str,
1476
                                        byte *buf, word32 bufLen, int type)
1477
{
1478
    int ret = WOLFSSL_SUCCESS;
1479
    WOLFSSL_X509 *x509 = NULL;
1480
1481
    if (str == NULL || buf == NULL) {
1482
        return WOLFSSL_FAILURE;
1483
    }
1484
1485
    /* OpenSSL X509_STORE_load_file fails on DER file, we will as well */
1486
    x509 = wolfSSL_X509_load_certificate_buffer(buf, bufLen, type);
1487
    if (x509 != NULL) {
1488
        ret = wolfSSL_X509_STORE_add_cert(str, x509);
1489
        if (ret != WOLFSSL_SUCCESS) {
1490
            WOLFSSL_MSG("Failed to load file");
1491
            ret = WOLFSSL_FAILURE;
1492
        }
1493
        if (ret == WOLFSSL_SUCCESS && str->owned != NULL) {
1494
            if (wolfSSL_sk_X509_push(str->owned, x509) <= 0) {
1495
                ret = WOLFSSL_FAILURE;
1496
            }
1497
            else {
1498
                x509 = NULL;
1499
            }
1500
        }
1501
        wolfSSL_X509_free(x509);
1502
        x509 = NULL;
1503
    }
1504
    else {
1505
        ret = WOLFSSL_FAILURE;
1506
    }
1507
1508
    return ret;
1509
}
1510
1511
#if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
1512
1513
static int X509StoreReadFile(const char *fname,
1514
                StaticBuffer *content, word32 *bytesRead, int *type)
1515
{
1516
    int ret = -1;
1517
    long sz = 0;
1518
#ifdef HAVE_CRL
1519
    const char* header = NULL;
1520
    const char* footer = NULL;
1521
#endif
1522
1523
    ret = wolfssl_read_file_static(fname, content, NULL, DYNAMIC_TYPE_FILE,
1524
        &sz);
1525
    if (ret == 0) {
1526
        *type = CERT_TYPE;
1527
        *bytesRead = (word32)sz;
1528
#ifdef HAVE_CRL
1529
        /* Look for CRL header and footer. */
1530
        if (wc_PemGetHeaderFooter(CRL_TYPE, &header, &footer) == 0 &&
1531
                (XSTRNSTR((char*)content->buffer, header, (word32)sz) !=
1532
                    NULL)) {
1533
            *type = CRL_TYPE;
1534
        }
1535
#endif
1536
    }
1537
1538
    return (ret == 0 ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE);
1539
}
1540
1541
static int X509StoreLoadFile(WOLFSSL_X509_STORE *str,
1542
                                        const char *fname)
1543
{
1544
    int ret = WOLFSSL_SUCCESS;
1545
    int type = 0;
1546
#ifndef WOLFSSL_SMALL_STACK
1547
    byte   stackBuffer[FILE_BUFFER_SIZE];
1548
#endif
1549
    StaticBuffer content;
1550
    word32 contentLen = 0;
1551
1552
#ifdef WOLFSSL_SMALL_STACK
1553
    static_buffer_init(&content);
1554
#else
1555
    static_buffer_init(&content, stackBuffer, FILE_BUFFER_SIZE);
1556
#endif
1557
1558
    WOLFSSL_MSG_EX("X509StoreLoadFile: Loading file: %s", fname);
1559
1560
    ret = X509StoreReadFile(fname, &content, &contentLen, &type);
1561
    if (ret != WOLFSSL_SUCCESS) {
1562
        WOLFSSL_MSG("Failed to load file");
1563
        ret = WOLFSSL_FAILURE;
1564
    }
1565
1566
    if ((ret == WOLFSSL_SUCCESS) && (type == CERT_TYPE)) {
1567
        ret = X509StoreLoadCertBuffer(str, content.buffer,
1568
                                        contentLen, WOLFSSL_FILETYPE_PEM);
1569
    }
1570
#ifdef HAVE_CRL
1571
    else if ((ret == WOLFSSL_SUCCESS) && (type == CRL_TYPE)) {
1572
        ret = BufferLoadCRL(str->cm->crl, content.buffer, contentLen,
1573
                                        WOLFSSL_FILETYPE_PEM, 0);
1574
    }
1575
#endif
1576
1577
    static_buffer_free(&content, NULL, DYNAMIC_TYPE_FILE);
1578
    return ret;
1579
}
1580
1581
/* Loads certificate(s) files in pem format into X509_STORE struct from either
1582
 * a file or directory.
1583
 * Returns WOLFSSL_SUCCESS on success or WOLFSSL_FAILURE if an error occurs.
1584
 */
1585
WOLFSSL_API int wolfSSL_X509_STORE_load_locations(WOLFSSL_X509_STORE *str,
1586
                                            const char *file, const char *dir)
1587
{
1588
    WOLFSSL_CTX* ctx;
1589
    char *name = NULL;
1590
    int ret = WOLFSSL_SUCCESS;
1591
#ifdef WOLFSSL_SMALL_STACK
1592
    ReadDirCtx* readCtx = NULL;
1593
#else
1594
    ReadDirCtx  readCtx[1];
1595
#endif
1596
1597
    WOLFSSL_ENTER("wolfSSL_X509_STORE_load_locations");
1598
1599
    if (str == NULL || str->cm == NULL || (file == NULL  && dir == NULL))
1600
        return WOLFSSL_FAILURE;
1601
1602
    /* tmp ctx for setting our cert manager */
1603
    ctx = wolfSSL_CTX_new_ex(cm_pick_method(str->cm->heap), str->cm->heap);
1604
    if (ctx == NULL)
1605
        return WOLFSSL_FAILURE;
1606
1607
    wolfSSL_CertManagerFree(ctx->cm);
1608
    ctx->cm = str->cm;
1609
1610
#ifdef HAVE_CRL
1611
    if (str->cm->crl == NULL) {
1612
        /* Workaround to allocate the internals to load CRL's but don't enable
1613
         * CRL checking by default */
1614
        if (wolfSSL_CertManagerEnableCRL(str->cm, WOLFSSL_CRL_CHECK)
1615
                != WOLFSSL_SUCCESS ||
1616
                wolfSSL_CertManagerDisableCRL(str->cm) != WOLFSSL_SUCCESS) {
1617
            WOLFSSL_MSG("Enable CRL failed");
1618
            wolfSSL_CTX_free(ctx);
1619
            return WOLFSSL_FAILURE;
1620
        }
1621
    }
1622
#endif
1623
1624
    /* Load individual file */
1625
    if (file) {
1626
        ret = X509StoreLoadFile(str, file);
1627
        if (ret != WOLFSSL_SUCCESS) {
1628
            WOLFSSL_MSG("Failed to load file");
1629
            ret = WOLFSSL_FAILURE;
1630
        }
1631
    }
1632
1633
    /* Load files in dir */
1634
    if (dir && ret == WOLFSSL_SUCCESS) {
1635
        int successes = 0;
1636
1637
        #ifdef WOLFSSL_SMALL_STACK
1638
            readCtx = (ReadDirCtx*)XMALLOC(sizeof(ReadDirCtx), ctx->heap,
1639
                                                    DYNAMIC_TYPE_TMP_BUFFER);
1640
            if (readCtx == NULL) {
1641
                WOLFSSL_MSG("Memory error");
1642
                wolfSSL_CTX_free(ctx);
1643
                return WOLFSSL_FAILURE;
1644
            }
1645
        #endif
1646
1647
        /* try to load each regular file in dir */
1648
        ret = wc_ReadDirFirst(readCtx, dir, &name);
1649
        while (ret == 0 && name) {
1650
            WOLFSSL_MSG(name);
1651
1652
            ret = X509StoreLoadFile(str, name);
1653
            /* Not failing on load errors */
1654
            if (ret != WOLFSSL_SUCCESS)
1655
                WOLFSSL_MSG("Failed to load file in path, continuing");
1656
            else
1657
                successes++;
1658
1659
            ret = wc_ReadDirNext(readCtx, dir, &name);
1660
        }
1661
        wc_ReadDirClose(readCtx);
1662
1663
        /* Success if at least one file in dir was loaded */
1664
        if (successes > 0)
1665
            ret = WOLFSSL_SUCCESS;
1666
        else {
1667
            WOLFSSL_ERROR(ret);
1668
            ret = WOLFSSL_FAILURE;
1669
        }
1670
1671
        #ifdef WOLFSSL_SMALL_STACK
1672
            XFREE(readCtx, ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
1673
        #endif
1674
    }
1675
1676
    ctx->cm = NULL;
1677
    wolfSSL_CTX_free(ctx);
1678
1679
    return ret;
1680
}
1681
1682
#if defined(XGETENV) && !defined(NO_GETENV)
1683
int wolfSSL_X509_STORE_set_default_paths(WOLFSSL_X509_STORE *str)
1684
{
1685
    int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE);
1686
    char* certDir = NULL;
1687
    char* certFile = NULL;
1688
1689
    WOLFSSL_ENTER("wolfSSL_X509_STORE_set_default_paths");
1690
1691
    certFile = wc_strdup_ex(XGETENV("SSL_CERT_FILE"), DYNAMIC_TYPE_TMP_BUFFER);
1692
    certDir = wc_strdup_ex(XGETENV("SSL_CERT_DIR"), DYNAMIC_TYPE_TMP_BUFFER);
1693
1694
    ret = wolfSSL_X509_STORE_load_locations(str, certFile, certDir);
1695
1696
    XFREE(certFile, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1697
    XFREE(certDir, NULL, DYNAMIC_TYPE_TMP_BUFFER);
1698
    return ret;
1699
}
1700
#endif /* XGETENV && !NO_GETENV */
1701
1702
#endif /* !NO_FILESYSTEM && !NO_WOLFSSL_DIR */
1703
1704
int wolfSSL_X509_CA_num(WOLFSSL_X509_STORE* store)
1705
{
1706
    int cnt_ret = 0;
1707
    Signer **table;
1708
1709
    WOLFSSL_ENTER("wolfSSL_X509_CA_num");
1710
    if (store == NULL || store->cm == NULL){
1711
        WOLFSSL_MSG("invalid parameter");
1712
        return WOLFSSL_FAILURE;
1713
    }
1714
1715
    table = store->cm->caTable;
1716
    if (table || (store->certs != NULL)){
1717
        if (wc_LockMutex(&store->cm->caLock) == 0){
1718
            if (table) {
1719
                int i = 0;
1720
                for (i = 0; i < CA_TABLE_SIZE; i++) {
1721
                    Signer* signer = table[i];
1722
                    while (signer) {
1723
                        Signer* next = signer->next;
1724
                        cnt_ret++;
1725
                        signer = next;
1726
                    }
1727
                }
1728
            }
1729
1730
            if (store->certs != NULL) {
1731
                cnt_ret += wolfSSL_sk_X509_num(store->certs);
1732
            }
1733
            wc_UnLockMutex(&store->cm->caLock);
1734
        }
1735
    }
1736
1737
    return cnt_ret;
1738
}
1739
1740
/******************************************************************************
1741
* wolfSSL_X509_STORE_GetCerts - retrieve stack of X509 in a certificate
1742
*                               store ctx
1743
*
1744
* This API can be used in SSL verify callback function to view cert chain
1745
* See examples/client/client.c and myVerify() function in test.h
1746
*
1747
* RETURNS:
1748
* returns stack of X509 certs on success, otherwise returns a NULL.
1749
*/
1750
WOLFSSL_STACK* wolfSSL_X509_STORE_GetCerts(WOLFSSL_X509_STORE_CTX* s)
1751
{
1752
    int  certIdx = 0;
1753
    WOLFSSL_BUFFER_INFO* cert = NULL;
1754
    DecodedCert* dCert = NULL;
1755
    WOLFSSL_X509* x509 = NULL;
1756
    WOLFSSL_STACK* sk = NULL;
1757
    int found = 0;
1758
1759
    if (s == NULL) {
1760
        return NULL;
1761
    }
1762
1763
    sk = wolfSSL_sk_X509_new_null();
1764
1765
    if (sk == NULL) {
1766
        return NULL;
1767
    }
1768
1769
    for (certIdx = s->totalCerts - 1; certIdx >= 0; certIdx--) {
1770
        /* get certificate buffer */
1771
        cert = &s->certs[certIdx];
1772
1773
        dCert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
1774
                                                DYNAMIC_TYPE_DCERT);
1775
1776
        if (dCert == NULL) {
1777
            goto error;
1778
        }
1779
        XMEMSET(dCert, 0, sizeof(DecodedCert));
1780
1781
        InitDecodedCert(dCert, cert->buffer, cert->length, NULL);
1782
1783
        /* Parse Certificate */
1784
        if (ParseCert(dCert, CERT_TYPE, NO_VERIFY, NULL)){
1785
            goto error;
1786
        }
1787
        x509 = wolfSSL_X509_new();
1788
1789
        if (x509 == NULL) {
1790
            goto error;
1791
        }
1792
        InitX509(x509, 1, NULL);
1793
1794
        if (CopyDecodedToX509(x509, dCert) == 0) {
1795
1796
            if (wolfSSL_sk_X509_push(sk, x509) <= 0) {
1797
                WOLFSSL_MSG("Unable to load x509 into stack");
1798
                wolfSSL_X509_free(x509);
1799
                x509 = NULL;
1800
                goto error;
1801
            }
1802
        }
1803
        else {
1804
            goto error;
1805
        }
1806
        found = 1;
1807
1808
        FreeDecodedCert(dCert);
1809
        XFREE(dCert, NULL, DYNAMIC_TYPE_DCERT);
1810
        dCert = NULL;
1811
    }
1812
1813
    if (!found) {
1814
        wolfSSL_sk_X509_pop_free(sk, NULL);
1815
        sk = NULL;
1816
    }
1817
    return sk;
1818
1819
error:
1820
    if (dCert) {
1821
        FreeDecodedCert(dCert);
1822
        XFREE(dCert, NULL, DYNAMIC_TYPE_DCERT);
1823
    }
1824
1825
    if (sk)
1826
        wolfSSL_sk_X509_pop_free(sk, NULL);
1827
1828
    return NULL;
1829
}
1830
#endif /* OPENSSL_EXTRA */
1831
1832
#ifdef OPENSSL_ALL
1833
WOLF_STACK_OF(WOLFSSL_X509_OBJECT)* wolfSSL_X509_STORE_get0_objects(
1834
    WOLFSSL_X509_STORE* store)
1835
{
1836
    WOLFSSL_STACK* ret = NULL;
1837
    WOLFSSL_STACK* cert_stack = NULL;
1838
#if ((defined(WOLFSSL_SIGNER_DER_CERT) && !defined(NO_FILESYSTEM)) || \
1839
     (defined(HAVE_CRL)))
1840
    WOLFSSL_X509_OBJECT* obj = NULL;
1841
#endif
1842
#if defined(WOLFSSL_SIGNER_DER_CERT) && !defined(NO_FILESYSTEM)
1843
    WOLFSSL_X509* x509 = NULL;
1844
    int i = 0;
1845
#endif
1846
    WOLFSSL_ENTER("wolfSSL_X509_STORE_get0_objects");
1847
1848
    if (store == NULL || store->cm == NULL) {
1849
        WOLFSSL_MSG("Missing or empty store");
1850
        return NULL;
1851
    }
1852
1853
    if (store->objs != NULL) {
1854
#if defined(WOLFSSL_SIGNER_DER_CERT) && !defined(NO_FILESYSTEM)
1855
        /* want to update objs stack by cm stack again before returning it*/
1856
        X509StoreFreeObjList(store, store->objs);
1857
        store->objs = NULL;
1858
#else
1859
        if (wolfSSL_sk_X509_OBJECT_num(store->objs) == 0) {
1860
            /* Let's try generating the stack again */
1861
            wolfSSL_sk_X509_OBJECT_pop_free(store->objs, NULL);
1862
            store->objs = NULL;
1863
        }
1864
        else
1865
            return store->objs;
1866
#endif
1867
    }
1868
1869
    if ((ret = wolfSSL_sk_X509_OBJECT_new()) == NULL) {
1870
        WOLFSSL_MSG("wolfSSL_sk_X509_OBJECT_new error");
1871
        goto err_cleanup;
1872
    }
1873
1874
#if defined(WOLFSSL_SIGNER_DER_CERT) && !defined(NO_FILESYSTEM)
1875
    cert_stack = wolfSSL_CertManagerGetCerts(store->cm);
1876
    store->numAdded = 0;
1877
    if (cert_stack == NULL && wolfSSL_sk_X509_num(store->certs) > 0) {
1878
        cert_stack = wolfSSL_sk_X509_new_null();
1879
        if (cert_stack == NULL) {
1880
            WOLFSSL_MSG("wolfSSL_sk_X509_OBJECT_new error");
1881
            goto err_cleanup;
1882
        }
1883
    }
1884
    for (i = 0; i < wolfSSL_sk_X509_num(store->certs); i++) {
1885
        if (wolfSSL_sk_X509_push(cert_stack,
1886
                             wolfSSL_sk_X509_value(store->certs, i)) > 0) {
1887
            store->numAdded++;
1888
        }
1889
    }
1890
    /* Do not modify stack until after we guarantee success to
1891
     * simplify cleanup logic handling cert merging above */
1892
    for (i = 0; i < wolfSSL_sk_X509_num(cert_stack); i++) {
1893
        x509 = (WOLFSSL_X509 *)wolfSSL_sk_value(cert_stack, i);
1894
        obj  = wolfSSL_X509_OBJECT_new();
1895
        if (obj == NULL) {
1896
            WOLFSSL_MSG("wolfSSL_X509_OBJECT_new error");
1897
            goto err_cleanup;
1898
        }
1899
        if (wolfSSL_sk_X509_OBJECT_push(ret, obj) <= 0) {
1900
            WOLFSSL_MSG("wolfSSL_sk_X509_OBJECT_push error");
1901
            wolfSSL_X509_OBJECT_free(obj);
1902
            goto err_cleanup;
1903
        }
1904
        obj->type = WOLFSSL_X509_LU_X509;
1905
        obj->data.x509 = x509;
1906
    }
1907
1908
    while (wolfSSL_sk_X509_num(cert_stack) > 0) {
1909
        wolfSSL_sk_X509_pop(cert_stack);
1910
    }
1911
#endif
1912
1913
#ifdef HAVE_CRL
1914
    if (store->cm->crl != NULL) {
1915
        int res;
1916
        obj = wolfSSL_X509_OBJECT_new();
1917
        if (obj == NULL) {
1918
            WOLFSSL_MSG("wolfSSL_X509_OBJECT_new error");
1919
            goto err_cleanup;
1920
        }
1921
        if (wolfSSL_sk_X509_OBJECT_push(ret, obj) <= 0) {
1922
            WOLFSSL_MSG("wolfSSL_sk_X509_OBJECT_push error");
1923
            wolfSSL_X509_OBJECT_free(obj);
1924
            goto err_cleanup;
1925
        }
1926
        obj->type = WOLFSSL_X509_LU_CRL;
1927
        wolfSSL_RefInc(&store->cm->crl->ref, &res);
1928
        if (res != 0) {
1929
            WOLFSSL_MSG("Failed to lock crl mutex");
1930
            goto err_cleanup;
1931
        }
1932
        obj->data.crl = store->cm->crl;
1933
    }
1934
#endif
1935
1936
    if (cert_stack)
1937
        wolfSSL_sk_X509_pop_free(cert_stack, NULL);
1938
    store->objs = ret;
1939
    return ret;
1940
err_cleanup:
1941
    if (ret != NULL)
1942
        X509StoreFreeObjList(store, ret);
1943
    if (cert_stack != NULL) {
1944
        while (store->numAdded > 0) {
1945
            wolfSSL_sk_X509_pop(cert_stack);
1946
            store->numAdded--;
1947
        }
1948
        wolfSSL_sk_X509_pop_free(cert_stack, NULL);
1949
    }
1950
    return NULL;
1951
}
1952
#endif /* OPENSSL_ALL */
1953
1954
#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
1955
    defined(WOLFSSL_WPAS_SMALL)
1956
WOLFSSL_X509_VERIFY_PARAM *wolfSSL_X509_STORE_get0_param(
1957
        const WOLFSSL_X509_STORE *ctx)
1958
{
1959
    if (ctx == NULL)
1960
        return NULL;
1961
    return ctx->param;
1962
}
1963
1964
#ifdef OPENSSL_EXTRA
1965
int wolfSSL_X509_STORE_set1_param(WOLFSSL_X509_STORE *ctx,
1966
        WOLFSSL_X509_VERIFY_PARAM *param)
1967
{
1968
    if (ctx == NULL)
1969
        return WOLFSSL_FAILURE;
1970
    return wolfSSL_X509_VERIFY_PARAM_set1(ctx->param, param);
1971
}
1972
#endif
1973
#endif
1974
1975
/******************************************************************************
1976
 * END OF X509_STORE APIs
1977
 *****************************************************************************/
1978
1979
#endif /* NO_CERTS */
1980
1981
#endif /* !WOLFCRYPT_ONLY */
1982
1983
#endif /* !WOLFSSL_X509_STORE_INCLUDED */
1984