2 # Copyright 2009, Google Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 """Code signing build tool.
33 This module sets up code signing.
34 It is used as follows:
35 env = Environment(tools = ["code_signing"])
36 To sign an EXE/DLL do:
37 env.SignedBinary('hello_signed.exe', 'hello.exe',
38 CERTIFICATE_FILE='bob.pfx',
39 CERTIFICATE_PASSWORD='123',
41 If no certificate file is specified, copying instead of signing will occur.
42 If an empty timestamp server string is specified, there will be no timestamp.
46 from SCons
.compat
._scons
_optparse
import OptionConflictError
51 # NOTE: SCons requires the use of this name, which fails gpylint.
52 """SCons entry point for this tool."""
55 SCons
.Script
.AddOption('--certificate-name',
56 dest
='certificate_name',
57 help='select which certificate to use')
59 ' --certificate-name <NAME> select which signing certificate to use')
60 except (OptionConflictError
, optparse
.OptionConflictError
):
61 # This gets catch to prevent duplicate help being added for this option
62 # for each build type.
66 # Path to Microsoft signtool.exe
67 SIGNTOOL
='"$VC80_DIR/common7/tools/bin/signtool.exe"',
68 # No certificate by default.
70 # No certificate password by default.
71 CERTIFICATE_PASSWORD
='',
72 # The default timestamp server.
73 TIMESTAMP_SERVER
='http://timestamp.verisign.com/scripts/timestamp.dll',
74 # The default certificate store.
75 CERTIFICATE_STORE
='my',
76 # Set the certificate name from the command line.
77 CERTIFICATE_NAME
=SCons
.Script
.GetOption('certificate_name'),
80 # Setup Builder for Signing
81 env
['BUILDERS']['SignedBinary'] = SCons
.Script
.Builder(
82 generator
=SignedBinaryGenerator
,
83 emitter
=SignedBinaryEmitter
)
86 def SignedBinaryEmitter(target
, source
, env
):
87 """Add the signing certificate (if any) to the source dependencies."""
88 if env
.subst('$CERTIFICATE_PATH'):
89 source
.append(env
.subst('$CERTIFICATE_PATH'))
93 def SignedBinaryGenerator(source
, target
, env
, for_signature
):
94 """A builder generator for code signing."""
95 source
= source
# Silence gpylint.
96 target
= target
# Silence gpylint.
97 for_signature
= for_signature
# Silence gpylint.
99 # Alway copy and make writable.
101 SCons
.Script
.Copy('$TARGET', '$SOURCE'),
102 SCons
.Script
.Chmod('$TARGET', 0755),
105 # Only do signing if there is a certificate file or certificate name.
106 if env
.subst('$CERTIFICATE_PATH') or env
.subst('$CERTIFICATE_NAME'):
107 # The command used to do signing (target added on below).
108 signing_cmd
= '$SIGNTOOL sign '
109 # Add in certificate file if any.
110 if env
.subst('$CERTIFICATE_PATH'):
111 signing_cmd
+= ' /f "$CERTIFICATE_PATH"'
112 # Add certificate password if any.
113 if env
.subst('$CERTIFICATE_PASSWORD'):
114 signing_cmd
+= ' /p "$CERTIFICATE_PASSWORD"'
115 # Add certificate store if any.
116 if env
.subst('$CERTIFICATE_NAME'):
117 # The command used to do signing (target added on below).
118 signing_cmd
+= ' /s "$CERTIFICATE_STORE" /n "$CERTIFICATE_NAME"'
119 # Add timestamp server if any.
120 if env
.subst('$TIMESTAMP_SERVER'):
121 signing_cmd
+= ' /t "$TIMESTAMP_SERVER"'
123 signing_cmd
+= ' "$TARGET"'
124 # Add the signing to the list of commands to perform.
125 commands
.append(signing_cmd
)