diff options
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/Makefile.asm-generic | 2 | ||||
| -rw-r--r-- | scripts/Makefile.fwinst | 4 | ||||
| -rw-r--r-- | scripts/Makefile.modinst | 2 | ||||
| -rw-r--r-- | scripts/Makefile.modpost | 77 | ||||
| -rw-r--r-- | scripts/dtc/dtc.h | 44 | ||||
| -rwxr-xr-x[-rw-r--r--] | scripts/sign-file | 498 | ||||
| -rwxr-xr-x | scripts/x509keyid | 268 |
7 files changed, 421 insertions, 474 deletions
diff --git a/scripts/Makefile.asm-generic b/scripts/Makefile.asm-generic index 40caf3c26cd5..d17e0ea911ed 100644 --- a/scripts/Makefile.asm-generic +++ b/scripts/Makefile.asm-generic | |||
| @@ -5,7 +5,7 @@ | |||
| 5 | # and for each file listed in this file with generic-y creates | 5 | # and for each file listed in this file with generic-y creates |
| 6 | # a small wrapper file in $(obj) (arch/$(SRCARCH)/include/generated/asm) | 6 | # a small wrapper file in $(obj) (arch/$(SRCARCH)/include/generated/asm) |
| 7 | 7 | ||
| 8 | kbuild-file := $(srctree)/arch/$(SRCARCH)/include/asm/Kbuild | 8 | kbuild-file := $(srctree)/arch/$(SRCARCH)/include/$(src)/Kbuild |
| 9 | -include $(kbuild-file) | 9 | -include $(kbuild-file) |
| 10 | 10 | ||
| 11 | include scripts/Kbuild.include | 11 | include scripts/Kbuild.include |
diff --git a/scripts/Makefile.fwinst b/scripts/Makefile.fwinst index c3f69ae275d1..4d908d16c035 100644 --- a/scripts/Makefile.fwinst +++ b/scripts/Makefile.fwinst | |||
| @@ -27,7 +27,7 @@ endif | |||
| 27 | installed-mod-fw := $(addprefix $(INSTALL_FW_PATH)/,$(mod-fw)) | 27 | installed-mod-fw := $(addprefix $(INSTALL_FW_PATH)/,$(mod-fw)) |
| 28 | 28 | ||
| 29 | installed-fw := $(addprefix $(INSTALL_FW_PATH)/,$(fw-shipped-all)) | 29 | installed-fw := $(addprefix $(INSTALL_FW_PATH)/,$(fw-shipped-all)) |
| 30 | installed-fw-dirs := $(sort $(dir $(installed-fw))) $(INSTALL_FW_PATH)/. | 30 | installed-fw-dirs := $(sort $(dir $(installed-fw))) $(INSTALL_FW_PATH)/./ |
| 31 | 31 | ||
| 32 | # Workaround for make < 3.81, where .SECONDEXPANSION doesn't work. | 32 | # Workaround for make < 3.81, where .SECONDEXPANSION doesn't work. |
| 33 | PHONY += $(INSTALL_FW_PATH)/$$(%) install-all-dirs | 33 | PHONY += $(INSTALL_FW_PATH)/$$(%) install-all-dirs |
| @@ -42,7 +42,7 @@ quiet_cmd_install = INSTALL $(subst $(srctree)/,,$@) | |||
| 42 | $(installed-fw-dirs): | 42 | $(installed-fw-dirs): |
| 43 | $(call cmd,mkdir) | 43 | $(call cmd,mkdir) |
| 44 | 44 | ||
| 45 | $(installed-fw): $(INSTALL_FW_PATH)/%: $(obj)/% | $$(dir $(INSTALL_FW_PATH)/%) | 45 | $(installed-fw): $(INSTALL_FW_PATH)/%: $(obj)/% | $(INSTALL_FW_PATH)/$$(dir %) |
| 46 | $(call cmd,install) | 46 | $(call cmd,install) |
| 47 | 47 | ||
| 48 | PHONY += __fw_install __fw_modinst FORCE | 48 | PHONY += __fw_install __fw_modinst FORCE |
diff --git a/scripts/Makefile.modinst b/scripts/Makefile.modinst index 3d13d3a3edfe..dda4b2b61927 100644 --- a/scripts/Makefile.modinst +++ b/scripts/Makefile.modinst | |||
| @@ -17,7 +17,7 @@ __modinst: $(modules) | |||
| 17 | @: | 17 | @: |
| 18 | 18 | ||
| 19 | quiet_cmd_modules_install = INSTALL $@ | 19 | quiet_cmd_modules_install = INSTALL $@ |
| 20 | cmd_modules_install = mkdir -p $(2); cp $@ $(2) ; $(mod_strip_cmd) $(2)/$(notdir $@) | 20 | cmd_modules_install = mkdir -p $(2); cp $@ $(2) ; $(mod_strip_cmd) $(2)/$(notdir $@) ; $(mod_sign_cmd) $(2)/$(notdir $@) |
| 21 | 21 | ||
| 22 | # Modules built outside the kernel source tree go into extra by default | 22 | # Modules built outside the kernel source tree go into extra by default |
| 23 | INSTALL_MOD_DIR ?= extra | 23 | INSTALL_MOD_DIR ?= extra |
diff --git a/scripts/Makefile.modpost b/scripts/Makefile.modpost index 002089141df4..a1cb0222ebe6 100644 --- a/scripts/Makefile.modpost +++ b/scripts/Makefile.modpost | |||
| @@ -14,8 +14,7 @@ | |||
| 14 | # 3) create one <module>.mod.c file pr. module | 14 | # 3) create one <module>.mod.c file pr. module |
| 15 | # 4) create one Module.symvers file with CRC for all exported symbols | 15 | # 4) create one Module.symvers file with CRC for all exported symbols |
| 16 | # 5) compile all <module>.mod.c files | 16 | # 5) compile all <module>.mod.c files |
| 17 | # 6) final link of the module to a <module.ko> (or <module.unsigned>) file | 17 | # 6) final link of the module to a <module.ko> file |
| 18 | # 7) signs the modules to a <module.ko> file | ||
| 19 | 18 | ||
| 20 | # Step 3 is used to place certain information in the module's ELF | 19 | # Step 3 is used to place certain information in the module's ELF |
| 21 | # section, including information such as: | 20 | # section, including information such as: |
| @@ -33,8 +32,6 @@ | |||
| 33 | # Step 4 is solely used to allow module versioning in external modules, | 32 | # Step 4 is solely used to allow module versioning in external modules, |
| 34 | # where the CRC of each module is retrieved from the Module.symvers file. | 33 | # where the CRC of each module is retrieved from the Module.symvers file. |
| 35 | 34 | ||
| 36 | # Step 7 is dependent on CONFIG_MODULE_SIG being enabled. | ||
| 37 | |||
| 38 | # KBUILD_MODPOST_WARN can be set to avoid error out in case of undefined | 35 | # KBUILD_MODPOST_WARN can be set to avoid error out in case of undefined |
| 39 | # symbols in the final module linking stage | 36 | # symbols in the final module linking stage |
| 40 | # KBUILD_MODPOST_NOFINAL can be set to skip the final link of modules. | 37 | # KBUILD_MODPOST_NOFINAL can be set to skip the final link of modules. |
| @@ -119,7 +116,6 @@ $(modules:.ko=.mod.o): %.mod.o: %.mod.c FORCE | |||
| 119 | targets += $(modules:.ko=.mod.o) | 116 | targets += $(modules:.ko=.mod.o) |
| 120 | 117 | ||
| 121 | # Step 6), final link of the modules | 118 | # Step 6), final link of the modules |
| 122 | ifneq ($(CONFIG_MODULE_SIG),y) | ||
| 123 | quiet_cmd_ld_ko_o = LD [M] $@ | 119 | quiet_cmd_ld_ko_o = LD [M] $@ |
| 124 | cmd_ld_ko_o = $(LD) -r $(LDFLAGS) \ | 120 | cmd_ld_ko_o = $(LD) -r $(LDFLAGS) \ |
| 125 | $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ | 121 | $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ |
| @@ -129,78 +125,7 @@ $(modules): %.ko :%.o %.mod.o FORCE | |||
| 129 | $(call if_changed,ld_ko_o) | 125 | $(call if_changed,ld_ko_o) |
| 130 | 126 | ||
| 131 | targets += $(modules) | 127 | targets += $(modules) |
| 132 | else | ||
| 133 | quiet_cmd_ld_ko_unsigned_o = LD [M] $@ | ||
| 134 | cmd_ld_ko_unsigned_o = \ | ||
| 135 | $(LD) -r $(LDFLAGS) \ | ||
| 136 | $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ | ||
| 137 | -o $@ $(filter-out FORCE,$^) \ | ||
| 138 | $(if $(AFTER_LINK),; $(AFTER_LINK)) | ||
| 139 | |||
| 140 | $(modules:.ko=.ko.unsigned): %.ko.unsigned :%.o %.mod.o FORCE | ||
| 141 | $(call if_changed,ld_ko_unsigned_o) | ||
| 142 | |||
| 143 | targets += $(modules:.ko=.ko.unsigned) | ||
| 144 | |||
| 145 | # Step 7), sign the modules | ||
| 146 | MODSECKEY = ./signing_key.priv | ||
| 147 | MODPUBKEY = ./signing_key.x509 | ||
| 148 | |||
| 149 | ifeq ($(wildcard $(MODSECKEY))+$(wildcard $(MODPUBKEY)),$(MODSECKEY)+$(MODPUBKEY)) | ||
| 150 | ifeq ($(KBUILD_SRC),) | ||
| 151 | # no O= is being used | ||
| 152 | SCRIPTS_DIR := scripts | ||
| 153 | else | ||
| 154 | SCRIPTS_DIR := $(KBUILD_SRC)/scripts | ||
| 155 | endif | ||
| 156 | SIGN_MODULES := 1 | ||
| 157 | else | ||
| 158 | SIGN_MODULES := 0 | ||
| 159 | endif | ||
| 160 | |||
| 161 | # only sign if it's an in-tree module | ||
| 162 | ifneq ($(KBUILD_EXTMOD),) | ||
| 163 | SIGN_MODULES := 0 | ||
| 164 | endif | ||
| 165 | 128 | ||
| 166 | # We strip the module as best we can - note that using both strip and eu-strip | ||
| 167 | # results in a smaller module than using either alone. | ||
| 168 | EU_STRIP = $(shell which eu-strip || echo true) | ||
| 169 | |||
| 170 | quiet_cmd_sign_ko_stripped_ko_unsigned = STRIP [M] $@ | ||
| 171 | cmd_sign_ko_stripped_ko_unsigned = \ | ||
| 172 | cp $< $@ && \ | ||
| 173 | strip -x -g $@ && \ | ||
| 174 | $(EU_STRIP) $@ | ||
| 175 | |||
| 176 | ifeq ($(SIGN_MODULES),1) | ||
| 177 | |||
| 178 | quiet_cmd_genkeyid = GENKEYID $@ | ||
| 179 | cmd_genkeyid = \ | ||
| 180 | perl $(SCRIPTS_DIR)/x509keyid $< $<.signer $<.keyid | ||
| 181 | |||
| 182 | %.signer %.keyid: % | ||
| 183 | $(call if_changed,genkeyid) | ||
| 184 | |||
| 185 | KEYRING_DEP := $(MODSECKEY) $(MODPUBKEY) $(MODPUBKEY).signer $(MODPUBKEY).keyid | ||
| 186 | quiet_cmd_sign_ko_ko_stripped = SIGN [M] $@ | ||
| 187 | cmd_sign_ko_ko_stripped = \ | ||
| 188 | sh $(SCRIPTS_DIR)/sign-file $(MODSECKEY) $(MODPUBKEY) $< $@ | ||
| 189 | else | ||
| 190 | KEYRING_DEP := | ||
| 191 | quiet_cmd_sign_ko_ko_unsigned = NO SIGN [M] $@ | ||
| 192 | cmd_sign_ko_ko_unsigned = \ | ||
| 193 | cp $< $@ | ||
| 194 | endif | ||
| 195 | |||
| 196 | $(modules): %.ko :%.ko.stripped $(KEYRING_DEP) FORCE | ||
| 197 | $(call if_changed,sign_ko_ko_stripped) | ||
| 198 | |||
| 199 | $(patsubst %.ko,%.ko.stripped,$(modules)): %.ko.stripped :%.ko.unsigned FORCE | ||
| 200 | $(call if_changed,sign_ko_stripped_ko_unsigned) | ||
| 201 | |||
| 202 | targets += $(modules) | ||
| 203 | endif | ||
| 204 | 129 | ||
| 205 | # Add FORCE to the prequisites of a target to force it to be always rebuilt. | 130 | # Add FORCE to the prequisites of a target to force it to be always rebuilt. |
| 206 | # --------------------------------------------------------------------------- | 131 | # --------------------------------------------------------------------------- |
diff --git a/scripts/dtc/dtc.h b/scripts/dtc/dtc.h index d501c8605f26..3e42a071070e 100644 --- a/scripts/dtc/dtc.h +++ b/scripts/dtc/dtc.h | |||
| @@ -161,51 +161,27 @@ struct node { | |||
| 161 | struct label *labels; | 161 | struct label *labels; |
| 162 | }; | 162 | }; |
| 163 | 163 | ||
| 164 | static inline struct label *for_each_label_next(struct label *l) | ||
| 165 | { | ||
| 166 | do { | ||
| 167 | l = l->next; | ||
| 168 | } while (l && l->deleted); | ||
| 169 | |||
| 170 | return l; | ||
| 171 | } | ||
| 172 | |||
| 173 | #define for_each_label(l0, l) \ | ||
| 174 | for ((l) = (l0); (l); (l) = for_each_label_next(l)) | ||
| 175 | |||
| 176 | #define for_each_label_withdel(l0, l) \ | 164 | #define for_each_label_withdel(l0, l) \ |
| 177 | for ((l) = (l0); (l); (l) = (l)->next) | 165 | for ((l) = (l0); (l); (l) = (l)->next) |
| 178 | 166 | ||
| 179 | static inline struct property *for_each_property_next(struct property *p) | 167 | #define for_each_label(l0, l) \ |
| 180 | { | 168 | for_each_label_withdel(l0, l) \ |
| 181 | do { | 169 | if (!(l)->deleted) |
| 182 | p = p->next; | ||
| 183 | } while (p && p->deleted); | ||
| 184 | |||
| 185 | return p; | ||
| 186 | } | ||
| 187 | |||
| 188 | #define for_each_property(n, p) \ | ||
| 189 | for ((p) = (n)->proplist; (p); (p) = for_each_property_next(p)) | ||
| 190 | 170 | ||
| 191 | #define for_each_property_withdel(n, p) \ | 171 | #define for_each_property_withdel(n, p) \ |
| 192 | for ((p) = (n)->proplist; (p); (p) = (p)->next) | 172 | for ((p) = (n)->proplist; (p); (p) = (p)->next) |
| 193 | 173 | ||
| 194 | static inline struct node *for_each_child_next(struct node *c) | 174 | #define for_each_property(n, p) \ |
| 195 | { | 175 | for_each_property_withdel(n, p) \ |
| 196 | do { | 176 | if (!(p)->deleted) |
| 197 | c = c->next_sibling; | ||
| 198 | } while (c && c->deleted); | ||
| 199 | |||
| 200 | return c; | ||
| 201 | } | ||
| 202 | |||
| 203 | #define for_each_child(n, c) \ | ||
| 204 | for ((c) = (n)->children; (c); (c) = for_each_child_next(c)) | ||
| 205 | 177 | ||
| 206 | #define for_each_child_withdel(n, c) \ | 178 | #define for_each_child_withdel(n, c) \ |
| 207 | for ((c) = (n)->children; (c); (c) = (c)->next_sibling) | 179 | for ((c) = (n)->children; (c); (c) = (c)->next_sibling) |
| 208 | 180 | ||
| 181 | #define for_each_child(n, c) \ | ||
| 182 | for_each_child_withdel(n, c) \ | ||
| 183 | if (!(c)->deleted) | ||
| 184 | |||
| 209 | void add_label(struct label **labels, char *label); | 185 | void add_label(struct label **labels, char *label); |
| 210 | void delete_labels(struct label **labels); | 186 | void delete_labels(struct label **labels); |
| 211 | 187 | ||
diff --git a/scripts/sign-file b/scripts/sign-file index e58e34e50ac5..87ca59d36e7e 100644..100755 --- a/scripts/sign-file +++ b/scripts/sign-file | |||
| @@ -1,115 +1,429 @@ | |||
| 1 | #!/bin/sh | 1 | #!/usr/bin/perl -w |
| 2 | # | 2 | # |
| 3 | # Sign a module file using the given key. | 3 | # Sign a module file using the given key. |
| 4 | # | 4 | # |
| 5 | # Format: sign-file <key> <x509> <src-file> <dst-file> | 5 | # Format: |
| 6 | # | 6 | # |
| 7 | 7 | # ./scripts/sign-file [-v] <key> <x509> <module> [<dest>] | |
| 8 | scripts=`dirname $0` | 8 | # |
| 9 | 9 | # | |
| 10 | CONFIG_MODULE_SIG_SHA512=y | 10 | use strict; |
| 11 | if [ -r .config ] | 11 | use FileHandle; |
| 12 | then | 12 | use IPC::Open2; |
| 13 | . ./.config | 13 | |
| 14 | fi | 14 | my $verbose = 0; |
| 15 | 15 | if ($#ARGV >= 0 && $ARGV[0] eq "-v") { | |
| 16 | key="$1" | 16 | $verbose = 1; |
| 17 | x509="$2" | 17 | shift; |
| 18 | src="$3" | 18 | } |
| 19 | dst="$4" | 19 | |
| 20 | 20 | die "Format: ./scripts/sign-file [-v] <key> <x509> <module> [<dest>]\n" | |
| 21 | if [ ! -r "$key" ] | 21 | if ($#ARGV != 2 && $#ARGV != 3); |
| 22 | then | 22 | |
| 23 | echo "Can't read private key" >&2 | 23 | my $private_key = $ARGV[0]; |
| 24 | exit 2 | 24 | my $x509 = $ARGV[1]; |
| 25 | fi | 25 | my $module = $ARGV[2]; |
| 26 | 26 | my $dest = ($#ARGV == 3) ? $ARGV[3] : $ARGV[2] . "~"; | |
| 27 | if [ ! -r "$x509" ] | 27 | |
| 28 | then | 28 | die "Can't read private key\n" unless (-r $private_key); |
| 29 | echo "Can't read X.509 certificate" >&2 | 29 | die "Can't read X.509 certificate\n" unless (-r $x509); |
| 30 | exit 2 | 30 | die "Can't read module\n" unless (-r $module); |
| 31 | fi | 31 | |
| 32 | if [ ! -r "$x509.signer" ] | 32 | # |
| 33 | then | 33 | # Read the kernel configuration |
| 34 | echo "Can't read Signer name" >&2 | 34 | # |
| 35 | exit 2; | 35 | my %config = ( |
| 36 | fi | 36 | CONFIG_MODULE_SIG_SHA512 => 1 |
| 37 | if [ ! -r "$x509.keyid" ] | 37 | ); |
| 38 | then | 38 | |
| 39 | echo "Can't read Key identifier" >&2 | 39 | if (-r ".config") { |
| 40 | exit 2; | 40 | open(FD, "<.config") || die ".config"; |
| 41 | fi | 41 | while (<FD>) { |
| 42 | if ($_ =~ /^(CONFIG_.*)=[ym]/) { | ||
| 43 | $config{$1} = 1; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | close(FD); | ||
| 47 | } | ||
| 48 | |||
| 49 | # | ||
| 50 | # Function to read the contents of a file into a variable. | ||
| 51 | # | ||
| 52 | sub read_file($) | ||
| 53 | { | ||
| 54 | my ($file) = @_; | ||
| 55 | my $contents; | ||
| 56 | my $len; | ||
| 57 | |||
| 58 | open(FD, "<$file") || die $file; | ||
| 59 | binmode FD; | ||
| 60 | my @st = stat(FD); | ||
| 61 | die $file if (!@st); | ||
| 62 | $len = read(FD, $contents, $st[7]) || die $file; | ||
| 63 | close(FD) || die $file; | ||
| 64 | die "$file: Wanted length ", $st[7], ", got ", $len, "\n" | ||
| 65 | if ($len != $st[7]); | ||
| 66 | return $contents; | ||
| 67 | } | ||
| 68 | |||
| 69 | ############################################################################### | ||
| 70 | # | ||
| 71 | # First of all, we have to parse the X.509 certificate to find certain details | ||
| 72 | # about it. | ||
| 73 | # | ||
| 74 | # We read the DER-encoded X509 certificate and parse it to extract the Subject | ||
| 75 | # name and Subject Key Identifier. Theis provides the data we need to build | ||
| 76 | # the certificate identifier. | ||
| 77 | # | ||
| 78 | # The signer's name part of the identifier is fabricated from the commonName, | ||
| 79 | # the organizationName or the emailAddress components of the X.509 subject | ||
| 80 | # name. | ||
| 81 | # | ||
| 82 | # The subject key ID is used to select which of that signer's certificates | ||
| 83 | # we're intending to use to sign the module. | ||
| 84 | # | ||
| 85 | ############################################################################### | ||
| 86 | my $x509_certificate = read_file($x509); | ||
| 87 | |||
| 88 | my $UNIV = 0 << 6; | ||
| 89 | my $APPL = 1 << 6; | ||
| 90 | my $CONT = 2 << 6; | ||
| 91 | my $PRIV = 3 << 6; | ||
| 92 | |||
| 93 | my $CONS = 0x20; | ||
| 94 | |||
| 95 | my $BOOLEAN = 0x01; | ||
| 96 | my $INTEGER = 0x02; | ||
| 97 | my $BIT_STRING = 0x03; | ||
| 98 | my $OCTET_STRING = 0x04; | ||
| 99 | my $NULL = 0x05; | ||
| 100 | my $OBJ_ID = 0x06; | ||
| 101 | my $UTF8String = 0x0c; | ||
| 102 | my $SEQUENCE = 0x10; | ||
| 103 | my $SET = 0x11; | ||
| 104 | my $UTCTime = 0x17; | ||
| 105 | my $GeneralizedTime = 0x18; | ||
| 106 | |||
| 107 | my %OIDs = ( | ||
| 108 | pack("CCC", 85, 4, 3) => "commonName", | ||
| 109 | pack("CCC", 85, 4, 6) => "countryName", | ||
| 110 | pack("CCC", 85, 4, 10) => "organizationName", | ||
| 111 | pack("CCC", 85, 4, 11) => "organizationUnitName", | ||
| 112 | pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 1, 1) => "rsaEncryption", | ||
| 113 | pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 1, 5) => "sha1WithRSAEncryption", | ||
| 114 | pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 9, 1) => "emailAddress", | ||
| 115 | pack("CCC", 85, 29, 35) => "authorityKeyIdentifier", | ||
| 116 | pack("CCC", 85, 29, 14) => "subjectKeyIdentifier", | ||
| 117 | pack("CCC", 85, 29, 19) => "basicConstraints" | ||
| 118 | ); | ||
| 119 | |||
| 120 | ############################################################################### | ||
| 121 | # | ||
| 122 | # Extract an ASN.1 element from a string and return information about it. | ||
| 123 | # | ||
| 124 | ############################################################################### | ||
| 125 | sub asn1_extract($$@) | ||
| 126 | { | ||
| 127 | my ($cursor, $expected_tag, $optional) = @_; | ||
| 128 | |||
| 129 | return [ -1 ] | ||
| 130 | if ($cursor->[1] == 0 && $optional); | ||
| 131 | |||
| 132 | die $x509, ": ", $cursor->[0], ": ASN.1 data underrun (elem ", $cursor->[1], ")\n" | ||
| 133 | if ($cursor->[1] < 2); | ||
| 134 | |||
| 135 | my ($tag, $len) = unpack("CC", substr(${$cursor->[2]}, $cursor->[0], 2)); | ||
| 136 | |||
| 137 | if ($expected_tag != -1 && $tag != $expected_tag) { | ||
| 138 | return [ -1 ] | ||
| 139 | if ($optional); | ||
| 140 | die $x509, ": ", $cursor->[0], ": ASN.1 unexpected tag (", $tag, | ||
| 141 | " not ", $expected_tag, ")\n"; | ||
| 142 | } | ||
| 143 | |||
| 144 | $cursor->[0] += 2; | ||
| 145 | $cursor->[1] -= 2; | ||
| 146 | |||
| 147 | die $x509, ": ", $cursor->[0], ": ASN.1 long tag\n" | ||
| 148 | if (($tag & 0x1f) == 0x1f); | ||
| 149 | die $x509, ": ", $cursor->[0], ": ASN.1 indefinite length\n" | ||
| 150 | if ($len == 0x80); | ||
| 151 | |||
| 152 | if ($len > 0x80) { | ||
| 153 | my $l = $len - 0x80; | ||
| 154 | die $x509, ": ", $cursor->[0], ": ASN.1 data underrun (len len $l)\n" | ||
| 155 | if ($cursor->[1] < $l); | ||
| 156 | |||
| 157 | if ($l == 0x1) { | ||
| 158 | $len = unpack("C", substr(${$cursor->[2]}, $cursor->[0], 1)); | ||
| 159 | } elsif ($l = 0x2) { | ||
| 160 | $len = unpack("n", substr(${$cursor->[2]}, $cursor->[0], 2)); | ||
| 161 | } elsif ($l = 0x3) { | ||
| 162 | $len = unpack("C", substr(${$cursor->[2]}, $cursor->[0], 1)) << 16; | ||
| 163 | $len = unpack("n", substr(${$cursor->[2]}, $cursor->[0] + 1, 2)); | ||
| 164 | } elsif ($l = 0x4) { | ||
| 165 | $len = unpack("N", substr(${$cursor->[2]}, $cursor->[0], 4)); | ||
| 166 | } else { | ||
| 167 | die $x509, ": ", $cursor->[0], ": ASN.1 element too long (", $l, ")\n"; | ||
| 168 | } | ||
| 169 | |||
| 170 | $cursor->[0] += $l; | ||
| 171 | $cursor->[1] -= $l; | ||
| 172 | } | ||
| 173 | |||
| 174 | die $x509, ": ", $cursor->[0], ": ASN.1 data underrun (", $len, ")\n" | ||
| 175 | if ($cursor->[1] < $len); | ||
| 176 | |||
| 177 | my $ret = [ $tag, [ $cursor->[0], $len, $cursor->[2] ] ]; | ||
| 178 | $cursor->[0] += $len; | ||
| 179 | $cursor->[1] -= $len; | ||
| 180 | |||
| 181 | return $ret; | ||
| 182 | } | ||
| 183 | |||
| 184 | ############################################################################### | ||
| 185 | # | ||
| 186 | # Retrieve the data referred to by a cursor | ||
| 187 | # | ||
| 188 | ############################################################################### | ||
| 189 | sub asn1_retrieve($) | ||
| 190 | { | ||
| 191 | my ($cursor) = @_; | ||
| 192 | my ($offset, $len, $data) = @$cursor; | ||
| 193 | return substr($$data, $offset, $len); | ||
| 194 | } | ||
| 195 | |||
| 196 | ############################################################################### | ||
| 197 | # | ||
| 198 | # Roughly parse the X.509 certificate | ||
| 199 | # | ||
| 200 | ############################################################################### | ||
| 201 | my $cursor = [ 0, length($x509_certificate), \$x509_certificate ]; | ||
| 202 | |||
| 203 | my $cert = asn1_extract($cursor, $UNIV | $CONS | $SEQUENCE); | ||
| 204 | my $tbs = asn1_extract($cert->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 205 | my $version = asn1_extract($tbs->[1], $CONT | $CONS | 0, 1); | ||
| 206 | my $serial_number = asn1_extract($tbs->[1], $UNIV | $INTEGER); | ||
| 207 | my $sig_type = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 208 | my $issuer = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 209 | my $validity = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 210 | my $subject = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 211 | my $key = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 212 | my $issuer_uid = asn1_extract($tbs->[1], $CONT | $CONS | 1, 1); | ||
| 213 | my $subject_uid = asn1_extract($tbs->[1], $CONT | $CONS | 2, 1); | ||
| 214 | my $extension_list = asn1_extract($tbs->[1], $CONT | $CONS | 3, 1); | ||
| 215 | |||
| 216 | my $subject_key_id = (); | ||
| 217 | my $authority_key_id = (); | ||
| 218 | |||
| 219 | # | ||
| 220 | # Parse the extension list | ||
| 221 | # | ||
| 222 | if ($extension_list->[0] != -1) { | ||
| 223 | my $extensions = asn1_extract($extension_list->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 224 | |||
| 225 | while ($extensions->[1]->[1] > 0) { | ||
| 226 | my $ext = asn1_extract($extensions->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 227 | my $x_oid = asn1_extract($ext->[1], $UNIV | $OBJ_ID); | ||
| 228 | my $x_crit = asn1_extract($ext->[1], $UNIV | $BOOLEAN, 1); | ||
| 229 | my $x_val = asn1_extract($ext->[1], $UNIV | $OCTET_STRING); | ||
| 230 | |||
| 231 | my $raw_oid = asn1_retrieve($x_oid->[1]); | ||
| 232 | next if (!exists($OIDs{$raw_oid})); | ||
| 233 | my $x_type = $OIDs{$raw_oid}; | ||
| 234 | |||
| 235 | my $raw_value = asn1_retrieve($x_val->[1]); | ||
| 236 | |||
| 237 | if ($x_type eq "subjectKeyIdentifier") { | ||
| 238 | my $vcursor = [ 0, length($raw_value), \$raw_value ]; | ||
| 239 | |||
| 240 | $subject_key_id = asn1_extract($vcursor, $UNIV | $OCTET_STRING); | ||
| 241 | } | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | ############################################################################### | ||
| 246 | # | ||
| 247 | # Determine what we're going to use as the signer's name. In order of | ||
| 248 | # preference, take one of: commonName, organizationName or emailAddress. | ||
| 249 | # | ||
| 250 | ############################################################################### | ||
| 251 | my $org = ""; | ||
| 252 | my $cn = ""; | ||
| 253 | my $email = ""; | ||
| 254 | |||
| 255 | while ($subject->[1]->[1] > 0) { | ||
| 256 | my $rdn = asn1_extract($subject->[1], $UNIV | $CONS | $SET); | ||
| 257 | my $attr = asn1_extract($rdn->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 258 | my $n_oid = asn1_extract($attr->[1], $UNIV | $OBJ_ID); | ||
| 259 | my $n_val = asn1_extract($attr->[1], -1); | ||
| 260 | |||
| 261 | my $raw_oid = asn1_retrieve($n_oid->[1]); | ||
| 262 | next if (!exists($OIDs{$raw_oid})); | ||
| 263 | my $n_type = $OIDs{$raw_oid}; | ||
| 264 | |||
| 265 | my $raw_value = asn1_retrieve($n_val->[1]); | ||
| 266 | |||
| 267 | if ($n_type eq "organizationName") { | ||
| 268 | $org = $raw_value; | ||
| 269 | } elsif ($n_type eq "commonName") { | ||
| 270 | $cn = $raw_value; | ||
| 271 | } elsif ($n_type eq "emailAddress") { | ||
| 272 | $email = $raw_value; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | my $signers_name = $email; | ||
| 277 | |||
| 278 | if ($org && $cn) { | ||
| 279 | # Don't use the organizationName if the commonName repeats it | ||
| 280 | if (length($org) <= length($cn) && | ||
| 281 | substr($cn, 0, length($org)) eq $org) { | ||
| 282 | $signers_name = $cn; | ||
| 283 | goto got_id_name; | ||
| 284 | } | ||
| 285 | |||
| 286 | # Or a signifcant chunk of it | ||
| 287 | if (length($org) >= 7 && | ||
| 288 | length($cn) >= 7 && | ||
| 289 | substr($cn, 0, 7) eq substr($org, 0, 7)) { | ||
| 290 | $signers_name = $cn; | ||
| 291 | goto got_id_name; | ||
| 292 | } | ||
| 293 | |||
| 294 | $signers_name = $org . ": " . $cn; | ||
| 295 | } elsif ($org) { | ||
| 296 | $signers_name = $org; | ||
| 297 | } elsif ($cn) { | ||
| 298 | $signers_name = $cn; | ||
| 299 | } | ||
| 300 | |||
| 301 | got_id_name: | ||
| 302 | |||
| 303 | die $x509, ": ", "X.509: Couldn't find the Subject Key Identifier extension\n" | ||
| 304 | if (!$subject_key_id); | ||
| 305 | |||
| 306 | my $key_identifier = asn1_retrieve($subject_key_id->[1]); | ||
| 307 | |||
| 308 | ############################################################################### | ||
| 309 | # | ||
| 310 | # Create and attach the module signature | ||
| 311 | # | ||
| 312 | ############################################################################### | ||
| 42 | 313 | ||
| 43 | # | 314 | # |
| 44 | # Signature parameters | 315 | # Signature parameters |
| 45 | # | 316 | # |
| 46 | algo=1 # Public-key crypto algorithm: RSA | 317 | my $algo = 1; # Public-key crypto algorithm: RSA |
| 47 | hash= # Digest algorithm | 318 | my $hash = 0; # Digest algorithm |
| 48 | id_type=1 # Identifier type: X.509 | 319 | my $id_type = 1; # Identifier type: X.509 |
| 49 | 320 | ||
| 50 | # | 321 | # |
| 51 | # Digest the data | 322 | # Digest the data |
| 52 | # | 323 | # |
| 53 | dgst= | 324 | my ($dgst, $prologue) = (); |
| 54 | if [ "$CONFIG_MODULE_SIG_SHA1" = "y" ] | 325 | if (exists $config{"CONFIG_MODULE_SIG_SHA1"}) { |
| 55 | then | 326 | $prologue = pack("C*", |
| 56 | prologue="0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14" | 327 | 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, |
| 57 | dgst=-sha1 | 328 | 0x2B, 0x0E, 0x03, 0x02, 0x1A, |
| 58 | hash=2 | 329 | 0x05, 0x00, 0x04, 0x14); |
| 59 | elif [ "$CONFIG_MODULE_SIG_SHA224" = "y" ] | 330 | $dgst = "-sha1"; |
| 60 | then | 331 | $hash = 2; |
| 61 | prologue="0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1C" | 332 | } elsif (exists $config{"CONFIG_MODULE_SIG_SHA224"}) { |
| 62 | dgst=-sha224 | 333 | $prologue = pack("C*", |
| 63 | hash=7 | 334 | 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, |
| 64 | elif [ "$CONFIG_MODULE_SIG_SHA256" = "y" ] | 335 | 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, |
| 65 | then | 336 | 0x05, 0x00, 0x04, 0x1C); |
| 66 | prologue="0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20" | 337 | $dgst = "-sha224"; |
| 67 | dgst=-sha256 | 338 | $hash = 7; |
| 68 | hash=4 | 339 | } elsif (exists $config{"CONFIG_MODULE_SIG_SHA256"}) { |
| 69 | elif [ "$CONFIG_MODULE_SIG_SHA384" = "y" ] | 340 | $prologue = pack("C*", |
| 70 | then | 341 | 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, |
| 71 | prologue="0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30" | 342 | 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, |
| 72 | dgst=-sha384 | 343 | 0x05, 0x00, 0x04, 0x20); |
| 73 | hash=5 | 344 | $dgst = "-sha256"; |
| 74 | elif [ "$CONFIG_MODULE_SIG_SHA512" = "y" ] | 345 | $hash = 4; |
| 75 | then | 346 | } elsif (exists $config{"CONFIG_MODULE_SIG_SHA384"}) { |
| 76 | prologue="0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40" | 347 | $prologue = pack("C*", |
| 77 | dgst=-sha512 | 348 | 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, |
| 78 | hash=6 | 349 | 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, |
| 79 | else | 350 | 0x05, 0x00, 0x04, 0x30); |
| 80 | echo "$0: Can't determine hash algorithm" >&2 | 351 | $dgst = "-sha384"; |
| 81 | exit 2 | 352 | $hash = 5; |
| 82 | fi | 353 | } elsif (exists $config{"CONFIG_MODULE_SIG_SHA512"}) { |
| 83 | 354 | $prologue = pack("C*", | |
| 84 | ( | 355 | 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, |
| 85 | perl -e "binmode STDOUT; print pack(\"C*\", $prologue)" || exit $? | 356 | 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, |
| 86 | openssl dgst $dgst -binary $src || exit $? | 357 | 0x05, 0x00, 0x04, 0x40); |
| 87 | ) >$src.dig || exit $? | 358 | $dgst = "-sha512"; |
| 359 | $hash = 6; | ||
| 360 | } else { | ||
| 361 | die "Can't determine hash algorithm"; | ||
| 362 | } | ||
| 363 | |||
| 364 | # | ||
| 365 | # Generate the digest and read from openssl's stdout | ||
| 366 | # | ||
| 367 | my $digest; | ||
| 368 | $digest = readpipe("openssl dgst $dgst -binary $module") || die "openssl dgst"; | ||
| 88 | 369 | ||
| 89 | # | 370 | # |
| 90 | # Generate the binary signature, which will be just the integer that comprises | 371 | # Generate the binary signature, which will be just the integer that comprises |
| 91 | # the signature with no metadata attached. | 372 | # the signature with no metadata attached. |
| 92 | # | 373 | # |
| 93 | openssl rsautl -sign -inkey $key -keyform PEM -in $src.dig -out $src.sig || exit $? | 374 | my $pid; |
| 94 | signerlen=`stat -c %s $x509.signer` | 375 | $pid = open2(*read_from, *write_to, |
| 95 | keyidlen=`stat -c %s $x509.keyid` | 376 | "openssl rsautl -sign -inkey $private_key -keyform PEM") || |
| 96 | siglen=`stat -c %s $src.sig` | 377 | die "openssl rsautl"; |
| 378 | binmode write_to; | ||
| 379 | print write_to $prologue . $digest || die "pipe to openssl rsautl"; | ||
| 380 | close(write_to) || die "pipe to openssl rsautl"; | ||
| 381 | |||
| 382 | binmode read_from; | ||
| 383 | my $signature; | ||
| 384 | read(read_from, $signature, 4096) || die "pipe from openssl rsautl"; | ||
| 385 | close(read_from) || die "pipe from openssl rsautl"; | ||
| 386 | $signature = pack("n", length($signature)) . $signature, | ||
| 387 | |||
| 388 | waitpid($pid, 0) || die; | ||
| 389 | die "openssl rsautl died: $?" if ($? >> 8); | ||
| 97 | 390 | ||
| 98 | # | 391 | # |
| 99 | # Build the signed binary | 392 | # Build the signed binary |
| 100 | # | 393 | # |
| 101 | ( | 394 | my $unsigned_module = read_file($module); |
| 102 | cat $src || exit $? | 395 | |
| 103 | echo '~Module signature appended~' || exit $? | 396 | my $magic_number = "~Module signature appended~\n"; |
| 104 | cat $x509.signer $x509.keyid || exit $? | 397 | |
| 398 | my $info = pack("CCCCCxxxN", | ||
| 399 | $algo, $hash, $id_type, | ||
| 400 | length($signers_name), | ||
| 401 | length($key_identifier), | ||
| 402 | length($signature)); | ||
| 105 | 403 | ||
| 106 | # Preface each signature integer with a 2-byte BE length | 404 | if ($verbose) { |
| 107 | perl -e "binmode STDOUT; print pack(\"n\", $siglen)" || exit $? | 405 | print "Size of unsigned module: ", length($unsigned_module), "\n"; |
| 108 | cat $src.sig || exit $? | 406 | print "Size of signer's name : ", length($signers_name), "\n"; |
| 407 | print "Size of key identifier : ", length($key_identifier), "\n"; | ||
| 408 | print "Size of signature : ", length($signature), "\n"; | ||
| 409 | print "Size of informaton : ", length($info), "\n"; | ||
| 410 | print "Size of magic number : ", length($magic_number), "\n"; | ||
| 411 | print "Signer's name : '", $signers_name, "'\n"; | ||
| 412 | print "Digest : $dgst\n"; | ||
| 413 | } | ||
| 109 | 414 | ||
| 110 | # Generate the information block | 415 | open(FD, ">$dest") || die $dest; |
| 111 | perl -e "binmode STDOUT; print pack(\"CCCCCxxxN\", $algo, $hash, $id_type, $signerlen, $keyidlen, $siglen + 2)" || exit $? | 416 | binmode FD; |
| 112 | ) >$dst~ || exit $? | 417 | print FD |
| 418 | $unsigned_module, | ||
| 419 | $signers_name, | ||
| 420 | $key_identifier, | ||
| 421 | $signature, | ||
| 422 | $info, | ||
| 423 | $magic_number | ||
| 424 | ; | ||
| 425 | close FD || die $dest; | ||
| 113 | 426 | ||
| 114 | # Permit in-place signing | 427 | if ($#ARGV != 3) { |
| 115 | mv $dst~ $dst || exit $? | 428 | rename($dest, $module) || die $module; |
| 429 | } | ||
diff --git a/scripts/x509keyid b/scripts/x509keyid deleted file mode 100755 index c8e91a4af385..000000000000 --- a/scripts/x509keyid +++ /dev/null | |||
| @@ -1,268 +0,0 @@ | |||
| 1 | #!/usr/bin/perl -w | ||
| 2 | # | ||
| 3 | # Generate an identifier from an X.509 certificate that can be placed in a | ||
| 4 | # module signature to indentify the key to use. | ||
| 5 | # | ||
| 6 | # Format: | ||
| 7 | # | ||
| 8 | # ./scripts/x509keyid <x509-cert> <signer's-name> <key-id> | ||
| 9 | # | ||
| 10 | # We read the DER-encoded X509 certificate and parse it to extract the Subject | ||
| 11 | # name and Subject Key Identifier. The provide the data we need to build the | ||
| 12 | # certificate identifier. | ||
| 13 | # | ||
| 14 | # The signer's name part of the identifier is fabricated from the commonName, | ||
| 15 | # the organizationName or the emailAddress components of the X.509 subject | ||
| 16 | # name and written to the second named file. | ||
| 17 | # | ||
| 18 | # The subject key ID to select which of that signer's certificates we're | ||
| 19 | # intending to use to sign the module is written to the third named file. | ||
| 20 | # | ||
| 21 | use strict; | ||
| 22 | |||
| 23 | my $raw_data; | ||
| 24 | |||
| 25 | die "Need three filenames\n" if ($#ARGV != 2); | ||
| 26 | |||
| 27 | my $src = $ARGV[0]; | ||
| 28 | |||
| 29 | open(FD, "<$src") || die $src; | ||
| 30 | binmode FD; | ||
| 31 | my @st = stat(FD); | ||
| 32 | die $src if (!@st); | ||
| 33 | read(FD, $raw_data, $st[7]) || die $src; | ||
| 34 | close(FD); | ||
| 35 | |||
| 36 | my $UNIV = 0 << 6; | ||
| 37 | my $APPL = 1 << 6; | ||
| 38 | my $CONT = 2 << 6; | ||
| 39 | my $PRIV = 3 << 6; | ||
| 40 | |||
| 41 | my $CONS = 0x20; | ||
| 42 | |||
| 43 | my $BOOLEAN = 0x01; | ||
| 44 | my $INTEGER = 0x02; | ||
| 45 | my $BIT_STRING = 0x03; | ||
| 46 | my $OCTET_STRING = 0x04; | ||
| 47 | my $NULL = 0x05; | ||
| 48 | my $OBJ_ID = 0x06; | ||
| 49 | my $UTF8String = 0x0c; | ||
| 50 | my $SEQUENCE = 0x10; | ||
| 51 | my $SET = 0x11; | ||
| 52 | my $UTCTime = 0x17; | ||
| 53 | my $GeneralizedTime = 0x18; | ||
| 54 | |||
| 55 | my %OIDs = ( | ||
| 56 | pack("CCC", 85, 4, 3) => "commonName", | ||
| 57 | pack("CCC", 85, 4, 6) => "countryName", | ||
| 58 | pack("CCC", 85, 4, 10) => "organizationName", | ||
| 59 | pack("CCC", 85, 4, 11) => "organizationUnitName", | ||
| 60 | pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 1, 1) => "rsaEncryption", | ||
| 61 | pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 1, 5) => "sha1WithRSAEncryption", | ||
| 62 | pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 9, 1) => "emailAddress", | ||
| 63 | pack("CCC", 85, 29, 35) => "authorityKeyIdentifier", | ||
| 64 | pack("CCC", 85, 29, 14) => "subjectKeyIdentifier", | ||
| 65 | pack("CCC", 85, 29, 19) => "basicConstraints" | ||
| 66 | ); | ||
| 67 | |||
| 68 | ############################################################################### | ||
| 69 | # | ||
| 70 | # Extract an ASN.1 element from a string and return information about it. | ||
| 71 | # | ||
| 72 | ############################################################################### | ||
| 73 | sub asn1_extract($$@) | ||
| 74 | { | ||
| 75 | my ($cursor, $expected_tag, $optional) = @_; | ||
| 76 | |||
| 77 | return [ -1 ] | ||
| 78 | if ($cursor->[1] == 0 && $optional); | ||
| 79 | |||
| 80 | die $src, ": ", $cursor->[0], ": ASN.1 data underrun (elem ", $cursor->[1], ")\n" | ||
| 81 | if ($cursor->[1] < 2); | ||
| 82 | |||
| 83 | my ($tag, $len) = unpack("CC", substr(${$cursor->[2]}, $cursor->[0], 2)); | ||
| 84 | |||
| 85 | if ($expected_tag != -1 && $tag != $expected_tag) { | ||
| 86 | return [ -1 ] | ||
| 87 | if ($optional); | ||
| 88 | die $src, ": ", $cursor->[0], ": ASN.1 unexpected tag (", $tag, | ||
| 89 | " not ", $expected_tag, ")\n"; | ||
| 90 | } | ||
| 91 | |||
| 92 | $cursor->[0] += 2; | ||
| 93 | $cursor->[1] -= 2; | ||
| 94 | |||
| 95 | die $src, ": ", $cursor->[0], ": ASN.1 long tag\n" | ||
| 96 | if (($tag & 0x1f) == 0x1f); | ||
| 97 | die $src, ": ", $cursor->[0], ": ASN.1 indefinite length\n" | ||
| 98 | if ($len == 0x80); | ||
| 99 | |||
| 100 | if ($len > 0x80) { | ||
| 101 | my $l = $len - 0x80; | ||
| 102 | die $src, ": ", $cursor->[0], ": ASN.1 data underrun (len len $l)\n" | ||
| 103 | if ($cursor->[1] < $l); | ||
| 104 | |||
| 105 | if ($l == 0x1) { | ||
| 106 | $len = unpack("C", substr(${$cursor->[2]}, $cursor->[0], 1)); | ||
| 107 | } elsif ($l = 0x2) { | ||
| 108 | $len = unpack("n", substr(${$cursor->[2]}, $cursor->[0], 2)); | ||
| 109 | } elsif ($l = 0x3) { | ||
| 110 | $len = unpack("C", substr(${$cursor->[2]}, $cursor->[0], 1)) << 16; | ||
| 111 | $len = unpack("n", substr(${$cursor->[2]}, $cursor->[0] + 1, 2)); | ||
| 112 | } elsif ($l = 0x4) { | ||
| 113 | $len = unpack("N", substr(${$cursor->[2]}, $cursor->[0], 4)); | ||
| 114 | } else { | ||
| 115 | die $src, ": ", $cursor->[0], ": ASN.1 element too long (", $l, ")\n"; | ||
| 116 | } | ||
| 117 | |||
| 118 | $cursor->[0] += $l; | ||
| 119 | $cursor->[1] -= $l; | ||
| 120 | } | ||
| 121 | |||
| 122 | die $src, ": ", $cursor->[0], ": ASN.1 data underrun (", $len, ")\n" | ||
| 123 | if ($cursor->[1] < $len); | ||
| 124 | |||
| 125 | my $ret = [ $tag, [ $cursor->[0], $len, $cursor->[2] ] ]; | ||
| 126 | $cursor->[0] += $len; | ||
| 127 | $cursor->[1] -= $len; | ||
| 128 | |||
| 129 | return $ret; | ||
| 130 | } | ||
| 131 | |||
| 132 | ############################################################################### | ||
| 133 | # | ||
| 134 | # Retrieve the data referred to by a cursor | ||
| 135 | # | ||
| 136 | ############################################################################### | ||
| 137 | sub asn1_retrieve($) | ||
| 138 | { | ||
| 139 | my ($cursor) = @_; | ||
| 140 | my ($offset, $len, $data) = @$cursor; | ||
| 141 | return substr($$data, $offset, $len); | ||
| 142 | } | ||
| 143 | |||
| 144 | ############################################################################### | ||
| 145 | # | ||
| 146 | # Roughly parse the X.509 certificate | ||
| 147 | # | ||
| 148 | ############################################################################### | ||
| 149 | my $cursor = [ 0, length($raw_data), \$raw_data ]; | ||
| 150 | |||
| 151 | my $cert = asn1_extract($cursor, $UNIV | $CONS | $SEQUENCE); | ||
| 152 | my $tbs = asn1_extract($cert->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 153 | my $version = asn1_extract($tbs->[1], $CONT | $CONS | 0, 1); | ||
| 154 | my $serial_number = asn1_extract($tbs->[1], $UNIV | $INTEGER); | ||
| 155 | my $sig_type = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 156 | my $issuer = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 157 | my $validity = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 158 | my $subject = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 159 | my $key = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 160 | my $issuer_uid = asn1_extract($tbs->[1], $CONT | $CONS | 1, 1); | ||
| 161 | my $subject_uid = asn1_extract($tbs->[1], $CONT | $CONS | 2, 1); | ||
| 162 | my $extension_list = asn1_extract($tbs->[1], $CONT | $CONS | 3, 1); | ||
| 163 | |||
| 164 | my $subject_key_id = (); | ||
| 165 | my $authority_key_id = (); | ||
| 166 | |||
| 167 | # | ||
| 168 | # Parse the extension list | ||
| 169 | # | ||
| 170 | if ($extension_list->[0] != -1) { | ||
| 171 | my $extensions = asn1_extract($extension_list->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 172 | |||
| 173 | while ($extensions->[1]->[1] > 0) { | ||
| 174 | my $ext = asn1_extract($extensions->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 175 | my $x_oid = asn1_extract($ext->[1], $UNIV | $OBJ_ID); | ||
| 176 | my $x_crit = asn1_extract($ext->[1], $UNIV | $BOOLEAN, 1); | ||
| 177 | my $x_val = asn1_extract($ext->[1], $UNIV | $OCTET_STRING); | ||
| 178 | |||
| 179 | my $raw_oid = asn1_retrieve($x_oid->[1]); | ||
| 180 | next if (!exists($OIDs{$raw_oid})); | ||
| 181 | my $x_type = $OIDs{$raw_oid}; | ||
| 182 | |||
| 183 | my $raw_value = asn1_retrieve($x_val->[1]); | ||
| 184 | |||
| 185 | if ($x_type eq "subjectKeyIdentifier") { | ||
| 186 | my $vcursor = [ 0, length($raw_value), \$raw_value ]; | ||
| 187 | |||
| 188 | $subject_key_id = asn1_extract($vcursor, $UNIV | $OCTET_STRING); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | ############################################################################### | ||
| 194 | # | ||
| 195 | # Determine what we're going to use as the signer's name. In order of | ||
| 196 | # preference, take one of: commonName, organizationName or emailAddress. | ||
| 197 | # | ||
| 198 | ############################################################################### | ||
| 199 | my $org = ""; | ||
| 200 | my $cn = ""; | ||
| 201 | my $email = ""; | ||
| 202 | |||
| 203 | while ($subject->[1]->[1] > 0) { | ||
| 204 | my $rdn = asn1_extract($subject->[1], $UNIV | $CONS | $SET); | ||
| 205 | my $attr = asn1_extract($rdn->[1], $UNIV | $CONS | $SEQUENCE); | ||
| 206 | my $n_oid = asn1_extract($attr->[1], $UNIV | $OBJ_ID); | ||
| 207 | my $n_val = asn1_extract($attr->[1], -1); | ||
| 208 | |||
| 209 | my $raw_oid = asn1_retrieve($n_oid->[1]); | ||
| 210 | next if (!exists($OIDs{$raw_oid})); | ||
| 211 | my $n_type = $OIDs{$raw_oid}; | ||
| 212 | |||
| 213 | my $raw_value = asn1_retrieve($n_val->[1]); | ||
| 214 | |||
| 215 | if ($n_type eq "organizationName") { | ||
| 216 | $org = $raw_value; | ||
| 217 | } elsif ($n_type eq "commonName") { | ||
| 218 | $cn = $raw_value; | ||
| 219 | } elsif ($n_type eq "emailAddress") { | ||
| 220 | $email = $raw_value; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | my $id_name = $email; | ||
| 225 | |||
| 226 | if ($org && $cn) { | ||
| 227 | # Don't use the organizationName if the commonName repeats it | ||
| 228 | if (length($org) <= length($cn) && | ||
| 229 | substr($cn, 0, length($org)) eq $org) { | ||
| 230 | $id_name = $cn; | ||
| 231 | goto got_id_name; | ||
| 232 | } | ||
| 233 | |||
| 234 | # Or a signifcant chunk of it | ||
| 235 | if (length($org) >= 7 && | ||
| 236 | length($cn) >= 7 && | ||
| 237 | substr($cn, 0, 7) eq substr($org, 0, 7)) { | ||
| 238 | $id_name = $cn; | ||
| 239 | goto got_id_name; | ||
| 240 | } | ||
| 241 | |||
| 242 | $id_name = $org . ": " . $cn; | ||
| 243 | } elsif ($org) { | ||
| 244 | $id_name = $org; | ||
| 245 | } elsif ($cn) { | ||
| 246 | $id_name = $cn; | ||
| 247 | } | ||
| 248 | |||
| 249 | got_id_name: | ||
| 250 | |||
| 251 | ############################################################################### | ||
| 252 | # | ||
| 253 | # Output the signer's name and the key identifier that we're going to include | ||
| 254 | # in module signatures. | ||
| 255 | # | ||
| 256 | ############################################################################### | ||
| 257 | die $src, ": ", "X.509: Couldn't find the Subject Key Identifier extension\n" | ||
| 258 | if (!$subject_key_id); | ||
| 259 | |||
| 260 | my $id_key_id = asn1_retrieve($subject_key_id->[1]); | ||
| 261 | |||
| 262 | open(OUTFD, ">$ARGV[1]") || die $ARGV[1]; | ||
| 263 | print OUTFD $id_name; | ||
| 264 | close OUTFD || die $ARGV[1]; | ||
| 265 | |||
| 266 | open(OUTFD, ">$ARGV[2]") || die $ARGV[2]; | ||
| 267 | print OUTFD $id_key_id; | ||
| 268 | close OUTFD || die $ARGV[2]; | ||
