ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/security DistinguishedName.java DnameParam.java GenerateKey.java SignJar.java
Date Tue, 01 Jan 2002 08:41:29 GMT
donaldp     02/01/01 00:41:29

  Added:       proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/security
                        DistinguishedName.java DnameParam.java
                        GenerateKey.java SignJar.java
  Log:
  Moved security related classes into new package
  
  Revision  Changes    Path
  1.1                  jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/security/DistinguishedName.java
  
  Index: DistinguishedName.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.tools.ant.taskdefs.security;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  
  public class DistinguishedName
  {
      private ArrayList m_params = new ArrayList();
      private String m_name;
      private String m_path;
  
      public Iterator getParams()
      {
          return m_params.iterator();
      }
  
      public Object createParam()
      {
          final DnameParam param = new DnameParam();
          m_params.add( param );
          return param;
      }
  
      public String encode( final String string )
      {
          int end = string.indexOf( ',' );
          if( -1 == end )
          {
              return string;
          }
  
          final StringBuffer sb = new StringBuffer();
  
          int start = 0;
          while( -1 != end )
          {
              sb.append( string.substring( start, end ) );
              sb.append( "\\," );
              start = end + 1;
              end = string.indexOf( ',', start );
          }
  
          sb.append( string.substring( start ) );
  
          return sb.toString();
      }
  
      public String toString()
      {
          final int size = m_params.size();
          final StringBuffer sb = new StringBuffer();
          boolean firstPass = true;
  
          for( int i = 0; i < size; i++ )
          {
              if( !firstPass )
              {
                  sb.append( " ," );
              }
              firstPass = false;
  
              final DnameParam param = (DnameParam)m_params.get( i );
              sb.append( encode( param.getName() ) );
              sb.append( '=' );
              sb.append( encode( param.getValue() ) );
          }
  
          return sb.toString();
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/security/DnameParam.java
  
  Index: DnameParam.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.tools.ant.taskdefs.security;
  
  public final class DnameParam
  {
      private String m_name;
      private String m_value;
  
      public void setName( final String name )
      {
          m_name = name;
      }
  
      public void setValue( final String value )
      {
          m_value = value;
      }
  
      public String getName()
      {
          return m_name;
      }
  
      public String getValue()
      {
          return m_value;
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/security/GenerateKey.java
  
  Index: GenerateKey.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.tools.ant.taskdefs.security;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import org.apache.myrmidon.api.TaskException;
  import org.apache.tools.ant.Task;
  import org.apache.tools.ant.taskdefs.exec.ExecTask;
  
  /**
   * Generates a key.
   *
   * @author <a href="mailto:donaldp@apache.org">Peter Donald</a>
   */
  public class GenerateKey
      extends Task
  {
      /**
       * The alias of signer.
       */
      private String m_alias;
      private String m_dname;
      private DistinguishedName m_expandedDname;
      private String m_keyalg;
      private String m_keypass;
      private int m_keysize;
  
      /**
       * The name of keystore file.
       */
      private String m_keystore;
  
      private String m_sigalg;
      private String m_storepass;
      private String m_storetype;
      private int m_validity;
      private boolean m_verbose;
  
      public void setAlias( final String alias )
      {
          m_alias = alias;
      }
  
      public void setDname( final String dname )
          throws TaskException
      {
          if( null != m_expandedDname )
          {
              throw new TaskException( "It is not possible to specify dname both " +
                                       "as attribute and element." );
          }
          m_dname = dname;
      }
  
      public void setKeyalg( final String keyalg )
      {
          m_keyalg = keyalg;
      }
  
      public void setKeypass( final String keypass )
      {
          m_keypass = keypass;
      }
  
      public void setKeysize( final String keysize )
          throws TaskException
      {
          try
          {
              m_keysize = Integer.parseInt( keysize );
          }
          catch( final NumberFormatException nfe )
          {
              throw new TaskException( "KeySize attribute should be a integer" );
          }
      }
  
      public void setKeystore( final String keystore )
      {
          m_keystore = keystore;
      }
  
      public void setSigalg( final String sigalg )
      {
          m_sigalg = sigalg;
      }
  
      public void setStorepass( final String storepass )
      {
          m_storepass = storepass;
      }
  
      public void setStoretype( final String storetype )
      {
          m_storetype = storetype;
      }
  
      public void setValidity( final String validity )
          throws TaskException
      {
          try
          {
              m_validity = Integer.parseInt( validity );
          }
          catch( final NumberFormatException nfe )
          {
              throw new TaskException( "Validity attribute should be a integer" );
          }
      }
  
      public void setVerbose( final boolean verbose )
      {
          m_verbose = verbose;
      }
  
      public DistinguishedName createDname()
          throws TaskException
      {
          if( null != m_expandedDname )
          {
              throw new TaskException( "DName sub-element can only be specified once." );
          }
          if( null != m_dname )
          {
              throw new TaskException( "It is not possible to specify dname both " +
                                       "as attribute and element." );
          }
          m_expandedDname = new DistinguishedName();
          return m_expandedDname;
      }
  
      public void execute()
          throws TaskException
      {
          validate();
  
          final String message = "Generating Key for " + m_alias;
          getLogger().info( message );
  
          final ExecTask cmd = (ExecTask)getProject().createTask( "exec" );
          cmd.setExecutable( "keytool" );
  
          cmd.createArg().setValue( "-genkey " );
  
          if( m_verbose )
          {
              cmd.createArg().setValue( "-v " );
          }
  
          cmd.createArg().setValue( "-alias" );
          cmd.createArg().setValue( m_alias );
  
          if( null != m_dname )
          {
              cmd.createArg().setValue( "-dname" );
              cmd.createArg().setValue( m_dname );
          }
  
          if( null != m_expandedDname )
          {
              cmd.createArg().setValue( "-dname" );
              cmd.createArg().setValue( m_expandedDname.toString() );
          }
  
          if( null != m_keystore )
          {
              cmd.createArg().setValue( "-keystore" );
              cmd.createArg().setValue( m_keystore );
          }
  
          if( null != m_storepass )
          {
              cmd.createArg().setValue( "-storepass" );
              cmd.createArg().setValue( m_storepass );
          }
  
          if( null != m_storetype )
          {
              cmd.createArg().setValue( "-storetype" );
              cmd.createArg().setValue( m_storetype );
          }
  
          cmd.createArg().setValue( "-keypass" );
          if( null != m_keypass )
          {
              cmd.createArg().setValue( m_keypass );
          }
          else
          {
              cmd.createArg().setValue( m_storepass );
          }
  
          if( null != m_sigalg )
          {
              cmd.createArg().setValue( "-sigalg" );
              cmd.createArg().setValue( m_sigalg );
          }
  
          if( null != m_keyalg )
          {
              cmd.createArg().setValue( "-keyalg" );
              cmd.createArg().setValue( m_keyalg );
          }
  
          if( 0 < m_keysize )
          {
              cmd.createArg().setValue( "-keysize" );
              cmd.createArg().setValue( "" + m_keysize );
          }
  
          if( 0 < m_validity )
          {
              cmd.createArg().setValue( "-validity" );
              cmd.createArg().setValue( "" + m_validity );
          }
  
          cmd.execute();
      }
  
      private void validate() throws TaskException
      {
          if( null == m_alias )
          {
              final String message = "alias attribute must be set";
              throw new TaskException( message );
          }
  
          if( null == m_storepass )
          {
              final String message = "storepass attribute must be set";
              throw new TaskException( message );
          }
  
          if( null == m_dname && null == m_expandedDname )
          {
              final String message = "dname must be set";
              throw new TaskException( message );
          }
      }
  }
  
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/security/SignJar.java
  
  Index: SignJar.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.tools.ant.taskdefs.security;
  
  import java.io.File;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.Enumeration;
  import java.util.zip.ZipEntry;
  import java.util.zip.ZipFile;
  import org.apache.myrmidon.api.TaskException;
  import org.apache.tools.ant.DirectoryScanner;
  import org.apache.tools.ant.Task;
  import org.apache.tools.ant.taskdefs.exec.ExecTask;
  import org.apache.tools.ant.types.FileSet;
  
  /**
   * Sign a archive.
   *
   * @author Peter Donald <a href="mailto:donaldp@apache.org">donaldp@apache.org
   *      </a>
   * @author Nick Fortescue <a href="mailto:nick@ox.compsoc.net">
   *      nick@ox.compsoc.net</a>
   */
  public class SignJar
      extends Task
  {
      /**
       * the filesets of the jars to sign
       */
      protected ArrayList filesets = new ArrayList();
  
      /**
       * The alias of signer.
       */
      protected String alias;
      protected boolean internalsf;
  
      /**
       * The name of the jar file.
       */
      protected File jar;
      protected String keypass;
  
      /**
       * The name of keystore file.
       */
      protected File keystore;
      /**
       * Whether to assume a jar which has an appropriate .SF file in is already
       * signed.
       */
      protected boolean lazy;
      protected boolean sectionsonly;
      protected File sigfile;
      protected File signedjar;
  
      protected String storepass;
      protected String storetype;
      protected boolean verbose;
  
      public void setAlias( final String alias )
      {
          this.alias = alias;
      }
  
      public void setInternalsf( final boolean internalsf )
      {
          this.internalsf = internalsf;
      }
  
      public void setJar( final File jar )
      {
          this.jar = jar;
      }
  
      public void setKeypass( final String keypass )
      {
          this.keypass = keypass;
      }
  
      public void setKeystore( final File keystore )
      {
          this.keystore = keystore;
      }
  
      public void setLazy( final boolean lazy )
      {
          this.lazy = lazy;
      }
  
      public void setSectionsonly( final boolean sectionsonly )
      {
          this.sectionsonly = sectionsonly;
      }
  
      public void setSigfile( final File sigfile )
      {
          this.sigfile = sigfile;
      }
  
      public void setSignedjar( final File signedjar )
      {
          this.signedjar = signedjar;
      }
  
      public void setStorepass( final String storepass )
      {
          this.storepass = storepass;
      }
  
      public void setStoretype( final String storetype )
      {
          this.storetype = storetype;
      }
  
      public void setVerbose( final boolean verbose )
      {
          this.verbose = verbose;
      }
  
      /**
       * Adds a set of files (nested fileset attribute).
       *
       * @param set The feature to be added to the Fileset attribute
       */
      public void addFileset( final FileSet set )
      {
          filesets.add( set );
      }
  
      public void execute()
          throws TaskException
      {
          if( null == jar && null == filesets )
          {
              throw new TaskException( "jar must be set through jar attribute or nested filesets"
);
          }
          if( null != jar )
          {
              doOneJar( jar, signedjar );
              return;
          }
          else
          {
              //Assume null != filesets
  
              // deal with the filesets
              for( int i = 0; i < filesets.size(); i++ )
              {
                  FileSet fs = (FileSet)filesets.get( i );
                  DirectoryScanner ds = fs.getDirectoryScanner( getProject() );
                  String[] jarFiles = ds.getIncludedFiles();
                  for( int j = 0; j < jarFiles.length; j++ )
                  {
                      doOneJar( new File( fs.getDir( getProject() ), jarFiles[ j ] ), null
);
                  }
              }
          }
      }
  
      protected boolean isSigned( File file )
      {
          final String SIG_START = "META-INF/";
          final String SIG_END = ".SF";
  
          if( !file.exists() )
          {
              return false;
          }
          ZipFile jarFile = null;
          try
          {
              jarFile = new ZipFile( file );
              if( null == alias )
              {
                  Enumeration entries = jarFile.entries();
                  while( entries.hasMoreElements() )
                  {
                      String name = ( (ZipEntry)entries.nextElement() ).getName();
                      if( name.startsWith( SIG_START ) && name.endsWith( SIG_END )
)
                      {
                          return true;
                      }
                  }
                  return false;
              }
              else
              {
                  return jarFile.getEntry( SIG_START + alias.toUpperCase() +
                                           SIG_END ) != null;
              }
          }
          catch( IOException e )
          {
              return false;
          }
          finally
          {
              if( jarFile != null )
              {
                  try
                  {
                      jarFile.close();
                  }
                  catch( IOException e )
                  {
                  }
              }
          }
      }
  
      protected boolean isUpToDate( File jarFile, File signedjarFile )
      {
          if( null == jarFile )
          {
              return false;
          }
  
          if( null != signedjarFile )
          {
  
              if( !jarFile.exists() )
                  return false;
              if( !signedjarFile.exists() )
                  return false;
              if( jarFile.equals( signedjarFile ) )
                  return false;
              if( signedjarFile.lastModified() > jarFile.lastModified() )
                  return true;
          }
          else
          {
              if( lazy )
              {
                  return isSigned( jarFile );
              }
          }
  
          return false;
      }
  
      private void doOneJar( File jarSource, File jarTarget )
          throws TaskException
      {
          if( null == alias )
          {
              throw new TaskException( "alias attribute must be set" );
          }
  
          if( null == storepass )
          {
              throw new TaskException( "storepass attribute must be set" );
          }
  
          if( isUpToDate( jarSource, jarTarget ) )
              return;
  
          final StringBuffer sb = new StringBuffer();
  
          final ExecTask cmd = (ExecTask)getProject().createTask( "exec" );
          cmd.setExecutable( "jarsigner" );
  
          if( null != keystore )
          {
              cmd.createArg().setValue( "-keystore" );
              cmd.createArg().setValue( keystore.toString() );
          }
  
          if( null != storepass )
          {
              cmd.createArg().setValue( "-storepass" );
              cmd.createArg().setValue( storepass );
          }
  
          if( null != storetype )
          {
              cmd.createArg().setValue( "-storetype" );
              cmd.createArg().setValue( storetype );
          }
  
          if( null != keypass )
          {
              cmd.createArg().setValue( "-keypass" );
              cmd.createArg().setValue( keypass );
          }
  
          if( null != sigfile )
          {
              cmd.createArg().setValue( "-sigfile" );
              cmd.createArg().setValue( sigfile.toString() );
          }
  
          if( null != jarTarget )
          {
              cmd.createArg().setValue( "-signedjar" );
              cmd.createArg().setValue( jarTarget.toString() );
          }
  
          if( verbose )
          {
              cmd.createArg().setValue( "-verbose" );
          }
  
          if( internalsf )
          {
              cmd.createArg().setValue( "-internalsf" );
          }
  
          if( sectionsonly )
          {
              cmd.createArg().setValue( "-sectionsonly" );
          }
  
          cmd.createArg().setValue( jarSource.toString() );
  
          cmd.createArg().setValue( alias );
  
          getLogger().info( "Signing Jar : " + jarSource.getAbsolutePath() );
          cmd.execute();
      }
  }
  
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message