| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.tools; |
| 16 |
|
| 17 |
import org.globus.gsi.util.CertificateLoadUtil; |
| 18 |
import org.globus.gsi.util.CertificateUtil; |
| 19 |
|
| 20 |
|
| 21 |
import java.security.cert.X509Certificate; |
| 22 |
import java.util.TimeZone; |
| 23 |
import java.text.DateFormat; |
| 24 |
import java.text.SimpleDateFormat; |
| 25 |
|
| 26 |
import org.globus.common.CoGProperties; |
| 27 |
import org.globus.common.Version; |
| 28 |
|
| 29 |
/** Returns information about the Cert |
| 30 |
* <pre> |
| 31 |
* Syntax: java CertInfo [-help] [-file certfile] [-all] [-subject] [...] |
| 32 |
* Displays certificate information. Unless the optional -file |
| 33 |
* argument is given, the default location of the file containing the |
| 34 |
* certficate is assumed: |
| 35 |
* -- Config.getUserCertFile() |
| 36 |
* Options |
| 37 |
* -help, -usage Display usage |
| 38 |
* -version Display version |
| 39 |
* -file certfile Use 'certfile' at non-default location |
| 40 |
* -globus Prints information in globus format |
| 41 |
* Options determining what to print from certificate |
| 42 |
* -all Whole certificate |
| 43 |
* -subject Subject string of the cert |
| 44 |
* -issuer Issuer |
| 45 |
* -startdate Validity of cert: start date |
| 46 |
* -enddate Validity of cert: end date |
| 47 |
* </pre> |
| 48 |
*/ |
| 49 |
public class CertInfo { |
| 50 |
|
| 51 |
private static final int SUBJECT = 2; |
| 52 |
private static final int ISSUER = 4; |
| 53 |
private static final int SDATE = 8; |
| 54 |
private static final int EDATE = 16; |
| 55 |
private static final int ALL = 32; |
| 56 |
|
| 57 |
private static String message = |
| 58 |
"\n" + |
| 59 |
"Syntax: java CertInfo [-help] [-file certfile] [-all] [-subject] [...]\n\n" + |
| 60 |
"\tDisplays certificate information. Unless the optional \n" + |
| 61 |
"\tfile argument is given, the default location of the file\n" + |
| 62 |
"\tcontaining the certficate is assumed:\n\n" + |
| 63 |
"\t -- " + CoGProperties.getDefault().getUserCertFile() + "\n\n" + |
| 64 |
"\tOptions\n" + |
| 65 |
"\t-help | -usage\n" + |
| 66 |
"\t\tDisplay usage.\n" + |
| 67 |
"\t-version\n" + |
| 68 |
"\t\tDisplay version.\n" + |
| 69 |
"\t-file certfile\n" + |
| 70 |
"\t\tUse 'certfile' at non-default location.\n" + |
| 71 |
"\t-globus\n" + |
| 72 |
"\t\tPrints information in globus format.\n\n" + |
| 73 |
"\tOptions determining what to print from certificate\n\n" + |
| 74 |
"\t-all\n" + |
| 75 |
"\t\tWhole certificate.\n" + |
| 76 |
"\t-subject\n" + |
| 77 |
"\t\tSubject string of the cert.\n" + |
| 78 |
"\t-issuer\n" + |
| 79 |
"\t\tIssuer.\n" + |
| 80 |
"\t-startdate\n" + |
| 81 |
"\t\tValidity of cert: start date.\n" + |
| 82 |
"\t-enddate\n" + |
| 83 |
"\t\tValidity of cert: end date.\n\n"; |
| 84 |
|
| 85 |
public static void main(String args[]) { |
| 86 |
|
| 87 |
String file = null; |
| 88 |
int options = 0; |
| 89 |
boolean error = false; |
| 90 |
boolean globusStyle = false; |
| 91 |
boolean debug = false; |
| 92 |
|
| 93 |
for (int i = 0; i < args.length; i++) { |
| 94 |
if (args[i].equalsIgnoreCase("-file")) { |
| 95 |
file = args[++i]; |
| 96 |
} else if (args[i].equalsIgnoreCase("-subject")) { |
| 97 |
options |= SUBJECT; |
| 98 |
} else if (args[i].equalsIgnoreCase("-issuer")) { |
| 99 |
options |= ISSUER; |
| 100 |
} else if (args[i].equalsIgnoreCase("-startdate")) { |
| 101 |
options |= SDATE; |
| 102 |
} else if (args[i].equalsIgnoreCase("-enddate")) { |
| 103 |
options |= EDATE; |
| 104 |
} else if (args[i].equalsIgnoreCase("-all")) { |
| 105 |
options |= ALL; |
| 106 |
} else if (args[i].equalsIgnoreCase("-globus")) { |
| 107 |
globusStyle = true; |
| 108 |
} else if (args[i].equalsIgnoreCase("-version")) { |
| 109 |
System.err.println(Version.getVersion()); |
| 110 |
System.exit(1); |
| 111 |
} else if (args[i].equalsIgnoreCase("-help") || |
| 112 |
args[i].equalsIgnoreCase("-usage")) { |
| 113 |
System.err.println(message); |
| 114 |
System.exit(1); |
| 115 |
} else { |
| 116 |
System.err.println("Error: argument not recognized : " + args[i]); |
| 117 |
error = true; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
if (error) { |
| 122 |
System.err.println("\nUsage: java CertInfo [-help] [-file certfile] [-all] [-subject] [...]\n"); |
| 123 |
System.err.println("Use -help to display full usage."); |
| 124 |
System.exit(1); |
| 125 |
} |
| 126 |
|
| 127 |
if (file == null) { |
| 128 |
file = CoGProperties.getDefault().getUserCertFile(); |
| 129 |
} |
| 130 |
|
| 131 |
X509Certificate cert = null; |
| 132 |
|
| 133 |
try { |
| 134 |
cert = CertificateLoadUtil.loadCertificate(file); |
| 135 |
} catch(Exception e) { |
| 136 |
System.err.println("Unable to load the certificate : " + e.getMessage()); |
| 137 |
System.exit(1); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
if (options == 0) { |
| 142 |
options = SUBJECT | ISSUER | SDATE | EDATE; |
| 143 |
} |
| 144 |
|
| 145 |
if ((options & SUBJECT) != 0) { |
| 146 |
String dn = null; |
| 147 |
if (globusStyle) { |
| 148 |
dn = CertificateUtil.toGlobusID(cert.getSubjectDN()); |
| 149 |
} else { |
| 150 |
dn = cert.getSubjectDN().getName(); |
| 151 |
} |
| 152 |
System.out.println("subject : " + dn); |
| 153 |
} |
| 154 |
|
| 155 |
if ((options & ISSUER) != 0) { |
| 156 |
String dn = null; |
| 157 |
if (globusStyle) { |
| 158 |
dn = CertificateUtil.toGlobusID(cert.getIssuerDN()); |
| 159 |
} else { |
| 160 |
dn = cert.getIssuerDN().getName(); |
| 161 |
} |
| 162 |
System.out.println("issuer : " + dn); |
| 163 |
} |
| 164 |
|
| 165 |
TimeZone tz = null; |
| 166 |
DateFormat df = null; |
| 167 |
if (globusStyle) { |
| 168 |
tz = TimeZone.getTimeZone("GMT"); |
| 169 |
df = new SimpleDateFormat("MMM dd HH:mm:ss yyyy z"); |
| 170 |
df.setTimeZone(tz); |
| 171 |
} |
| 172 |
|
| 173 |
if ((options & SDATE) != 0) { |
| 174 |
String dt = null; |
| 175 |
if (globusStyle) { |
| 176 |
dt = df.format(cert.getNotBefore()); |
| 177 |
} else { |
| 178 |
dt = cert.getNotBefore().toString(); |
| 179 |
} |
| 180 |
System.out.println("start date : " + dt); |
| 181 |
} |
| 182 |
|
| 183 |
if ((options & EDATE) != 0) { |
| 184 |
String dt = null; |
| 185 |
if (globusStyle) { |
| 186 |
dt = df.format(cert.getNotAfter()); |
| 187 |
} else { |
| 188 |
dt = cert.getNotAfter().toString(); |
| 189 |
} |
| 190 |
System.out.println("end date : " + dt); |
| 191 |
} |
| 192 |
|
| 193 |
if ((options & ALL) != 0) { |
| 194 |
System.out.println("certificate :"); |
| 195 |
System.out.println(cert.toString()); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
} |