pax_global_header00006660000000000000000000000064127057250640014522gustar00rootroot0000000000000052 comment=0ccfc09eea60aee403834d84a1372292897d55af pesign-0.112/000077500000000000000000000000001270572506400130125ustar00rootroot00000000000000pesign-0.112/.gitignore000066400000000000000000000002321270572506400147770ustar00rootroot00000000000000.*.d .*.P .*.sw? *.der *.cer *.strace *.E *.esl *.auth *-d719b2cb-3d3a-4596-a3bc-dad00e67656f *-8be4df61-93ca-11d2-aa0d-00e098032b8c *.tar.* *.rpm core.* pesign-0.112/COPYING000066400000000000000000000432541270572506400140550ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 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. This program 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-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. pesign-0.112/Make.defaults000066400000000000000000000043131270572506400154210ustar00rootroot00000000000000prefix ?= /usr/ prefix := $(abspath $(prefix))/ libdir ?= $(prefix)lib64/ libdatadir ?= $(prefix)lib/ libexecdir ?= $(prefix)libexec/ datadir ?= $(prefix)share/ mandir ?= $(datadir)man/ includedir ?= $(prefix)include/ bindir ?= $(prefix)bin/ pcdir ?= $(libdir)pkgconfig/ docdir ?= $(prefix)share/doc/ DESTDIR ?= INSTALLROOT = $(DESTDIR) INSTALL ?= install CROSS_COMPILE ?= $(bindir) PKG_CONFIG = $(CROSS_COMPILE)pkg-config CC := $(if $(filter default,$(origin CC)),$(CROSS_COMPILE)gcc,$(CC)) CCLD := $(if $(filter undefined,$(origin CCLD)),$(CC),$(CCLD)) CFLAGS ?= -O0 -g3 -fvar-tracking -fvar-tracking-assignments \ -Wall -Werror -Wextra AS := $(CROSS_COMPILE)as AR := $(CROSS_COMPILE)gcc-ar RANLIB := $(CROSS_COMPILE)gcc-ranlib OBJCOPY := $(CROSS_COMPILE)objcopy LD := $(CROSS_COMPILE)ld INSTALL := $(CROSS_COMPILE)install PKGS = HOSTARCH = $(shell uname -m | sed s,i[3456789]86,ia32,) ARCH := $(shell uname -m | sed s,i[3456789]86,ia32,) SOFLAGS = -shared clang_cflags = gcc_cflags = -Wmaybe-uninitialized cflags = $(CFLAGS) $(ARCH3264) \ -Wall -Werror -Wno-cpp -Wsign-compare -Wno-unused-result \ -Wno-unused-function\ -std=gnu11 -fshort-wchar -fPIC -flto -fno-strict-aliasing \ -fno-merge-constants -fkeep-inline-functions \ -D_GNU_SOURCE -DCONFIG_$(ARCH) -I${TOPDIR}/include \ $(if $(filter $(CC),clang),$(clang_cflags), ) \ $(if $(filter $(CC),gcc),$(gcc_cflags), ) \ $(call pkg-config-cflags) -Werror clang_ccldflags = gcc_ccldflags = -fno-merge-constants \ -Wl,--fatal-warnings,--no-allow-shlib-undefined,--default-symver \ -Wl,-O2 -Wl,--no-undefined-version -Wl,-z,relro,-z,now \ --Wl,--no-add-needed,--no-copy-dt-needed-entries,--as-needed ccldflags = $(cflags) $(CCLDFLAGS) $(LDFLAGS) \ $(if $(filter $(CCLD),clang),$(clang_ccldflags),) \ $(if $(filter $(CCLD),gcc),$(gcc_ccldflags),) \ $(call pkg-config-ccldflags) efi_cflags = $(cflags) ASFLAGS = $(ARCH3264) CPPFLAGS ?= LDLIBS = $(foreach lib,$(LIBS),-l$(lib)) $(call pkg-config-ldlibs) ifeq ($(ARCH),ia64) efi_cflags += -mfixed-range=f32-f127 endif ifeq ($(ARCH), ia32) ifeq ($(HOSTARCH), x86_64) ARCH3264 := -m32 endif endif ifeq ($(ARCH), x86_64) efi_cflags += -mno-red-zone ifeq ($(HOSTARCH), ia32) ARCH3264 := -m64 endif endif pesign-0.112/Make.deps000066400000000000000000000005421270572506400145450ustar00rootroot00000000000000SRCDIR = $(realpath .) TOPDIR = $(realpath ..) include $(TOPDIR)/Make.version include $(TOPDIR)/Make.rules include $(TOPDIR)/Make.defaults .%.d : %.c $(CC) $(cflags) $(CPPFLAGS) -MM -MG -MF $@ $^ .%.d : %.S $(CC) $(cflags) $(CPPFLAGS) -MM -MG -MF $@ $^ SOURCES ?= deps : echo SOURCES=$(SOURCES) deps : $(call deps-of,$(filter-out %.h,$(SOURCES))) pesign-0.112/Make.efirules000066400000000000000000000007541270572506400154350ustar00rootroot00000000000000default : all .PHONY: default all deps clean install test include $(TOPDIR)/Make.version %.efi : %.so $(OBJCOPY) -j .text -j .sdata -j .data -j .dynamic -j .dynsym -j .rel \ -j .rela -j .reloc --target=$(FORMAT) $*.so $@ %.so: %.o $(LD) $(LDFLAGS) -o $@ $^ $(LIBS) %.o: %.c $(CC) $(INCDIR) $(CFLAGS) $(CPPFLAGS) -D__UEFI__ -c $< -o $@ %.S: %.c $(CC) $(INCDIR) $(CFLAGS) $(CPPFLAGS) -D__UEFI__ -S $< -o $@ %.E: %.c $(CC) $(INCDIR) $(CFLAGS) $(CPPFLAGS) -D__UEFI__ -E $< -o $@ pesign-0.112/Make.rules000066400000000000000000000031051270572506400147420ustar00rootroot00000000000000default : all .PHONY: default all deps clean install install_systemd install_sysvinit test include $(TOPDIR)/Make.version all : deps deps : clean : install : install_systemd : install_sysvinit : test : %.a : $(AR) -cvqs $@ $^ % : %.o $(CCLD) $(ccldflags) $(CPPFLAGS) -o $@ $^ $(LDLIBS) %.so : $(CCLD) $(ccldflags) $(CPPFLAGS) $(SOFLAGS) \ -Wl,-soname,$@.$(MAJOR_VERSION) \ -o $@ $^ $(LDLIBS) %.o: %.c $(CC) $(cflags) $(CPPFLAGS) -c -o $@ $(filter %.c %.o %.S,$^) %.S: %.c $(CC) $(cflags) $(CPPFLAGS) -S $< -o $@ %.E: %.c $(CC) $(cflags) $(CPPFLAGS) -E $< -o $@ %.c : %.h .%.d : define substitute-version = sed \ -e "s,@@VERSION@@,$(VERSION),g" \ -e "s,@@LIBDIR@@,$(libdir),g" \ -e "s,@@LIBEXECDIR@@,$(libexecdir),g" \ $(1) > $(2) endef %.pc : %.pc.in @$(call substitute-version,$<,$@) %.spec : %.spec.in @$(call substitute-version,$<,$@) %.service : %.service.in @$(call substitute-version,$<,$@) %.sysvinit : %.sysvinit.in @$(call substitute-version,$<,$@) pkg-config-cflags = \ $(shell if [ -n "$(PKGS)" ]; then $(PKG_CONFIG) --cflags $(PKGS); fi) pkg-config-ldflags = \ $(shell if [ -n "$(PKGS)" ]; then $(PKG_CONFIG) --libs-only-L --libs-only-other $(PKGS) ; fi) pkg-config-ldlibs = \ $(shell if [ -n "$(PKGS)" ]; then $(PKG_CONFIG) --libs-only-l $(PKGS) ; fi) objects-of = \ $(patsubst %.c,%.o,$(1)) define deps-of = $(foreach src,$(filter %.c,$(1)),$(patsubst %.c,.%.d,$(src))) \ $(foreach src,$(filter %.S,$(1)),$(patsubst %.S,.%.d,$(src))) endef $(TOPDIR)/libdpe/%.a $(TOPDIR)/libdpe/% : $(MAKE) -C $(TOPDIR)/libdpe $(notdir $@) pesign-0.112/Make.version000066400000000000000000000001221270572506400152710ustar00rootroot00000000000000MAJOR_VERSION = 0 MINOR_VERSION = 112 VERSION = $(MAJOR_VERSION).$(MINOR_VERSION) pesign-0.112/Makefile000066400000000000000000000032531270572506400144550ustar00rootroot00000000000000SRCDIR = $(realpath .) TOPDIR = $(realpath .) include $(TOPDIR)/Make.version include $(TOPDIR)/Make.rules include $(TOPDIR)/Make.defaults SUBDIRS := include libdpe src install : $(INSTALL) -d -m 755 $(INSTALLROOT)$(docdir)/pesign-$(VERSION)/ $(INSTALL) -pm 644 COPYING $(INSTALLROOT)$(docdir)/pesign-$(VERSION)/ @set -e ; for x in $(SUBDIRS) ; do \ $(MAKE) -C $$x $@ ; \ done install_systemd install_sysvinit : install @set -e ; for x in $(SUBDIRS) ; do \ $(MAKE) -C $$x $@ ; \ done distclean : | clean clean deps all : | Make.version @set -e ; for x in $(SUBDIRS) ; do \ $(MAKE) -C $$x $@ ; \ done $(SUBDIRS) : $(MAKE) -C $@ all .PHONY: $(SUBDIRS) GITTAG = $(VERSION) test-archive: @rm -rf /tmp/pesign-$(VERSION) /tmp/pesign-$(VERSION)-tmp @mkdir -p /tmp/pesign-$(VERSION)-tmp @git archive --format=tar $(shell git branch | awk '/^*/ { print $$2 }') | ( cd /tmp/pesign-$(VERSION)-tmp/ ; tar x ) @git diff | ( cd /tmp/pesign-$(VERSION)-tmp/ ; patch -s -p1 -b -z .gitdiff ) @mv /tmp/pesign-$(VERSION)-tmp/ /tmp/pesign-$(VERSION)/ @dir=$$PWD; cd /tmp; tar -c --bzip2 -f $$dir/pesign-$(VERSION).tar.bz2 pesign-$(VERSION) @rm -rf /tmp/pesign-$(VERSION) @echo "The archive is in pesign-$(VERSION).tar.bz2" tag: git tag -s $(GITTAG) refs/heads/master archive: tag @rm -rf /tmp/pesign-$(VERSION) /tmp/pesign-$(VERSION)-tmp @mkdir -p /tmp/pesign-$(VERSION)-tmp @git archive --format=tar $(GITTAG) | ( cd /tmp/pesign-$(VERSION)-tmp/ ; tar x ) @mv /tmp/pesign-$(VERSION)-tmp/ /tmp/pesign-$(VERSION)/ @dir=$$PWD; cd /tmp; tar -c --bzip2 -f $$dir/pesign-$(VERSION).tar.bz2 pesign-$(VERSION) @rm -rf /tmp/pesign-$(VERSION) @echo "The archive is in pesign-$(VERSION).tar.bz2" pesign-0.112/README000066400000000000000000000003551270572506400136750ustar00rootroot00000000000000Signing tool for PE-COFF binaries, hopefully at least vaguely compliant with the PE and Authenticode specifications. This is vaguely analogous to the tool described by http://msdn.microsoft.com/en-us/library/8s9b9yaz%28v=vs.80%29.aspx pesign-0.112/TODO000066400000000000000000000015771270572506400135140ustar00rootroot00000000000000Stuff that needs to get done: In no particular order, this stuff still needs to be done (or is in progress): 1) file i/o a) get an in-memory representation via mmap *done* b) render in-memory representation back to disk also via mmap *done-ish* 2) signatures a) read hash from stand-alone file *done* b) write hash to stand-alone file *done* c) verify hash from stand-alone file d) read cert from file *done* e) create hash/digest of file *done* f) create new signature (aka "sign the hash") i) generate SpcContentInfo *done* ii) sign it *done* g) create cert table *done for the very basic (ms-supported) case* h) find cert table *eh* i) implant cert in table *eh* 3) make hash algorithm configurable 4) test suite 5) test suite 6) docs 7) efikeygen - random serial number + check for dupes against local nss db See also: https://github.com/vathpela/pesign/issues/milestones pesign-0.112/include/000077500000000000000000000000001270572506400144355ustar00rootroot00000000000000pesign-0.112/include/Makefile000066400000000000000000000004421270572506400160750ustar00rootroot00000000000000SRCDIR = $(realpath .) TOPDIR = $(realpath ..) include $(TOPDIR)/Make.version include $(TOPDIR)/Make.rules include $(TOPDIR)/Make.defaults SUBDIRS = libdpe clean all install : for x in $(SUBDIRS) ; do \ $(MAKE) -C $$x $@ ; \ done $(SUBDIRS) : $(MAKE) -C $@ all .PHONY: $(SUBDIRS) pesign-0.112/include/libdpe/000077500000000000000000000000001270572506400156745ustar00rootroot00000000000000pesign-0.112/include/libdpe/Makefile000066400000000000000000000004311270572506400173320ustar00rootroot00000000000000SRCDIR = $(realpath .) TOPDIR = $(realpath ../..) include $(TOPDIR)/Make.version include $(TOPDIR)/Make.rules include $(TOPDIR)/Make.defaults #install: # $(INSTALL) -d -m 755 $(INSTALLROOT)$(PREFIX)/include/libdpe/ # $(INSTALL) -m 644 *.h $(INSTALLROOT)$(PREFIX)/include/libdpe/ pesign-0.112/include/libdpe/libdpe.h000066400000000000000000000055241270572506400173120ustar00rootroot00000000000000/* * Copyright 2011 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #ifndef LIBDPE_H #define LIBDPE_H 1 #include #include typedef enum { PE_K_NONE, PE_K_MZ, PE_K_PE_OBJ, PE_K_PE_EXE, PE_K_PE_ROM, PE_K_PE64_OBJ, PE_K_PE64_EXE, PE_K_NUM /* terminating entry */ } Pe_Kind; typedef enum { PE_C_NULL, PE_C_READ, PE_C_RDWR, PE_C_WRITE, PE_C_CLR, PE_C_SET, PE_C_FDDONE, PE_C_FDREAD, PE_C_READ_MMAP, PE_C_RDWR_MMAP, PE_C_WRITE_MMAP, PE_C_READ_MMAP_PRIVATE, PE_C_EMPTY, PE_C_NUM /* last entry */ } Pe_Cmd; typedef enum { PE_DATA_DIR_EXPORTS = 1, PE_DATA_DIR_IMPORTS, PE_DATA_DIR_RESOURCES, PE_DATA_DIR_EXCEPTIONS, PE_DATA_DIR_CERTIFICATES, PE_DATA_DIR_BASE_RELOCATIONS, PE_DATA_DIR_DEBUG, PE_DATA_DIR_ARCH, PE_DATA_DIR_GLOBAL_POINTER, PE_DATA_TLS, PE_DATA_LOAD_CONFIG, PE_DATA_BOUND_IMPORT, PE_DATA_IMPORT_ADDRESS, PE_DATA_DELAY_IMPORTS, PE_DATA_CLR_RUNTIME_HEADER, PE_DATA_RESERVED, PE_DATA_NUM /* last entry */ } Pe_DataDir_Type; typedef struct Pe Pe; typedef struct Pe_Scn Pe_Scn; extern Pe *pe_begin(int fildes, Pe_Cmd cmd, Pe *ref); extern Pe *pe_clone(Pe *pe, Pe_Cmd cmd); extern Pe *pe_memory(char *image, size_t size); extern int pe_end(Pe *pe); extern loff_t pe_update(Pe *pe, Pe_Cmd cmd); extern Pe_Kind pe_kind(Pe *Pe) __attribute__ ((__pure__)); extern Pe_Scn *pe_nextscn(Pe *pe, Pe_Scn *scn); extern Pe_Scn *pe_getscn(Pe *pe, size_t idx); extern struct section_header *pe_getshdr(Pe_Scn *scn, struct section_header *dst); extern struct pe_hdr *pe_getpehdr(Pe *pe, struct pe_hdr *pehdr); extern char *pe_rawfile(Pe *pe, size_t *ptr); extern int pe_getdatadir(Pe *pe, data_directory **dd); extern void *pe_getopthdr(Pe *pe); extern uint32_t pe_get_file_alignment(Pe *pe); extern uint32_t pe_get_scn_alignment(Pe *pe); extern int pe_set_image_size(Pe *pe); extern int pe_extend_file(Pe *pe, size_t size, uint32_t *new_space, int align); extern int pe_freespace(Pe *pe, uint32_t offset, size_t size); extern int pe_shorten_file(Pe *pe, size_t size); extern int pe_clearcert(Pe *pe); extern int pe_alloccert(Pe *pe, size_t len); extern int pe_populatecert(Pe *pe, void *cert, size_t len); extern int pe_errno(void); extern const char *pe_errmsg(int error); #endif /* LIBDPE_H */ pesign-0.112/include/libdpe/pe.h000066400000000000000000000360471270572506400164630ustar00rootroot00000000000000/* * Copyright 2011 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #ifndef PE_H #define PE_H 1 #include #define MZ_MAGIC 0x5a4d /* "MZ" */ struct mz_hdr { uint16_t magic; /* MZ_MAGIC */ uint16_t lbsize; /* size of last used block */ uint16_t blocks; /* pages in file, 0x3 */ uint16_t relocs; /* relocations */ uint16_t hdrsize; /* header size in "paragraphs" */ uint16_t min_extra_pps; /* .bss */ uint16_t max_extra_pps; /* runtime limit for the arena size */ uint16_t ss; /* relative stack segment */ uint16_t sp; /* initial %sp register */ uint16_t checksum; /* word checksum */ uint16_t ip; /* initial %ip register */ uint16_t cs; /* initial %cs relative to load segment */ uint16_t reloc_table_offset; /* offset of the first relocation */ uint16_t overlay_num; /* overlay number. set to 0. */ uint16_t reserved0[4]; /* reserved */ uint16_t oem_id; /* oem identifier */ uint16_t oem_info; /* oem specific */ uint16_t reserved1[10]; /* reserved */ uint32_t peaddr; /* address of pe header */ char message[64]; /* message to print */ }; struct mz_reloc { uint16_t offset; uint16_t segment; }; #define PE_MAGIC 0x00004550 /* "PE\0\0" */ #define PE_OPT_MAGIC_PE32 0x010b #define PE_OPT_MAGIC_PE32_ROM 0x0107 #define PE_OPT_MAGIC_PE32PLUS 0x020b /* machine type */ #define IMAGE_FILE_MACHINE_UNKNOWN 0x0000 #define IMAGE_FILE_MACHINE_AM33 0x01d3 #define IMAGE_FILE_MACHINE_AMD64 0x8664 #define IMAGE_FILE_MACHINE_ARM 0x01c0 #define IMAGE_FILE_MACHINE_ARMV7 0x01c4 #define IMAGE_FILE_MACHINE_EBC 0x0ebc #define IMAGE_FILE_MACHINE_I386 0x014c #define IMAGE_FILE_MACHINE_IA64 0x0200 #define IMAGE_FILE_MACHINE_M32R 0x9041 #define IMAGE_FILE_MACHINE_MIPS16 0x0266 #define IMAGE_FILE_MACHINE_MIPSFPU 0x0366 #define IMAGE_FILE_MACHINE_MIPSFPU16 0x0466 #define IMAGE_FILE_MACHINE_POWERPC 0x01f0 #define IMAGE_FILE_MACHINE_POWERPCFP 0x01f1 #define IMAGE_FILE_MACHINE_R4000 0x0166 #define IMAGE_FILE_MACHINE_SH3 0x01a2 #define IMAGE_FILE_MACHINE_SH3DSP 0x01a3 #define IMAGE_FILE_MACHINE_SH3E 0x01a4 #define IMAGE_FILE_MACHINE_SH4 0x01a6 #define IMAGE_FILE_MACHINE_SH5 0x01a8 #define IMAGE_FILE_MACHINE_THUMB 0x01c2 #define IMAGE_FILE_MACHINE_WCEMIPSV2 0x0169 /* flags */ #define IMAGE_FILE_RELOCS_STRIPPED 0x0001 #define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002 #define IMAGE_FILE_LINE_NUMS_STRIPPED 0x0004 #define IMAGE_FILE_LOCAL_SYMS_STRIPPED 0x0008 #define IMAGE_FILE_AGGRESSIVE_WS_TRIM 0x0010 #define IMAGE_FILE_LARGE_ADDRESS_AWARE 0x0020 #define IMAGE_FILE_16BIT_MACHINE 0x0040 #define IMAGE_FILE_BYTES_REVERSED_LO 0x0080 #define IMAGE_FILE_32BIT_MACHINE 0x0100 #define IMAGE_FILE_DEBUG_STRIPPED 0x0200 #define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP 0x0400 #define IMAGE_FILE_NET_RUN_FROM_SWAP 0x0800 #define IMAGE_FILE_SYSTEM 0x1000 #define IMAGE_FILE_DLL 0x2000 #define IMAGE_FILE_UP_SYSTEM_ONLY 0x4000 #define IMAGE_FILE_BYTES_REVERSED_HI 0x8000 struct pe_hdr { uint32_t magic; /* PE magic */ uint16_t machine; /* machine type */ uint16_t sections; /* number of sections */ uint32_t timestamp; /* time_t */ uint32_t symbol_table; /* symbol table offset */ uint32_t symbols; /* number of symbols */ uint16_t opt_hdr_size; /* size of optional header */ uint16_t flags; /* flags */ }; #define IMAGE_FILE_OPT_ROM_MAGIC 0x107 #define IMAGE_FILE_OPT_PE32_MAGIC 0x10b #define IMAGE_FILE_OPT_PE32_PLUS_MAGIC 0x20b #define IMAGE_SUBSYSTEM_UNKNOWN 0 #define IMAGE_SUBSYSTEM_NATIVE 1 #define IMAGE_SUBSYSTEM_WINDOWS_GUI 2 #define IMAGE_SUBSYSTEM_WINDOWS_CUI 3 #define IMAGE_SUBSYSTEM_POSIX_CUI 7 #define IMAGE_SUBSYSTEM_WINDOWS_CE_GUI 9 #define IMAGE_SUBSYSTEM_EFI_APPLICATION 10 #define IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER 11 #define IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER 12 #define IMAGE_SUBSYSTEM_EFI_ROM_IMAGE 13 #define IMAGE_SUBSYSTEM_XBOX 14 #define IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE 0x0040 #define IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY 0x0080 #define IMAGE_DLL_CHARACTERISTICS_NX_COMPAT 0x0100 #define IMAGE_DLLCHARACTERISTICS_NO_ISOLATION 0x0200 #define IMAGE_DLLCHARACTERISTICS_NO_SEH 0x0400 #define IMAGE_DLLCHARACTERISTICS_NO_BIND 0x0800 #define IMAGE_DLLCHARACTERISTICS_WDM_DRIVER 0x2000 #define IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE 0x8000 /* the fact that pe32 isn't padded where pe32+ is 64-bit means union won't * work right. vomit. */ struct pe32_opt_hdr { /* "standard" header */ uint16_t magic; /* file type */ uint8_t ld_major; /* linker major version */ uint8_t ld_minor; /* linker minor version */ uint32_t text_size; /* size of text section(s) */ uint32_t data_size; /* size of data section(s) */ uint32_t bss_size; /* size of bss section(s) */ uint32_t entry_point; /* file offset of entry point */ uint32_t code_base; /* relative code addr in ram */ uint32_t data_base; /* relative data addr in ram */ /* "windows" header */ uint32_t image_base; /* preferred load address */ uint32_t section_align; /* alignment in bytes */ uint32_t file_align; /* file alignment in bytes */ uint16_t os_major; /* major OS version */ uint16_t os_minor; /* minor OS version */ uint16_t image_major; /* major image version */ uint16_t image_minor; /* minor image version */ uint16_t subsys_major; /* major subsystem version */ uint16_t subsys_minor; /* minor subsystem version */ uint32_t win32_version; /* reserved, must be 0 */ uint32_t image_size; /* image size */ uint32_t header_size; /* header size rounded up to file_align */ uint32_t csum; /* checksum */ uint16_t subsys; /* subsystem */ uint16_t dll_flags; /* more flags! */ uint32_t stack_size_req;/* amt of stack requested */ uint32_t stack_size; /* amt of stack required */ uint32_t heap_size_req; /* amt of heap requested */ uint32_t heap_size; /* amt of heap required */ uint32_t loader_flags; /* reserved, must be 0 */ uint32_t data_dirs; /* number of data dir entries */ }; struct pe32plus_opt_hdr { uint16_t magic; /* file type */ uint8_t major; /* linker major version */ uint8_t minor; /* linker minor version */ uint32_t text_size; /* size of text section(s) */ uint32_t data_size; /* size of data section(s) */ uint32_t bss_size; /* size of bss section(s) */ uint32_t entry_point; /* file offset of entry point */ uint32_t code_base; /* relative code addr in ram */ /* "windows" header */ uint64_t image_base; /* preferred load address */ uint32_t section_align; /* alignment in bytes */ uint32_t file_align; /* file alignment in bytes */ uint16_t os_major; /* major OS version */ uint16_t os_minor; /* minor OS version */ uint16_t image_major; /* major image version */ uint16_t image_minor; /* minor image version */ uint16_t subsys_major; /* major subsystem version */ uint16_t subsys_minor; /* minor subsystem version */ uint32_t win32_version; /* reserved, must be 0 */ uint32_t image_size; /* image size */ uint32_t header_size; /* header size rounded up to file_align */ uint32_t csum; /* checksum */ uint16_t subsys; /* subsystem */ uint16_t dll_flags; /* more flags! */ uint64_t stack_size_req;/* amt of stack requested */ uint64_t stack_size; /* amt of stack required */ uint64_t heap_size_req; /* amt of heap requested */ uint64_t heap_size; /* amt of heap required */ uint32_t loader_flags; /* reserved, must be 0 */ uint32_t data_dirs; /* number of data dir entries */ }; typedef struct _data_dirent { uint32_t virtual_address; /* relative to load address */ uint32_t size; } data_dirent; typedef struct _data_directory { data_dirent exports; /* .edata */ data_dirent imports; /* .idata */ data_dirent resources; /* .rsrc */ data_dirent exceptions; /* .pdata */ data_dirent certs; /* certs */ data_dirent base_relocations; /* .reloc */ data_dirent debug; /* .debug */ data_dirent arch; /* reservered */ data_dirent global_ptr; /* global pointer reg. Size=0 */ data_dirent tls; /* .tls */ data_dirent load_config; /* load configuration structure */ data_dirent bound_imports; /* no idea */ data_dirent import_addrs; /* import address table */ data_dirent delay_imports; /* delay-load import table */ data_dirent clr_runtime_hdr; /* .cor (object only) */ data_dirent reserved; } data_directory; struct section_header { char name[8]; /* name or "/12\0" string tbl offset */ uint32_t virtual_size; /* size of loaded section in ram */ uint32_t virtual_address; /* relative virtual address */ uint32_t raw_data_size; /* size of the section */ uint32_t data_addr; /* file pointer to first page of sec */ uint32_t relocs; /* file pointer to relocation entries */ uint32_t line_numbers; /* line numbers! */ uint16_t num_relocs; /* number of relocations */ uint16_t num_lin_numbers; /* srsly. */ uint32_t flags; }; /* they actually defined 0x00000000 as well, but I think we'll skip that one. */ #define IMAGE_SCN_RESERVED_0 0x00000001 #define IMAGE_SCN_RESERVED_1 0x00000002 #define IMAGE_SCN_RESERVED_2 0x00000004 #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 /* don't pad - obsolete */ #define IMAGE_SCN_RESERVED_3 0x00000010 #define IMAGE_SCN_CNT_CODE 0x00000020 /* .text */ #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 /* .data */ #define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080 /* .bss */ #define IMAGE_SCN_LNK_OTHER 0x00000100 /* reserved */ #define IMAGE_SCN_LNK_INFO 0x00000200 /* .drectve comments */ #define IMAGE_SCN_RESERVED_4 0x00000400 #define IMAGE_SCN_LNK_REMOVE 0x00000800 /* .o only - scn to be rm'd*/ #define IMAGE_SCN_LNK_COMDAT 0x00001000 /* .o only - COMDAT data */ #define IMAGE_SCN_RESERVED_5 0x00002000 /* spec omits this */ #define IMAGE_SCN_RESERVED_6 0x00004000 /* spec omits this */ #define IMAGE_SCN_GPREL 0x00008000 /* global pointer referenced data */ /* spec lists 0x20000 twice, I suspect they meant 0x10000 for one of them */ #define IMAGE_SCN_MEM_PURGEABLE 0x00010000 /* reserved for "future" use */ #define IMAGE_SCN_16BIT 0x00020000 /* reserved for "future" use */ #define IMAGE_SCN_LOCKED 0x00040000 /* reserved for "future" use */ #define IMAGE_SCN_PRELOAD 0x00080000 /* reserved for "future" use */ /* and here they just stuck a 1-byte integer in the middle of a bitfield */ #define IMAGE_SCN_ALIGN_1BYTES 0x00100000 /* it does what it says on the box */ #define IMAGE_SCN_ALIGN_2BYTES 0x00200000 #define IMAGE_SCN_ALIGN_4BYTES 0x00300000 #define IMAGE_SCN_ALIGN_8BYTES 0x00400000 #define IMAGE_SCN_ALIGN_16BYTES 0x00500000 #define IMAGE_SCN_ALIGN_32BYTES 0x00600000 #define IMAGE_SCN_ALIGN_64BYTES 0x00700000 #define IMAGE_SCN_ALIGN_128BYTES 0x00800000 #define IMAGE_SCN_ALIGN_256BYTES 0x00900000 #define IMAGE_SCN_ALIGN_512BYTES 0x00a00000 #define IMAGE_SCN_ALIGN_1024BYTES 0x00b00000 #define IMAGE_SCN_ALIGN_2048BYTES 0x00c00000 #define IMAGE_SCN_ALIGN_4096BYTES 0x00d00000 #define IMAGE_SCN_ALIGN_8192BYTES 0x00e00000 #define IMAGE_SCN_LNK_NRELOC_OVFL 0x01000000 /* extended relocations */ #define IMAGE_SCN_MEM_DISCARDABLE 0x02000000 /* scn can be discarded */ #define IMAGE_SCN_MEM_NOT_CACHED 0x04000000 /* cannot be cached */ #define IMAGE_SCN_MEM_NOT_PAGED 0x08000000 /* not pageable */ #define IMAGE_SCN_MEM_SHARED 0x10000000 /* can be shared */ #define IMAGE_SCN_MEM_EXECUTE 0x20000000 /* can be executed as code */ #define IMAGE_SCN_MEM_READ 0x40000000 /* readable */ #define IMAGE_SCN_MEM_WRITE 0x80000000 /* writeable */ typedef enum { IMAGE_REL_AMD64_ABSOLUTE = 0, IMAGE_REL_AMD64_ADDR64, IMAGE_REL_AMD64_ADDR32, IMAGE_REL_AMD64_ADDR32N, IMAGE_REL_AMD64_REL32, IMAGE_REL_AMD64_REL32_1, IMAGE_REL_AMD64_REL32_2, IMAGE_REL_AMD64_REL32_3, IMAGE_REL_AMD64_REL32_4, IMAGE_REL_AMD64_REL32_5, IMAGE_REL_AMD64_SECTION, IMAGE_REL_AMD64_SECREL, IMAGE_REL_AMD64_SECREL7, IMAGE_REL_AMD64_TOKEN, IMAGE_REL_AMD64_SREL32, IMAGE_REL_AMD64_PAIR, IMAGE_REL_AMD64_SSPAN32, } x64_coff_reloc_type; typedef enum { IMAGE_REL_ARM_ABSOLUTE, IMAGE_REL_ARM_ADDR32, IMAGE_REL_ARM_ADDR32N, IMAGE_REL_ARM_BRANCH2, IMAGE_REL_ARM_BRANCH1, IMAGE_REL_ARM_SECTION, IMAGE_REL_ARM_SECREL, } arm_coff_reloc_type; typedef enum { IMAGE_REL_SH3_ABSOLUTE, IMAGE_REL_SH3_DIRECT16, IMAGE_REL_SH3_DIRECT32, IMAGE_REL_SH3_DIRECT8, IMAGE_REL_SH3_DIRECT8_WORD, IMAGE_REL_SH3_DIRECT8_LONG, IMAGE_REL_SH3_DIRECT4, IMAGE_REL_SH3_DIRECT4_WORD, IMAGE_REL_SH3_DIRECT4_LONG, IMAGE_REL_SH3_PCREL8_WORD, IMAGE_REL_SH3_PCREL8_LONG, IMAGE_REL_SH3_PCREL12_WORD, IMAGE_REL_SH3_STARTOF_SECTION, IMAGE_REL_SH3_SIZEOF_SECTION, IMAGE_REL_SH3_SECTION, IMAGE_REL_SH3_SECREL, IMAGE_REL_SH3_DIRECT32_NB, IMAGE_REL_SH3_GPREL4_LONG, IMAGE_REL_SH3_TOKEN, IMAGE_REL_SHM_PCRELPT, IMAGE_REL_SHM_REFLO, IMAGE_REL_SHM_REFHALF, IMAGE_REL_SHM_RELLO, IMAGE_REL_SHM_RELHALF, IMAGE_REL_SHM_PAIR, IMAGE_REL_SHM_NOMODE, } sh_coff_reloc_type; typedef enum { IMAGE_REL_PPC_ABSOLUTE, IMAGE_REL_PPC_ADDR64, IMAGE_REL_PPC_ADDR32, IMAGE_REL_PPC_ADDR24, IMAGE_REL_PPC_ADDR16, IMAGE_REL_PPC_ADDR14, IMAGE_REL_PPC_REL24, IMAGE_REL_PPC_REL14, IMAGE_REL_PPC_ADDR32N, IMAGE_REL_PPC_SECREL, IMAGE_REL_PPC_SECTION, IMAGE_REL_PPC_SECREL16, IMAGE_REL_PPC_REFHI, IMAGE_REL_PPC_REFLO, IMAGE_REL_PPC_PAIR, IMAGE_REL_PPC_SECRELLO, IMAGE_REL_PPC_GPREL, IMAGE_REL_PPC_TOKEN, } ppc_coff_reloc_type; typedef enum { IMAGE_REL_I386_ABSOLUTE, IMAGE_REL_I386_DIR16, IMAGE_REL_I386_REL16, IMAGE_REL_I386_DIR32, IMAGE_REL_I386_DIR32NB, IMAGE_REL_I386_SEG12, IMAGE_REL_I386_SECTION, IMAGE_REL_I386_SECREL, IMAGE_REL_I386_TOKEN, IMAGE_REL_I386_SECREL7, IMAGE_REL_I386_REL32, } x86_coff_reloc_type; typedef enum { IMAGE_REL_IA64_ABSOLUTE, IMAGE_REL_IA64_IMM14, IMAGE_REL_IA64_IMM22, IMAGE_REL_IA64_IMM64, IMAGE_REL_IA64_DIR32, IMAGE_REL_IA64_DIR64, IMAGE_REL_IA64_PCREL21B, IMAGE_REL_IA64_PCREL21M, IMAGE_REL_IA64_PCREL21F, IMAGE_REL_IA64_GPREL22, IMAGE_REL_IA64_LTOFF22, IMAGE_REL_IA64_SECTION, IMAGE_REL_IA64_SECREL22, IMAGE_REL_IA64_SECREL64I, IMAGE_REL_IA64_SECREL32, IMAGE_REL_IA64_DIR32NB, IMAGE_REL_IA64_SREL14, IMAGE_REL_IA64_SREL22, IMAGE_REL_IA64_SREL32, IMAGE_REL_IA64_UREL32, IMAGE_REL_IA64_PCREL60X, IMAGE_REL_IA64_PCREL60B, IMAGE_REL_IA64_PCREL60F, IMAGE_REL_IA64_PCREL60I, IMAGE_REL_IA64_PCREL60M, IMAGE_REL_IA64_IMMGPREL6, IMAGE_REL_IA64_TOKEN, IMAGE_REL_IA64_GPREL32, IMAGE_REL_IA64_ADDEND, } ia64_coff_reloc_type; struct coff_reloc { uint32_t virtual_address; uint32_t symbol_table_index; union { x64_coff_reloc_type x64_type; arm_coff_reloc_type arm_type; sh_coff_reloc_type sh_type; ppc_coff_reloc_type ppc_type; x86_coff_reloc_type x86_type; ia64_coff_reloc_type ia64_type; uint16_t data; }; }; #endif /* PE_H */ pesign-0.112/libdpe/000077500000000000000000000000001270572506400142515ustar00rootroot00000000000000pesign-0.112/libdpe/.gitignore000066400000000000000000000000421270572506400162350ustar00rootroot00000000000000*~ *.o *.so *.a *.sw? .*.P core.* pesign-0.112/libdpe/Makefile000066400000000000000000000023131270572506400157100ustar00rootroot00000000000000SRCDIR = $(realpath .) TOPDIR = $(realpath ..) include $(TOPDIR)/Make.version include $(TOPDIR)/Make.rules include $(TOPDIR)/Make.defaults LIBTARGETS=libdpe.so STATICLIBTARGETS=libdpe.a TARGETS=$(LIBTARGETS) $(STATICLIBTARGETS) LIBDPE_SOURCES = $(wildcard *.c) ALL_SOURCES=$(LIBDPE_SOURCES) -include $(call deps-of,$(ALL_SOURCES)) all : deps $(TARGETS) libdpe.a : $(call objects-of,$(LIBDPE_SOURCES)) libdpe.a : LIBS=pthread # libdpe.a : | $(wildcard *.h) $(wildcard $(TOPDIR)/include/libdpe/*.h) libdpe.so : $(call objects-of,$(LIBDPE_SOURCES)) libdpe.so : LIBS=pthread # libdpe.so : | $(wildcard *.h) $(wildcard $(TOPDIR)/include/libdpe/*.h) deps : $(ALL_SOURCES) $(MAKE) -f $(TOPDIR)/Make.deps deps SOURCES="$(ALL_SOURCES)" clean : @rm -rfv *~ *.o *.a *.so *.so.* .*.d #install : # $(INSTALL) -d -m 755 $(DESTDIR)$(libdir) # $(foreach x,$(LIBTARGETS), \ # $(INSTALL) -m 755 $(x) $(DESTDIR)$(libdir)/$(x).$(VERSION) ;\ # ln -fs $(x).$(VERSION) $(DESTDIR)$(libdir)/$(x).$(MAJOR_VERSION) ;\ # ln -fs $(x).$(VERSION) $(DESTDIR)$(libdir)/$(x); ) # $(foreach x,$(STATICLIBTARGETS), \ # $(INSTALL) -m 644 $(x) $(DESTDIR)$(libdir)/$(x); ) .SECONDARY : $(foreach x,$(LIBTARGETS),$(x).$(VERSION) $(x).$(MAJOR_VERSION)) pesign-0.112/libdpe/common.h000066400000000000000000000104611270572506400157140ustar00rootroot00000000000000/* * Copyright 2011 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #ifndef LIBDPE_COMMON_H #define LIBDPE_COMMON_H 1 #include #include #define pwrite_retry(fd, buf, len, off) \ TEMP_FAILURE_RETRY (pwrite (fd, buf, len, off)) #define write_retry(fd, buf, n) \ TEMP_FAILURE_RETRY (write (fd, buf, n)) #define pread_retry(fd, buf, len, off) \ TEMP_FAILURE_RETRY (pread (fd, buf, len, off)) #define is_64_bit(pe) ((pe)->flags & IMAGE_FILE_32BIT_MACHINE) #define ALIGNMENT_PADDING(address, align) ((align - (address % align)) % align) #define xfree(x) ({if (x) { free(x); x = NULL; }}) #define xmunmap(addr, size) ({if (addr) { munmap(addr,size); addr = NULL; }}) #include static inline void * __attribute__ ((unused)) compute_mem_addr(Pe *pe, off_t offset) { /* XXX this might not work when we're not mmapped */ return (char *)pe->map_address + le32_to_cpu(offset); } static inline uint32_t __attribute__ ((unused)) compute_file_addr(Pe *pe, void *addr) { /* XXX this might not work when we're not mmapped */ return cpu_to_le32((char *)addr - ((char *)pe->map_address)); } static inline size_t __attribute__ ((unused)) get_shnum(void *map_address, size_t maxsize __attribute__((__unused__))) { size_t result = 0; void *buf = (void *)map_address; struct mz_hdr *mz = (struct mz_hdr *)buf; off_t hdr = (off_t)le32_to_cpu(mz->peaddr); struct pe_hdr *pe = (struct pe_hdr *)(buf + hdr); uint16_t sections = pe->sections; result = le16_to_cpu(sections); return result; } static inline Pe_Kind __attribute__ ((unused)) determine_kind(void *buf, size_t len __attribute__((__unused__))) { Pe_Kind retval = PE_K_NONE; uint16_t mz_magic = MZ_MAGIC; struct mz_hdr *mz = (struct mz_hdr *)buf; if (cmp_le16(&mz->magic, &mz_magic)) return retval; retval = PE_K_MZ; off_t hdr = (off_t)le32_to_cpu(mz->peaddr); struct pe_hdr *pe = (struct pe_hdr *)(buf + hdr); uint32_t pe_magic = PE_MAGIC; if (cmp_le32(&pe->magic, &pe_magic)) return retval; if (pe->flags & IMAGE_FILE_EXECUTABLE_IMAGE) { if (le32_to_cpu(pe->opt_hdr_size) == 0) { /* this PE header is invalid, so return PE_K_MZ */ return retval; } struct pe32_opt_hdr *peo = (struct pe32_opt_hdr *)(buf + hdr + sizeof(*pe)); /* if we don't have an optional header, fall back to testing * our machine type list... */ switch (le16_to_cpu(peo->magic)) { case PE_OPT_MAGIC_PE32: retval = PE_K_PE_EXE; break; case PE_OPT_MAGIC_PE32_ROM: retval = PE_K_PE_ROM; break; case PE_OPT_MAGIC_PE32PLUS: retval = PE_K_PE64_EXE; break; default: /* some magic we don't know? Guess based on * machine type */ retval = is_64_bit(pe) ? PE_K_PE64_EXE : PE_K_PE_EXE; break; } } else { /* this is an object file */ retval = is_64_bit(pe) ? PE_K_PE64_OBJ : PE_K_PE_OBJ; } return retval; } #undef choose_kind #undef is_64_bit static inline Pe * __attribute__ ((unused)) allocate_pe(int fildes, void *map_address, size_t maxsize, Pe_Cmd cmd, Pe *parent, Pe_Kind kind, size_t extra) { Pe *result = (Pe *) calloc(1, sizeof (Pe) + extra); if (result == NULL) { __libpe_seterrno(PE_E_NOMEM); } else { result->kind = kind; result->ref_count = 1; result->cmd = cmd; result->fildes = fildes; result->maximum_size = maxsize; result->map_address = map_address; result->parent = parent; } return result; } /* We often have to update a flag iff a value changed. Make this * convenient. */ #define update_if_changed(var, exp, flag) \ ({ \ __typeof__ (var) *_var = &(var); \ __typeof__ (exp) _exp = (exp); \ if (*_var != _exp) { \ *_var = _exp; \ (flag) |= PE_F_DIRTY; \ } \ }) #endif /* LIBDPE_COMMON_H */ pesign-0.112/libdpe/endian.h000066400000000000000000000041451270572506400156640ustar00rootroot00000000000000/* * Copyright 2011 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #ifndef ENDIAN_H #define ENDIAN_H #include #include #include #if __BYTE_ORDER == __LITTLE_ENDIAN #define cpu_to_le16(x) (x) #define cpu_to_le32(x) (x) #define cpu_to_le64(x) (x) #define le16_to_cpu(x) (x) #define le32_to_cpu(x) (x) #define le64_to_cpu(x) (x) #define cpu_to_be16(x) __bswap_16(x) #define cpu_to_be32(x) __bswap_32(x) #define cpu_to_be64(x) __bswap_64(x) #define be16_to_cpu(x) __bswap_16(x) #define be32_to_cpu(x) __bswap_32(x) #define be64_to_cpu(x) __bswap_64(x) #else #define cpu_to_be16(x) (x) #define cpu_to_be32(x) (x) #define cpu_to_be64(x) (x) #define be16_to_cpu(x) (x) #define be32_to_cpu(x) (x) #define be64_to_cpu(x) (x) #define cpu_to_le16(x) __bswap_16(x) #define cpu_to_le32(x) __bswap_32(x) #define cpu_to_le64(x) __bswap_64(x) #define le16_to_cpu(x) __bswap_16(x) #define le32_to_cpu(x) __bswap_32(x) #define le64_to_cpu(x) __bswap_64(x) #endif static inline uint32_t __attribute__((unused)) SwapBytes32(uint32_t x) { return __bswap_32(x); } static inline int __attribute__((unused)) cmp_le16(uint16_t *ledata, uint16_t *cpudata) { uint16_t tmp = le16_to_cpu(*ledata); return memcmp(&tmp, cpudata, sizeof(*cpudata)); } static inline int __attribute__((unused)) cmp_le32(uint32_t *ledata, uint32_t *cpudata) { uint32_t tmp = le32_to_cpu(*ledata); return memcmp(&tmp, cpudata, sizeof(*cpudata)); } #endif /* ENDIAN_H */ /* vim:set shiftwidth=8 softtabstop=8: */ pesign-0.112/libdpe/libdpe.c000066400000000000000000000020451270572506400156550ustar00rootroot00000000000000/* * Copyright 2011 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include #include "libdpe.h" Pe *pe_clone(Pe *pe __attribute__((__unused__)), Pe_Cmd cmd __attribute__((__unused__))) { return NULL; } Pe *pe_memory(char *image __attribute__((__unused__)), size_t size __attribute__((__unused__))) { return NULL; } Pe_Kind pe_kind(Pe *pe) { return pe == NULL ? PE_K_NONE : pe->kind; } pesign-0.112/libdpe/libdpe.h000066400000000000000000000066661270572506400156770ustar00rootroot00000000000000/* * Copyright 2011-2012 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #ifndef LIBDPE_PRIV_H #define LIBDPE_PRIV_H 1 #include #include "endian.h" enum { PE_F_DIRTY = 0x1, PE_F_MMAPPED = 0x40, PE_F_MALLOCED = 0x80, PE_F_FILEDATA = 0x100, }; enum { PE_E_NOERROR = 0, PE_E_UNKNOWN_ERROR, PE_E_INVALID_HANDLE, PE_E_NOMEM, PE_E_INVALID_FILE, PE_E_WRITE_ERROR, PE_E_INVALID_INDEX, PE_E_INVALID_OP, PE_E_INVALID_CMD, PE_E_INVALID_OPERAND, PE_E_WRONG_ORDER_PEHDR, PE_E_FD_DISABLED, PE_E_FD_MISMATCH, PE_E_UPDATE_RO, PE_E_NUM /* terminating entry */ }; extern void __libpe_seterrno(int value); struct Pe_Scn { size_t index; struct Pe *pe; struct section_header *shdr; unsigned int shdr_flags; unsigned int flags; char *rawdata_base; char *data_base; struct Pe_ScnList *list; }; typedef struct Pe_ScnList { unsigned int cnt; unsigned int max; struct Pe_ScnList *next; struct Pe_Scn data[0]; } Pe_ScnList; struct Pe { /* Address to which the file was mapped. NULL if not mapped. */ char *map_address; Pe *parent; Pe *next; /* command used to create this object */ Pe_Cmd cmd; Pe_Kind kind; int fildes; size_t maximum_size; int flags; int ref_count; union { struct { struct mz_hdr *mzhdr; struct pe_hdr *pehdr; void *reserved0; void *reserved1; struct section_header *shdr; Pe_ScnList *scns_last; unsigned int scnincr; Pe_ScnList scns; } pe; struct { struct mz_hdr *mzhdr; struct pe_hdr *pehdr; void *reserved0; void *reserved1; struct section_header *shdr; Pe_ScnList *scns_last; unsigned int scnincr; Pe_ScnList scns; } pe32_obj; struct { struct mz_hdr *mzhdr; struct pe_hdr *pehdr; void *reserved0; void *reserved1; struct section_header *shdr; Pe_ScnList *scns_last; unsigned int scnincr; Pe_ScnList scns; } pe32_rom; struct { struct mz_hdr *mzhdr; struct pe_hdr *pehdr; struct pe32_opt_hdr *opthdr; data_directory *datadir; struct section_header *shdr; Pe_ScnList *scns_last; unsigned int scnincr; Pe_ScnList scns; } pe32_exe; struct { struct mz_hdr *mzhdr; struct pe_hdr *pehdr; void *reserved0; void *reserved1; struct section_header *shdr; Pe_ScnList *scns_last; unsigned int scnincr; Pe_ScnList scns; } pe32plus_obj; struct { struct mz_hdr *mzhdr; struct pe_hdr *pehdr; struct pe32plus_opt_hdr *opthdr; data_directory *datadir; struct section_header *shdr; Pe_ScnList *scns_last; unsigned int scnincr; Pe_ScnList scns; } pe32plus_exe; } state; }; #include "common.h" extern off_t __pe_updatemmap(Pe *pe, size_t shnum); extern int __pe_updatefile(Pe *pe, size_t shnum); extern off_t __pe_updatenull(Pe *pe, size_t shnum); extern char *__libpe_readall(Pe *pe); #endif /* LIBDPE_PRIV_H */ pesign-0.112/libdpe/pe_addcert.c000066400000000000000000000041731270572506400165140ustar00rootroot00000000000000/* * Copyright 2012 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include #include "libdpe.h" int pe_clearcert(Pe *pe) { int rc; data_directory *dd = NULL; rc = pe_getdatadir(pe, &dd); if (rc < 0) return rc; if (dd->certs.virtual_address != 0) { pe_freespace(pe, dd->certs.virtual_address, dd->certs.size); memset(&dd->certs, '\0', sizeof (dd->certs)); } return 0; } int pe_alloccert(Pe *pe, size_t size) { int rc; data_directory *dd = NULL; pe_clearcert(pe); uint32_t new_space = 0; rc = pe_extend_file(pe, size, &new_space, 8); if (rc < 0) return rc; rc = pe_getdatadir(pe, &dd); if (rc < 0) return rc; void *addr = compute_mem_addr(pe, new_space); /* We leave the whole list empty until finalize...*/ memset(addr, '\0', size); dd->certs.virtual_address = compute_file_addr(pe, addr); dd->certs.size += size; return 0; } int pe_populatecert(Pe *pe, void *cert, size_t size) { int rc; data_directory *dd = NULL; rc = pe_getdatadir(pe, &dd); if (rc < 0) return rc; if (size != dd->certs.size) return -1; void *mem = compute_mem_addr(pe, dd->certs.virtual_address); if (!mem) return -1; memcpy(mem, cert, size); uint64_t max_size = pe->maximum_size; uint32_t new_space; uint32_t page_size = sysconf(_SC_PAGESIZE); pe_extend_file(pe, 0, &new_space, page_size); uint64_t new_max_size = pe->maximum_size; mem = compute_mem_addr(pe, 0); msync(mem, new_max_size, MS_SYNC); new_max_size -= max_size; pe_shorten_file(pe, new_max_size); return 0; } pesign-0.112/libdpe/pe_allocspace.c000066400000000000000000000074251270572506400172170ustar00rootroot00000000000000/* * Copyright 2012 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include "libdpe.h" #include #include #include #define adjust(x,y) ((x) = (typeof (x))(((uint8_t *)(x)) + (y))) static void pe_fix_addresses(Pe *pe, int64_t offset) { pe->map_address += offset; adjust(pe->state.pe.mzhdr, offset); adjust(pe->state.pe.pehdr, offset); adjust(pe->state.pe.reserved0, offset); adjust(pe->state.pe.reserved1, offset); adjust(pe->state.pe.shdr, offset); size_t scncnt = get_shnum(pe->map_address, pe->maximum_size); /* in general this means we couldn't identify any sections, so * they must not need to be fixed up. */ if (scncnt == (size_t)-1l) return; for (size_t cnt = 0; cnt < scncnt; cnt++) { pe->state.pe.scns.data[cnt].shdr = &pe->state.pe.shdr[cnt]; adjust(pe->state.pe.scns.data[cnt].rawdata_base, offset); adjust(pe->state.pe.scns.data[cnt].data_base, offset); } } #undef adjust #define align(val, align) (((val) + (align) -1 ) & (- (align))) int pe_set_image_size(Pe *pe) { uint32_t image_size = 0; struct pe_hdr *pehdr = pe->state.pe.pehdr; struct pe32plus_opt_hdr *opthdr = pe->state.pe32plus_exe.opthdr; Pe_Scn *scn = NULL; struct section_header shdr = { 0, }, tmp_shdr; if (pehdr->sections < 1) return -1; for (int i = 0; i < pehdr->sections; i++) { scn = pe_nextscn(pe, scn); if (scn == NULL) break; pe_getshdr(scn, &tmp_shdr); if (tmp_shdr.virtual_size > 0) memcpy (&shdr, &tmp_shdr, sizeof(shdr)); } int falign = pe_get_file_alignment(pe); int salign = pe_get_scn_alignment(pe); image_size = shdr.virtual_address - opthdr->image_base + align(align(shdr.virtual_size, falign), salign); pe->state.pe32plus_exe.opthdr->image_size = image_size; return 0; } int pe_extend_file(Pe *pe, size_t size, uint32_t *new_space, int align) { void *new = NULL; if (align) align = ALIGNMENT_PADDING(pe->maximum_size, align); int extra = size + align; int rc = ftruncate(pe->fildes, pe->maximum_size + extra); if (rc < 0) return -1; new = mremap(pe->map_address, pe->maximum_size, pe->maximum_size + extra, MREMAP_MAYMOVE); if (new == MAP_FAILED) { __libpe_seterrno (PE_E_NOMEM); return -1; } if (new != pe->map_address) pe_fix_addresses(pe, (uint8_t *)new-(uint8_t *)pe->map_address); char *addr = compute_mem_addr(pe, pe->maximum_size); memset(addr, '\0', extra); *new_space = compute_file_addr(pe, addr + align); pe->maximum_size = pe->maximum_size + extra; return 0; } int pe_shorten_file(Pe *pe, size_t size) { void *new = NULL; new = mremap(pe->map_address, pe->maximum_size, pe->maximum_size - size, 0); if (new == MAP_FAILED) { __libpe_seterrno (PE_E_NOMEM); return -1; } int rc = ftruncate(pe->fildes, pe->maximum_size - size); if (rc < 0) return -1; pe->maximum_size -= size; return 0; } int pe_freespace(Pe *pe, uint32_t offset, size_t size) { void *addr = compute_mem_addr(pe, offset); memset(addr, '\0', size); if (offset + size == pe->maximum_size) pe_shorten_file(pe, size); /* XXX PJFIX TODO: this should actually de-allocate the space, *if* * it's the certificate list, when it isn't at the end of the file, * too. */ return 0; } pesign-0.112/libdpe/pe_begin.c000066400000000000000000000221151270572506400161660ustar00rootroot00000000000000/* * Copyright 2011 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include #include #include #include #include #include #include #include #include #include "libdpe.h" static inline Pe * file_read_pe_obj(int fildes __attribute__((__unused__)), void *map_address __attribute__((__unused__)), unsigned char *p_ident __attribute__((__unused__)), size_t maxsize __attribute__((__unused__)), Pe_Cmd cmd __attribute__((__unused__)), Pe *parent __attribute__((__unused__))) { return NULL; } static inline Pe * file_read_pe_exe(int fildes, void *map_address, unsigned char *p_ident, size_t maxsize, Pe_Cmd cmd __attribute__((__unused__)), Pe *parent) { Pe_Kind kind = determine_kind(p_ident, maxsize); size_t scncnt = get_shnum(map_address, maxsize); if (scncnt == (size_t) -1l) { /* Could not determine the number of sections. */ return NULL; } if (scncnt > SIZE_MAX / sizeof(Pe_Scn) + sizeof (struct section_header)) return NULL; const size_t scnmax = (scncnt ?: (cmd == PE_C_RDWR || cmd == PE_C_RDWR_MMAP) ? 1: 0); Pe *pe = allocate_pe(fildes, map_address, maxsize, cmd, parent, kind, scnmax * sizeof (Pe_Scn)); if (pe == NULL) return NULL; pe->state.pe32_obj.mzhdr = (struct mz_hdr *) ((char *)map_address); pe->state.pe32_obj.pehdr = (struct pe_hdr *)((char *)map_address + (off_t)le32_to_cpu(pe->state.pe32_obj.mzhdr->peaddr)); assert((unsigned int)scncnt == scncnt); pe->state.pe32_obj.scns.cnt = scncnt; pe->state.pe32_obj.scns.max = scnmax; pe->state.pe32_obj.scnincr = 10; size_t ddsize = 0; switch (kind) { case PE_K_PE_OBJ: pe->state.pe32_obj.shdr = (struct section_header *) ((char *)pe->state.pe32_obj.pehdr + sizeof (struct pe_hdr)); break; case PE_K_PE_EXE: pe->state.pe32_exe.opthdr = (struct pe32_opt_hdr *) ((char *)pe->state.pe32_exe.pehdr + sizeof (struct pe_hdr)); pe->state.pe32_exe.datadir = (data_directory *) ((char *)pe->state.pe32_exe.opthdr + sizeof (struct pe32_opt_hdr)); ddsize = le32_to_cpu( pe->state.pe32_exe.opthdr->data_dirs); pe->state.pe32_exe.shdr = (struct section_header *) ((char *)pe->state.pe32_exe.datadir + (sizeof (data_dirent) * ddsize)); break; case PE_K_PE64_OBJ: pe->state.pe32plus_obj.shdr = (struct section_header *) ((char *)pe->state.pe32plus_obj.pehdr + sizeof (struct pe_hdr)); break; case PE_K_PE64_EXE: pe->state.pe32plus_exe.opthdr = (struct pe32plus_opt_hdr *) ((char *)pe->state.pe32plus_exe.pehdr + sizeof (struct pe_hdr)); pe->state.pe32plus_exe.datadir = (data_directory *) ((char *)pe->state.pe32plus_exe.opthdr + sizeof (struct pe32plus_opt_hdr)); ddsize = le32_to_cpu( pe->state.pe32plus_exe.opthdr->data_dirs); pe->state.pe32plus_exe.shdr = (struct section_header *) ((char *)pe->state.pe32plus_exe.datadir + (sizeof (data_dirent) * ddsize)); break; default: break; } for (size_t cnt = 0; cnt < scncnt; cnt++) { pe->state.pe.scns.data[cnt].index = cnt; pe->state.pe.scns.data[cnt].pe = pe; pe->state.pe.scns.data[cnt].shdr = &pe->state.pe.shdr[cnt]; uint32_t raw_data_size = le32_to_cpu(pe->state.pe.shdr[cnt].raw_data_size); uint32_t data_addr = le32_to_cpu(pe->state.pe.shdr[cnt].data_addr); if (data_addr < maxsize && maxsize - data_addr <= raw_data_size) pe->state.pe.scns.data[cnt].rawdata_base = pe->state.pe.scns.data[cnt].data_base = ((char *)map_address + data_addr); pe->state.pe.scns.data[cnt].list = &pe->state.pe.scns; } return pe; } Pe * __libpe_read_mmapped_file(int fildes, void *map_address, size_t maxsize, Pe_Cmd cmd, Pe *parent) { unsigned char *p_ident = (unsigned char *) map_address; Pe_Kind kind = determine_kind (p_ident, maxsize); switch (kind) { case PE_K_PE_OBJ: case PE_K_PE64_OBJ: return file_read_pe_obj(fildes, map_address, p_ident, maxsize, cmd, parent); case PE_K_PE64_EXE: case PE_K_PE_EXE: return file_read_pe_exe(fildes, map_address, p_ident, maxsize, cmd, parent); default: break; } return allocate_pe(fildes, map_address, maxsize, cmd, parent, PE_K_NONE, 0); } static Pe * read_unmmapped_file(int fildes, size_t maxsize, Pe_Cmd cmd, Pe *parent) { union { struct { struct mz_hdr mz; struct pe_hdr pe; }; unsigned char raw[1]; } mem; ssize_t nread = pread_retry (fildes, &mem.mz, sizeof(mem.mz), 0); if (nread == -1) return NULL; /* this handles MZ-only binaries wrong, but who cares, really? */ off_t peaddr = le32_to_cpu(mem.mz.peaddr); ssize_t prev_nread = nread; nread += pread_retry (fildes, &mem.pe, sizeof(mem.pe), peaddr); if (nread == prev_nread) return NULL; mem.mz.peaddr = cpu_to_le32(offsetof(typeof(mem), pe)); Pe_Kind kind = determine_kind(&mem, nread); switch (kind) { case PE_K_PE_OBJ: return file_read_pe_obj(fildes, NULL, mem.raw, maxsize, cmd, parent); case PE_K_PE_EXE: return file_read_pe_exe(fildes, NULL, mem.raw, maxsize, cmd, parent); default: break; } return allocate_pe(fildes, NULL, maxsize, cmd, parent, PE_K_NONE, 0); } static Pe * read_file(int fildes, size_t maxsize, Pe_Cmd cmd, Pe *parent) { void *map_address = NULL; int use_mmap = (cmd == PE_C_READ_MMAP || cmd == PE_C_RDWR_MMAP || cmd == PE_C_WRITE_MMAP || cmd == PE_C_READ_MMAP_PRIVATE); if (use_mmap) { if (parent == NULL) { if (maxsize == ~((size_t) 0)) { struct stat st; if (fstat(fildes, &st) == 0 && (sizeof(size_t) >= sizeof(st.st_size)|| st.st_size <= ~((size_t)0))) maxsize = (size_t) st.st_size; } map_address = mmap(NULL, maxsize, cmd == PE_C_READ_MMAP ? PROT_READ : PROT_READ|PROT_WRITE, cmd == PE_C_READ_MMAP_PRIVATE || cmd == PE_C_READ_MMAP ? MAP_PRIVATE : MAP_SHARED, fildes, 0); if (map_address == MAP_FAILED) map_address = NULL; } else { assert (maxsize != ~((size_t)0)); map_address = parent->map_address; } } if (map_address != NULL) { assert(map_address != MAP_FAILED); struct Pe *result = __libpe_read_mmapped_file(fildes, map_address, maxsize, cmd, parent); if (result == NULL && (parent == NULL || parent->map_address != map_address)) munmap(map_address, maxsize); else if (parent == NULL) result->flags |= PE_F_MMAPPED; return result; } return read_unmmapped_file(fildes, maxsize, cmd, parent); } static struct Pe * write_file (int fd, Pe_Cmd cmd) { #define NSCNSALLOC 10 Pe *result = allocate_pe(fd, NULL, 0, cmd, NULL, PE_K_PE_EXE, NSCNSALLOC * sizeof (Pe_Scn)); if (result != NULL) { result->flags = PE_F_DIRTY; result->state.pe.scnincr = NSCNSALLOC; result->state.pe.scns_last = &result->state.pe.scns; result->state.pe.scns.max = NSCNSALLOC; } return result; } static Pe * dup_pe(int fildes, Pe_Cmd cmd __attribute__((__unused__)), Pe *ref) { if (fildes == -1) { fildes = ref->fildes; } else if (ref->fildes != -1 && fildes != ref->fildes) { __libpe_seterrno(PE_E_FD_MISMATCH); return NULL; } if (ref->cmd != PE_C_READ && ref->cmd != PE_C_READ_MMAP && ref->cmd != PE_C_WRITE && ref->cmd != PE_C_WRITE_MMAP && ref->cmd != PE_C_RDWR && ref->cmd != PE_C_RDWR_MMAP && ref->cmd != PE_C_READ_MMAP_PRIVATE) { __libpe_seterrno(PE_E_INVALID_OP); return NULL; } /* for now, just increment the refcount and return the same object */ ref->ref_count++; return ref; } Pe * pe_begin(int fildes, Pe_Cmd cmd, Pe *ref) { Pe *retval = NULL; if (fcntl(fildes, F_GETFL) == -1 && errno == EBADF) { __libpe_seterrno(PE_E_INVALID_FILE); return NULL; } switch (cmd) { case PE_C_NULL: break; case PE_C_READ_MMAP_PRIVATE: if (ref != NULL && ref->cmd != PE_C_READ_MMAP_PRIVATE) { __libpe_seterrno(PE_E_INVALID_CMD); break; } /* fall through */ case PE_C_READ: case PE_C_READ_MMAP: if (ref != NULL) retval = dup_pe(fildes, cmd, ref); else retval = read_file(fildes, ~((size_t)0), cmd, NULL); break; case PE_C_RDWR: case PE_C_RDWR_MMAP: if (ref != NULL) { if (ref->cmd != PE_C_RDWR && ref->cmd != PE_C_RDWR_MMAP && ref->cmd != PE_C_WRITE && ref->cmd != PE_C_WRITE_MMAP) { __libpe_seterrno(PE_E_INVALID_CMD); retval = NULL; } } else { retval = read_file(fildes, ~((size_t) 0), cmd, NULL); } break; case PE_C_WRITE: case PE_C_WRITE_MMAP: retval = write_file(fildes, cmd); break; default: __libpe_seterrno(PE_E_INVALID_CMD); break; } return retval; } pesign-0.112/libdpe/pe_end.c000066400000000000000000000037701270572506400156560ustar00rootroot00000000000000/* * Copyright 2011 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include #include "libdpe.h" int pe_end(Pe *pe) { Pe *parent = NULL; if (pe == NULL) { /* This is allowed and is a no-op. */ return 0; } if (pe->ref_count != 0 && --pe->ref_count != 0) { int result = pe->ref_count; return result; } parent = pe->parent; switch (pe->kind) { case PE_K_NONE: case PE_K_MZ: break; case PE_K_PE_OBJ: case PE_K_PE_EXE: case PE_K_PE_ROM: case PE_K_PE64_OBJ: case PE_K_PE64_EXE: { Pe_ScnList *list = &pe->state.pe.scns; do { size_t cnt = list->max; while (cnt-- > 0) { Pe_Scn *scn = &list->data[cnt]; if ((scn->shdr_flags & PE_F_MALLOCED)) xfree(scn->shdr); if (scn->data_base != scn->rawdata_base) xfree(scn->data_base); if (pe->map_address == NULL) xfree(scn->rawdata_base); } Pe_ScnList *oldp = list; list = list->next; assert(list == NULL || oldp->cnt == oldp->max); if (oldp != &pe->state.pe.scns) xfree(oldp); } while (list); } break; case PE_K_NUM: default: break; } if (pe->map_address != NULL && parent == NULL) { if (pe->flags & PE_F_MALLOCED) xfree(pe->map_address); else if (pe->flags & PE_F_MMAPPED) xmunmap(pe->map_address, pe->maximum_size); } xfree(pe); return (parent != NULL && parent->ref_count ? pe_end(parent) : 0); } pesign-0.112/libdpe/pe_error.c000066400000000000000000000074061270572506400162410ustar00rootroot00000000000000/* * Copyright 2011 Red Hat, Inc. * All rights reserved. * * 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. * * This program 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, see . * * Author(s): Peter Jones */ #include #include #include "libdpe.h" static int global_error; int pe_errno (void) { int result = global_error; global_error = PE_E_NOERROR; return result; } static const char msgstr[] = { #define PE_E_NOERROR_IDX 0 "no error" "\0" #define PE_E_UNKNOWN_ERROR_IDX \ (PE_E_NOERROR_IDX + sizeof "no error") "unknown error" "\0" #define PE_E_INVALID_HANDLE_IDX \ (PE_E_UNKNOWN_ERROR_IDX + sizeof "unknown error") "invalid 'Pe' handle" "\0" #define PE_E_NOMEM_IDX \ (PE_E_INVALID_HANDLE_IDX + sizeof "invalid 'Pe' handle") "out of memory" "\0" #define PE_E_INVALID_FILE_IDX \ (PE_E_NOMEM_IDX + sizeof "out of memory") "invalid file descriptor" "\0" #define PE_E_WRITE_ERROR_IDX \ (PE_E_INVALID_FILE_IDX + sizeof "invalid file descriptor") "cannot write data to file" "\0" #define PE_E_INVALID_INDEX_IDX \ (PE_E_WRITE_ERROR_IDX + sizeof "cannot write data to file") "invalid section index" "\0" #define PE_E_INVALID_OP_IDX \ (PE_E_INVALID_INDEX_IDX + sizeof "invalid section index") "invalid operation" "\0" #define PE_E_INVALID_CMD_IDX \ (PE_E_INVALID_OP_IDX + sizeof "invalid operation") "invalid command" "\0" #define PE_E_INVALID_OPERAND_IDX \ (PE_E_INVALID_CMD_IDX + sizeof "invalid command") "invalid operand" "\0" #define PE_E_WRONG_ORDER_PEHDR_IDX \ (PE_E_INVALID_OPERAND_IDX + sizeof "invalid operand") "executable header not created first" "\0" #define PE_E_FD_DISABLED_IDX \ (PE_E_WRONG_ORDER_PEHDR_IDX + sizeof "executable header not created first") "file descriptor disabled" "\0" #define PE_E_FD_MISMATCH_IDX \ (PE_E_FD_DISABLED_IDX + sizeof "file descriptor disabled") "file descriptor mismatch" "\0" #define PE_E_UPDATE_RO_IDX \ (PE_E_FD_MISMATCH_IDX + sizeof "file descriptor mismatch") "update() for write on read-only file" }; static const uint16_t msgidx[PE_E_NUM] = { [PE_E_NOERROR] = PE_E_NOERROR_IDX, [PE_E_UNKNOWN_ERROR] = PE_E_UNKNOWN_ERROR_IDX, [PE_E_INVALID_HANDLE] = PE_E_INVALID_HANDLE_IDX, [PE_E_NOMEM] = PE_E_NOMEM_IDX, [PE_E_INVALID_FILE] = PE_E_INVALID_FILE_IDX, [PE_E_WRITE_ERROR] = PE_E_WRITE_ERROR_IDX, [PE_E_INVALID_INDEX] = PE_E_INVALID_INDEX_IDX, [PE_E_INVALID_OP] = PE_E_INVALID_OP_IDX, [PE_E_INVALID_CMD] = PE_E_INVALID_CMD_IDX, [PE_E_INVALID_OPERAND] = PE_E_INVALID_OPERAND_IDX, [PE_E_WRONG_ORDER_PEHDR] = PE_E_WRONG_ORDER_PEHDR_IDX, [PE_E_FD_DISABLED] = PE_E_FD_DISABLED_IDX, [PE_E_FD_MISMATCH] = PE_E_FD_MISMATCH_IDX, [PE_E_UPDATE_RO] = PE_E_UPDATE_RO_IDX, }; #define nmsgidx ((int) (sizeof (msgidx) / sizeof (msgidx[0]))) void __libpe_seterrno(int value) { global_error = value >= 0 && value <= nmsgidx ? value : PE_E_UNKNOWN_ERROR; } const char * pe_errmsg(int error) { int last_error = global_error; if (error == 0) { assert(msgidx[last_error] < sizeof(msgstr)); return last_error != 0 ? msgstr + msgidx[last_error] : NULL; } else if (error < -1 || error >= nmsgidx) { return msgstr + PE_E_UNKNOWN_ERROR_IDX; } assert (msgidx[error == -1 ? last_error : error] < sizeof (msgstr)); return msgstr + msgidx[error == -1 ? last_error : error]; } pesign-0.112/libdpe/pe_getdatadir.c000066400000000000000000000021741270572506400172150ustar00rootroot00000000000000/* * Copyright 2011 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include "libdpe.h" int pe_getdatadir(Pe *pe, data_directory **dd) { int rc = -1; if (!dd) { __libpe_seterrno(PE_E_INVALID_INDEX); return rc; } switch (pe->kind) { case PE_K_PE_EXE: { *dd = pe->state.pe32_exe.datadir; rc = 0; break; } case PE_K_PE64_EXE: { *dd = pe->state.pe32plus_exe.datadir; rc = 0; break; } case PE_K_PE_OBJ: case PE_K_PE64_OBJ: case PE_K_PE_ROM: default: break; } return rc; } pesign-0.112/libdpe/pe_getpehdr.c000066400000000000000000000015731270572506400167110ustar00rootroot00000000000000/* * Copyright 2012 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include "libdpe.h" struct pe_hdr * pe_getpehdr(Pe *pe, struct pe_hdr *dest) { if (pe == NULL) return NULL; memcpy(dest, pe->state.pe.pehdr, sizeof(*dest)); return dest; } pesign-0.112/libdpe/pe_getscn.c000066400000000000000000000025341270572506400163700ustar00rootroot00000000000000/* * Copyright 2011 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include "libdpe.h" Pe_Scn * pe_getscn(Pe *pe, size_t idx) { if (pe == NULL) return NULL; switch (pe->kind) { case PE_K_PE_OBJ: case PE_K_PE_EXE: case PE_K_PE_ROM: case PE_K_PE64_OBJ: case PE_K_PE64_EXE: break; default: __libpe_seterrno(PE_E_INVALID_HANDLE); return NULL; } Pe_Scn *result = NULL; Pe_ScnList *runp = &pe->state.pe.scns; while (1) { if (idx < runp->max) { if (idx < runp->cnt) result = &runp->data[idx]; else __libpe_seterrno(PE_E_INVALID_INDEX); break; } idx -= runp->max; runp = runp->next; if (runp == NULL) { __libpe_seterrno(PE_E_INVALID_INDEX); break; } } return result; } pesign-0.112/libdpe/pe_getshdr.c000066400000000000000000000020121270572506400165340ustar00rootroot00000000000000/* * Copyright 2012 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include "libdpe.h" struct section_header * pe_getshdr(Pe_Scn *scn, struct section_header *dst) { struct section_header *result = NULL; if (scn == NULL) return NULL; if (dst == NULL) { __libpe_seterrno(PE_E_INVALID_OPERAND); return NULL; } result = memcpy(dst, scn->shdr, sizeof(*dst)); return result; } pesign-0.112/libdpe/pe_nextscn.c000066400000000000000000000023041270572506400165620ustar00rootroot00000000000000/* * Copyright 2011 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include #include "libdpe.h" Pe_Scn * pe_nextscn(Pe *pe, Pe_Scn *scn) { Pe_Scn *result = NULL; if (pe == NULL) return NULL; if (scn == NULL) { if (pe->state.pe.scns.cnt > 0) result = &pe->state.pe.scns.data[0]; } else { Pe_ScnList *list = scn->list; if (scn + 1 < &list->data[list->cnt]) { result = scn + 1; } else if (scn + 1 == &list->data[list->max] && (list = list->next) != NULL) { assert(list->cnt > 0); result = &list->data[0]; } } return result; } pesign-0.112/libdpe/pe_opthdr.c000066400000000000000000000034101270572506400163770ustar00rootroot00000000000000/* * Copyright 2011 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include "libdpe.h" void * pe_getopthdr(Pe *pe) { switch (pe_kind(pe)) { case PE_K_PE_EXE: return pe->state.pe32_exe.opthdr; case PE_K_PE64_EXE: return pe->state.pe32plus_exe.opthdr; default: return NULL; } } uint32_t pe_get_file_alignment(Pe *pe) { struct pe32_opt_hdr *pe32opthdr = NULL; struct pe32plus_opt_hdr *pe64opthdr = NULL; switch (pe_kind(pe)) { case PE_K_PE_EXE: { void *opthdr = pe_getopthdr(pe); pe32opthdr = opthdr; return pe32opthdr->file_align; } case PE_K_PE64_EXE: { void *opthdr = pe_getopthdr(pe); pe64opthdr = opthdr; return pe64opthdr->file_align; break; } default: break; } return -1; } uint32_t pe_get_scn_alignment(Pe *pe) { struct pe32_opt_hdr *pe32opthdr = NULL; struct pe32plus_opt_hdr *pe64opthdr = NULL; switch (pe_kind(pe)) { case PE_K_PE_EXE: { void *opthdr = pe_getopthdr(pe); pe32opthdr = opthdr; return pe32opthdr->section_align; } case PE_K_PE64_EXE: { void *opthdr = pe_getopthdr(pe); pe64opthdr = opthdr; return pe64opthdr->section_align; break; } default: break; } return -1; } pesign-0.112/libdpe/pe_rawfile.c000066400000000000000000000021121270572506400165260ustar00rootroot00000000000000/* * Copyright 2012 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include "libdpe.h" char * pe_rawfile(Pe *pe, size_t *size) { char *result; if (pe == NULL) { __libpe_seterrno(PE_E_INVALID_HANDLE); error_out: if (size != NULL) *size = 0; return NULL; } if (pe->map_address == NULL && __libpe_readall(pe) == NULL) goto error_out; if (size != NULL) *size = pe->maximum_size; result = (char *)pe->map_address; return result; } pesign-0.112/libdpe/pe_readall.c000066400000000000000000000014531270572506400165100ustar00rootroot00000000000000/* * Copyright 2012 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include "libdpe.h" char * __libpe_readall(Pe *pe __attribute__((__unused__))) { return NULL; } pesign-0.112/libdpe/pe_update.c000066400000000000000000000056361270572506400163750ustar00rootroot00000000000000/* * Copyright 2011 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include "libdpe.h" #include #include #include static off_t write_file(Pe *pe, off_t size, size_t shnum) { struct stat st; if (fstat(pe->fildes, &st) != 0) { __libpe_seterrno(PE_E_WRITE_ERROR); return -1; } if (pe->parent == NULL && (pe->maximum_size == ~((size_t)0) || (size_t)size > pe->maximum_size) && ftruncate(pe->fildes, size) != 0) { __libpe_seterrno(PE_E_WRITE_ERROR); return -1; } if (pe->map_address == NULL && pe->cmd == PE_C_WRITE_MMAP) { pe->map_address = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, pe->fildes, 0); if (pe->map_address == MAP_FAILED) pe->map_address = NULL; } if (pe->map_address != NULL) { if (__pe_updatemmap(pe, shnum) != 0) size = -1; } else { if (__pe_updatefile(pe, shnum) != 0) size = -1; } if (size != -1 && pe->parent == NULL && pe->maximum_size != ~((size_t)0) && (size_t)size < pe->maximum_size && ftruncate(pe->fildes, size) != 0) { __libpe_seterrno(PE_E_WRITE_ERROR); size = -1; } if (size != -1 && (st.st_mode & (S_ISUID | S_ISGID)) && (fchmod(pe->fildes, st.st_mode) != 0)) { __libpe_seterrno(PE_E_WRITE_ERROR); size = -1; } if (size != -1 && pe->parent == NULL) pe->maximum_size = size; return size; } loff_t pe_update(Pe *pe, Pe_Cmd cmd) { if (cmd != PE_C_NULL && cmd != PE_C_WRITE && cmd != PE_C_WRITE_MMAP) { __libpe_seterrno(PE_E_INVALID_CMD); return -1; } if (pe == NULL) return -1; if (pe->kind != PE_K_PE_EXE && pe->kind != PE_K_PE64_EXE && pe->kind != PE_K_PE_OBJ && pe->kind != PE_K_PE64_OBJ && pe->kind != PE_K_PE_ROM) { __libpe_seterrno(PE_E_INVALID_HANDLE); return -1; } size_t shnum = (pe->state.pe.scns_last->cnt == 0 ? 0 : 1 + pe->state.pe.scns_last->data[ pe->state.pe.scns_last->cnt - 1].index); off_t size = __pe_updatenull(pe, shnum); if (size != -1 && (cmd == PE_C_WRITE || PE_C_WRITE_MMAP)) { if (pe->cmd != PE_C_RDWR && pe->cmd != PE_C_RDWR_MMAP && pe->cmd != PE_C_WRITE && pe->cmd != PE_C_WRITE_MMAP) { __libpe_seterrno(PE_E_UPDATE_RO); size = -1; } else if (pe->fildes == -1) { __libpe_seterrno(PE_E_FD_DISABLED); size = -1; } else { size = write_file(pe, size, shnum); } } return size; } pesign-0.112/libdpe/pe_updatefile.c000066400000000000000000000075051270572506400172320ustar00rootroot00000000000000/* * Copyright 2012 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include "libdpe.h" #include #include #include static struct section_header * __attribute__((unused)) __get_last_section(Pe *pe) { Pe_Scn *scn = NULL; Pe_Scn *ret = NULL; while ((scn = pe_nextscn(pe, scn)) != NULL) { if (!ret) { ret = scn; continue; } if (ret->shdr->virtual_address < scn->shdr->virtual_address) ret = scn; } if (ret) return ret->shdr; return NULL; } static int compare_sections (const void *a, const void *b) { const Pe_Scn **scna = (const Pe_Scn **)a; const Pe_Scn **scnb = (const Pe_Scn **)b; int rc; if ((*scna)->shdr->virtual_address > (*scnb)->shdr->virtual_address) return 1; if ((*scnb)->shdr->virtual_address > (*scna)->shdr->virtual_address) return -1; if ((*scna)->shdr->data_addr > (*scnb)->shdr->data_addr) return 1; if ((*scnb)->shdr->data_addr > (*scna)->shdr->data_addr) return -1; rc = strcmp((*scna)->shdr->name, (*scnb)->shdr->name); if (rc != 0) return rc; if ((*scna)->shdr->virtual_size > (*scnb)->shdr->virtual_size) return 1; if ((*scnb)->shdr->virtual_size > (*scna)->shdr->virtual_size) return -1; if ((*scna)->shdr->raw_data_size > (*scnb)->shdr->raw_data_size) return 1; if ((*scnb)->shdr->raw_data_size > (*scna)->shdr->raw_data_size) return -1; return 0; } static void __attribute__((unused)) sort_sections (Pe_Scn **scns, Pe_ScnList *list) { Pe_Scn **scnp = scns; do { for (size_t cnt = 0; cnt < list->cnt; ++cnt) *scnp++ = &list->data[cnt]; } while ((list = list->next) != NULL); qsort(scns, scnp-scns, sizeof(*scns), compare_sections); } off_t __pe_updatemmap(Pe *pe, size_t shnum __attribute__((__unused__))) { /* This needs to write back the whole file: * 1) mz/pe/pe-o headers * 2) section headers and sections * 3) data directory table and data directories * * We also need to check if the signature is valid and if not, * make sure it's not in the data directory. */ struct mz_hdr *mzhdr = pe->state.pe.mzhdr; struct pe_hdr *pehdr = pe->state.pe.pehdr; if (pe->flags & PE_F_DIRTY) { off_t offset = 0; memcpy(pe->map_address + offset, mzhdr, sizeof(*mzhdr)); offset += le32_to_cpu(mzhdr->peaddr); memcpy(pe->map_address + offset, pehdr, sizeof(*pehdr)); } /* it's not dirty any more, so clear the flag. */ pe->flags &= ~PE_F_DIRTY; /* flush back to disk */ char *msync_start = ((char *) pe->map_address + (~(sysconf(_SC_PAGESIZE) -1 ))); data_directory *dd = NULL; int rc = pe_getdatadir(pe, &dd); if (rc < 0) { /* XXX set an error here */ return -1; } char *msync_end = (char *)dd + sizeof(*dd); msync(msync_start, msync_end - msync_start, MS_SYNC); #warning this is not done yet. //struct section_header *sh = __get_last_section(pe); size_t dd_size = sizeof (*dd) / sizeof (dd->exports); data_dirent *dde = &dd->exports; for (unsigned int i = 0; i < dd_size; i++, dde++) { if (dde->size != 0) { char *addr = compute_mem_addr(pe, dde->virtual_address); msync(addr, dde->size, MS_SYNC); } } return 0; } int __pe_updatefile(Pe *pe __attribute__((__unused__)), size_t shnum __attribute__((__unused__))) { __libpe_seterrno(PE_E_UNKNOWN_ERROR); return 1; } pesign-0.112/libdpe/pe_updatenull.c000066400000000000000000000026011270572506400172550ustar00rootroot00000000000000/* * Copyright 2012 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include "libdpe.h" static int pe_default_mzhdr(Pe *pe, struct mz_hdr *mzhdr) { /* Always write the mz magic */ uint16_t mz_magic = cpu_to_le16(MZ_MAGIC); update_if_changed(mzhdr->magic, mz_magic, pe->flags); /* XXX FIXME: write a real MZ header */ /* print "the only way to win is not to play" */ return 0; } static int pe_default_pehdr(Pe *pe, struct pe_hdr *pehdr, size_t shnum __attribute__((__unused__))) { /* Always write the pe magic */ uint32_t pe_magic = cpu_to_le32(PE_MAGIC); update_if_changed(pehdr->magic, pe_magic, pe->flags); return 0; } off_t __pe_updatenull(Pe *pe __attribute__((__unused__)), size_t shnum __attribute__((__unused__))) { return 0; } pesign-0.112/src/000077500000000000000000000000001270572506400136015ustar00rootroot00000000000000pesign-0.112/src/.gitignore000066400000000000000000000001671270572506400155750ustar00rootroot00000000000000*~ *.o *.so *.a *.efi pesign authvar ms client efikeygen efisiglist pesigcheck peverify pesign.service pesign.sysvinit pesign-0.112/src/Makefile000066400000000000000000000077201270572506400152470ustar00rootroot00000000000000SRCDIR = $(realpath .) TOPDIR = $(realpath ..) include $(TOPDIR)/Make.version include $(TOPDIR)/Make.rules include $(TOPDIR)/Make.defaults BINTARGETS=authvar client efikeygen efisiglist pesigcheck pesign SVCTARGETS=pesign.sysvinit pesign.service TARGETS=$(BINTARGETS) $(SVCTARGETS) all : deps $(TARGETS) COMMON_SOURCES = cms_common.c content_info.c oid.c password.c \ signed_data.c signer_info.c ucs2.c COMMON_PE_SOURCES = wincert.c cms_pe_common.c AUTHVAR_SOURCES = authvar.c authvar_context.c CLIENT_SOURCES = pesign_context.c actions.c client.c EFIKEYGEN_SOURCES = efikeygen.c EFISIGLIST_SOURCES = efisiglist.c siglist.c PESIGCHECK_SOURCES = pesigcheck.c pesigcheck_context.c certdb.c PESIGN_SOURCES = pesign.c pesign_context.c actions.c daemon.c ALL_SOURCES=$(COMMON_SOURCES) $(AUTHVAR_SORUCES) $(CLIENT_SOURCES) \ $(EFIKEYGEN_SOURCES) $(EFISIGLIST_SOURCES) $(PESIGCHECK_SOURCES) \ $(PESIGN_SOURCES) -include $(call deps-of,$(ALL_SOURCES)) authvar : $(call objects-of,$(AUTHVAR_SOURCES) $(COMMON_SOURCES)) # authvar : LDLIBS+=$(TOPDIR)/libdpe/libdpe.a authvar : PKGS=efivar nss nspr popt client : $(call objects-of,$(CLIENT_SOURCES) $(COMMON_SOURCES) $(COMMON_PE_SOURCES)) client : LDLIBS+=$(TOPDIR)/libdpe/libdpe.a client : PKGS=efivar nss nspr popt efikeygen : $(call objects-of,$(EFIKEYGEN_SOURCES) $(COMMON_SOURCES)) efikeygen : PKGS=efivar nss nspr popt uuid efisiglist : $(call objects-of,$(EFISIGLIST_SOURCES) $(COMMON_SOURCES)) efisiglist : PKGS=efivar nss nspr popt pesigcheck : $(call objects-of,$(PESIGCHECK_SOURCES) $(COMMON_SOURCES) $(COMMON_PE_SOURCES)) pesigcheck : LDLIBS+=$(TOPDIR)/libdpe/libdpe.a pesigcheck : PKGS=efivar nss nspr popt pesign : $(call objects-of,$(PESIGN_SOURCES) $(COMMON_SOURCES) $(COMMON_PE_SOURCES)) pesign : LDLIBS+=$(TOPDIR)/libdpe/libdpe.a pesign : PKGS=efivar nss nspr popt deps : $(ALL_SOURCES) $(MAKE) -f $(TOPDIR)/Make.deps deps SOURCES="$(ALL_SOURCES)" clean : @rm -rfv *.o *.a *.so $(TARGETS) @rm -rfv .*.d install_systemd: pesign.service $(INSTALL) -d -m 755 $(INSTALLROOT)$(libdatadir)tmpfiles.d/ $(INSTALL) -m 644 tmpfiles.conf $(INSTALLROOT)$(libdatadir)tmpfiles.d/pesign.conf $(INSTALL) -d -m 755 $(INSTALLROOT)$(libdatadir)systemd/system/ $(INSTALL) -m 644 pesign.service $(INSTALLROOT)$(libdatadir)systemd/system/ install_sysvinit: pesign.sysvinit $(INSTALL) -d -m 755 $(INSTALLROOT)/etc/rc.d/init.d/ $(INSTALL) -m 755 pesign.sysvinit $(INSTALLROOT)/etc/rc.d/init.d/pesign install : $(INSTALL) -d -m 700 $(INSTALLROOT)/etc/pki/pesign/ $(INSTALL) -d -m 700 $(INSTALLROOT)/etc/pki/pesign-rh-test/ $(INSTALL) -d -m 770 $(INSTALLROOT)/var/run/pesign/ $(INSTALL) -d -m 755 $(INSTALLROOT)$(bindir) $(INSTALL) -m 755 authvar $(INSTALLROOT)$(bindir) $(INSTALL) -m 755 pesign $(INSTALLROOT)$(bindir) $(INSTALL) -m 755 client $(INSTALLROOT)$(bindir)pesign-client $(INSTALL) -m 755 efikeygen $(INSTALLROOT)$(bindir) $(INSTALL) -m 755 efisiglist $(INSTALLROOT)$(bindir) $(INSTALL) -m 755 pesigcheck $(INSTALLROOT)$(bindir) $(INSTALL) -d -m 755 $(INSTALLROOT)/etc/popt.d/ $(INSTALL) -m 644 pesign.popt $(INSTALLROOT)/etc/popt.d/ $(INSTALL) -d -m 755 $(INSTALLROOT)$(mandir)man1/ $(INSTALL) -m 644 pesign.1 $(INSTALLROOT)$(mandir)man1/ $(INSTALL) -m 644 pesign-client.1 $(INSTALLROOT)$(mandir)man1/ $(INSTALL) -m 644 efikeygen.1 $(INSTALLROOT)$(mandir)man1/ $(INSTALL) -m 644 pesigcheck.1 $(INSTALLROOT)$(mandir)man1/ $(INSTALL) -m 644 authvar.1 $(INSTALLROOT)$(mandir)man1/ $(INSTALL) -m 644 efisiglist.1 $(INSTALLROOT)$(mandir)man1/ $(INSTALL) -d -m 755 $(INSTALLROOT)/etc/rpm/ $(INSTALL) -m 644 macros.pesign $(INSTALLROOT)/etc/rpm/ $(INSTALL) -d -m 755 $(INSTALLROOT)$(libexecdir)/pesign/ $(INSTALL) -m 750 pesign-authorize-users $(INSTALLROOT)$(libexecdir)/pesign/ $(INSTALL) -m 750 pesign-authorize-groups $(INSTALLROOT)$(libexecdir)/pesign/ $(INSTALL) -d -m 700 $(INSTALLROOT)/etc/pesign $(INSTALL) -m 600 /dev/null $(INSTALLROOT)/etc/pesign/users $(INSTALL) -m 600 /dev/null $(INSTALLROOT)/etc/pesign/groups .PHONY: all deps clean install pesign-0.112/src/actions.c000066400000000000000000000244531270572506400154150ustar00rootroot00000000000000/* * Copyright 2011-2012 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #include #include #include #include #include #include #include #include "pesign.h" #include #include #include #include #include #include #include #include static int saw_content; static void handle_bytes(void *arg __attribute__((__unused__)), const char *buf __attribute__((__unused__)), unsigned long len __attribute__((__unused__))) { saw_content = 1; } static PRBool decryption_allowed(SECAlgorithmID *algid __attribute__((__unused__)), PK11SymKey *key __attribute__((__unused__))) { return PR_TRUE; } void insert_signature(cms_context *cms, int signum) { SECItem *sig = &cms->newsig; if (signum == -1) signum = cms->num_signatures; SECItem **signatures = realloc(cms->signatures, sizeof (SECItem *) * (cms->num_signatures + 1)); if (!signatures) { err: cms->log(cms, LOG_ERR, "insert signature: could not allocate " "memory: %m"); exit(1); } cms->signatures = signatures; if (signum != cms->num_signatures) { memmove(cms->signatures[signum+1], cms->signatures[signum], sizeof(SECItem) * (cms->num_signatures - signum)); } SECItem *newsig = malloc(sizeof (*newsig)); memcpy(newsig, sig, sizeof (*newsig)); newsig->data = malloc(sig->len); if (!newsig->data) goto err; memcpy(newsig->data, sig->data, newsig->len); cms->signatures[signum] = newsig; cms->num_signatures++; free(cms->newsig.data); memset(&cms->newsig, '\0', sizeof (cms->newsig)); } int list_signatures(pesign_context *ctx) { cert_iter iter; int rc = cert_iter_init(&iter, ctx->inpe); if (rc < 0) { printf("No certificate list found.\n"); return rc; } void *data; ssize_t datalen; int nsigs = 0; rc = 0; while (1) { rc = next_cert(&iter, &data, &datalen); if (rc <= 0) break; SEC_PKCS7DecoderContext *dc = NULL; saw_content = 0; dc = SEC_PKCS7DecoderStart(handle_bytes, NULL, NULL, NULL, NULL, NULL, decryption_allowed); if (dc == NULL) { fprintf(stderr, "SEC_PKCS7DecoderStart failed\n"); exit(1); } SECStatus status = SEC_PKCS7DecoderUpdate(dc, data, datalen); if (status != SECSuccess) { fprintf(stderr, "Found invalid certificate\n"); continue; } SEC_PKCS7ContentInfo *cinfo = SEC_PKCS7DecoderFinish(dc); if (cinfo == NULL) { fprintf(stderr, "Found invalid certificate\n"); continue; } nsigs++; printf("---------------------------------------------\n"); printf("certificate address is %p\n", data); printf("Content was%s encrypted.\n", SEC_PKCS7ContentIsEncrypted(cinfo) ? "" : " not"); if (SEC_PKCS7ContentIsSigned(cinfo)) { char *signer_cname, *signer_ename; SECItem *signing_time; if (saw_content) { printf("Signature is "); PORT_SetError(0); if (SEC_PKCS7VerifySignature(cinfo, certUsageEmailSigner, PR_FALSE)) { printf("valid.\n"); } else { printf("invalid (Reason: 0x%08x).\n", (uint32_t)PORT_GetError()); } } else { printf("Content is detached; signature cannot " "be verified.\n"); } signer_cname = SEC_PKCS7GetSignerCommonName(cinfo); if (signer_cname != NULL) { printf("The signer's common name is %s\n", signer_cname); PORT_Free(signer_cname); } else { printf("No signer common name.\n"); } signer_ename = SEC_PKCS7GetSignerEmailAddress(cinfo); if (signer_ename != NULL) { printf("The signer's email address is %s\n", signer_ename); PORT_Free(signer_ename); } else { printf("No signer email address.\n"); } signing_time = SEC_PKCS7GetSigningTime(cinfo); if (signing_time != NULL) { char *signing_time_str = DER_TimeChoiceDayToAscii(signing_time); printf("Signing time: %s\n", signing_time_str); PORT_Free(signing_time_str); } else { printf("No signing time included.\n"); } printf("There were%s certs or crls included.\n", SEC_PKCS7ContainsCertsOrCrls(cinfo) ? "" : " no"); SEC_PKCS7DestroyContentInfo(cinfo); } } if (nsigs) { printf("---------------------------------------------\n"); } else { printf("No signatures found.\n"); } return rc; } static const char *sig_begin_marker ="-----BEGIN AUTHENTICODE SIGNATURE-----\n"; static const char *sig_end_marker = "\n-----END AUTHENTICODE SIGNATURE-----\n"; void export_pubkey(pesign_context *p_ctx) { cms_context *ctx = p_ctx->cms_ctx; int rc; SECItem derPublicKey = ctx->cert->derPublicKey; rc = write(p_ctx->outkeyfd, derPublicKey.data, derPublicKey.len); close(p_ctx->outkeyfd); if (rc >= 0 && (unsigned long)rc == derPublicKey.len) exit(0); exit(1); } void export_cert(pesign_context *p_ctx) { cms_context *ctx = p_ctx->cms_ctx; int rc; SECItem derCert = ctx->cert->derCert; rc = write(p_ctx->outcertfd, derCert.data, derCert.len); close(p_ctx->outcertfd); if (rc >= 0 && (unsigned long)rc == derCert.len) exit(0); exit(1); } ssize_t export_signature(cms_context *cms, int fd, int ascii_armor) { ssize_t ret = 0; int rc = 0; SECItem *sig = &cms->newsig; unsigned char *data = sig->data; int datalen = sig->len; if (ascii_armor) { char *ascii = BTOA_DataToAscii(data, datalen); if (!ascii) { cms->log(cms, LOG_ERR, "error exporting signature"); failure: close(fd); return -1; } rc = write(fd, sig_begin_marker, strlen(sig_begin_marker)); if (rc < 0) { cms->log(cms, LOG_ERR, "error exporting signature: %m"); goto failure; } ret += rc; rc = write(fd, ascii, strlen(ascii)); if (rc < 0) { cms->log(cms, LOG_ERR, "error exporting signature: %m"); goto failure; } ret += rc; rc = write(fd, sig_end_marker, strlen(sig_end_marker)); if (rc < 0) { cms->log(cms, LOG_ERR, "error exporting signature: %m"); goto failure; } ret += rc; PORT_Free(ascii); } else { rc = write(fd, data, datalen); if (rc < 0) { cms->log(cms, LOG_ERR, "error exporting signature: %m"); goto failure; } ret += rc; } return ret; } void parse_signature(pesign_context *ctx) { int rc; char *sig; size_t siglen; rc = read_file(ctx->insigfd, &sig, &siglen); if (rc < 0) { fprintf(stderr, "pesign: could not read signature.\n"); exit(1); } unsigned char *der; unsigned int derlen; /* XXX FIXME: ignoring length for now */ char *base64 = strstr(sig, sig_begin_marker); if (base64) { base64 += strlen(sig_begin_marker); char *end = strstr(base64, sig_end_marker); if (!end) { fprintf(stderr, "pesign: Invalid signature.\n"); exit(1); } derlen = end - base64; base64[derlen] = '\0'; unsigned char *dertmp; dertmp = ATOB_AsciiToData(base64, &derlen); der = malloc(derlen); memmove(der, dertmp, derlen); PORT_Free(dertmp); } else { der = malloc(siglen); memmove(der, sig, siglen); derlen = siglen; } free(sig); ctx->cms_ctx->newsig.data = der; ctx->cms_ctx->newsig.len = derlen; #if 0 SEC_PKCS7DecoderContext *dc = NULL; saw_content = 0; dc = SEC_PKCS7DecoderStart(handle_bytes, NULL, NULL, NULL, NULL, NULL, decryption_allowed); if (dc == NULL) { decoder_error: fprintf(stderr, "pesign: Invalid signature.\n"); PORT_Free(der); exit(1); } SECStatus status = SEC_PKCS7DecoderUpdate(dc, (char *)der, derlen); if (status != SECSuccess) goto decoder_error; SEC_PKCS7ContentInfo *cinfo = SEC_PKCS7DecoderFinish(dc); if (!cinfo) goto decoder_error; ctx->cinfo = cinfo; PORT_Free(der); #endif } void import_raw_signature(pesign_context *pctx) { if (pctx->rawsigfd < 0 || pctx->insattrsfd < 0) { fprintf(stderr, "pesign: raw signature and signed attributes " "must both be imported.\n"); exit(1); } cms_context *ctx = pctx->cms_ctx; ctx->raw_signature = SECITEM_AllocItem(ctx->arena, NULL, 0); ctx->raw_signature->type = siBuffer; int rc = read_file(pctx->rawsigfd, (char **)&ctx->raw_signature->data, (size_t *)&ctx->raw_signature->len); if (rc < 0) { fprintf(stderr, "pesign: could not read raw signature: %m\n"); exit(1); } ctx->raw_signed_attrs = SECITEM_AllocItem(ctx->arena, NULL, 0); ctx->raw_signed_attrs->type = siBuffer; rc = read_file(pctx->insattrsfd, (char **)&ctx->raw_signed_attrs->data, (size_t *)&ctx->raw_signed_attrs->len); if (rc < 0) { fprintf(stderr, "pesign: could not read raw signed attributes" ": %m\n"); exit(1); } } int generate_sattr_blob(pesign_context *ctx) { int rc; SECItem sa; SpcContentInfo ci; memset(&ci, '\0', sizeof (ci)); rc = generate_spc_content_info(ctx->cms_ctx, &ci); if (rc < 0) { fprintf(stderr, "Could not generate content info: %s\n", PORT_ErrorToString(PORT_GetError())); exit(1); } rc = generate_signed_attributes(ctx->cms_ctx, &sa); if (rc < 0) { fprintf(stderr, "Could not generate signed attributes: %s\n", PORT_ErrorToString(PORT_GetError())); exit(1); } return write(ctx->outsattrsfd, sa.data, sa.len); } void check_signature_space(pesign_context *ctx) { ssize_t available = available_cert_space(ctx->outpe); ssize_t target = ctx->cms_ctx->newsig.len + sizeof (win_certificate); target += ALIGNMENT_PADDING(target, 8); if (available >= target) return; fprintf(stderr, "Could not add new signature: insufficient space.\n"); exit(1); } void allocate_signature_space(Pe *pe, ssize_t sigspace) { int rc; rc = pe_alloccert(pe, sigspace); if (rc < 0) { fprintf(stderr, "Could not allocate space for signature: %m\n"); exit(1); } } void remove_signature(pesign_context *p_ctx) { cms_context *ctx = p_ctx->cms_ctx; free(ctx->signatures[p_ctx->signum]->data); if (p_ctx->signum != ctx->num_signatures - 1) memmove(ctx->signatures[p_ctx->signum], ctx->signatures[p_ctx->signum+1], sizeof(SECItem) * (ctx->num_signatures - 1)); ctx->num_signatures--; } pesign-0.112/src/actions.h000066400000000000000000000026721270572506400154210ustar00rootroot00000000000000/* * Copyright 2011 Red Hat, Inc. * All rights reserved. * * 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; version 2 of the License. * * This program 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, see . * * Author(s): Peter Jones */ #ifndef PESIGN_CRYPTO_H #define PESIGN_CRYPTO_H 1 #include #include #include "wincert.h" extern int list_signatures(pesign_context *ctx); extern void check_signature_space(pesign_context *ctx); extern void allocate_signature_space(Pe *pe, ssize_t sigspace); extern ssize_t export_signature(cms_context *cms, int fd, int ascii_armor); extern void import_raw_signature(pesign_context *pctx); extern void remove_signature(pesign_context *ctx); extern void export_pubkey(pesign_context *ctx); extern void export_cert(pesign_context *ctx); extern int generate_sattr_blob(pesign_context *pctx); extern void parse_signature(pesign_context *ctx); extern void insert_signature(cms_context *cms, int signum); #endif /* PESIGN_CRYPTO_H */ pesign-0.112/src/authvar.1000066400000000000000000000031541270572506400153400ustar00rootroot00000000000000.TH AUTHVAR "1" "June 2015" .SH NAME authvar \- handles authenticated variables for pesign .SH SYNOPSIS \fBauthvar\fR [\-?] [\-a|\-\-append] [\-d|\-\-certdir=] [\-c|\-\-clear] [\-s|\-\-set] [\-N|\-\-namespace={|}] [\-n|\-\-name=] [\-t|\-\-timestamp=