Merge pull request #8655 from SparkiDev/asn1_oid_update

ASN.1 OIDs and sum: Change algorithm for sum
This commit is contained in:
David Garske
2025-05-07 11:43:54 -07:00
committed by GitHub
22 changed files with 20748 additions and 627 deletions

View File

@@ -34,6 +34,8 @@
#if defined(WOLFSSL_ASN_PRINT) && !defined(NO_FILESYSTEM)
#include "oid_names.h"
/* Increment allocated data by this much. */
#define DATA_INC_LEN 256
@@ -50,6 +52,20 @@ static Asn1PrintOptions opts;
/* ASN.1 parsing state. */
static Asn1 asn1;
static const char* asn1App_OidToName(unsigned char* oid, word32 len)
{
int i;
for (i = 0; i < asn1App_oid_names_len; i++) {
if ((len == asn1App_oid_name[i].len) &&
(XMEMCMP(oid, asn1App_oid_name[i].oid, len) == 0)) {
return asn1App_oid_name[i].name;
}
}
return NULL;
}
/* Read the contents of a file into a dynamically allocated buffer.
*
* Uses realloc as input may be stdin.
@@ -65,9 +81,10 @@ static int asn1App_ReadFile(FILE* fp, unsigned char** pdata, word32* plen)
int ret = 0;
word32 len = 0;
size_t read_len;
/* Allocate a minimum amount. */
unsigned char* data = (unsigned char*)XMALLOC(DATA_INC_LEN, NULL, DYNAMIC_TYPE_TMP_BUFFER);
unsigned char* data;
/* Allocate a minimum amount. */
data = (unsigned char*)XMALLOC(DATA_INC_LEN, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (data != NULL) {
/* Read more data. */
while ((read_len = fread(data + len, 1, DATA_INC_LEN, fp)) != 0) {
@@ -87,7 +104,8 @@ static int asn1App_ReadFile(FILE* fp, unsigned char** pdata, word32* plen)
}
/* Make space for more data to be added to buffer. */
p = (unsigned char*)XREALLOC(data, len + DATA_INC_LEN, NULL, DYNAMIC_TYPE_TMP_BUFFER);
p = (unsigned char*)XREALLOC(data, len + DATA_INC_LEN, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (p == NULL) {
/* Reallocation failed - free current buffer. */
XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
@@ -299,6 +317,7 @@ const char* usage[] = {
" -B, --base64 file contents are Base64 encoded",
#endif
" -d, --dump show all ASN.1 item data as a hex dump",
" -D, --der file format is DER",
" -h, --headers show all ASN.1 item headers as a hex dump",
" -i, --indent indent tag name with depth",
" -l, --length LEN display length bytes of data",
@@ -340,6 +359,7 @@ int main(int argc, char* argv[])
int file_format = FORMAT_DER;
word32 indent = 0;
int pem_skip = 0;
int format_set = 0;
/* Reset options. */
(void)wc_Asn1PrintOptions_Init(&opts);
@@ -365,6 +385,11 @@ int main(int argc, char* argv[])
(strcmp(argv[0], "--dump") == 0)) {
wc_Asn1PrintOptions_Set(&opts, ASN1_PRINT_OPT_SHOW_DATA, 1);
}
else if ((strcmp(argv[0], "-D") == 0) ||
(strcmp(argv[0], "--der") == 0)) {
file_format = FORMAT_DER;
format_set = 1;
}
/* Dump ASN.1 item headers. */
else if ((strcmp(argv[0], "-h") == 0) ||
(strcmp(argv[0], "--headers") == 0)) {
@@ -421,6 +446,7 @@ int main(int argc, char* argv[])
else if ((strcmp(argv[0], "-p") == 0) ||
(strcmp(argv[0], "--pem") == 0)) {
file_format = FORMAT_PEM;
format_set = 1;
}
#endif
/* Skip a number of PEM blocks. */
@@ -451,10 +477,25 @@ int main(int argc, char* argv[])
return 1;
}
else {
int nameLen;
if (fp != stdin) {
fprintf(stderr, "At most one input file can be supplied.\n");
return 1;
}
if (!format_set) {
nameLen = (int)XSTRLEN(argv[0]);
if (nameLen > 3) {
if (XMEMCMP(argv[0] + nameLen - 4, ".pem", 4) == 0) {
file_format = FORMAT_PEM;
}
else if (XMEMCMP(argv[0] + nameLen - 4, ".der", 4) == 0) {
file_format = FORMAT_DER;
}
}
}
/* Name of file to read. */
fp = fopen(argv[0], "r");
if (fp == NULL) {
@@ -472,6 +513,7 @@ int main(int argc, char* argv[])
(void)wc_Asn1_Init(&asn1);
(void)wc_Asn1_SetFile(&asn1, stdout);
(void)wc_Asn1_SetOidToNameCb(&asn1, asn1App_OidToName);
/* Process file based on type. */
if (file_format == FORMAT_DER) {

11603
examples/asn1/dumpasn1.cfg Normal file

File diff suppressed because it is too large Load Diff

137
examples/asn1/gen_oid_names.rb Executable file
View File

@@ -0,0 +1,137 @@
#!/usr/bin/ruby
class OidName
def initialize(oid, name)
@oid = oid
@name = name
end
def der_to_str(d)
s = "(byte*)\""
d.each do |b|
s += sprintf("\\x%02x", b)
end
s + "\""
end
def write()
puts <<EOF
{ #{der_to_str(@oid)}, #{@oid.length},
"#{@name.gsub(/\"/, '\\"')}" },
EOF
end
end
class OidNames
def initialize()
@oid_name = []
end
def decode_dotted(oid)
i = 0
n = 0
der = []
oid.split(/ /).each do |s|
t = s.to_i
i += 1
if i == 1
n = t * 40
next
elsif i == 2
n += t
else
n = t
end
if n == 0
der << 0
end
tmp = []
bit = 0;
while n > 0
tmp << ((n & 0x7f) | bit)
n >>= 7
bit = 0x80
end
der += tmp.reverse
end
der
end
def add(oid, name)
@oid_name << OidName.new(decode_dotted(oid), name)
end
def write_struct()
puts <<EOF
typedef struct asn1App_OidName {
byte* oid;
word32 len;
const char* name;
} asn1App_OidName;
EOF
end
def write()
puts <<EOF
/* oid_names.h
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/* Generated using (from wolfssl):
* cd examples/asn1
* ruby ./gen_oid_names.rb dumpasn1.cfg > oid_names.h
*/
EOF
puts
write_struct()
puts
puts "static asn1App_OidName asn1App_oid_name[#{@oid_name.length}] = {"
@oid_name.each do |o|
o.write()
end
puts "};"
puts
puts "int asn1App_oid_names_len = #{@oid_name.length};"
puts
end
end
oid = ""
oidNames = OidNames.new()
File.readlines(ARGV[0]).each do |l|
next if l.length == 0
next if l[0] == '#'
var, value = l.split(/ = /)
case var
when /OID/
oid = value
when /Description/
oidNames.add(oid, value.strip)
end
end
oidNames.write()

View File

@@ -8,5 +8,9 @@ noinst_PROGRAMS += examples/asn1/asn1
examples_asn1_asn1_SOURCES = examples/asn1/asn1.c
examples_asn1_asn1_LDADD = src/libwolfssl@LIBSUFFIX@.la $(LIB_STATIC_ADD)
examples_asn1_asn1_DEPENDENCIES = src/libwolfssl@LIBSUFFIX@.la
EXTRA_DIST += examples/asn1/oid_names.h \
examples/asn1/dumpasn1.cfg \
examples/asn1/gen_oid_names.rb
endif

5572
examples/asn1/oid_names.h Normal file

File diff suppressed because it is too large Load Diff