📄 digitalidinfo.java
字号:
/*** $Id: DigitalIDInfo.java,v 1.5 2001/05/07 12:37:22 kunugi Exp $**** Copyright (c) 2000-2001 Jeff Gay** on behalf of ICEMail.org <http://www.icemail.org>** Copyright (c) 1998-2000 by Timothy Gerard Endres** ** This program is free software.** ** You may redistribute it and/or modify it under the terms of the GNU** General Public License as published by the Free Software Foundation.** Version 2 of the license should be included with this distribution in** the file LICENSE, as well as License.html. If the license is not** included with this distribution, you may find a copy at the FSF web** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.**** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR** REDISTRIBUTION OF THIS SOFTWARE. */package org.icemail.mail.smime;import java.io.*;import java.security.PrivateKey;import java.security.NoSuchAlgorithmException;import iaik.pkcs.PKCSException;import iaik.pkcs.pkcs12.NetscapeP12;import iaik.pkcs.pkcs12.MicrosoftP12;import iaik.x509.X509Certificate;import org.icemail.smime.MissingCertificateException;import org.icemail.smime.PasswordCancelException;import org.icemail.util.AWTUtilities;public class DigitalIDInfo{ boolean decrypted = false; String emailStr; String password; Object pkcs12Obj; public DigitalIDInfo( String emailStr, String password, Object o ) { this.emailStr = emailStr; this.password = password; this.pkcs12Obj = o; } public void setEMailString( String emailStr ) { this.emailStr = emailStr; } public boolean isDecrypted() { return this.decrypted; } public boolean isEncrypted() { return ! this.decrypted; } public String getEmailString() { return this.emailStr; } public String getPassword() { return this.password; } public String getType() { if ( pkcs12Obj instanceof MicrosoftP12 ) { return "P12"; } else if ( pkcs12Obj instanceof NetscapeP12 ) { return "PFX"; } return "UKN"; } public boolean isMicrosoftP12() { return (this.pkcs12Obj instanceof MicrosoftP12); } public boolean isNetscapeP12() { return (this.pkcs12Obj instanceof NetscapeP12); } public void encrypt() throws PKCSException, NoSuchAlgorithmException { if ( this.pkcs12Obj instanceof NetscapeP12 ) { ((NetscapeP12) this.pkcs12Obj).encrypt( this.password ); } else if ( this.pkcs12Obj instanceof MicrosoftP12 ) { ((MicrosoftP12) this.pkcs12Obj).encrypt( this.password ); } } public PrivateKey getPrivateKey() throws PKCSException, PasswordCancelException { PrivateKey result = null; if ( this.isNetscapeP12() ) { NetscapeP12 np12 = this.getNetscapeObject(); result = np12.getPrivateKey(); } else if ( this.isMicrosoftP12() ) { MicrosoftP12 mp12 = this.getMicrosoftObject(); result = mp12.getPrivateKey(); } return result; } public X509Certificate[] getCertificateChain() throws PKCSException, PasswordCancelException { X509Certificate[] result = null; if ( this.isNetscapeP12() ) { NetscapeP12 np12 = this.getNetscapeObject(); if ( np12 != null ) { result = np12.getCertificateChain(); } } else if ( this.isMicrosoftP12() ) { MicrosoftP12 mp12 = this.getMicrosoftObject(); if ( mp12 != null ) { result = mp12.getCertificateChain(); } } return result; } /** * If it returns null, the user canceled the password dialog. */ public NetscapeP12 getNetscapeObject() throws PKCSException, PasswordCancelException { NetscapeP12 result = (NetscapeP12) this.pkcs12Obj; if ( ! this.decrypted ) { String pass = this.password; if ( pass == null ) { PKCSPasswordDialog dlg = new PKCSPasswordDialog( null, this.emailStr, this.emailStr ); dlg.setLocation( AWTUtilities.computeDialogLocation( dlg ) ); dlg.show(); pass = dlg.getPassword(); if ( pass == null ) { throw new PasswordCancelException( this.emailStr ); } } ByteArrayOutputStream bout = new ByteArrayOutputStream(); try { result.writeTo( bout ); result.decrypt( pass ); this.decrypted = true; this.password = pass; } catch ( IOException ex ) { throw new PKCSException( "IOException: " + ex.getMessage() ); } catch ( PKCSException ex ) { ex.printStackTrace(); this.decrypted = false; this.password = null; ByteArrayInputStream bin = new ByteArrayInputStream( bout.toByteArray() ); try { this.pkcs12Obj = new NetscapeP12( bin ); } catch ( IOException ex2 ) { } // UNDONE - throw "DecryptFailureException"!!! // JOptionPane.showMessageDialog // ( null, // "Could not decrypt ID. Check the password.", // "Decrypt Failed", // JOptionPane.WARNING_MESSAGE ); } } return result; } /** * If it returns null, the user canceled the password dialog. */ public MicrosoftP12 getMicrosoftObject() throws PKCSException, PasswordCancelException { MicrosoftP12 result = (MicrosoftP12) this.pkcs12Obj; if ( ! this.decrypted ) { String pass = this.password; if ( pass == null ) { PKCSPasswordDialog dlg = new PKCSPasswordDialog( null, this.emailStr, this.emailStr ); dlg.setLocation( AWTUtilities.computeDialogLocation( dlg ) ); dlg.show(); pass = dlg.getPassword(); if ( pass == null ) { throw new PasswordCancelException( this.emailStr ); } } ByteArrayOutputStream bout = new ByteArrayOutputStream(); try { result.writeTo( bout ); result.decrypt( pass ); this.decrypted = true; this.password = pass; } catch ( IOException ex ) { throw new PKCSException( "IOException: " + ex.getMessage() ); } catch ( PKCSException ex ) { ex.printStackTrace(); this.decrypted = false; this.password = null; ByteArrayInputStream bin = new ByteArrayInputStream( bout.toByteArray() ); try { this.pkcs12Obj = new MicrosoftP12( bin ); } catch ( IOException ex2 ) { } // UNDONE - throw "DecryptFailureException"!!! } } return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -