| 1 |
779 |
jeremybenn |
/* ExportCmd.java -- The export command handler of the keytool
|
| 2 |
|
|
Copyright (C) 2006, 2007 Free Software Foundation, Inc.
|
| 3 |
|
|
|
| 4 |
|
|
This file is part of GNU Classpath.
|
| 5 |
|
|
|
| 6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
| 7 |
|
|
it under the terms of the GNU General Public License as published by
|
| 8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
| 9 |
|
|
any later version.
|
| 10 |
|
|
|
| 11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
| 12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 14 |
|
|
General Public License for more details.
|
| 15 |
|
|
|
| 16 |
|
|
You should have received a copy of the GNU General Public License
|
| 17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
| 18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
| 19 |
|
|
02110-1301 USA.
|
| 20 |
|
|
|
| 21 |
|
|
Linking this library statically or dynamically with other modules is
|
| 22 |
|
|
making a combined work based on this library. Thus, the terms and
|
| 23 |
|
|
conditions of the GNU General Public License cover the whole
|
| 24 |
|
|
combination.
|
| 25 |
|
|
|
| 26 |
|
|
As a special exception, the copyright holders of this library give you
|
| 27 |
|
|
permission to link this library with independent modules to produce an
|
| 28 |
|
|
executable, regardless of the license terms of these independent
|
| 29 |
|
|
modules, and to copy and distribute the resulting executable under
|
| 30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
| 31 |
|
|
independent module, the terms and conditions of the license of that
|
| 32 |
|
|
module. An independent module is a module which is not derived from
|
| 33 |
|
|
or based on this library. If you modify this library, you may extend
|
| 34 |
|
|
this exception to your version of the library, but you are not
|
| 35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
| 36 |
|
|
exception statement from your version. */
|
| 37 |
|
|
|
| 38 |
|
|
|
| 39 |
|
|
package gnu.classpath.tools.keytool;
|
| 40 |
|
|
|
| 41 |
|
|
import gnu.classpath.Configuration;
|
| 42 |
|
|
import gnu.classpath.tools.common.ClasspathToolParser;
|
| 43 |
|
|
import gnu.classpath.tools.getopt.Option;
|
| 44 |
|
|
import gnu.classpath.tools.getopt.OptionException;
|
| 45 |
|
|
import gnu.classpath.tools.getopt.OptionGroup;
|
| 46 |
|
|
import gnu.classpath.tools.getopt.Parser;
|
| 47 |
|
|
import gnu.java.util.Base64;
|
| 48 |
|
|
|
| 49 |
|
|
import java.io.IOException;
|
| 50 |
|
|
import java.io.PrintWriter;
|
| 51 |
|
|
import java.security.KeyStoreException;
|
| 52 |
|
|
import java.security.cert.Certificate;
|
| 53 |
|
|
import java.security.cert.CertificateEncodingException;
|
| 54 |
|
|
import java.util.logging.Logger;
|
| 55 |
|
|
|
| 56 |
|
|
/**
|
| 57 |
|
|
* The <b>-export</b> keytool command handler is used to read the certificate
|
| 58 |
|
|
* associated with a designated alias from the key store, and write it to a
|
| 59 |
|
|
* designated file.
|
| 60 |
|
|
* <p>
|
| 61 |
|
|
* Possible options for this command are:
|
| 62 |
|
|
* <p>
|
| 63 |
|
|
* <dl>
|
| 64 |
|
|
* <dt>-alias ALIAS</dt>
|
| 65 |
|
|
* <dd>Every entry, be it a <i>Key Entry</i> or a <i>Trusted
|
| 66 |
|
|
* Certificate</i>, in a key store is uniquely identified by a user-defined
|
| 67 |
|
|
* <i>Alias</i> string. Use this option to specify the <i>Alias</i> to use
|
| 68 |
|
|
* when referring to an entry in the key store. Unless specified otherwise,
|
| 69 |
|
|
* a default value of <code>mykey</code> shall be used when this option is
|
| 70 |
|
|
* omitted from the command line.
|
| 71 |
|
|
* <p></dd>
|
| 72 |
|
|
*
|
| 73 |
|
|
* <dt>-file FILE_NAME</dt>
|
| 74 |
|
|
* <dd>The fully qualified path of the file where the certificate will be
|
| 75 |
|
|
* exported to. If omitted, STDOUT will be used instead.
|
| 76 |
|
|
* <p></dd>
|
| 77 |
|
|
*
|
| 78 |
|
|
* <dt>-storetype STORE_TYPE</dt>
|
| 79 |
|
|
* <dd>Use this option to specify the type of the key store to use. The
|
| 80 |
|
|
* default value, if this option is omitted, is that of the property
|
| 81 |
|
|
* <code>keystore.type</code> in the security properties file, which is
|
| 82 |
|
|
* obtained by invoking the {@link java.security.KeyStore#getDefaultType()}
|
| 83 |
|
|
* static method.
|
| 84 |
|
|
* <p></dd>
|
| 85 |
|
|
*
|
| 86 |
|
|
* <dt>-keystore URL</dt>
|
| 87 |
|
|
* <dd>Use this option to specify the location of the key store to use.
|
| 88 |
|
|
* The default value is a file {@link java.net.URL} referencing the file
|
| 89 |
|
|
* named <code>.keystore</code> located in the path returned by the call to
|
| 90 |
|
|
* {@link java.lang.System#getProperty(String)} using <code>user.home</code>
|
| 91 |
|
|
* as argument.
|
| 92 |
|
|
* <p>
|
| 93 |
|
|
* If a URL was specified, but was found to be malformed --e.g. missing
|
| 94 |
|
|
* protocol element-- the tool will attempt to use the URL value as a file-
|
| 95 |
|
|
* name (with absolute or relative path-name) of a key store --as if the
|
| 96 |
|
|
* protocol was <code>file:</code>.
|
| 97 |
|
|
* <p></dd>
|
| 98 |
|
|
*
|
| 99 |
|
|
* <dt>-storepass PASSWORD</dt>
|
| 100 |
|
|
* <dd>Use this option to specify the password protecting the key store. If
|
| 101 |
|
|
* this option is omitted from the command line, you will be prompted to
|
| 102 |
|
|
* provide a password.
|
| 103 |
|
|
* <p></dd>
|
| 104 |
|
|
*
|
| 105 |
|
|
* <dt>-provider PROVIDER_CLASS_NAME</dt>
|
| 106 |
|
|
* <dd>A fully qualified class name of a Security Provider to add to the
|
| 107 |
|
|
* current list of Security Providers already installed in the JVM in-use.
|
| 108 |
|
|
* If a provider class is specified with this option, and was successfully
|
| 109 |
|
|
* added to the runtime --i.e. it was not already installed-- then the tool
|
| 110 |
|
|
* will attempt to removed this Security Provider before exiting.
|
| 111 |
|
|
* <p></dd>
|
| 112 |
|
|
*
|
| 113 |
|
|
* <dt>-rfc</dt>
|
| 114 |
|
|
* <dd>Use RFC-1421 specifications when encoding the output.
|
| 115 |
|
|
* <p></dd>
|
| 116 |
|
|
*
|
| 117 |
|
|
* <dt>-v</dt>
|
| 118 |
|
|
* <dd>Output the certificate in binary DER encoding. This is the default
|
| 119 |
|
|
* output format of the command if neither <code>-rfc</code> nor
|
| 120 |
|
|
* <code>-v</code> options were detected on the command line. If both this
|
| 121 |
|
|
* option and the <code>-rfc</code> option are detected on the command
|
| 122 |
|
|
* line, the tool will opt for the RFC-1421 style encoding.</dd>
|
| 123 |
|
|
* </dl>
|
| 124 |
|
|
*/
|
| 125 |
|
|
class ExportCmd extends Command
|
| 126 |
|
|
{
|
| 127 |
|
|
private static final Logger log = Logger.getLogger(ExportCmd.class.getName());
|
| 128 |
|
|
protected String _alias;
|
| 129 |
|
|
protected String _certFileName;
|
| 130 |
|
|
protected String _ksType;
|
| 131 |
|
|
protected String _ksURL;
|
| 132 |
|
|
protected String _ksPassword;
|
| 133 |
|
|
protected String _providerClassName;
|
| 134 |
|
|
protected boolean rfc;
|
| 135 |
|
|
|
| 136 |
|
|
// default 0-arguments constructor
|
| 137 |
|
|
|
| 138 |
|
|
// public setters -----------------------------------------------------------
|
| 139 |
|
|
|
| 140 |
|
|
/** @param alias the alias to use. */
|
| 141 |
|
|
public void setAlias(String alias)
|
| 142 |
|
|
{
|
| 143 |
|
|
this._alias = alias;
|
| 144 |
|
|
}
|
| 145 |
|
|
|
| 146 |
|
|
/** @param pathName the fully qualified path name of the file to process. */
|
| 147 |
|
|
public void setFile(String pathName)
|
| 148 |
|
|
{
|
| 149 |
|
|
this._certFileName = pathName;
|
| 150 |
|
|
}
|
| 151 |
|
|
|
| 152 |
|
|
/** @param type the key-store type to use. */
|
| 153 |
|
|
public void setStoretype(String type)
|
| 154 |
|
|
{
|
| 155 |
|
|
this._ksType = type;
|
| 156 |
|
|
}
|
| 157 |
|
|
|
| 158 |
|
|
/** @param url the key-store URL to use. */
|
| 159 |
|
|
public void setKeystore(String url)
|
| 160 |
|
|
{
|
| 161 |
|
|
this._ksURL = url;
|
| 162 |
|
|
}
|
| 163 |
|
|
|
| 164 |
|
|
/** @param password the key-store password to use. */
|
| 165 |
|
|
public void setStorepass(String password)
|
| 166 |
|
|
{
|
| 167 |
|
|
this._ksPassword = password;
|
| 168 |
|
|
}
|
| 169 |
|
|
|
| 170 |
|
|
/** @param className a security provider fully qualified class name to use. */
|
| 171 |
|
|
public void setProvider(String className)
|
| 172 |
|
|
{
|
| 173 |
|
|
this._providerClassName = className;
|
| 174 |
|
|
}
|
| 175 |
|
|
|
| 176 |
|
|
/**
|
| 177 |
|
|
* @param flag whether to use, or not, RFC-1421 format when exporting the
|
| 178 |
|
|
* certificate(s).
|
| 179 |
|
|
*/
|
| 180 |
|
|
public void setRfc(String flag)
|
| 181 |
|
|
{
|
| 182 |
|
|
this.rfc = Boolean.valueOf(flag).booleanValue();
|
| 183 |
|
|
}
|
| 184 |
|
|
|
| 185 |
|
|
// life-cycle methods -------------------------------------------------------
|
| 186 |
|
|
|
| 187 |
|
|
void setup() throws Exception
|
| 188 |
|
|
{
|
| 189 |
|
|
setOutputStreamParam(_certFileName);
|
| 190 |
|
|
setKeyStoreParams(_providerClassName, _ksType, _ksPassword, _ksURL);
|
| 191 |
|
|
setAliasParam(_alias);
|
| 192 |
|
|
if (Configuration.DEBUG)
|
| 193 |
|
|
{
|
| 194 |
|
|
log.fine("-export handler will use the following options:"); //$NON-NLS-1$
|
| 195 |
|
|
log.fine(" -alias=" + alias); //$NON-NLS-1$
|
| 196 |
|
|
log.fine(" -file=" + _certFileName); //$NON-NLS-1$
|
| 197 |
|
|
log.fine(" -storetype=" + storeType); //$NON-NLS-1$
|
| 198 |
|
|
log.fine(" -keystore=" + storeURL); //$NON-NLS-1$
|
| 199 |
|
|
log.fine(" -provider=" + provider); //$NON-NLS-1$
|
| 200 |
|
|
log.fine(" -rfc=" + rfc); //$NON-NLS-1$
|
| 201 |
|
|
log.fine(" -v=" + verbose); //$NON-NLS-1$
|
| 202 |
|
|
}
|
| 203 |
|
|
}
|
| 204 |
|
|
|
| 205 |
|
|
void start() throws KeyStoreException, CertificateEncodingException,
|
| 206 |
|
|
IOException
|
| 207 |
|
|
{
|
| 208 |
|
|
if (Configuration.DEBUG)
|
| 209 |
|
|
log.entering(this.getClass().getName(), "start"); //$NON-NLS-1$
|
| 210 |
|
|
ensureStoreContainsAlias();
|
| 211 |
|
|
Certificate certificate;
|
| 212 |
|
|
if (store.isCertificateEntry(alias))
|
| 213 |
|
|
{
|
| 214 |
|
|
if (Configuration.DEBUG)
|
| 215 |
|
|
log.fine("Alias [" + alias + "] is a trusted certificate"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 216 |
|
|
certificate = store.getCertificate(alias);
|
| 217 |
|
|
}
|
| 218 |
|
|
else
|
| 219 |
|
|
{
|
| 220 |
|
|
if (Configuration.DEBUG)
|
| 221 |
|
|
log.fine("Alias [" + alias + "] is a key entry"); //$NON-NLS-1$ //$NON-NLS-2$
|
| 222 |
|
|
Certificate[] chain = store.getCertificateChain(alias);
|
| 223 |
|
|
certificate = chain[0];
|
| 224 |
|
|
}
|
| 225 |
|
|
|
| 226 |
|
|
byte[] derBytes = certificate.getEncoded();
|
| 227 |
|
|
if (rfc)
|
| 228 |
|
|
{
|
| 229 |
|
|
String encoded = Base64.encode(derBytes, 72);
|
| 230 |
|
|
PrintWriter pw = new PrintWriter(outStream, true);
|
| 231 |
|
|
pw.println("-----BEGIN CERTIFICATE-----"); //$NON-NLS-1$
|
| 232 |
|
|
pw.println(encoded);
|
| 233 |
|
|
pw.println("-----END CERTIFICATE-----"); //$NON-NLS-1$
|
| 234 |
|
|
}
|
| 235 |
|
|
else
|
| 236 |
|
|
outStream.write(derBytes);
|
| 237 |
|
|
|
| 238 |
|
|
// stream is closed in Command.teardown()
|
| 239 |
|
|
if (Configuration.DEBUG)
|
| 240 |
|
|
log.exiting(this.getClass().getName(), "start"); //$NON-NLS-1$
|
| 241 |
|
|
}
|
| 242 |
|
|
|
| 243 |
|
|
// own methods --------------------------------------------------------------
|
| 244 |
|
|
|
| 245 |
|
|
Parser getParser()
|
| 246 |
|
|
{
|
| 247 |
|
|
if (Configuration.DEBUG)
|
| 248 |
|
|
log.entering(this.getClass().getName(), "getParser"); //$NON-NLS-1$
|
| 249 |
|
|
Parser result = new ClasspathToolParser(Main.EXPORT_CMD, true);
|
| 250 |
|
|
result.setHeader(Messages.getString("ExportCmd.17")); //$NON-NLS-1$
|
| 251 |
|
|
result.setFooter(Messages.getString("ExportCmd.18")); //$NON-NLS-1$
|
| 252 |
|
|
OptionGroup options = new OptionGroup(Messages.getString("ExportCmd.19")); //$NON-NLS-1$
|
| 253 |
|
|
options.add(new Option(Main.ALIAS_OPT,
|
| 254 |
|
|
Messages.getString("ExportCmd.20"), //$NON-NLS-1$
|
| 255 |
|
|
Messages.getString("ExportCmd.21")) //$NON-NLS-1$
|
| 256 |
|
|
{
|
| 257 |
|
|
public void parsed(String argument) throws OptionException
|
| 258 |
|
|
{
|
| 259 |
|
|
_alias = argument;
|
| 260 |
|
|
}
|
| 261 |
|
|
});
|
| 262 |
|
|
options.add(new Option(Main.FILE_OPT,
|
| 263 |
|
|
Messages.getString("ExportCmd.22"), //$NON-NLS-1$
|
| 264 |
|
|
Messages.getString("ExportCmd.23")) //$NON-NLS-1$
|
| 265 |
|
|
{
|
| 266 |
|
|
public void parsed(String argument) throws OptionException
|
| 267 |
|
|
{
|
| 268 |
|
|
_certFileName = argument;
|
| 269 |
|
|
}
|
| 270 |
|
|
});
|
| 271 |
|
|
options.add(new Option(Main.STORETYPE_OPT,
|
| 272 |
|
|
Messages.getString("ExportCmd.24"), //$NON-NLS-1$
|
| 273 |
|
|
Messages.getString("ExportCmd.25")) //$NON-NLS-1$
|
| 274 |
|
|
{
|
| 275 |
|
|
public void parsed(String argument) throws OptionException
|
| 276 |
|
|
{
|
| 277 |
|
|
_ksType = argument;
|
| 278 |
|
|
}
|
| 279 |
|
|
});
|
| 280 |
|
|
options.add(new Option(Main.KEYSTORE_OPT,
|
| 281 |
|
|
Messages.getString("ExportCmd.26"), //$NON-NLS-1$
|
| 282 |
|
|
Messages.getString("ExportCmd.27")) //$NON-NLS-1$
|
| 283 |
|
|
{
|
| 284 |
|
|
public void parsed(String argument) throws OptionException
|
| 285 |
|
|
{
|
| 286 |
|
|
_ksURL = argument;
|
| 287 |
|
|
}
|
| 288 |
|
|
});
|
| 289 |
|
|
options.add(new Option(Main.STOREPASS_OPT,
|
| 290 |
|
|
Messages.getString("ExportCmd.28"), //$NON-NLS-1$
|
| 291 |
|
|
Messages.getString("ExportCmd.29")) //$NON-NLS-1$
|
| 292 |
|
|
{
|
| 293 |
|
|
public void parsed(String argument) throws OptionException
|
| 294 |
|
|
{
|
| 295 |
|
|
_ksPassword = argument;
|
| 296 |
|
|
}
|
| 297 |
|
|
});
|
| 298 |
|
|
options.add(new Option(Main.PROVIDER_OPT,
|
| 299 |
|
|
Messages.getString("ExportCmd.30"), //$NON-NLS-1$
|
| 300 |
|
|
Messages.getString("ExportCmd.31")) //$NON-NLS-1$
|
| 301 |
|
|
{
|
| 302 |
|
|
public void parsed(String argument) throws OptionException
|
| 303 |
|
|
{
|
| 304 |
|
|
_providerClassName = argument;
|
| 305 |
|
|
}
|
| 306 |
|
|
});
|
| 307 |
|
|
options.add(new Option(Main.RFC_OPT,
|
| 308 |
|
|
Messages.getString("ExportCmd.32")) //$NON-NLS-1$
|
| 309 |
|
|
{
|
| 310 |
|
|
public void parsed(String argument) throws OptionException
|
| 311 |
|
|
{
|
| 312 |
|
|
rfc = true;
|
| 313 |
|
|
}
|
| 314 |
|
|
});
|
| 315 |
|
|
options.add(new Option(Main.VERBOSE_OPT,
|
| 316 |
|
|
Messages.getString("ExportCmd.33")) //$NON-NLS-1$
|
| 317 |
|
|
{
|
| 318 |
|
|
public void parsed(String argument) throws OptionException
|
| 319 |
|
|
{
|
| 320 |
|
|
verbose = true;
|
| 321 |
|
|
}
|
| 322 |
|
|
});
|
| 323 |
|
|
result.add(options);
|
| 324 |
|
|
if (Configuration.DEBUG)
|
| 325 |
|
|
log.exiting(this.getClass().getName(), "getParser", result); //$NON-NLS-1$
|
| 326 |
|
|
return result;
|
| 327 |
|
|
}
|
| 328 |
|
|
}
|