Skip to content

Commit 9cdee20

Browse files
committed
ASN.1 print: implementation to parse and print added
New API to parse and print DER/BER data from a buffer. Add an example to parse DER, Base64 and PEM files and print out ASN.1 items.
1 parent f8559b7 commit 9cdee20

11 files changed

Lines changed: 1679 additions & 83 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ examples/sctp/sctp-server
7373
examples/sctp/sctp-server-dtls
7474
examples/sctp/sctp-client
7575
examples/sctp/sctp-client-dtls
76+
examples/asn1/asn1
7677
server_ready
7778
snifftest
7879
output

configure.ac

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4016,6 +4016,26 @@ else
40164016
ENABLED_BIGNUM="yes"
40174017
fi
40184018

4019+
case $host_os in
4020+
*linux* | *darwin* | *freebsd*)
4021+
DEF_ASN_PRINT="yes"
4022+
;;
4023+
*)
4024+
DEF_ASN_PRINT="no"
4025+
;;
4026+
esac
4027+
4028+
AC_ARG_ENABLE([asn-print],
4029+
[AS_HELP_STRING([--enable-asn-print],[Enable ASN Print API (default: enabled)])],
4030+
[ ENABLED_ASN_PRINT=$enableval ],
4031+
[ ENABLED_ASN_PRINT=$DEF_ASN_PRINT ]
4032+
)
4033+
4034+
if test "$ENABLED_ASN_PRINT" = "yes"
4035+
then
4036+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_ASN_PRINT"
4037+
fi
4038+
40194039

