2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.ivy.plugins.signer.bouncycastle;
21
import java.io.FileInputStream;
22
import java.io.FileOutputStream;
23
import java.io.IOException;
24
import java.io.InputStream;
25
import java.io.OutputStream;
26
import java.security.NoSuchAlgorithmException;
27
import java.security.NoSuchProviderException;
28
import java.security.Security;
29
import java.security.SignatureException;
30
import java.util.Iterator;
32
import org.apache.ivy.plugins.signer.SignatureGenerator;
33
import org.bouncycastle.bcpg.ArmoredOutputStream;
34
import org.bouncycastle.bcpg.BCPGOutputStream;
35
import org.bouncycastle.jce.provider.BouncyCastleProvider;
36
import org.bouncycastle.openpgp.PGPException;
37
import org.bouncycastle.openpgp.PGPPrivateKey;
38
import org.bouncycastle.openpgp.PGPSecretKey;
39
import org.bouncycastle.openpgp.PGPSecretKeyRing;
40
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
41
import org.bouncycastle.openpgp.PGPSignature;
42
import org.bouncycastle.openpgp.PGPSignatureGenerator;
43
import org.bouncycastle.openpgp.PGPUtil;
45
public class OpenPGPSignatureGenerator implements SignatureGenerator {
47
private static final long MASK = 0xFFFFFFFFL;
50
Security.addProvider(new BouncyCastleProvider());
54
private String secring;
55
private String password;
58
private PGPSecretKey pgpSec;
60
public String getName() {
64
public void setName(String name) {
68
public String getExtension() {
72
public void setPassword(String password) {
73
this.password = password;
76
public void setSecring(String secring) {
77
this.secring = secring;
80
public void setKeyId(String keyId) {
81
if (!"auto".equals(keyId)) {
86
public void sign(File src, File dest) throws IOException {
87
OutputStream out = null;
88
InputStream in = null;
89
InputStream keyIn = null;
92
if (secring == null) {
93
secring = System.getProperty("user.home") + "/.gnupg/secring.gpg";
97
keyIn = new FileInputStream(secring);
98
pgpSec = readSecretKey(keyIn);
101
PGPPrivateKey pgpPrivKey = pgpSec.extractPrivateKey(password.toCharArray(), BouncyCastleProvider.PROVIDER_NAME);
102
PGPSignatureGenerator sGen = new PGPSignatureGenerator(pgpSec.getPublicKey().getAlgorithm(), PGPUtil.SHA1, BouncyCastleProvider.PROVIDER_NAME);
103
sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
105
in = new FileInputStream(src);
106
out = new BCPGOutputStream(new ArmoredOutputStream(new FileOutputStream(dest)));
109
while ((ch = in.read()) >= 0) {
110
sGen.update((byte) ch);
113
sGen.generate().encode(out);
114
} catch (SignatureException e) {
115
IOException ioexc = new IOException();
118
} catch (PGPException e) {
119
IOException ioexc = new IOException();
122
} catch (NoSuchAlgorithmException e) {
123
IOException ioexc = new IOException();
126
} catch (NoSuchProviderException e) {
127
IOException ioexc = new IOException();
134
} catch (IOException e) {}
139
} catch (IOException e) {}
144
} catch (IOException e) {}
149
private PGPSecretKey readSecretKey(InputStream in) throws IOException, PGPException {
150
in = PGPUtil.getDecoderStream(in);
151
PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(in);
153
PGPSecretKey key = null;
154
for (Iterator it = pgpSec.getKeyRings(); key == null && it.hasNext(); ) {
155
PGPSecretKeyRing kRing = (PGPSecretKeyRing) it.next();
157
for (Iterator it2 = kRing.getSecretKeys(); key == null && it2.hasNext(); ) {
158
PGPSecretKey k = (PGPSecretKey) it2.next();
159
if ((keyId == null) && k.isSigningKey()) {
162
if ((keyId != null) && (Long.valueOf(keyId, 16).longValue() == (k.getKeyID() & MASK))) {
169
throw new IllegalArgumentException("Can't find encryption key" +
170
(keyId != null ? " '" + keyId + "' " : " ") + "in key ring.");