1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.openpgp.ant; |
18 | |
|
19 | |
import org.apache.tools.ant.Task; |
20 | |
import org.apache.tools.ant.BuildException; |
21 | |
import org.apache.tools.ant.DirectoryScanner; |
22 | |
import org.apache.tools.ant.util.FileNameMapper; |
23 | |
import org.apache.tools.ant.util.GlobPatternMapper; |
24 | |
import org.apache.tools.ant.util.FileUtils; |
25 | |
import org.apache.tools.ant.types.FileSet; |
26 | |
import org.apache.tools.ant.types.Mapper; |
27 | |
import org.apache.commons.openpgp.*; |
28 | |
import org.bouncycastle.openpgp.PGPException; |
29 | |
|
30 | |
import java.io.*; |
31 | |
import java.util.Collection; |
32 | |
import java.util.ArrayList; |
33 | |
import java.util.Iterator; |
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public class OpenPgpSignerTask extends Task { |
38 | |
private File secring; |
39 | |
private File pubring; |
40 | |
private String password; |
41 | |
private String keyId; |
42 | 0 | private Collection<FileSet> tosign = new ArrayList<FileSet>(); |
43 | |
private File artefact; |
44 | 0 | private boolean asciiarmor = true; |
45 | |
private Mapper mapperElement; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public void setSecring(File secring) { |
52 | 0 | this.secring = secring; |
53 | 0 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public void setPubring(File pubring) { |
60 | 0 | this.pubring = pubring; |
61 | 0 | } |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public void setKeyId(String keyId) { |
68 | 0 | this.keyId = keyId; |
69 | 0 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public void setAsciiarmor(boolean asciiarmor) { |
76 | 0 | this.asciiarmor = asciiarmor; |
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public void setPassword(String password) { |
84 | 0 | this.password = password; |
85 | 0 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public void setArtefact(File artefact) { |
92 | 0 | this.artefact = artefact; |
93 | 0 | } |
94 | |
|
95 | |
|
96 | |
public void add(FileSet fs) { |
97 | 0 | tosign.add(fs); |
98 | 0 | } |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public Mapper createMapper() throws BuildException { |
106 | 0 | if (mapperElement != null) { |
107 | 0 | throw new BuildException("Cannot define more than one mapper", |
108 | |
getLocation()); |
109 | |
} |
110 | 0 | mapperElement = new Mapper(getProject()); |
111 | 0 | return mapperElement; |
112 | |
} |
113 | |
|
114 | |
public void execute() { |
115 | 0 | if (secring == null) { |
116 | 0 | throw new BuildException("secring attribute compulsory"); |
117 | |
} |
118 | 0 | if (pubring == null) { |
119 | 0 | throw new BuildException("pubring attribute compulsory"); |
120 | |
} |
121 | 0 | if (password == null) { |
122 | 0 | throw new BuildException("password attribute compulsory"); |
123 | |
} |
124 | 0 | if (tosign.size() == 0 && artefact == null) { |
125 | 0 | throw new BuildException("supply the attribute 'artefact' or one nested fileset"); |
126 | |
} |
127 | 0 | if (!secring.exists() || !secring.canRead()) { |
128 | 0 | throw new BuildException("secret keyring file '" + secring.getAbsolutePath() + "' does not exist or is not readable"); |
129 | |
} |
130 | 0 | if (!pubring.exists() || !pubring.canRead()) { |
131 | 0 | throw new BuildException("public keyring file '" + pubring.getAbsolutePath() + "' does not exist or is not readable"); |
132 | |
} |
133 | |
FileInputStream secStream; |
134 | |
FileInputStream pubStream; |
135 | 0 | KeyRing keyRing = null; |
136 | |
try { |
137 | 0 | secStream = new FileInputStream(secring); |
138 | 0 | pubStream = new FileInputStream(pubring); |
139 | 0 | keyRing = new BouncyCastleKeyRing(secStream, |
140 | |
pubStream, password.toCharArray() ); |
141 | 0 | } catch (IOException ioe) { |
142 | 0 | throw new BuildException(ioe); |
143 | 0 | } catch (PGPException pgpe) { |
144 | 0 | throw new BuildException(pgpe); |
145 | 0 | } |
146 | 0 | if (artefact != null) { |
147 | 0 | dosign(keyRing, artefact); |
148 | |
} |
149 | 0 | if (tosign.size() != 0) { |
150 | 0 | for (FileSet fileset : tosign) { |
151 | 0 | dosign(keyRing, fileset); |
152 | |
} |
153 | |
} |
154 | 0 | FileUtils.close(secStream); |
155 | 0 | FileUtils.close(pubStream); |
156 | 0 | } |
157 | |
private void dosign(KeyRing keyRing, FileSet fs) { |
158 | 0 | DirectoryScanner ds = fs.getDirectoryScanner(getProject()); |
159 | 0 | String[] artefacts = ds.getIncludedFiles(); |
160 | 0 | for (String artefact : artefacts) { |
161 | 0 | dosign(keyRing, new File(fs.getDir(getProject()), artefact), fs.getDir(getProject()), artefact); |
162 | |
} |
163 | 0 | } |
164 | |
private void dosign(KeyRing keyRing, File oneartefact) { |
165 | 0 | dosign(keyRing, oneartefact, oneartefact.getParentFile(), oneartefact.getName()); |
166 | 0 | } |
167 | |
private void dosign(KeyRing keyRing, File oneartefact, File basedir, String relpath) { |
168 | 0 | FileInputStream fis = null; |
169 | 0 | FileOutputStream fos = null; |
170 | |
File signature; |
171 | |
|
172 | |
try { |
173 | 0 | fis = new FileInputStream(oneartefact); |
174 | 0 | FileNameMapper mapper = getMapper(); |
175 | 0 | String [] mappedFiles = mapper.mapFileName(relpath); |
176 | 0 | if (mappedFiles == null || mappedFiles.length != 1) { |
177 | 0 | throw new BuildException("mapper returned more or less than one output"); |
178 | |
} |
179 | 0 | signature = new File(basedir, mappedFiles[0]); |
180 | 0 | fos = new FileOutputStream(signature); |
181 | 0 | OpenPgpSigner signer = new BouncyCastleOpenPgpSigner(); |
182 | 0 | signer.detachedSign(fis, fos, keyId, keyRing, asciiarmor); |
183 | 0 | } catch (FileNotFoundException fnfe) { |
184 | 0 | throw new BuildException(fnfe); |
185 | 0 | } catch (IOException ioe) { |
186 | 0 | throw new BuildException(ioe); |
187 | 0 | } catch (OpenPgpException opgpe) { |
188 | 0 | throw new BuildException(opgpe); |
189 | 0 | } |
190 | 0 | FileUtils.close(fos); |
191 | 0 | FileUtils.close(fis); |
192 | |
|
193 | 0 | } |
194 | |
|
195 | |
|
196 | |
|
197 | |
private FileNameMapper getMapper() { |
198 | 0 | FileNameMapper mapper = null; |
199 | 0 | if (mapperElement != null) { |
200 | 0 | mapper = mapperElement.getImplementation(); |
201 | |
} else { |
202 | 0 | mapper = new GlobPatternMapper(); |
203 | 0 | mapper.setFrom("*"); |
204 | 0 | if (asciiarmor) { |
205 | 0 | mapper.setTo("*.asc"); |
206 | |
} else { |
207 | 0 | mapper.setTo("*.sig"); |
208 | |
} |
209 | |
} |
210 | 0 | return mapper; |
211 | |
} |
212 | |
|
213 | |
} |