use of org.mozilla.jss.asn1.NULL in project jss by dogtagpki.
the class EncryptedContentInfo method createPBE.
// /////////////////////////////////////////////////////////////////////
// Crypto shortcuts
// /////////////////////////////////////////////////////////////////////
/**
* Creates a new EncryptedContentInfo, where the data is encrypted
* with a password-based key.
*
* @param pbeAlg The algorithm for generating a symmetric key from
* a password, salt, and iteration count.
* @param password The password to use in generating the key.
* @param salt The salt to use in generating the key.
* @param iterationCount The number of hashing iterations to perform
* while generating the key.
* @param charToByteConverter The mechanism for converting the characters
* in the password into bytes. If null, the default mechanism
* will be used, which is UTF8.
* @param toBeEncrypted The bytes to be encrypted and stored in the
* EncryptedContentInfo. Before they are encrypted, they will be
* padded using PKCS padding.
*/
public static EncryptedContentInfo createPBE(PBEAlgorithm pbeAlg, Password password, byte[] salt, int iterationCount, KeyGenerator.CharToByteConverter charToByteConverter, byte[] toBeEncrypted) throws NotInitializedException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException, TokenException, CharConversionException {
try {
CryptoManager cman = CryptoManager.getInstance();
// generate key
CryptoToken token = cman.getInternalCryptoToken();
KeyGenerator kg = token.getKeyGenerator(pbeAlg);
PBEKeyGenParams pbekgParams = new PBEKeyGenParams(password, salt, iterationCount);
if (charToByteConverter != null) {
kg.setCharToByteConverter(charToByteConverter);
}
kg.initialize(pbekgParams);
SymmetricKey key = kg.generate();
// generate IV
EncryptionAlgorithm encAlg = pbeAlg.getEncryptionAlg();
AlgorithmParameterSpec params = null;
Class<?>[] paramClasses = encAlg.getParameterClasses();
for (int i = 0; i < paramClasses.length; i++) {
if (paramClasses[i].equals(javax.crypto.spec.IvParameterSpec.class)) {
params = new IVParameterSpec(kg.generatePBE_IV());
break;
} else if (paramClasses[i].equals(RC2ParameterSpec.class)) {
params = new RC2ParameterSpec(key.getStrength(), kg.generatePBE_IV());
break;
}
}
// perform encryption
Cipher cipher = token.getCipherContext(encAlg);
cipher.initEncrypt(key, params);
byte[] encrypted = cipher.doFinal(Cipher.pad(toBeEncrypted, encAlg.getBlockSize()));
// make encryption algorithm identifier
PBEParameter pbeParam = new PBEParameter(salt, iterationCount);
AlgorithmIdentifier encAlgID = new AlgorithmIdentifier(pbeAlg.toOID(), pbeParam);
// create EncryptedContentInfo
EncryptedContentInfo encCI = new EncryptedContentInfo(ContentInfo.DATA, encAlgID, new OCTET_STRING(encrypted));
return encCI;
} catch (IllegalBlockSizeException e) {
throw new RuntimeException("IllegalBlockSizeException in EncryptedContentInfo" + ".createPBE: " + e.getMessage(), e);
} catch (BadPaddingException e) {
throw new RuntimeException("BadPaddingException in EncryptedContentInfo" + ".createPBE: " + e.getMessage(), e);
}
}
use of org.mozilla.jss.asn1.NULL in project jss by dogtagpki.
the class EncryptedContentInfo method createPBE.
// /////////////////////////////////////////////////////////////////////
// Crypto shortcuts
// /////////////////////////////////////////////////////////////////////
/**
* Creates a new EncryptedContentInfo, where the data is encrypted
* with a password-based key.
*
* @param pbeAlg The algorithm for generating a symmetric key from
* a password, salt, and iteration count.
* @param password The password to use in generating the key.
* @param salt The salt to use in generating the key.
* @param iterationCount The number of hashing iterations to perform
* while generating the key.
* @param charToByteConverter The mechanism for converting the characters
* in the password into bytes. If null, the default mechanism
* will be used, which is UTF8.
* @param toBeEncrypted The bytes to be encrypted and stored in the
* EncryptedContentInfo. Before they are encrypted, they will be
* padded using PKCS padding.
*/
public static EncryptedContentInfo createPBE(PBEAlgorithm pbeAlg, Password password, byte[] salt, int iterationCount, KeyGenerator.CharToByteConverter charToByteConverter, byte[] toBeEncrypted) throws NotInitializedException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException, TokenException, CharConversionException {
try {
CryptoManager cman = CryptoManager.getInstance();
// generate key
CryptoToken token = cman.getInternalCryptoToken();
KeyGenerator kg = token.getKeyGenerator(pbeAlg);
PBEKeyGenParams pbekgParams = new PBEKeyGenParams(password, salt, iterationCount);
if (charToByteConverter != null) {
kg.setCharToByteConverter(charToByteConverter);
}
kg.initialize(pbekgParams);
SymmetricKey key = kg.generate();
// generate IV
EncryptionAlgorithm encAlg = pbeAlg.getEncryptionAlg();
AlgorithmParameterSpec params = null;
Class<?>[] paramClasses = encAlg.getParameterClasses();
for (int i = 0; i < paramClasses.length; i++) {
if (paramClasses[i].equals(IVParameterSpec.class)) {
params = new IVParameterSpec(kg.generatePBE_IV());
break;
}
}
// perform encryption
Cipher cipher = token.getCipherContext(encAlg);
cipher.initEncrypt(key, params);
byte[] encrypted = cipher.doFinal(Cipher.pad(toBeEncrypted, encAlg.getBlockSize()));
// make encryption algorithm identifier
PBEParameter pbeParam = new PBEParameter(salt, iterationCount);
AlgorithmIdentifier encAlgID = new AlgorithmIdentifier(pbeAlg.toOID(), pbeParam);
// create EncryptedContentInfo
EncryptedContentInfo encCI = new EncryptedContentInfo(ContentInfo.DATA, encAlgID, new OCTET_STRING(encrypted));
return encCI;
} catch (IllegalBlockSizeException e) {
throw new RuntimeException("IllegalBlockSizeException in EncryptedContentInfo" + ".createPBE: " + e.getMessage(), e);
} catch (BadPaddingException e) {
throw new RuntimeException("BadPaddingException in EncryptedContentInfo" + ".createPBE: " + e.getMessage(), e);
}
}
use of org.mozilla.jss.asn1.NULL in project jss by dogtagpki.
the class CertRepContent method encode.
@Override
public void encode(Tag implicitTag, OutputStream ostream) throws IOException {
SEQUENCE encoding = new SEQUENCE();
// create sequence of certificates
if (caPubs != null) {
SEQUENCE certs = new SEQUENCE();
for (int i = 0; i < caPubs.length; i++) {
certs.addElement(new ANY(SEQUENCE.TAG, caPubs[i]));
}
encoding.addElement(new Tag(1), certs);
}
encoding.addElement(response);
encoding.encode(implicitTag, ostream);
}
use of org.mozilla.jss.asn1.NULL in project jss by dogtagpki.
the class CertResponse method encode.
@Override
public void encode(Tag implicitTag, OutputStream ostream) throws IOException {
SEQUENCE seq = new SEQUENCE();
seq.addElement(certReqId);
seq.addElement(status);
if (certifiedKeyPair != null) {
seq.addElement(certifiedKeyPair);
}
seq.encode(implicitTag, ostream);
}
use of org.mozilla.jss.asn1.NULL in project jss by dogtagpki.
the class IssuingDistributionPoint method setFullName.
/**
* Sets the <code>fullName</code> of the <code>DistributionPointName</code>. It may be set to <code>null</code>.
* If it is set to a non-null value, <code>relativeName</code> will be
* set to <code>null</code>, because at most one of these two attributes
* can be specified at a time.
*
* @exception GeneralNamesException If an error occurs encoding the
* name.
*/
public void setFullName(GeneralNames fullName) throws GeneralNamesException, IOException {
this.fullName = fullName;
if (fullName != null) {
// encode the name to catch any problems with it
DerOutputStream derOut = new DerOutputStream();
fullName.encode(derOut);
try {
ANY raw = new ANY(derOut.toByteArray());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
raw.encodeWithAlternateTag(Tag.get(0), bos);
fullNameEncoding = new ANY(bos.toByteArray());
} catch (InvalidBERException e) {
// in DerOutputStream
throw new GeneralNamesException(e.toString());
}
this.relativeName = null;
}
}
Aggregations