~ubuntu-branches/ubuntu/oneiric/ivy/oneiric

« back to all changes in this revision

Viewing changes to src/java/org/apache/ivy/plugins/signer/bouncycastle/OpenPGPSignatureGenerator.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2011-02-06 20:56:32 UTC
  • mfrom: (3.2.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110206205632-sqhg7bx09z0ywj79
Tags: 2.2.0-1
* New upstream release.
* Update pom.xml to the latest version.
* Add Build-Depends and Suggests on libbcprov-java and libbcpg-java.
* Add mh_clean call in clean target.
* Bump Standards-Version to 3.9.1. No changes were required.
* Add myself to Uploaders.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
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.
 
16
 *
 
17
 */
 
18
package org.apache.ivy.plugins.signer.bouncycastle;
 
19
 
 
20
import java.io.File;
 
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;
 
31
 
 
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;
 
44
 
 
45
public class OpenPGPSignatureGenerator implements SignatureGenerator {
 
46
    
 
47
    private static final long MASK = 0xFFFFFFFFL;
 
48
 
 
49
    static {
 
50
        Security.addProvider(new BouncyCastleProvider());
 
51
    }
 
52
 
 
53
    private String name;
 
54
    private String secring;
 
55
    private String password;
 
56
    private String keyId;    
 
57
    
 
58
    private PGPSecretKey pgpSec;
 
59
    
 
60
    public String getName() {
 
61
        return name;
 
62
    }
 
63
    
 
64
    public void setName(String name) {
 
65
        this.name = name;
 
66
    }
 
67
    
 
68
    public String getExtension() {
 
69
        return "asc";
 
70
    }
 
71
    
 
72
    public void setPassword(String password) {
 
73
        this.password = password;
 
74
    }
 
75
    
 
76
    public void setSecring(String secring) {
 
77
        this.secring = secring;
 
78
    }
 
79
    
 
80
    public void setKeyId(String keyId) {
 
81
        if (!"auto".equals(keyId)) {
 
82
            this.keyId = keyId;
 
83
        }
 
84
    }
 
85
    
 
86
    public void sign(File src, File dest) throws IOException {
 
87
        OutputStream out = null;
 
88
        InputStream in = null;
 
89
        InputStream keyIn = null;
 
90
        
 
91
        try {
 
92
            if (secring == null) {
 
93
                secring = System.getProperty("user.home") + "/.gnupg/secring.gpg";
 
94
            }
 
95
            
 
96
            if (pgpSec == null) {
 
97
                keyIn = new FileInputStream(secring);
 
98
                pgpSec = readSecretKey(keyIn);
 
99
            }
 
100
            
 
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);
 
104
    
 
105
            in = new FileInputStream(src);
 
106
            out = new BCPGOutputStream(new ArmoredOutputStream(new FileOutputStream(dest)));
 
107
            
 
108
            int ch = 0;
 
109
            while ((ch = in.read()) >= 0) {
 
110
                sGen.update((byte) ch);
 
111
            }
 
112
            
 
113
            sGen.generate().encode(out);
 
114
        } catch (SignatureException e) {
 
115
            IOException ioexc = new IOException();
 
116
            ioexc.initCause(e);
 
117
            throw ioexc;
 
118
        } catch (PGPException e) {
 
119
            IOException ioexc = new IOException();
 
120
            ioexc.initCause(e);
 
121
            throw ioexc;
 
122
        } catch (NoSuchAlgorithmException e) {
 
123
            IOException ioexc = new IOException();
 
124
            ioexc.initCause(e);
 
125
            throw ioexc;
 
126
        } catch (NoSuchProviderException e) {
 
127
            IOException ioexc = new IOException();
 
128
            ioexc.initCause(e);
 
129
            throw ioexc;
 
130
        } finally {
 
131
            if (out != null) {
 
132
                try {
 
133
                    out.close();
 
134
                } catch (IOException e) {}
 
135
            }
 
136
            if (in != null) {
 
137
                try {
 
138
                    in.close();
 
139
                } catch (IOException e) {}
 
140
            }
 
141
            if (keyIn != null) {
 
142
                try {
 
143
                    keyIn.close();
 
144
                } catch (IOException e) {}
 
145
            }
 
146
        }
 
147
    }
 
148
 
 
149
    private PGPSecretKey readSecretKey(InputStream in) throws IOException, PGPException {
 
150
        in = PGPUtil.getDecoderStream(in);
 
151
        PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(in);
 
152
 
 
153
        PGPSecretKey key = null; 
 
154
        for (Iterator it = pgpSec.getKeyRings(); key == null && it.hasNext(); ) {
 
155
            PGPSecretKeyRing kRing = (PGPSecretKeyRing) it.next();   
 
156
            
 
157
            for (Iterator it2 = kRing.getSecretKeys(); key == null && it2.hasNext(); ) {
 
158
                PGPSecretKey k = (PGPSecretKey) it2.next();
 
159
                if ((keyId == null) && k.isSigningKey()) {
 
160
                    key = k;
 
161
                }
 
162
                if ((keyId != null) && (Long.valueOf(keyId, 16).longValue() == (k.getKeyID() & MASK))) {
 
163
                    key = k;
 
164
                }
 
165
            }
 
166
        }
 
167
        
 
168
        if (key == null) {
 
169
            throw new IllegalArgumentException("Can't find encryption key" + 
 
170
                (keyId != null ? " '" + keyId + "' " : " ") + "in key ring.");
 
171
        }
 
172
        
 
173
        return key;
 
174
    }
 
175
 
 
176
}