import x509/pkcs7 parsers from linux
Subsystems (6)
cover import x509/pkcs7 parsers from linux
Asn1 parsers of x509 certificates and pkcs7 messages are required to implement image authentication and variable authentication as part of UEFI secure boot feature.
As we discussed before in the thread[1], most people insisted that we should re-use corresponding source code from Linux repository for this purpose.
Here is my attempt to import all the necessary files from Linux; Those will eventually be part of UEFI secure boot implementation, but I'd like to get early feedback from other peoples before submitting the whole patchset so that they will be better formatted for merging.
My approach here is
* files from Linux 5.0
(will be updated to the latest when I will submit them as finalized
patches.)
* modify files as little as possible
* mark/protect unavoidable changes with "#if(n)def __UBOOT__" so that future fixes/differences in Linux repository will easily be applied to U-Boot.
Please note that checkpatch.pl will complain with a bunch of warnings/errors but I intentionally left them unchanged for the sake of better maintainability I said above.
Any comments will be appreciated.
-Takahiro Akashi
[1] https://lists.denx.de/pipermail/u-boot/2019-April/366423.html
AKASHI Takahiro (9):
cmd: add asn1_compiler
Makefile: add build script for asn1 parsers
lib: add asn1 decoder
lib: add oid registry utility
lib: crypto: add public key utility
lib: crypto: add public_key_verify_signature()
lib: crypto: add x509 parser
lib: crypto: add pkcs7 message parser
lib: crypto: add rsa public key parser
cmd/Kconfig | 3 + include/crypto/internal/rsa.h | 62 ++ include/crypto/pkcs7.h | 51 + include/crypto/public_key.h | 89 ++ include/keys/asymmetric-type.h | 92 ++ include/linux/asn1.h | 69 ++ include/linux/asn1_ber_bytecode.h | 93 ++ include/linux/asn1_decoder.h | 24 + include/linux/oid_registry.h | 103 ++ lib/Kconfig | 12 + lib/Makefile | 18 + lib/asn1_decoder.c | 520 ++++++++++ lib/build_OID_registry | 207 ++++ lib/crypto/Kconfig | 38 + lib/crypto/Makefile | 46 + lib/crypto/asymmetric_type.c | 655 ++++++++++++ lib/crypto/pkcs7.asn1 | 135 +++ lib/crypto/pkcs7_parser.c | 690 ++++++++++++ lib/crypto/pkcs7_parser.h | 69 ++ lib/crypto/public_key.c | 344 ++++++ lib/crypto/public_key_local.c | 69 ++ lib/crypto/rsa_helper.c | 81 ++ lib/crypto/rsapubkey.asn1 | 4 + lib/crypto/x509.asn1 | 60 ++ lib/crypto/x509_akid.asn1 | 35 + lib/crypto/x509_cert_parser.c | 644 ++++++++++++ lib/crypto/x509_parser.h | 61 ++ lib/crypto/x509_public_key.c | 284 +++++ lib/oid_registry.c | 178 ++++ scripts/Makefile | 3 + scripts/Makefile.build | 2 +- scripts/asn1_compiler.c | 1615 +++++++++++++++++++++++++++++ 32 files changed, 6355 insertions(+), 1 deletion(-) create mode 100644 include/crypto/internal/rsa.h create mode 100644 include/crypto/pkcs7.h create mode 100644 include/crypto/public_key.h create mode 100644 include/keys/asymmetric-type.h create mode 100644 include/linux/asn1.h create mode 100644 include/linux/asn1_ber_bytecode.h create mode 100644 include/linux/asn1_decoder.h create mode 100644 include/linux/oid_registry.h create mode 100644 lib/asn1_decoder.c create mode 100755 lib/build_OID_registry create mode 100644 lib/crypto/Kconfig create mode 100644 lib/crypto/Makefile create mode 100644 lib/crypto/asymmetric_type.c create mode 100644 lib/crypto/pkcs7.asn1 create mode 100644 lib/crypto/pkcs7_parser.c create mode 100644 lib/crypto/pkcs7_parser.h create mode 100644 lib/crypto/public_key.c create mode 100644 lib/crypto/public_key_local.c create mode 100644 lib/crypto/rsa_helper.c create mode 100644 lib/crypto/rsapubkey.asn1 create mode 100644 lib/crypto/x509.asn1 create mode 100644 lib/crypto/x509_akid.asn1 create mode 100644 lib/crypto/x509_cert_parser.c create mode 100644 lib/crypto/x509_parser.h create mode 100644 lib/crypto/x509_public_key.c create mode 100644 lib/oid_registry.c create mode 100644 scripts/asn1_compiler.c
Signature · 1 line
2.21.0
RFC 1/9 cmd: add asn1_compiler
Imported from linux kernel v5.0.
This host command will be used to create a ASN1 parser, for example, for pkcs7 or x509.
--- cmd/Kconfig | 3 + include/linux/asn1.h | 69 ++ include/linux/asn1_ber_bytecode.h | 93 ++ include/linux/asn1_decoder.h | 24 + scripts/Makefile | 3 + scripts/asn1_compiler.c | 1615 +++++++++++++++++++++++++++++ 6 files changed, 1807 insertions(+) create mode 100644 include/linux/asn1.h create mode 100644 include/linux/asn1_ber_bytecode.h create mode 100644 include/linux/asn1_decoder.h create mode 100644 scripts/asn1_compiler.c
cmd/Kconfig +3 -0
@@ -183,6 +183,9 @@ endmenu config BUILD_BIN2C bool +config BUILD_ASN1 + bool + comment "Commands" menu "Info commands"
include/linux/asn1.h new file +69 -0
69 lines not on this page · raw message
include/linux/asn1_ber_bytecode.h new file +93 -0
93 lines not on this page · raw message
include/linux/asn1_decoder.h new file +24 -0
@@ -0,0 +1,24 @@ +/* ASN.1 decoder +* +* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. +* Written by David Howells ([email protected]) +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public Licence +* as published by the Free Software Foundation; either version +* 2 of the Licence, or (at your option) any later version. +*/ + +#ifndef _LINUX_ASN1_DECODER_H +#define _LINUX_ASN1_DECODER_H + +#include <linux/asn1.h> + +struct asn1_decoder ; + +extern int asn1_ber_decoder (const struct asn1_decoder *decoder, +void *context, +const unsigned char *data, +size_t datalen); + +#endif /* _LINUX_ASN1_DECODER_H */
scripts/Makefile +3 -0
@@ -5,6 +5,9 @@ # --------------------------------------------------------------------------- hostprogs-$(CONFIG_BUILD_BIN2C) += bin2c +hostprogs-$(CONFIG_BUILD_ASN1) += asn1_compiler + +HOSTCFLAGS_asn1_compiler.o = -idirafter $(srctree)/include always := $(hostprogs-y)
scripts/asn1_compiler.c new file +1615 -0
1,615 lines not on this page · raw message
Signature · 1 line
2.21.0
RFC 2/9 Makefile: add build script for asn1 parsers
In reply to AKASHI TakahiroThis rule will be used to build x509 and pkcs7 parsers.
--- scripts/Makefile.build | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
scripts/Makefile.build +4 -2
@@ -331,7 +331,7 @@ quiet_cmd_asn1_compiler = ASN.1 $@ cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \ $(subst .h,.c,$@) $(subst .c,.h,$@) -$(obj)/%-asn1.c $(obj)/%-asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler +$(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler $(call cmd,asn1_compiler) # Build the compiled-in targets
@@ -419,9 +419,11 @@ targets += $(multi-used-y) $(multi-used-m) intermediate_targets = $(foreach sfx, $(2), \ $(patsubst %$(strip $(1)),%$(sfx), \ $(filter %$(strip $(1)), $(targets)))) +# %.asn1.o <- %.asn1.[ch] <- %.asn1 # %.lex.o <- %.lex.c <- %.l # %.tab.o <- %.tab.[ch] <- %.y -targets += $(call intermediate_targets, .lex.o, .lex.c) \ +targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) \ + $(call intermediate_targets, .lex.o, .lex.c) \ $(call intermediate_targets, .tab.o, .tab.c .tab.h) # Descending
Signature · 1 line
2.21.0
RFC 3/9 lib: add asn1 decoder
In reply to AKASHI TakahiroImported from linux kernel v5.0.
--- lib/Kconfig | 6 + lib/Makefile | 1 + lib/asn1_decoder.c | 520 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 527 insertions(+) create mode 100644 lib/asn1_decoder.c
lib/Kconfig +6 -0
@@ -521,6 +521,12 @@ config SMBIOS_PRODUCT_NAME endmenu +config ASN1 + bool + select BUILD_ASN1 + help + Enable asn1 decoder library. + source lib/efi/Kconfig source lib/efi_loader/Kconfig source lib/optee/Kconfig
lib/Makefile +1 -0
@@ -17,6 +17,7 @@ obj-$(CONFIG_OF_LIVE) += of_live.o obj-$(CONFIG_CMD_DHRYSTONE) += dhry/ obj-$(CONFIG_ARCH_AT91) += at91/ obj-$(CONFIG_OPTEE) += optee/ +obj-$(CONFIG_BUILD_ASN1) += asn1_decoder.o obj-$(CONFIG_AES) += aes.o
lib/asn1_decoder.c new file +520 -0
520 lines not on this page · raw message
Signature · 1 line
2.21.0
RFC 4/9 lib: add oid registry utility
In reply to AKASHI TakahiroImported from linux kernel v5.0.
--- include/linux/oid_registry.h | 103 +++++++++++++++++ lib/Kconfig | 5 + lib/Makefile | 16 +++ lib/build_OID_registry | 207 +++++++++++++++++++++++++++++++++++ lib/oid_registry.c | 178 ++++++++++++++++++++++++++++++ 5 files changed, 509 insertions(+) create mode 100644 include/linux/oid_registry.h create mode 100755 lib/build_OID_registry create mode 100644 lib/oid_registry.c
include/linux/oid_registry.h new file +103 -0
@@ -0,0 +1,103 @@ +/* ASN.1 Object identifier (OID) registry +* +* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. +* Written by David Howells (dhowells at redhat.com) +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public Licence +* as published by the Free Software Foundation; either version +* 2 of the Licence, or (at your option) any later version. +*/ + +#ifndef _LINUX_OID_REGISTRY_H +#define _LINUX_OID_REGISTRY_H + +#include <linux/types.h> + +/* +* OIDs are turned into these values if possible, or OID__NR if not held here. +* +* NOTE! Do not mess with the format of each line as this is read by +* build_OID_registry.pl to generate the data for look_up_OID(). +*/ +enum OID { + OID_id_dsa_with_sha1,/* 1.2.840.10030.4.3 */ + OID_id_dsa,/* 1.2.840.10040.4.1 */ + OID_id_ecdsa_with_sha1,/* 1.2.840.10045.4.1 */ + OID_id_ecPublicKey,/* 1.2.840.10045.2.1 */ + +/* PKCS#1 {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-1(1)} */ + OID_rsaEncryption,/* 1.2.840.113549.1.1.1 */ + OID_md2WithRSAEncryption,/* 1.2.840.113549.1.1.2 */ + OID_md3WithRSAEncryption,/* 1.2.840.113549.1.1.3 */ + OID_md4WithRSAEncryption,/* 1.2.840.113549.1.1.4 */ + OID_sha1WithRSAEncryption,/* 1.2.840.113549.1.1.5 */ + OID_sha256WithRSAEncryption,/* 1.2.840.113549.1.1.11 */ + OID_sha384WithRSAEncryption,/* 1.2.840.113549.1.1.12 */ + OID_sha512WithRSAEncryption,/* 1.2.840.113549.1.1.13 */ + OID_sha224WithRSAEncryption,/* 1.2.840.113549.1.1.14 */ +/* PKCS#7 {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-7(7)} */ + OID_data,/* 1.2.840.113549.1.7.1 */ + OID_signed_data,/* 1.2.840.113549.1.7.2 */ +/* PKCS#9 {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-9(9)} */ + OID_email_address,/* 1.2.840.113549.1.9.1 */ + OID_contentType,/* 1.2.840.113549.1.9.3 */ + OID_messageDigest,/* 1.2.840.113549.1.9.4 */ + OID_signingTime,/* 1.2.840.113549.1.9.5 */ + OID_smimeCapabilites,/* 1.2.840.113549.1.9.15 */ + OID_smimeAuthenticatedAttrs,/* 1.2.840.113549.1.9.16.2.11 */ + +/* {iso(1) member-body(2) us(840) rsadsi(113549) digestAlgorithm(2)} */ + OID_md2,/* 1.2.840.113549.2.2 */ + OID_md4,/* 1.2.840.113549.2.4 */ + OID_md5,/* 1.2.840.113549.2.5 */ + +/* Microsoft Authenticode & Software Publishing */ + OID_msIndirectData,/* 1.3.6.1.4.1.311.2.1.4 */ + OID_msStatementType,/* 1.3.6.1.4.1.311.2.1.11 */ + OID_msSpOpusInfo,/* 1.3.6.1.4.1.311.2.1.12 */ + OID_msPeImageDataObjId,/* 1.3.6.1.4.1.311.2.1.15 */ + OID_msIndividualSPKeyPurpose,/* 1.3.6.1.4.1.311.2.1.21 */ + OID_msOutlookExpress,/* 1.3.6.1.4.1.311.16.4 */ + + OID_certAuthInfoAccess,/* 1.3.6.1.5.5.7.1.1 */ + OID_sha1,/* 1.3.14.3.2.26 */ + OID_sha256,/* 2.16.840.1.101.3.4.2.1 */ + OID_sha384,/* 2.16.840.1.101.3.4.2.2 */ + OID_sha512,/* 2.16.840.1.101.3.4.2.3 */ + OID_sha224,/* 2.16.840.1.101.3.4.2.4 */ + +/* Distinguished Name attribute IDs [RFC 2256] */ + OID_commonName,/* 2.5.4.3 */ + OID_surname,/* 2.5.4.4 */ + OID_countryName,/* 2.5.4.6 */ + OID_locality,/* 2.5.4.7 */ + OID_stateOrProvinceName,/* 2.5.4.8 */ + OID_organizationName,/* 2.5.4.10 */ + OID_organizationUnitName,/* 2.5.4.11 */ + OID_title,/* 2.5.4.12 */ + OID_description,/* 2.5.4.13 */ + OID_name,/* 2.5.4.41 */ + OID_givenName,/* 2.5.4.42 */ + OID_initials,/* 2.5.4.43 */ + OID_generationalQualifier,/* 2.5.4.44 */ + +/* Certificate extension IDs */ + OID_subjectKeyIdentifier,/* 2.5.29.14 */ + OID_keyUsage,/* 2.5.29.15 */ + OID_subjectAltName,/* 2.5.29.17 */ + OID_issuerAltName,/* 2.5.29.18 */ + OID_basicConstraints,/* 2.5.29.19 */ + OID_crlDistributionPoints,/* 2.5.29.31 */ + OID_certPolicies,/* 2.5.29.32 */ + OID_authorityKeyIdentifier,/* 2.5.29.35 */ + OID_extKeyUsage,/* 2.5.29.37 */ + +OID__NR +}; + +extern enum OID look_up_OID (const void *data,size_t datasize); +extern int sprint_oid (const void *,size_t ,char *,size_t ); +extern int sprint_OID (enum OID ,char *,size_t ); + +#endif /* _LINUX_OID_REGISTRY_H */
lib/Kconfig +5 -0
@@ -527,6 +527,11 @@ config ASN1 help Enable asn1 decoder library. +config OID_REGISTRY + bool + help + Enable fast lookup object identifier registry. + source lib/efi/Kconfig source lib/efi_loader/Kconfig source lib/optee/Kconfig
lib/Makefile +16 -0
@@ -118,4 +118,20 @@ else obj-y += vsprintf.o strto.o strmhz.o endif +# +# Build a fast OID lookip registry from include/linux/oid_registry.h +# +obj-$(CONFIG_OID_REGISTRY) += oid_registry.o + +$(obj)/oid_registry.o: $(obj)/oid_registry_data.c + +$(obj)/oid_registry_data.c: $(srctree)/include/linux/oid_registry.h \ + $(src)/build_OID_registry + $(call cmd,build_OID_registry) + +quiet_cmd_build_OID_registry = GEN $@ + cmd_build_OID_registry = perl $(srctree)/$(src)/build_OID_registry $< $@ + +clean-files += oid_registry_data.c + subdir-ccflags-$(CONFIG_CC_OPTIMIZE_LIBS_FOR_SPEED) += -O2
lib/build_OID_registry new file +207 -0
207 lines not on this page · raw message
lib/oid_registry.c new file +178 -0
178 lines not on this page · raw message
Signature · 1 line
2.21.0
RFC 5/9 lib: crypto: add public key utility
In reply to AKASHI TakahiroImported from linux kernel v5.0.
--- include/crypto/public_key.h | 89 +++++ include/keys/asymmetric-type.h | 92 +++++ lib/crypto/Kconfig | 19 + lib/crypto/Makefile | 10 + lib/crypto/asymmetric_type.c | 655 +++++++++++++++++++++++++++++++++ lib/crypto/public_key.c | 344 +++++++++++++++++ 6 files changed, 1209 insertions(+) create mode 100644 include/crypto/public_key.h create mode 100644 include/keys/asymmetric-type.h create mode 100644 lib/crypto/Kconfig create mode 100644 lib/crypto/Makefile create mode 100644 lib/crypto/asymmetric_type.c create mode 100644 lib/crypto/public_key.c
include/crypto/public_key.h new file +89 -0
@@ -0,0 +1,89 @@ +/* Asymmetric public-key algorithm definitions +* +* See Documentation/crypto/asymmetric-keys.txt +* +* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. +* Written by David Howells (dhowells at redhat.com) +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public Licence +* as published by the Free Software Foundation; either version +* 2 of the Licence, or (at your option) any later version. +*/ + +#ifndef _LINUX_PUBLIC_KEY_H +#define _LINUX_PUBLIC_KEY_H + +#ifndef __UBOOT__ +#include <linux/keyctl.h> +#endif +#include <linux/types.h> + +/* +* Cryptographic data for the public-key subtype of the asymmetric key type. +* +* Note that this may include private part of the key as well as the public +* part. +*/ +struct public_key { +void *key; +u32 keylen; +bool key_is_private; +const char *id_type; +const char *pkey_algo; +}; + +extern void public_key_free (struct public_key *key); + +/* +* Public key cryptography signature data +*/ +struct public_key_signature { +struct asymmetric_key_id *auth_ids[2 ]; +u8 *s;/* Signature */ +u32 s_size;/* Number of bytes in signature */ +u8 *digest; +u8 digest_size;/* Number of bytes in digest */ +const char *pkey_algo; +const char *hash_algo; +const char *encoding; +}; + +extern void public_key_signature_free (struct public_key_signature *sig); + +#ifndef __UBOOT__ +extern struct asymmetric_key_subtype public_key_subtype; + +struct key ; +struct key_type ; +union key_payload ; + +extern int restrict_link_by_signature (struct key *dest_keyring, +const struct key_type *type, +const union key_payload *payload, +struct key *trust_keyring); + +extern int restrict_link_by_key_or_keyring (struct key *dest_keyring, +const struct key_type *type, +const union key_payload *payload, +struct key *trusted); + +extern int restrict_link_by_key_or_keyring_chain (struct key *trust_keyring, +const struct key_type *type, +const union key_payload *payload, +struct key *trusted); + +extern int query_asymmetric_key (const struct kernel_pkey_params *, +struct kernel_pkey_query *); + +extern int encrypt_blob (struct kernel_pkey_params *,const void *,void *); +extern int decrypt_blob (struct kernel_pkey_params *,const void *,void *); +extern int create_signature (struct kernel_pkey_params *,const void *,void *); +extern int verify_signature (const struct key *, +const struct public_key_signature *); + +int public_key_verify_signature (const struct public_key *pkey, +const struct public_key_signature *sig); +#endif /* !__UBOOT__ */ + +#endif /* _LINUX_PUBLIC_KEY_H */
include/keys/asymmetric-type.h new file +92 -0
92 lines not on this page · raw message
lib/crypto/Kconfig new file +19 -0
@@ -0,0 +1,19 @@ +menuconfig ASYMMETRIC_KEY_TYPE + bool "Asymmetric (public-key cryptographic) key Support" + help + This option provides support for a key type that holds the data for + the asymmetric keys used for public key cryptographic operations such + as encryption, decryption, signature generation and signature + verification. + +if ASYMMETRIC_KEY_TYPE + +config ASYMMETRIC_PUBLIC_KEY_SUBTYPE + bool "Asymmetric public-key crypto algorithm subtype" + help + This option provides support for asymmetric public key type handling. + If signature generation and/or verification are to be used, + appropriate hash algorithms (such as SHA-1) must be available. + ENOPKG will be reported if the requisite algorithm is unavailable. + +endif # ASYMMETRIC_KEY_TYPE
lib/crypto/Makefile new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Makefile for asymmetric cryptographic keys +# + +obj-$(CONFIG_ASYMMETRIC_KEY_TYPE) += asymmetric_keys.o + +asymmetric_keys-y := asymmetric_type.o + +obj-$(CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o
lib/crypto/asymmetric_type.c new file +655 -0
655 lines not on this page · raw message
lib/crypto/public_key.c new file +344 -0
344 lines not on this page · raw message
Signature · 1 line
2.21.0
RFC 6/9 lib: crypto: add public_key_verify_signature()
In reply to AKASHI Takahiro--- include/crypto/public_key.h | 2 +- lib/crypto/Makefile | 2 +- lib/crypto/public_key_local.c | 69 +++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 lib/crypto/public_key_local.c
include/crypto/public_key.h +1 -1
@@ -81,9 +81,9 @@ extern int decrypt_blob(struct kernel_pkey_params *, const void *, void *);extern int create_signature (struct kernel_pkey_params *,const void *,void *);extern int verify_signature (const struct key *,const struct public_key_signature *); +#endif /* !__UBOOT__ */ int public_key_verify_signature (const struct public_key *pkey,const struct public_key_signature *sig); -#endif /* !__UBOOT__ */ #endif /* _LINUX_PUBLIC_KEY_H */
lib/crypto/Makefile +1 -1
@@ -7,4 +7,4 @@ obj-$(CONFIG_ASYMMETRIC_KEY_TYPE) += asymmetric_keys.o asymmetric_keys-y := asymmetric_type.o -obj-$(CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o +obj-$(CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o public_key_local.o
lib/crypto/public_key_local.c new file +69 -0
69 lines not on this page · raw message
Signature · 1 line
2.21.0
RFC 7/9 lib: crypto: add x509 parser
In reply to AKASHI TakahiroImported from linux kernel v5.0.
--- lib/Kconfig | 1 + lib/Makefile | 1 + lib/crypto/Kconfig | 10 + lib/crypto/Makefile | 17 + lib/crypto/x509.asn1 | 60 ++++ lib/crypto/x509_akid.asn1 | 35 ++ lib/crypto/x509_cert_parser.c | 644 ++++++++++++++++++++++++++++++++++ lib/crypto/x509_parser.h | 61 ++++ lib/crypto/x509_public_key.c | 284 +++++++++++++++ 9 files changed, 1113 insertions(+) create mode 100644 lib/crypto/x509.asn1 create mode 100644 lib/crypto/x509_akid.asn1 create mode 100644 lib/crypto/x509_cert_parser.c create mode 100644 lib/crypto/x509_parser.h create mode 100644 lib/crypto/x509_public_key.c
lib/Kconfig +1 -0
@@ -253,6 +253,7 @@ config AES present. source lib/rsa/Kconfig +source lib/crypto/Kconfig config TPM bool "Trusted Platform Module (TPM) Support"
lib/Makefile +1 -0
@@ -18,6 +18,7 @@ obj-$(CONFIG_CMD_DHRYSTONE) += dhry/ obj-$(CONFIG_ARCH_AT91) += at91/ obj-$(CONFIG_OPTEE) += optee/ obj-$(CONFIG_BUILD_ASN1) += asn1_decoder.o +obj-y += crypto/ obj-$(CONFIG_AES) += aes.o
lib/crypto/Kconfig +10 -0
@@ -16,4 +16,14 @@ config ASYMMETRIC_PUBLIC_KEY_SUBTYPE appropriate hash algorithms (such as SHA-1) must be available. ENOPKG will be reported if the requisite algorithm is unavailable. +config X509_CERTIFICATE_PARSER + bool "X.509 certificate parser" + depends on ASYMMETRIC_PUBLIC_KEY_SUBTYPE + select ASN1 + select OID_REGISTRY + help + This option provides support for parsing X.509 format blobs for key + data and provides the ability to instantiate a crypto key from a + public key packet found inside the certificate. + endif # ASYMMETRIC_KEY_TYPE
lib/crypto/Makefile +17 -0
@@ -8,3 +8,20 @@ obj-$(CONFIG_ASYMMETRIC_KEY_TYPE) += asymmetric_keys.o asymmetric_keys-y := asymmetric_type.o obj-$(CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o public_key_local.o + +# +# X.509 Certificate handling +# +obj-$(CONFIG_X509_CERTIFICATE_PARSER) += x509_key_parser.o +x509_key_parser-y := \ + x509.asn1.o \ + x509_akid.asn1.o \ + x509_cert_parser.o \ + x509_public_key.o + +$(obj)/x509_cert_parser.o: \ + $(obj)/x509.asn1.h \ + $(obj)/x509_akid.asn1.h + +$(obj)/x509.asn1.o: $(obj)/x509.asn1.c $(obj)/x509.asn1.h +$(obj)/x509_akid.asn1.o: $(obj)/x509_akid.asn1.c $(obj)/x509_akid.asn1.h
lib/crypto/x509.asn1 new file +60 -0
60 lines not on this page · raw message
lib/crypto/x509_akid.asn1 new file +35 -0
@@ -0,0 +1,35 @@ +-- X.509 AuthorityKeyIdentifier +-- rfc5280 section 4.2.1.1 + +AuthorityKeyIdentifier ::= SEQUENCE { + keyIdentifier [0] IMPLICIT KeyIdentifier OPTIONAL, + authorityCertIssuer [1] IMPLICIT GeneralNames OPTIONAL, + authorityCertSerialNumber [2] IMPLICIT CertificateSerialNumber OPTIONAL + } + +KeyIdentifier ::= OCTET STRING ({ x509_akid_note_kid }) + +CertificateSerialNumber ::= INTEGER ({ x509_akid_note_serial }) + +GeneralNames ::= SEQUENCE OF GeneralName + +GeneralName ::= CHOICE { + otherName [0] ANY, + rfc822Name [1] IA5String, + dNSName [2] IA5String, + x400Address [3] ANY, + directoryName [4] Name ({ x509_akid_note_name }), + ediPartyName [5] ANY, + uniformResourceIdentifier [6] IA5String, + iPAddress [7] OCTET STRING, + registeredID [8] OBJECT IDENTIFIER + } + +Name ::= SEQUENCE OF RelativeDistinguishedName + +RelativeDistinguishedName ::= SET OF AttributeValueAssertion + +AttributeValueAssertion ::= SEQUENCE { + attributeType OBJECT IDENTIFIER ({ x509_note_OID }), + attributeValue ANY ({ x509_extract_name_segment }) + }
lib/crypto/x509_cert_parser.c new file +644 -0
644 lines not on this page · raw message
lib/crypto/x509_parser.h new file +61 -0
61 lines not on this page · raw message
lib/crypto/x509_public_key.c new file +284 -0
284 lines not on this page · raw message
Signature · 1 line
2.21.0
RFC 8/9 lib: crypto: add pkcs7 message parser
In reply to AKASHI TakahiroImported from linux kernel v5.0.
--- include/crypto/pkcs7.h | 51 +++ lib/crypto/Kconfig | 9 + lib/crypto/Makefile | 11 + lib/crypto/pkcs7.asn1 | 135 ++++++++ lib/crypto/pkcs7_parser.c | 690 ++++++++++++++++++++++++++++++++++++++ lib/crypto/pkcs7_parser.h | 69 ++++ scripts/Makefile.build | 4 +- 7 files changed, 966 insertions(+), 3 deletions(-) create mode 100644 include/crypto/pkcs7.h create mode 100644 lib/crypto/pkcs7.asn1 create mode 100644 lib/crypto/pkcs7_parser.c create mode 100644 lib/crypto/pkcs7_parser.h
include/crypto/pkcs7.h new file +51 -0
@@ -0,0 +1,51 @@ +/* PKCS#7 crypto data parser +* +* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. +* Written by David Howells (dhowells at redhat.com) +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public Licence +* as published by the Free Software Foundation; either version +* 2 of the Licence, or (at your option) any later version. +*/ + +#ifndef _CRYPTO_PKCS7_H +#define _CRYPTO_PKCS7_H + +#ifndef __UBOOT__ +#include <linux/verification.h> +#include <crypto/public_key.h> +#endif + +struct key ; +struct pkcs7_message ; + +/* +* pkcs7_parser.c +*/ +extern struct pkcs7_message *pkcs7_parse_message (const void *data, +size_t datalen); +extern void pkcs7_free_message (struct pkcs7_message *pkcs7); + +extern int pkcs7_get_content_data (const struct pkcs7_message *pkcs7, +const void **_data,size_t *_datalen, +size_t *_headerlen); + +#ifndef __UBOOT__ +/* +* pkcs7_trust.c +*/ +extern int pkcs7_validate_trust (struct pkcs7_message *pkcs7, +struct key *trust_keyring); + +/* +* pkcs7_verify.c +*/ +extern int pkcs7_verify (struct pkcs7_message *pkcs7, +enum key_being_used_for usage); + +extern int pkcs7_supply_detached_data (struct pkcs7_message *pkcs7, +const void *data,size_t datalen); +#endif + +#endif /* _CRYPTO_PKCS7_H */
lib/crypto/Kconfig +9 -0
@@ -26,4 +26,13 @@ config X509_CERTIFICATE_PARSER data and provides the ability to instantiate a crypto key from a public key packet found inside the certificate. +config PKCS7_MESSAGE_PARSER + bool "PKCS#7 message parser" + depends on X509_CERTIFICATE_PARSER + select ASN1 + select OID_REGISTRY + help + This option provides support for parsing PKCS#7 format messages for + signature data and provides the ability to verify the signature. + endif # ASYMMETRIC_KEY_TYPE
lib/crypto/Makefile +11 -0
@@ -25,3 +25,14 @@ $(obj)/x509_cert_parser.o: \ $(obj)/x509.asn1.o: $(obj)/x509.asn1.c $(obj)/x509.asn1.h $(obj)/x509_akid.asn1.o: $(obj)/x509_akid.asn1.c $(obj)/x509_akid.asn1.h + +# +# PKCS#7 message handling +# +obj-$(CONFIG_PKCS7_MESSAGE_PARSER) += pkcs7_message.o +pkcs7_message-y := \ + pkcs7.asn1.o \ + pkcs7_parser.o + +$(obj)/pkcs7_parser.o: $(obj)/pkcs7.asn1.h +$(obj)/pkcs7.asn1.o: $(obj)/pkcs7.asn1.c $(obj)/pkcs7.asn1.h
lib/crypto/pkcs7.asn1 new file +135 -0
135 lines not on this page · raw message
lib/crypto/pkcs7_parser.c new file +690 -0
690 lines not on this page · raw message
lib/crypto/pkcs7_parser.h new file +69 -0
69 lines not on this page · raw message
scripts/Makefile.build +1 -3
@@ -419,11 +419,9 @@ targets += $(multi-used-y) $(multi-used-m) intermediate_targets = $(foreach sfx, $(2), \ $(patsubst %$(strip $(1)),%$(sfx), \ $(filter %$(strip $(1)), $(targets)))) -# %.asn1.o <- %.asn1.[ch] <- %.asn1 # %.lex.o <- %.lex.c <- %.l # %.tab.o <- %.tab.[ch] <- %.y -targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) \ - $(call intermediate_targets, .lex.o, .lex.c) \ +targets += $(call intermediate_targets, .lex.o, .lex.c) \ $(call intermediate_targets, .tab.o, .tab.c .tab.h) # Descending
Signature · 1 line
2.21.0
RFC 9/9 lib: crypto: add rsa public key parser
In reply to AKASHI TakahiroImported from linux kernel v5.0.
--- include/crypto/internal/rsa.h | 62 +++++++++++++++++++++++++++ lib/crypto/Makefile | 10 ++++- lib/crypto/rsa_helper.c | 81 +++++++++++++++++++++++++++++++++++ lib/crypto/rsapubkey.asn1 | 4 ++ 4 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 include/crypto/internal/rsa.h create mode 100644 lib/crypto/rsa_helper.c create mode 100644 lib/crypto/rsapubkey.asn1
include/crypto/internal/rsa.h new file +62 -0
@@ -0,0 +1,62 @@ +/* +* RSA internal helpers +* +* Copyright (c) 2015, Intel Corporation +* Authors: Tadeusz Struk <[email protected]> +* +* This program 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. +* +*/ +#ifndef _RSA_HELPER_ +#define _RSA_HELPER_ +#include <linux/types.h> + +/** +* rsa_key - RSA key structure +* @n : RSA modulus raw byte stream +* @e : RSA public exponent raw byte stream +* @d : RSA private exponent raw byte stream +* @p : RSA prime factor p of n raw byte stream +* @q : RSA prime factor q of n raw byte stream +* @dp : RSA exponent d mod (p - 1) raw byte stream +* @dq : RSA exponent d mod (q - 1) raw byte stream +* @qinv : RSA CRT coefficient q^(-1) mod p raw byte stream +* @n_sz : length in bytes of RSA modulus n +* @e_sz : length in bytes of RSA public exponent +* @d_sz : length in bytes of RSA private exponent +* @p_sz : length in bytes of p field +* @q_sz : length in bytes of q field +* @dp_sz : length in bytes of dp field +* @dq_sz : length in bytes of dq field +* @qinv_sz : length in bytes of qinv field +*/ +struct rsa_key { +const u8 *n; +const u8 *e; +const u8 *d; +const u8 *p; +const u8 *q; +const u8 *dp; +const u8 *dq; +const u8 *qinv; +size_t n_sz; +size_t e_sz; +size_t d_sz; +size_t p_sz; +size_t q_sz; +size_t dp_sz; +size_t dq_sz; +size_t qinv_sz; +}; + +int rsa_parse_pub_key (struct rsa_key *rsa_key,const void *key, +unsigned int key_len); + +int rsa_parse_priv_key (struct rsa_key *rsa_key,const void *key, +unsigned int key_len); + +extern struct crypto_template rsa_pkcs1pad_tmpl; +#endif
lib/crypto/Makefile +9 -1
@@ -17,7 +17,9 @@ x509_key_parser-y := \ x509.asn1.o \ x509_akid.asn1.o \ x509_cert_parser.o \ - x509_public_key.o + x509_public_key.o \ + rsapubkey.asn1.o \ + rsa_helper.o $(obj)/x509_cert_parser.o: \ $(obj)/x509.asn1.h \
@@ -36,3 +38,9 @@ pkcs7_message-y := \ $(obj)/pkcs7_parser.o: $(obj)/pkcs7.asn1.h $(obj)/pkcs7.asn1.o: $(obj)/pkcs7.asn1.c $(obj)/pkcs7.asn1.h + +# +# RSA public key parser +# +$(obj)/rsapubkey.asn1.o: $(obj)/rsapubkey.asn1.c $(obj)/rsapubkey.asn1.h +$(obj)/rsa_helper.o: $(obj)/rsapubkey.asn1.h
lib/crypto/rsa_helper.c new file +81 -0
81 lines not on this page · raw message
lib/crypto/rsapubkey.asn1 new file +4 -0
@@ -0,0 +1,4 @@ +RsaPubKey ::= SEQUENCE { + n INTEGER ({ rsa_get_n }), + e INTEGER ({ rsa_get_e }) +}
Signature · 1 line
2.21.0
cover import x509/pkcs7 parsers from linux
In reply to AKASHI TakahiroPing.
Does anybody have any comments on this patch set?
Happy or unhappy with my approach here?
Thanks,
-Takahiro Akashi
Quoted 106 lines · AKASHI Takahiro
Asn1 parsers of x509 certificates and pkcs7 messages are required to implement image authentication and variable authentication as part of UEFI secure boot feature.
As we discussed before in the thread[1], most people insisted that we should re-use corresponding source code from Linux repository for this purpose.
Here is my attempt to import all the necessary files from Linux; Those will eventually be part of UEFI secure boot implementation, but I'd like to get early feedback from other peoples before submitting the whole patchset so that they will be better formatted for merging.
My approach here is
* files from Linux 5.0
(will be updated to the latest when I will submit them as finalized
patches.)
* modify files as little as possible
* mark/protect unavoidable changes with "#if(n)def__UBOOT__" so that future fixes/differences in Linux repository will easily be applied to U-Boot.Please note that checkpatch.pl will complain with a bunch of warnings/errors but I intentionally left them unchanged for the sake of better maintainability I said above.
Any comments will be appreciated.
-Takahiro Akashi[1] https://lists.denx.de/pipermail/u-boot/2019-April/366423.html
AKASHI Takahiro (9):
cmd: addasn1_compiler
Makefile: add build script for asn1 parsers
lib: add asn1 decoder
lib: add oid registry utility
lib: crypto: add public key utility
lib: crypto: addpublic_key_verify_signature()
lib: crypto: add x509 parser
lib: crypto: add pkcs7 message parser
lib: crypto: add rsa public key parsercmd/Kconfig | 3 + include/crypto/internal/rsa.h | 62 ++ include/crypto/pkcs7.h | 51 + include/crypto/public_key.h | 89 ++ include/keys/asymmetric-type.h | 92 ++ include/linux/asn1.h | 69 ++ include/linux/asn1_ber_bytecode.h | 93 ++ include/linux/asn1_decoder.h | 24 + include/linux/oid_registry.h | 103 ++ lib/Kconfig | 12 + lib/Makefile | 18 + lib/asn1_decoder.c | 520 ++++++++++ lib/build_OID_registry | 207 ++++ lib/crypto/Kconfig | 38 + lib/crypto/Makefile | 46 + lib/crypto/asymmetric_type.c | 655 ++++++++++++ lib/crypto/pkcs7.asn1 | 135 +++ lib/crypto/pkcs7_parser.c | 690 ++++++++++++ lib/crypto/pkcs7_parser.h | 69 ++ lib/crypto/public_key.c | 344 ++++++ lib/crypto/public_key_local.c | 69 ++ lib/crypto/rsa_helper.c | 81 ++ lib/crypto/rsapubkey.asn1 | 4 + lib/crypto/x509.asn1 | 60 ++ lib/crypto/x509_akid.asn1 | 35 + lib/crypto/x509_cert_parser.c | 644 ++++++++++++ lib/crypto/x509_parser.h | 61 ++ lib/crypto/x509_public_key.c | 284 +++++ lib/oid_registry.c | 178 ++++ scripts/Makefile | 3 + scripts/Makefile.build | 2 +- scripts/asn1_compiler.c | 1615 +++++++++++++++++++++++++++++ 32 files changed, 6355 insertions(+), 1 deletion(-) create mode 100644 include/crypto/internal/rsa.h create mode 100644 include/crypto/pkcs7.h create mode 100644 include/crypto/public_key.h create mode 100644 include/keys/asymmetric-type.h create mode 100644 include/linux/asn1.h create mode 100644 include/linux/asn1_ber_bytecode.h create mode 100644 include/linux/asn1_decoder.h create mode 100644 include/linux/oid_registry.h create mode 100644 lib/asn1_decoder.c create mode 100755 lib/build_OID_registry create mode 100644 lib/crypto/Kconfig create mode 100644 lib/crypto/Makefile create mode 100644 lib/crypto/asymmetric_type.c create mode 100644 lib/crypto/pkcs7.asn1 create mode 100644 lib/crypto/pkcs7_parser.c create mode 100644 lib/crypto/pkcs7_parser.h create mode 100644 lib/crypto/public_key.c create mode 100644 lib/crypto/public_key_local.c create mode 100644 lib/crypto/rsa_helper.c create mode 100644 lib/crypto/rsapubkey.asn1 create mode 100644 lib/crypto/x509.asn1 create mode 100644 lib/crypto/x509_akid.asn1 create mode 100644 lib/crypto/x509_cert_parser.c create mode 100644 lib/crypto/x509_parser.h create mode 100644 lib/crypto/x509_public_key.c create mode 100644 lib/oid_registry.c create mode 100644 scripts/asn1_compiler.cSignature · 1 line
2.21.0