QSC Post Quantum Cryptographic Library 1.1.0.2 (B2)
A post quantum secure library written in Ansi C
Loading...
Searching...
No Matches
asn1.h
Go to the documentation of this file.
1/* 2020-2026 Quantum Resistant Cryptographic Solutions Corporation
2 * All Rights Reserved.
3 *
4 * NOTICE:
5 * This software and all accompanying materials are the exclusive property of
6 * Quantum Resistant Cryptographic Solutions Corporation (QRCS). The intellectual
7 * and technical concepts contained herein are proprietary to QRCS and are
8 * protected under applicable Canadian, U.S., and international copyright,
9 * patent, and trade secret laws.
10 *
11 * CRYPTOGRAPHIC ALGORITHMS AND IMPLEMENTATIONS:
12 * - This software includes implementations of cryptographic primitives and
13 * algorithms that are standardized or in the public domain, such as AES
14 * and SHA-3, which are not proprietary to QRCS.
15 * - This software also includes cryptographic primitives, constructions, and
16 * algorithms designed by QRCS, including but not limited to RCS, SCB, CSX, QMAC, and
17 * related components, which are proprietary to QRCS.
18 * - All source code, implementations, protocol compositions, optimizations,
19 * parameter selections, and engineering work contained in this software are
20 * original works of QRCS and are protected under this license.
21 *
22 * LICENSE AND USE RESTRICTIONS:
23 * - This software is licensed under the Quantum Resistant Cryptographic Solutions
24 * Public Research and Evaluation License (QRCS-PREL), 2025-2026.
25 * - Permission is granted solely for non-commercial evaluation, academic research,
26 * cryptographic analysis, interoperability testing, and feasibility assessment.
27 * - Commercial use, production deployment, commercial redistribution, or
28 * integration into products or services is strictly prohibited without a
29 * separate written license agreement executed with QRCS.
30 * - Licensing and authorized distribution are solely at the discretion of QRCS.
31 *
32 * EXPERIMENTAL CRYPTOGRAPHY NOTICE:
33 * Portions of this software may include experimental, novel, or evolving
34 * cryptographic designs. Use of this software is entirely at the user's risk.
35 *
36 * DISCLAIMER:
37 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38 * IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS
39 * FOR A PARTICULAR PURPOSE, SECURITY, OR NON-INFRINGEMENT. QRCS DISCLAIMS ALL
40 * LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
41 * ARISING FROM THE USE OR MISUSE OF THIS SOFTWARE.
42 *
43 * FULL LICENSE:
44 * This software is subject to the Quantum Resistant Cryptographic Solutions
45 * Public Research and Evaluation License (QRCS-PREL), 2025-2026. The complete license terms
46 * are provided in the accompanying LICENSE file or at https://www.qrcscorp.ca.
47 *
48 * Written by: John G. Underhill
49 * Contact: contact@qrcscorp.ca
50 */
51
52#ifndef QSC_ASN1_H
53#define QSC_ASN1_H
54
55#include "qsccommon.h"
56#include "encoding.h"
57
58QSC_CPLUSPLUS_ENABLED_START
59
90
95#define QSC_ASN1_OID_MAX_SIZE 32U
96
101#define QSC_ASN1_OID_MAX_ARCS 16U
102
107#define QSC_ASN1_BIT_STRING_MAX_UNUSED_BITS 7U
108
125
140
145{
146 const uint8_t* data;
147 size_t length;
148 uint8_t unused;
150
155{
156 uint16_t year;
157 uint8_t month;
158 uint8_t day;
159 uint8_t hour;
160 uint8_t minute;
161 uint8_t second;
164
180QSC_EXPORT_API qsc_asn1_status qsc_asn1_der_decode_exact(const uint8_t* der, size_t derlen, qsc_encoding_ber_element** element);
181
200QSC_EXPORT_API qsc_asn1_status qsc_asn1_der_get_child_region(const uint8_t* der, size_t derlen, size_t index, const uint8_t** child, size_t* childlen);
201
215
232QSC_EXPORT_API qsc_asn1_status qsc_asn1_require_sequence(const qsc_encoding_ber_element* element, size_t minchildren, size_t maxchildren);
233
247
259
271
285
297
309
320QSC_EXPORT_API bool qsc_asn1_element_is_tag(const qsc_encoding_ber_element* element, uint8_t tagclass, bool constructed, uint32_t tagnumber);
321
332QSC_EXPORT_API qsc_asn1_status qsc_asn1_require_tag(const qsc_encoding_ber_element* element, uint8_t tagclass, bool constructed, uint32_t tagnumber);
333
342
352
362
372
382
392
403QSC_EXPORT_API qsc_asn1_status qsc_asn1_decode_octet_string(const qsc_encoding_ber_element* element, uint8_t* output, size_t otplen, size_t* outlen);
404
414
423
433
443
453QSC_EXPORT_API qsc_asn1_status qsc_asn1_oid_to_string(const qsc_asn1_oid* oid, char* output, size_t otplen);
454
472QSC_EXPORT_API qsc_asn1_status qsc_asn1_decode_string(const qsc_encoding_ber_element* element, char* output, size_t otplen, size_t* outlen);
473
483
493
509
510QSC_CPLUSPLUS_ENABLED_END
511
512#endif
QSC_EXPORT_API qsc_asn1_status qsc_asn1_decode_time(const qsc_encoding_ber_element *element, qsc_asn1_time *value)
Decodes a UTCTime or GeneralizedTime value to a normalized structure.
Definition asn1.c:1060
enum qsc_asn1_status_t qsc_asn1_status
ASN.1 helper status codes.
QSC_EXPORT_API qsc_asn1_status qsc_asn1_der_decode_exact(const uint8_t *der, size_t derlen, qsc_encoding_ber_element **element)
Decode a complete DER element and reject trailing octets.
Definition asn1.c:268
#define QSC_ASN1_OID_MAX_ARCS
The maximum number of arcs stored in the decoded OBJECT IDENTIFIER structure.
Definition asn1.h:101
QSC_EXPORT_API struct qsc_asn1_bit_string_t qsc_asn1_bit_string
A normalized ASN.1 BIT STRING representation.
QSC_EXPORT_API bool qsc_asn1_is_integer(const qsc_encoding_ber_element *element)
Tests whether an ASN.1 element is an INTEGER.
Definition asn1.c:462
QSC_EXPORT_API bool qsc_asn1_oid_are_equal(const qsc_asn1_oid *a, const qsc_asn1_oid *b)
Compares two decoded OBJECT IDENTIFIER values for equality.
Definition asn1.c:931
QSC_EXPORT_API struct qsc_asn1_time_t qsc_asn1_time
A normalized ASN.1 time representation.
QSC_EXPORT_API qsc_asn1_status qsc_asn1_decode_uint64(const qsc_encoding_ber_element *element, uint64_t *value)
Decodes a non-negative INTEGER into a 64-bit unsigned integer.
Definition asn1.c:681
QSC_EXPORT_API qsc_asn1_status qsc_asn1_decode_octet_string(const qsc_encoding_ber_element *element, uint8_t *output, size_t otplen, size_t *outlen)
Copies the contents of an OCTET STRING into a caller-provided buffer.
Definition asn1.c:731
QSC_EXPORT_API qsc_asn1_status qsc_asn1_decode_string(const qsc_encoding_ber_element *element, char *output, size_t otplen, size_t *outlen)
Copies the contents of a supported ASN.1 string type into a character buffer.
Definition asn1.c:997
QSC_EXPORT_API bool qsc_asn1_is_boolean(const qsc_encoding_ber_element *element)
Tests whether an ASN.1 element is a BOOLEAN.
Definition asn1.c:446
QSC_EXPORT_API qsc_asn1_status qsc_asn1_decode_integer_u64(const qsc_encoding_ber_element *element, uint64_t *value)
Decodes an ASN.1 INTEGER into a 64-bit unsigned value.
Definition asn1.c:478
QSC_EXPORT_API bool qsc_asn1_oid_compare(const qsc_asn1_oid *a, const qsc_asn1_oid *b)
Compares two decoded ASN.1 OBJECT IDENTIFIER values.
Definition asn1.c:384
QSC_EXPORT_API qsc_asn1_status qsc_asn1_der_size(const qsc_encoding_ber_element *element, size_t *size)
Computes the encoded DER size of a decoded ASN.1 element.
Definition asn1.c:1245
QSC_EXPORT_API qsc_asn1_status qsc_asn1_decode_bit_string(const qsc_encoding_ber_element *element, qsc_asn1_bit_string *value)
Decodes a BIT STRING to a normalized descriptor.
Definition asn1.c:774
QSC_EXPORT_API bool qsc_asn1_element_is_tag(const qsc_encoding_ber_element *element, uint8_t tagclass, bool constructed, uint32_t tagnumber)
Tests whether an ASN.1 element matches an expected tag description.
Definition asn1.c:527
QSC_EXPORT_API qsc_asn1_status qsc_asn1_decode_null(const qsc_encoding_ber_element *element)
Validates that an ASN.1 NULL element is correctly encoded.
Definition asn1.c:827
QSC_EXPORT_API struct qsc_asn1_oid_t qsc_asn1_oid
A decoded ASN.1 OBJECT IDENTIFIER.
QSC_EXPORT_API bool qsc_asn1_is_oid(const qsc_encoding_ber_element *element)
Tests whether an ASN.1 element is an OBJECT IDENTIFIER.
Definition asn1.c:511
QSC_EXPORT_API qsc_asn1_status qsc_asn1_oid_to_string(const qsc_asn1_oid *oid, char *output, size_t otplen)
Converts a decoded OBJECT IDENTIFIER to dotted-decimal text.
Definition asn1.c:948
QSC_EXPORT_API const qsc_encoding_ber_element * qsc_asn1_child_at(const qsc_encoding_ber_element *element, size_t index)
Gets a child element by index.
Definition asn1.c:576
QSC_EXPORT_API qsc_asn1_status qsc_asn1_require_sequence(const qsc_encoding_ber_element *element, size_t minchildren, size_t maxchildren)
Validates that an element is a constructed ASN.1 SEQUENCE.
Definition asn1.c:401
qsc_asn1_status_t
ASN.1 helper status codes.
Definition asn1.h:113
@ QSC_ASN1_STATUS_INVALID_INPUT
Definition asn1.h:116
@ QSC_ASN1_STATUS_FAILURE
Definition asn1.h:115
@ QSC_ASN1_STATUS_SUCCESS
Definition asn1.h:114
@ QSC_ASN1_STATUS_INVALID_TAG
Definition asn1.h:117
@ QSC_ASN1_STATUS_BUFFER_TOO_SMALL
Definition asn1.h:120
@ QSC_ASN1_STATUS_UNSUPPORTED
Definition asn1.h:123
@ QSC_ASN1_STATUS_INVALID_LENGTH
Definition asn1.h:118
@ QSC_ASN1_STATUS_OUT_OF_RANGE
Definition asn1.h:121
@ QSC_ASN1_STATUS_NOT_FOUND
Definition asn1.h:122
@ QSC_ASN1_STATUS_INVALID_ENCODING
Definition asn1.h:119
QSC_EXPORT_API qsc_asn1_status qsc_asn1_decode_boolean(const qsc_encoding_ber_element *element, bool *value)
Decodes a BOOLEAN value.
Definition asn1.c:649
QSC_EXPORT_API qsc_asn1_status qsc_asn1_decode_oid(const qsc_encoding_ber_element *element, qsc_asn1_oid *oid)
Decodes an OBJECT IDENTIFIER value.
Definition asn1.c:851
QSC_EXPORT_API int32_t qsc_asn1_time_compare(const qsc_asn1_time *a, const qsc_asn1_time *b)
Compares two normalized ASN.1 times.
Definition asn1.c:1205
QSC_EXPORT_API size_t qsc_asn1_child_count(const qsc_encoding_ber_element *element)
Gets the number of child elements in a constructed ASN.1 element.
Definition asn1.c:560
QSC_EXPORT_API bool qsc_asn1_is_null(const qsc_encoding_ber_element *element)
Tests whether an ASN.1 element is a NULL value.
Definition asn1.c:495
#define QSC_ASN1_OID_MAX_SIZE
The maximum number of encoded OBJECT IDENTIFIER bytes supported by the helper layer.
Definition asn1.h:95
QSC_EXPORT_API qsc_asn1_status qsc_asn1_require_tag(const qsc_encoding_ber_element *element, uint8_t tagclass, bool constructed, uint32_t tagnumber)
Validates that an ASN.1 element matches an expected tag description.
Definition asn1.c:543
QSC_EXPORT_API const qsc_encoding_ber_element * qsc_asn1_get_child(const qsc_encoding_ber_element *element, size_t index)
Retrieves a child element from a constructed ASN.1 element.
Definition asn1.c:428
QSC_EXPORT_API qsc_asn1_status qsc_asn1_get_explicit_child(const qsc_encoding_ber_element *element, const qsc_encoding_ber_element **child)
Gets the sole child of an explicitly tagged context-specific element.
Definition asn1.c:620
QSC_EXPORT_API const qsc_encoding_ber_element * qsc_asn1_find_context_child(const qsc_encoding_ber_element *element, uint32_t tagnumber)
Gets an optional context-specific child by tag number.
Definition asn1.c:592
QSC_EXPORT_API qsc_asn1_status qsc_asn1_der_get_child_region(const uint8_t *der, size_t derlen, size_t index, const uint8_t **child, size_t *childlen)
Returns the exact encoded DER slice of a child element within a parent element.
Definition asn1.c:301
Encoding and decoding functions for Base64, Hex, BER/DER, and PEM.
Contains common definitions for the Quantum Secure Cryptographic (QSC) library.
#define QSC_EXPORT_API
API export macro for Microsoft compilers when importing from a DLL.
Definition qsccommon.h:605
A normalized ASN.1 BIT STRING representation.
Definition asn1.h:145
const uint8_t * data
Definition asn1.h:146
size_t length
Definition asn1.h:147
uint8_t unused
Definition asn1.h:148
A decoded ASN.1 OBJECT IDENTIFIER.
Definition asn1.h:134
uint8_t data[QSC_ASN1_OID_MAX_SIZE]
Definition asn1.h:135
size_t arcscount
Definition asn1.h:138
uint32_t arcs[QSC_ASN1_OID_MAX_ARCS]
Definition asn1.h:136
size_t length
Definition asn1.h:137
A normalized ASN.1 time representation.
Definition asn1.h:155
uint8_t month
Definition asn1.h:157
uint16_t year
Definition asn1.h:156
uint8_t day
Definition asn1.h:158
uint8_t second
Definition asn1.h:161
uint8_t minute
Definition asn1.h:160
bool generalized
Definition asn1.h:162
uint8_t hour
Definition asn1.h:159
In-memory representation of a single BER/DER ASN.1 TLV element.
Definition encoding.h:311