Coverage Report - org.apache.commons.openpgp.ant.OpenPgpVerifierTask
 
Classes in this File Line Coverage Branch Coverage Complexity
OpenPgpVerifierTask
0%
0/81
0%
0/30
4,091
 
 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  
 package org.apache.commons.openpgp.ant;
 18  
 
 19  
 import java.io.File;
 20  
 import java.io.FileInputStream;
 21  
 import java.io.FileNotFoundException;
 22  
 import java.io.IOException;
 23  
 import org.apache.commons.openpgp.BouncyCastleKeyRing;
 24  
 import org.apache.commons.openpgp.BouncyCastleOpenPgpSignatureVerifier;
 25  
 import org.apache.commons.openpgp.KeyRing;
 26  
 import org.apache.commons.openpgp.OpenPgpException;
 27  
 import org.apache.commons.openpgp.OpenPgpSignatureVerifier;
 28  
 import org.apache.commons.openpgp.SignatureStatus;
 29  
 import org.apache.tools.ant.BuildException;
 30  
 import org.apache.tools.ant.Task;
 31  
 import org.apache.tools.ant.types.Mapper;
 32  
 import org.apache.tools.ant.util.FileNameMapper;
 33  
 import org.apache.tools.ant.util.FileUtils;
 34  
 import org.apache.tools.ant.util.GlobPatternMapper;
 35  
 import org.bouncycastle.openpgp.PGPException;
 36  
 
 37  
 /**
 38  
  * Verify a signature using the Bouncy Castle OpenPGP provider.
 39  
  *
 40  
  * @author <a href="mailto:dennisl@apache.org">Dennis Lundberg</a>
 41  
  */
 42  0
 public class OpenPgpVerifierTask extends Task {
 43  
     private File secring;
 44  
     private File pubring;
 45  
     private String password;
 46  
     private File artefact;
 47  0
     private boolean asciiarmor = true;
 48  
     private Mapper mapperElement;
 49  
     private String verifyproperty;
 50  
 
 51  
     /**
 52  
      * Set the secret keyring.
 53  
      * @param secring secret keyring file
 54  
      */
 55  
     public void setSecring(File secring) {
 56  0
         this.secring = secring;
 57  0
     }
 58  
 
 59  
     /**
 60  
      * Set the public keyring.
 61  
      * @param pubring public keyring file
 62  
      */
 63  
     public void setPubring(File pubring) {
 64  0
         this.pubring = pubring;
 65  0
     }
 66  
 
 67  
     /**
 68  
      * Use ASCII armored signature files?
 69  
      * @param asciiarmor ascii armored signatures?
 70  
      */
 71  
     public void setAsciiarmor(boolean asciiarmor) {
 72  0
         this.asciiarmor = asciiarmor;
 73  0
     }
 74  
 
 75  
     /**
 76  
      * Set the value of the password.
 77  
      * @param password value of the password
 78  
      */
 79  
     public void setPassword(String password) {
 80  0
         this.password = password;
 81  0
     }
 82  
 
 83  
     /**
 84  
      * Set the artefact to be handled.
 85  
      * @param artefact artefact to be handled
 86  
      */
 87  
     public void setArtefact(File artefact) {
 88  0
         this.artefact = artefact;
 89  0
     }
 90  
 
 91  
     /**
 92  
      * Set the name of the property that contains the result of the verification.
 93  
      * @param verifyproperty name of the property
 94  
      */
 95  
     public void setVerifyproperty(String verifyproperty) {
 96  0
         this.verifyproperty = verifyproperty;
 97  0
     }
 98  
 
 99  
     /**
 100  
      * Define the mapper to map source to destination files.
 101  
      * @return a mapper to be configured.
 102  
      * @exception org.apache.tools.ant.BuildException if more than one mapper is defined.
 103  
      */
 104  
     public Mapper createMapper() throws BuildException {
 105  0
         if (mapperElement != null) {
 106  0
             throw new BuildException("Cannot define more than one mapper",
 107  
                     getLocation());
 108  
         }
 109  0
         mapperElement = new Mapper(getProject());
 110  0
         return mapperElement;
 111  
     }
 112  
 
 113  
     public void execute() {
 114  0
         if (secring == null) {
 115  0
             throw new BuildException("secring attribute compulsory");
 116  
         }
 117  0
         if (pubring == null) {
 118  0
             throw new BuildException("pubring attribute compulsory");
 119  
         }
 120  0
         if (password == null) {
 121  0
             throw new BuildException("password attribute compulsory");
 122  
         }
 123  0
         if (artefact == null) {
 124  0
             throw new BuildException("The 'artefact' attribute is compulsory.");
 125  
         }
 126  0
         if (verifyproperty == null) {
 127  0
             throw new BuildException("The 'verifyproperty' attribute is compulsory.");
 128  
         }
 129  0
         if (!secring.exists() || !secring.canRead()) {
 130  0
             throw new  BuildException("secret keyring file '" + secring.getAbsolutePath() + "' does not exist or is not readable");
 131  
         }
 132  0
         if (!pubring.exists() || !pubring.canRead()) {
 133  0
             throw new  BuildException("public keyring file '" + pubring.getAbsolutePath() + "' does not exist or is not readable");
 134  
         }
 135  
         FileInputStream secStream;
 136  
         FileInputStream pubStream;
 137  0
         KeyRing keyRing = null;
 138  
         try {
 139  0
             secStream = new FileInputStream(secring);
 140  0
             pubStream = new FileInputStream(pubring);
 141  0
             keyRing = new BouncyCastleKeyRing(secStream,
 142  
                     pubStream, password.toCharArray() );
 143  0
         } catch (IOException ioe) {
 144  0
             throw new BuildException(ioe);
 145  0
         } catch (PGPException pgpe) {
 146  0
             throw new BuildException(pgpe);
 147  0
         }
 148  0
         if (artefact != null) {
 149  0
             doHandle(keyRing, artefact);
 150  
         }
 151  0
         FileUtils.close(secStream);
 152  0
         FileUtils.close(pubStream);
 153  0
     }
 154  
 
 155  
     private void doHandle(KeyRing keyRing, File oneartefact) {
 156  0
         doHandle(keyRing, oneartefact, oneartefact.getParentFile(), oneartefact.getName());
 157  0
     }
 158  
 
 159  
     private void doHandle(KeyRing keyRing, File oneartefact, File basedir, String relpath) {
 160  0
         FileInputStream artifactFis = null;
 161  0
         FileInputStream signatureFis = null;
 162  
         File signature;
 163  0
         boolean isValid = false;
 164  
 
 165  
         try {
 166  0
             artifactFis = new FileInputStream(oneartefact);
 167  0
             FileNameMapper mapper = getMapper();
 168  0
             String [] mappedFiles = mapper.mapFileName(relpath);
 169  0
             if (mappedFiles == null || mappedFiles.length != 1) {
 170  0
                 throw new BuildException("mapper returned more or less than one output");
 171  
             }
 172  0
             signature = new File(basedir, mappedFiles[0]);
 173  0
             signatureFis = new FileInputStream(signature);
 174  0
             OpenPgpSignatureVerifier verifier = new BouncyCastleOpenPgpSignatureVerifier();
 175  0
             SignatureStatus status = verifier.verifyDetachedSignature(artifactFis, signatureFis, keyRing);
 176  0
             isValid = status.isValid();
 177  0
         } catch (FileNotFoundException fnfe) {
 178  0
             throw new BuildException(fnfe);
 179  0
         } catch (IOException ioe) {
 180  0
             throw new BuildException(ioe);
 181  0
         } catch (OpenPgpException opgpe) {
 182  0
             throw new BuildException(opgpe);
 183  
         }
 184  
         finally {
 185  0
             getProject().setProperty(verifyproperty, Boolean.toString(isValid));
 186  0
         }
 187  0
         FileUtils.close(signatureFis);
 188  0
         FileUtils.close(artifactFis);
 189  0
     }
 190  
 
 191  
     /**
 192  
      * Return the mapper to use based on nested elements or use a default mapping.
 193  
      */
 194  
     private FileNameMapper getMapper() {
 195  0
         FileNameMapper mapper = null;
 196  0
         if (mapperElement != null) {
 197  0
             mapper = mapperElement.getImplementation();
 198  
         } else {
 199  0
             mapper = new GlobPatternMapper();
 200  0
             mapper.setFrom("*");
 201  0
             if (asciiarmor) {
 202  0
                 mapper.setTo("*.asc");
 203  
             } else {
 204  0
                 mapper.setTo("*.sig");
 205  
             }
 206  
         }
 207  0
         return mapper;
 208  
     }
 209  
 }