001: package org.bouncycastle.jce.provider;
002:
003: import javax.crypto.interfaces.PBEKey;
004: import javax.crypto.spec.PBEKeySpec;
005:
006: import org.bouncycastle.asn1.DERObjectIdentifier;
007: import org.bouncycastle.crypto.CipherParameters;
008: import org.bouncycastle.crypto.PBEParametersGenerator;
009: import org.bouncycastle.crypto.params.KeyParameter;
010: import org.bouncycastle.crypto.params.ParametersWithIV;
011:
012: public class JCEPBEKey implements PBEKey {
013: String algorithm;
014: DERObjectIdentifier oid;
015: int type;
016: int digest;
017: int keySize;
018: int ivSize;
019: CipherParameters param;
020: PBEKeySpec pbeKeySpec;
021: boolean tryWrong = false;
022:
023: /**
024: * @param param
025: */
026: public JCEPBEKey(String algorithm, DERObjectIdentifier oid,
027: int type, int digest, int keySize, int ivSize,
028: PBEKeySpec pbeKeySpec, CipherParameters param) {
029: this .algorithm = algorithm;
030: this .oid = oid;
031: this .type = type;
032: this .digest = digest;
033: this .keySize = keySize;
034: this .ivSize = ivSize;
035: this .pbeKeySpec = pbeKeySpec;
036: this .param = param;
037: }
038:
039: public String getAlgorithm() {
040: return algorithm;
041: }
042:
043: public String getFormat() {
044: return "RAW";
045: }
046:
047: public byte[] getEncoded() {
048: if (param != null) {
049: KeyParameter kParam;
050:
051: if (param instanceof ParametersWithIV) {
052: kParam = (KeyParameter) ((ParametersWithIV) param)
053: .getParameters();
054: } else {
055: kParam = (KeyParameter) param;
056: }
057:
058: return kParam.getKey();
059: } else {
060: if (type == PBE.PKCS12) {
061: return PBEParametersGenerator
062: .PKCS12PasswordToBytes(pbeKeySpec.getPassword());
063: } else {
064: return PBEParametersGenerator
065: .PKCS5PasswordToBytes(pbeKeySpec.getPassword());
066: }
067: }
068: }
069:
070: int getType() {
071: return type;
072: }
073:
074: int getDigest() {
075: return digest;
076: }
077:
078: int getKeySize() {
079: return keySize;
080: }
081:
082: int getIvSize() {
083: return ivSize;
084: }
085:
086: CipherParameters getParam() {
087: return param;
088: }
089:
090: /* (non-Javadoc)
091: * @see javax.crypto.interfaces.PBEKey#getPassword()
092: */
093: public char[] getPassword() {
094: return pbeKeySpec.getPassword();
095: }
096:
097: /* (non-Javadoc)
098: * @see javax.crypto.interfaces.PBEKey#getSalt()
099: */
100: public byte[] getSalt() {
101: return pbeKeySpec.getSalt();
102: }
103:
104: /* (non-Javadoc)
105: * @see javax.crypto.interfaces.PBEKey#getIterationCount()
106: */
107: public int getIterationCount() {
108: return pbeKeySpec.getIterationCount();
109: }
110:
111: public DERObjectIdentifier getOID() {
112: return oid;
113: }
114:
115: void setTryWrongPKCS12Zero(boolean tryWrong) {
116: this .tryWrong = tryWrong;
117: }
118:
119: boolean shouldTryWrongPKCS12() {
120: return tryWrong;
121: }
122: }
|