• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include "curlcheck.h"
25 
26 #include "vtls/x509asn1.h"
27 
unit_setup(void)28 static CURLcode unit_setup(void)
29 {
30   return CURLE_OK;
31 }
32 
unit_stop(void)33 static void unit_stop(void)
34 {
35 
36 }
37 
38 #if defined(USE_GNUTLS) || defined(USE_SCHANNEL) || defined(USE_SECTRANSP) || \
39   defined(USE_MBEDTLS)
40 
41 #ifndef ARRAYSIZE
42 #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
43 #endif
44 
45 struct test1657_spec {
46   CURLcode (*setbuf)(struct test1657_spec *spec, struct dynbuf *buf);
47   size_t n;
48   CURLcode exp_result;
49 };
50 
make1657_nested(struct test1657_spec * spec,struct dynbuf * buf)51 static CURLcode make1657_nested(struct test1657_spec *spec, struct dynbuf *buf)
52 {
53   CURLcode r;
54   size_t i;
55   unsigned char open_undef[] = { 0x32,  0x80 };
56   unsigned char close_undef[] = { 0x00,  0x00 };
57 
58   for(i = 0; i < spec->n; ++i) {
59     r = Curl_dyn_addn(buf, open_undef, sizeof(open_undef));
60     if(r)
61       return r;
62   }
63   for(i = 0; i < spec->n; ++i) {
64     r = Curl_dyn_addn(buf, close_undef, sizeof(close_undef));
65     if(r)
66       return r;
67   }
68   return CURLE_OK;
69 }
70 
71 static struct test1657_spec test1657_specs[] = {
72   { make1657_nested, 3, CURLE_OK },
73   { make1657_nested, 16, CURLE_OK },
74   { make1657_nested, 17, CURLE_BAD_FUNCTION_ARGUMENT },
75   { make1657_nested, 1024, CURLE_BAD_FUNCTION_ARGUMENT },
76 };
77 
do_test1657(struct test1657_spec * spec,size_t i,struct dynbuf * buf)78 static bool do_test1657(struct test1657_spec *spec, size_t i,
79                         struct dynbuf *buf)
80 {
81   CURLcode result;
82   struct Curl_asn1Element elem;
83   const char *in;
84 
85   memset(&elem, 0, sizeof(elem));
86   Curl_dyn_reset(buf);
87   result = spec->setbuf(spec, buf);
88   if(result) {
89     fprintf(stderr, "test %zu: error setting buf %d\n", i, result);
90     return FALSE;
91   }
92   in = Curl_dyn_ptr(buf);
93   result = Curl_x509_getASN1Element(&elem, in, in + Curl_dyn_len(buf));
94   if(result != spec->exp_result) {
95     fprintf(stderr, "test %zu: expect result %d, got %d\n",
96             i, spec->exp_result, result);
97     return FALSE;
98   }
99   return TRUE;
100 }
101 
102 UNITTEST_START
103 {
104   size_t i;
105   bool all_ok = TRUE;
106   struct dynbuf dbuf;
107 
108   Curl_dyn_init(&dbuf, 32*1024);
109 
110   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
111     fprintf(stderr, "curl_global_init() failed\n");
112     return TEST_ERR_MAJOR_BAD;
113   }
114 
115   for(i = 0; i < ARRAYSIZE(test1657_specs); ++i) {
116     if(!do_test1657(&test1657_specs[i], i, &dbuf))
117       all_ok = FALSE;
118   }
119   fail_unless(all_ok, "some tests of Curl_x509_getASN1Element() fails");
120 
121   Curl_dyn_free(&dbuf);
122   curl_global_cleanup();
123 }
124 UNITTEST_STOP
125 
126 #else
127 
128 UNITTEST_START
129 {
130   puts("not tested since Curl_x509_getASN1Element() is not built-in");
131 }
132 UNITTEST_STOP
133 
134 #endif
135