40204040
# AES
40214041
AC_ARG_ENABLE([aes],
@@ -8496,6 +8516,7 @@ AM_CONDITIONAL([BUILD_FASTMATH],[test "x$ENABLED_FASTMATH" = "xyes" || test "x$E
84968516
AM_CONDITIONAL([BUILD_HEAPMATH],[test "x$ENABLED_HEAPMATH" = "xyes" || test "x$ENABLED_USERSETTINGS" = "xyes"])
84978517
AM_CONDITIONAL([BUILD_EXAMPLE_SERVERS],[test "x$ENABLED_EXAMPLES" = "xyes" && test "x$ENABLED_LEANTLS" = "xno"])
84988518
AM_CONDITIONAL([BUILD_EXAMPLE_CLIENTS],[test "x$ENABLED_EXAMPLES" = "xyes"])
8519+
AM_CONDITIONAL([BUILD_EXAMPLE_ASN1],[test "x$ENABLED_EXAMPLES" = "xyes"] && [test "x$ENABLED_ASN_PRINT" = "xyes"] && [test "x$ENABLED_ASN" = "xyes"])
84998520
AM_CONDITIONAL([BUILD_TESTS],[test "x$ENABLED_EXAMPLES" = "xyes"])
85008521
AM_CONDITIONAL([BUILD_THREADED_EXAMPLES],[test "x$ENABLED_SINGLETHREADED" = "xno" && test "x$ENABLED_EXAMPLES" = "xyes" && test "x$ENABLED_LEANTLS" = "xno"])
85018522
AM_CONDITIONAL([BUILD_WOLFCRYPT_TESTS],[test "x$ENABLED_CRYPT_TESTS" = "xyes"])

doc/dox_comments/header_files/asn_public.h

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,3 +2167,148 @@ int wc_SetUnknownExtCallback(DecodedCert* cert,
21672167
int wc_CheckCertSigPubKey(const byte* cert, word32 certSz,
21682168
void* heap, const byte* pubKey,
21692169
word32 pubKeySz, int pubKeyOID);
2170+
2171+
/*!
2172+
\ingroup ASN
2173+
2174+
\brief This function initializes the ASN.1 print options.
2175+
2176+
\return 0 on success.
2177+
\return BAD_FUNC_ARG when asn1 is NULL.
2178+
2179+
\param opts The ASN.1 options for printing.
2180+
2181+
_Example_
2182+
\code
2183+
Asn1PrintOptions opt;
2184+
2185+
// Initialize ASN.1 print options before use.
2186+
wc_Asn1PrintOptions_Init(&opt);
2187+
\endcode
2188+
2189+
\sa wc_Asn1PrintOptions_Set
2190+
\sa wc_Asn1_PrintAll
2191+
*/
2192+
int wc_Asn1PrintOptions_Init(Asn1PrintOptions* opts);
2193+
2194+
/*!
2195+
\ingroup ASN
2196+
2197+
\brief This function sets a print option into an ASN.1 print options object.
2198+
2199+
\return 0 on success.
2200+
\return BAD_FUNC_ARG when asn1 is NULL.
2201+
\return BAD_FUNC_ARG when val is out of range for option.
2202+
2203+
\param opts The ASN.1 options for printing.
2204+
\param opt An option to set value for.
2205+
\param val The value to set.
2206+
2207+
_Example_
2208+
\code
2209+
Asn1PrintOptions opt;
2210+
2211+
// Initialize ASN.1 print options before use.
2212+
wc_Asn1PrintOptions_Init(&opt);
2213+
// Set the number of indents when printing tag name to be 1.
2214+
wc_Asn1PrintOptions_Set(&opt, ASN1_PRINT_OPT_INDENT, 1);
2215+
\endcode
2216+
2217+
\sa wc_Asn1PrintOptions_Init
2218+
\sa wc_Asn1_PrintAll
2219+
*/
2220+
int wc_Asn1PrintOptions_Set(Asn1PrintOptions* opts, enum Asn1PrintOpt opt,
2221+
word32 val);
2222+
2223+
/*!
2224+
\ingroup ASN
2225+
2226+
\brief This function initializes an ASN.1 parsing object.
2227+
2228+
\return 0 on success.
2229+
\return BAD_FUNC_ARG when asn1 is NULL.
2230+
2231+
\param asn1 ASN.1 parse object.
2232+
2233+
_Example_
2234+
\code
2235+
Asn1 asn1;
2236+
2237+
// Initialize ASN.1 parse object before use.
2238+
wc_Asn1_Init(&asn1);
2239+
\endcode
2240+
2241+
\sa wc_Asn1_SetFile
2242+
\sa wc_Asn1_PrintAll
2243+
*/
2244+
int wc_Asn1_Init(Asn1* asn1);
2245+
2246+
/*!
2247+
\ingroup ASN
2248+
2249+
\brief This function sets the file to use when printing into an ASN.1
2250+
parsing object.
2251+
2252+
\return 0 on success.
2253+
\return BAD_FUNC_ARG when asn1 is NULL.
2254+
\return BAD_FUNC_ARG when file is XBADFILE.
2255+
2256+
\param asn1 The ASN.1 parse object.
2257+
\param file File to print to.
2258+
2259+
_Example_
2260+
\code
2261+
Asn1 asn1;
2262+
2263+
// Initialize ASN.1 parse object before use.
2264+
wc_Asn1_Init(&asn1);
2265+
// Set standard out to be the file descriptor to write to.
2266+
wc_Asn1_SetFile(&asn1, stdout);
2267+
\endcode
2268+
2269+
\sa wc_Asn1_Init
2270+
\sa wc_Asn1_PrintAll
2271+
*/
2272+
int wc_Asn1_SetFile(Asn1* asn1, XFILE file);
2273+
2274+
/*!
2275+
\ingroup ASN
2276+
2277+
\brief Print all ASN.1 items.
2278+
2279+
\return 0 on success.
2280+
\return BAD_FUNC_ARG when asn1 or opts is NULL.
2281+
\return ASN_LEN_E when ASN.1 item's length too long.
2282+
\return ASN_DEPTH_E when end offset invalid.
2283+
\return ASN_PARSE_E when not all of an ASN.1 item parsed.
2284+
2285+
\param asn1 The ASN.1 parse object.
2286+
\param opts The ASN.1 print options.
2287+
\param data Buffer containing BER/DER data to print.
2288+
\param len Length of data to print in bytes.
2289+
2290+
\code
2291+
Asn1PrintOptions opts;
2292+
Asn1 asn1;
2293+
unsigned char data[] = { Initialize with DER/BER data };
2294+
word32 len = sizeof(data);
2295+
2296+
// Initialize ASN.1 print options before use.
2297+
wc_Asn1PrintOptions_Init(&opt);
2298+
// Set the number of indents when printing tag name to be 1.
2299+
wc_Asn1PrintOptions_Set(&opt, ASN1_PRINT_OPT_INDENT, 1);
2300+
2301+
// Initialize ASN.1 parse object before use.
2302+
wc_Asn1_Init(&asn1);
2303+
// Set standard out to be the file descriptor to write to.
2304+
wc_Asn1_SetFile(&asn1, stdout);
2305+
// Print all ASN.1 items in buffer with the specified print options.
2306+
wc_Asn1_PrintAll(&asn1, &opts, data, len);
2307+
\endcode
2308+
2309+
\sa wc_Asn1_Init
2310+
\sa wc_Asn1_SetFile
2311+
*/
2312+
int wc_Asn1_PrintAll(Asn1* asn1, Asn1PrintOptions* opts, unsigned char* data,
2313+
word32 len);
2314+

0 commit comments

Comments
 (0)