001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017:
018: package org.apache.harmony.tools.jarsigner;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.net.Proxy;
023: import java.net.URI;
024: import java.net.URISyntaxException;
025: import java.security.KeyStore;
026: import java.util.jar.JarFile;
027: import java.util.logging.Handler;
028: import java.util.logging.Level;
029: import java.util.logging.Logger;
030:
031: import org.apache.harmony.tools.keytool.KeytoolParameters;
032: import org.apache.harmony.tools.toolutils.KeyStoreLoaderSaver;
033:
034: /**
035: * The class encapsulates paramaters for jarsigner most of which are usually
036: * given in command line.
037: */
038: class JSParameters {
039: /**
040: * Default location of the keystore. Used when the value is not supplied by
041: * the user.
042: */
043: public static final String defaultKeystorePath = System
044: .getProperty("user.home")
045: + File.separator + ".keystore";
046:
047: /**
048: * The name of the logger for JarSigner.
049: */
050: public static final String loggerName = "org.apache.harmony.tools.jarsigner.JarSignerLogger";
051:
052: // the keystore to work with
053: private KeyStore keyStore;
054:
055: // JAR file to work with
056: private JarFile jarFile;
057:
058: // JAR file URI or path
059: private String jarURIPath;
060:
061: // alias to access an entry in keystore
062: private String alias;
063:
064: // should the jar be verified (if it is false, JAR is to be signed)
065: private boolean isVerify;
066:
067: // URL of the keystore
068: private String storeURI;
069:
070: // type of the store. Default type is set in java.security file.
071: private String storeType = KeyStore.getDefaultType();
072:
073: // password to access the store
074: private char[] storePass;
075:
076: // password to access the key entry
077: private char[] keyPass;
078:
079: // file name to be used when generating .SF and .DSA files
080: private String sigFileName;
081:
082: // file name to be used for signed JAR
083: private String signedJARName;
084:
085: // if used with -verify and -verbose options, makes jarsigner print
086: // certificate info
087: private boolean isCerts;
088:
089: // should the program be "verbose" or not
090: private boolean isVerbose;
091:
092: // should the .DSA file contain .SF file in it or not
093: private boolean isInternalSF;
094:
095: // if set to true, .SF file will not contain hash of the whole manifest file
096: private boolean isSectionsOnly;
097:
098: // class name of the provider to use if specific provider is not given
099: private String provider;
100:
101: // name of the provider to use if specific provider is not given
102: private String providerName;
103:
104: // class name of the provider to work with certificates
105: private String certProvider;
106:
107: // name of the provider to work with certificates
108: private String certProviderName;
109:
110: // class name of the provider to work with signatures
111: private String sigProvider;
112:
113: // name of the provider to work with signatures
114: private String sigProviderName;
115:
116: // class name of the provider to work with keystore
117: private String ksProvider;
118:
119: // name of the provider to work with keystore
120: private String ksProviderName;
121:
122: // class name of the provider to work with message digests
123: private String mdProvider;
124:
125: // name of the provider to work with message digests
126: private String mdProviderName;
127:
128: // timestamp authority URL
129: private URI tsaURI;
130:
131: // the alias identifying the TSA's certificate
132: private String tsaCertAlias;
133:
134: // alternative signer class name
135: private String altSigner;
136:
137: // classpath to alternative signer package
138: private String altSignerPath;
139:
140: // topic to print help on
141: private String helpTopic;
142:
143: // true if signature file name is processed by FileNameGenerator
144: // false if the name that the user has set is unchanged.
145: private boolean isSFNameProcessed;
146:
147: // algorithm of the key used to sign data
148: private String keyAlg;
149:
150: // algorithm of the signature used
151: private String sigAlg;
152:
153: // should the JarSigner turn off logging or not
154: private boolean isSilent;
155:
156: // proxy server address
157: private String proxy;
158:
159: // proxy server port
160: private int proxyPort;
161:
162: // proxy server type
163: private Proxy.Type proxyType;
164:
165: // set the fields of the JSParameters object to default values
166: void setDefault() {
167: keyStore = null;
168: jarFile = null;
169: jarURIPath = null;
170: alias = null;
171: storeURI = null;
172: storeType = KeyStore.getDefaultType();
173: storePass = null;
174: keyPass = null;
175: sigFileName = null;
176: signedJARName = null;
177: isVerify = false;
178: isCerts = false;
179: isVerbose = false;
180: isInternalSF = false;
181: isSectionsOnly = false;
182: provider = null;
183: providerName = null;
184: certProvider = null;
185: certProviderName = null;
186: sigProvider = null;
187: sigProviderName = null;
188: ksProvider = null;
189: ksProviderName = null;
190: mdProvider = null;
191: mdProviderName = null;
192: tsaURI = null;
193: tsaCertAlias = null;
194: altSigner = null;
195: altSignerPath = null;
196: helpTopic = null;
197: isSFNameProcessed = false;
198: keyAlg = null;
199: sigAlg = null;
200: isSilent = false;
201: proxy = null;
202: proxyPort = 8888;
203: proxyType = Proxy.Type.HTTP;
204: }
205:
206: // Getters and setters down here
207: /**
208: * @param alias
209: */
210: public void setAlias(String alias) {
211: this .alias = alias;
212: }
213:
214: /**
215: * @param altSigner
216: */
217: public void setAltSigner(String altSigner) {
218: this .altSigner = altSigner;
219: }
220:
221: /**
222: * @param altSignerPath
223: */
224: public void setAltSignerPath(String altSignerPath) {
225: this .altSignerPath = altSignerPath;
226: }
227:
228: /**
229: * @param certProvider
230: */
231: public void setCertProvider(String certProvider) {
232: this .certProvider = certProvider;
233: }
234:
235: /**
236: * @param certProviderName
237: */
238: public void setCertProviderName(String certProviderName) {
239: this .certProviderName = certProviderName;
240: }
241:
242: /**
243: * @param helpTopic
244: */
245: public void setHelpTopic(String helpTopic) {
246: this .helpTopic = helpTopic;
247: }
248:
249: /**
250: * @param isCerts
251: */
252: public void setCerts(boolean isCerts) {
253: this .isCerts = isCerts;
254: }
255:
256: /**
257: * @param isInternalSF
258: */
259: public void setInternalSF(boolean isInternalSF) {
260: this .isInternalSF = isInternalSF;
261: }
262:
263: /**
264: * @param isSectionsOnly
265: */
266: public void setSectionsOnly(boolean isSectionsOnly) {
267: this .isSectionsOnly = isSectionsOnly;
268: }
269:
270: /**
271: * @param isVerbose
272: */
273: public void setVerbose(boolean isVerbose) {
274: if (!isSilent) {
275: Logger logger = Logger.getLogger(loggerName);
276: Handler[] handlers = logger.getHandlers();
277: for (Handler handler : handlers) {
278: if (isVerbose) {
279: logger.setLevel(Level.FINE);
280: handler.setLevel(Level.FINE);
281: } else {
282: logger.setLevel(Level.INFO);
283: handler.setLevel(Level.INFO);
284: }
285: }
286: }
287: this .isVerbose = isVerbose;
288: }
289:
290: /**
291: * @param isVerify
292: */
293: public void setVerify(boolean isVerify) {
294: this .isVerify = isVerify;
295: }
296:
297: /**
298: * @param jarFile
299: */
300: public void setJarFile(JarFile jarFile) {
301: this .jarFile = jarFile;
302: }
303:
304: /**
305: * @param jarURIPath
306: */
307: public void setJarURIorPath(String jarURIPath) {
308: this .jarURIPath = jarURIPath;
309: }
310:
311: /**
312: * @param keyAlg
313: */
314: void setKeyAlg(String keyAlg) {
315: this .keyAlg = keyAlg;
316: }
317:
318: /**
319: * @param keyPass
320: */
321: public void setKeyPass(char[] keyPass) {
322: this .keyPass = keyPass;
323: }
324:
325: /**
326: * @param keyStore
327: */
328: void setKeyStore(KeyStore keyStore) {
329: this .keyStore = keyStore;
330: }
331:
332: /**
333: * @param ksProvider
334: */
335: public void setKsProvider(String ksProvider) {
336: this .ksProvider = ksProvider;
337: }
338:
339: /**
340: * @param ksProviderName
341: */
342: public void setKsProviderName(String ksProviderName) {
343: this .ksProviderName = ksProviderName;
344: }
345:
346: /**
347: * @param mdProvider
348: */
349: public void setMdProvider(String mdProvider) {
350: this .mdProvider = mdProvider;
351: }
352:
353: /**
354: * @param mdProviderName
355: */
356: public void setMdProviderName(String mdProviderName) {
357: this .mdProviderName = mdProviderName;
358: }
359:
360: /**
361: * @param provider
362: */
363: public void setProvider(String provider) {
364: this .provider = provider;
365: }
366:
367: /**
368: * @param providerName
369: */
370: public void setProviderName(String providerName) {
371: this .providerName = providerName;
372: }
373:
374: /**
375: * @param proxy
376: */
377: public void setProxy(String proxy) {
378: this .proxy = proxy;
379: }
380:
381: /**
382: * @param proxyPort
383: */
384: public void setProxyPort(int proxyPort) {
385: this .proxyPort = proxyPort;
386: }
387:
388: /**
389: * @param proxyType
390: */
391: public void setProxyType(Proxy.Type proxyType) {
392: this .proxyType = proxyType;
393: }
394:
395: /**
396: * @param sigAlg
397: */
398: void setSigAlg(String sigAlg) {
399: this .sigAlg = sigAlg;
400: }
401:
402: /**
403: * @param sigFileName
404: */
405: public void setSigFileName(String sigFileName) {
406: this .sigFileName = sigFileName;
407: isSFNameProcessed = false;
408: }
409:
410: /**
411: * @param signedJARName
412: */
413: public void setSignedJARName(String signedJARName) {
414: this .signedJARName = signedJARName;
415: }
416:
417: /**
418: * @param sigProvider
419: */
420: public void setSigProvider(String sigProvider) {
421: this .sigProvider = sigProvider;
422: }
423:
424: /**
425: * @param sigProviderName
426: */
427: public void setSigProviderName(String sigProviderName) {
428: this .sigProviderName = sigProviderName;
429: }
430:
431: /**
432: * @param isSilent
433: */
434: public void setSilent(boolean isSilent) {
435: Logger logger = Logger.getLogger(loggerName);
436: Handler[] handlers = logger.getHandlers();
437: for (Handler handler : handlers) {
438: if (isSilent) {
439: logger.setLevel(Level.OFF);
440: } else {
441: if (isVerbose) {
442: logger.setLevel(Level.FINE);
443: handler.setLevel(Level.FINE);
444: } else {
445: logger.setLevel(Level.INFO);
446: handler.setLevel(Level.INFO);
447: }
448: }
449: }
450: this .isSilent = isSilent;
451: }
452:
453: /**
454: * @param storePass
455: */
456: public void setStorePass(char[] storePass) {
457: this .storePass = storePass;
458: }
459:
460: /**
461: * @param storeType
462: */
463: public void setStoreType(String storeType) {
464: this .storeType = storeType;
465: }
466:
467: /**
468: * @param storeURI
469: */
470: public void setStoreURI(String storeURI) {
471: this .storeURI = storeURI;
472: }
473:
474: /**
475: * @param tsaCertAlias
476: */
477: public void setTsaCertAlias(String tsaCertAlias) {
478: this .tsaCertAlias = tsaCertAlias;
479: }
480:
481: /**
482: * @param tsaURI
483: */
484: public void setTsaURI(URI tsaURI) {
485: this .tsaURI = tsaURI;
486: }
487:
488: /**
489: * @return
490: */
491: String getAlias() {
492: return alias;
493: }
494:
495: /**
496: * @return
497: */
498: String getAltSigner() {
499: return altSigner;
500: }
501:
502: /**
503: * @return
504: */
505: String getAltSignerPath() {
506: return altSignerPath;
507: }
508:
509: /**
510: * @return
511: */
512: String getCertProvider() {
513: return certProvider;
514: }
515:
516: /**
517: * @return
518: */
519: String getCertProviderName() {
520: return certProviderName;
521: }
522:
523: /**
524: * @return
525: */
526: String getHelpTopic() {
527: return helpTopic;
528: }
529:
530: /**
531: * @return
532: */
533: boolean isCerts() {
534: return isCerts;
535: }
536:
537: /**
538: * @return
539: */
540: boolean isInternalSF() {
541: return isInternalSF;
542: }
543:
544: /**
545: * @return
546: */
547: boolean isSectionsOnly() {
548: return isSectionsOnly;
549: }
550:
551: /**
552: * @return
553: */
554: boolean isSilent() {
555: return isSilent;
556: }
557:
558: /**
559: * @return
560: */
561: boolean isVerbose() {
562: return isVerbose;
563: }
564:
565: /**
566: * @return
567: */
568: boolean isVerify() {
569: return isVerify;
570: }
571:
572: /**
573: * @return
574: * @throws IOException
575: */
576: JarFile getJarFile() throws IOException {
577: if (jarFile == null) {
578: try {
579: File file;
580: try {
581: // try to open the file as if jarURIPath is an URI
582: URI jarURI = new URI(jarURIPath);
583: file = new File(jarURI);
584: } catch (URISyntaxException e) {
585: // try to open the file as if jarURIPath is a path
586: file = new File(jarURIPath);
587: } catch (IllegalArgumentException e) {
588: file = new File(jarURIPath);
589: }
590: jarFile = new JarFile(file, isVerify);
591: } catch (IOException e) {
592: throw (IOException) new IOException(
593: "Failed to load JAR file " + jarURIPath)
594: .initCause(e);
595: }
596: }
597: return jarFile;
598: }
599:
600: /**
601: * @return
602: */
603: String getJarURIorPath() {
604: return jarURIPath;
605: }
606:
607: /**
608: * @return
609: */
610: String getKeyAlg() {
611: return keyAlg;
612: }
613:
614: /**
615: * @return
616: */
617: char[] getKeyPass() {
618: return keyPass;
619: }
620:
621: /**
622: * @return
623: * @throws JarSignerException
624: */
625: KeyStore getKeyStore() throws JarSignerException {
626: if (keyStore == null) {
627: String ksProvName = (ksProviderName != null) ? ksProviderName
628: : providerName;
629: // If the path to the store is not specified, try to open
630: // the store using the default path.
631: if (storeURI == null) {
632: storeURI = KeytoolParameters.defaultKeystorePath;
633: }
634: try {
635: keyStore = KeyStoreLoaderSaver.loadStore(storeURI,
636: storeType, storePass, ksProvName);
637: } catch (Exception e) {
638: throw new JarSignerException(
639: "Cannot load the keystore " + storeURI, e);
640: }
641: }
642: return keyStore;
643: }
644:
645: /**
646: * @return
647: */
648: String getKsProvider() {
649: return ksProvider;
650: }
651:
652: /**
653: * @return
654: */
655: String getKsProviderName() {
656: return ksProviderName;
657: }
658:
659: /**
660: * @return
661: */
662: String getMdProvider() {
663: return mdProvider;
664: }
665:
666: /**
667: * @return
668: */
669: String getMdProviderName() {
670: return mdProviderName;
671: }
672:
673: /**
674: * @return
675: */
676: String getProvider() {
677: return provider;
678: }
679:
680: /**
681: * @return
682: */
683: String getProviderName() {
684: return providerName;
685: }
686:
687: /**
688: * @return
689: */
690: String getProxy() {
691: return proxy;
692: }
693:
694: /**
695: * @return
696: */
697: int getProxyPort() {
698: return proxyPort;
699: }
700:
701: /**
702: * @return
703: */
704: Proxy.Type getProxyType() {
705: return proxyType;
706: }
707:
708: /**
709: * @return
710: */
711: String getSigAlg() {
712: return sigAlg;
713: }
714:
715: /**
716: * @return
717: */
718: String getSigFileName() {
719: // If the file name is not processed by FileNameGenerator.
720: if (!isSFNameProcessed) {
721: sigFileName = FileNameGenerator.generateFileName(
722: sigFileName, alias);
723: isSFNameProcessed = true;
724: }
725: return sigFileName;
726: }
727:
728: /**
729: * @return
730: */
731: String getSignedJARName() {
732: return signedJARName;
733: }
734:
735: /**
736: * @return
737: */
738: String getSigProvider() {
739: return sigProvider;
740: }
741:
742: /**
743: * @return
744: */
745: String getSigProviderName() {
746: return sigProviderName;
747: }
748:
749: /**
750: * @return
751: */
752: char[] getStorePass() {
753: return storePass;
754: }
755:
756: /**
757: * @return
758: */
759: String getStoreType() {
760: if (storeType == null) {
761: storeType = KeyStore.getDefaultType();
762: }
763: return storeType;
764: }
765:
766: /**
767: * @return
768: */
769: String getStoreURI() {
770: return storeURI;
771: }
772:
773: /**
774: * @return
775: */
776: String getTsaCertAlias() {
777: return tsaCertAlias;
778: }
779:
780: /**
781: * @return
782: */
783: URI getTsaURI() {
784: return tsaURI;
785: }
786: }
|