001/** 002 * Copyright 2012 Emmanuel Bourg 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package net.jsign; 018 019import java.io.File; 020 021import org.apache.tools.ant.BuildException; 022import org.apache.tools.ant.Task; 023import org.apache.tools.ant.types.FileSet; 024 025/** 026 * Ant task for signing files with Authenticode. 027 * 028 * @author Emmanuel Bourg 029 * @since 1.0 030 */ 031public class JsignTask extends Task { 032 033 /** The file to be signed. */ 034 private File file; 035 036 /** The set of files to be signed. */ 037 private FileSet fileset; 038 039 /** The program name embedded in the signature. */ 040 private String name; 041 042 /** The program URL embedded in the signature. */ 043 private String url; 044 045 /** The digest algorithm to use for the signature. */ 046 private String algorithm; 047 048 /** The keystore file, the SunPKCS11 configuration file or the cloud keystore name. */ 049 private String keystore; 050 051 /** The password for the keystore. */ 052 private String storepass; 053 054 /** The type of the keystore. */ 055 private String storetype; 056 057 /** The alias of the certificate in the keystore. */ 058 private String alias; 059 060 /** The file containing the certificate chain (PKCS#7 format). */ 061 private File certfile; 062 063 /** The file containing the private key (PEM or PVK format) */ 064 private File keyfile; 065 066 /** The password for the key in the store (if different from the keystore password) or in the keyfile. */ 067 private String keypass; 068 069 /** The URL of the timestamping authority. */ 070 private String tsaurl; 071 072 /** The protocol used for the timestamping */ 073 private String tsmode; 074 075 /** The number of retries for timestamping */ 076 private int tsretries = -1; 077 078 /** The number of seconds to wait between timestamping retries */ 079 private int tsretrywait = -1; 080 081 /** Tells if previous signatures should be replaced */ 082 private boolean replace; 083 084 /** The encoding of the script to be signed (UTF-8 by default). */ 085 private String encoding = "UTF-8"; 086 087 /** Tells if a detached signature should be generated or reused. */ 088 private boolean detached; 089 090 public void setFile(File file) { 091 this.file = file; 092 } 093 094 public void addFileset(FileSet fileset) { 095 this.fileset = fileset; 096 } 097 098 public void setName(String name) { 099 this.name = name; 100 } 101 102 public void setUrl(String url) { 103 this.url = url; 104 } 105 106 public void setAlg(String alg) { 107 this.algorithm = alg; 108 } 109 110 public void setTsmode(String tsmode) { 111 this.tsmode = tsmode; 112 } 113 114 public void setKeystore(String keystore) { 115 this.keystore = keystore; 116 } 117 118 public void setStorepass(String storepass) { 119 this.storepass = storepass; 120 } 121 122 public void setStoretype(String storetype) { 123 this.storetype = storetype; 124 } 125 126 public void setAlias(String alias) { 127 this.alias = alias; 128 } 129 130 public void setCertfile(File certfile) { 131 this.certfile = certfile; 132 } 133 134 public void setKeyfile(File keyfile) { 135 this.keyfile = keyfile; 136 } 137 138 public void setKeypass(String keypass) { 139 this.keypass = keypass; 140 } 141 142 public void setTsaurl(String tsaurl) { 143 this.tsaurl = tsaurl; 144 } 145 146 public void setTsretries(int tsretries) { 147 this.tsretries = tsretries; 148 } 149 150 public void setTsretrywait(int tsretrywait) { 151 this.tsretrywait = tsretrywait; 152 } 153 154 public void setReplace(boolean replace) { 155 this.replace = replace; 156 } 157 158 public void setEncoding(String encoding) { 159 this.encoding = encoding; 160 } 161 162 public void setDetached(boolean detached) { 163 this.detached = detached; 164 } 165 166 @Override 167 public void execute() throws BuildException { 168 try { 169 SignerHelper helper = new SignerHelper(new AntConsole(this), "attribute"); 170 helper.setBaseDir(getProject().getBaseDir()); 171 172 helper.name(name); 173 helper.url(url); 174 helper.alg(algorithm); 175 helper.keystore(keystore); 176 helper.storepass(storepass); 177 helper.storetype(storetype); 178 helper.alias(alias); 179 helper.certfile(certfile); 180 helper.keyfile(keyfile); 181 helper.keypass(keypass); 182 helper.tsaurl(tsaurl); 183 helper.tsmode(tsmode); 184 helper.tsretries(tsretries); 185 helper.tsretrywait(tsretrywait); 186 helper.replace(replace); 187 helper.encoding(encoding); 188 helper.detached(detached); 189 190 if (fileset != null) { 191 for(String fileElement : fileset.getDirectoryScanner().getIncludedFiles()) { 192 helper.sign(new File(fileset.getDir(), fileElement)); 193 } 194 } else { 195 helper.sign(file); 196 } 197 } catch (Exception e) { 198 throw new BuildException(e.getMessage(), e, getLocation()); 199 } 200 } 201}