Search in sources :

Example 26 with NULL

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);
    }
}
Also used : PBEParameter(org.mozilla.jss.pkix.primitive.PBEParameter) CryptoToken(org.mozilla.jss.crypto.CryptoToken) IVParameterSpec(org.mozilla.jss.crypto.IVParameterSpec) SymmetricKey(org.mozilla.jss.crypto.SymmetricKey) IllegalBlockSizeException(org.mozilla.jss.crypto.IllegalBlockSizeException) CryptoManager(org.mozilla.jss.CryptoManager) BadPaddingException(javax.crypto.BadPaddingException) AlgorithmIdentifier(org.mozilla.jss.pkix.primitive.AlgorithmIdentifier) PBEKeyGenParams(org.mozilla.jss.crypto.PBEKeyGenParams) OCTET_STRING(org.mozilla.jss.asn1.OCTET_STRING) EncryptionAlgorithm(org.mozilla.jss.crypto.EncryptionAlgorithm) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) Cipher(org.mozilla.jss.crypto.Cipher) KeyGenerator(org.mozilla.jss.crypto.KeyGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 27 with NULL

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);
    }
}
Also used : PBEParameter(org.mozilla.jss.pkix.primitive.PBEParameter) CryptoToken(org.mozilla.jss.crypto.CryptoToken) IVParameterSpec(org.mozilla.jss.crypto.IVParameterSpec) SymmetricKey(org.mozilla.jss.crypto.SymmetricKey) IllegalBlockSizeException(org.mozilla.jss.crypto.IllegalBlockSizeException) CryptoManager(org.mozilla.jss.CryptoManager) BadPaddingException(javax.crypto.BadPaddingException) AlgorithmIdentifier(org.mozilla.jss.pkix.primitive.AlgorithmIdentifier) PBEKeyGenParams(org.mozilla.jss.crypto.PBEKeyGenParams) OCTET_STRING(org.mozilla.jss.asn1.OCTET_STRING) EncryptionAlgorithm(org.mozilla.jss.crypto.EncryptionAlgorithm) Cipher(org.mozilla.jss.crypto.Cipher) KeyGenerator(org.mozilla.jss.crypto.KeyGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 28 with NULL

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);
}
Also used : SEQUENCE(org.mozilla.jss.asn1.SEQUENCE) Tag(org.mozilla.jss.asn1.Tag) ANY(org.mozilla.jss.asn1.ANY)

Example 29 with NULL

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);
}
Also used : SEQUENCE(org.mozilla.jss.asn1.SEQUENCE)

Example 30 with NULL

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;
    }
}
Also used : InvalidBERException(org.mozilla.jss.asn1.InvalidBERException) DerOutputStream(org.mozilla.jss.netscape.security.util.DerOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ANY(org.mozilla.jss.asn1.ANY)

Aggregations

SEQUENCE (org.mozilla.jss.asn1.SEQUENCE)33 OCTET_STRING (org.mozilla.jss.asn1.OCTET_STRING)19 InvalidBERException (org.mozilla.jss.asn1.InvalidBERException)17 ANY (org.mozilla.jss.asn1.ANY)14 CryptoToken (org.mozilla.jss.crypto.CryptoToken)14 AlgorithmIdentifier (org.mozilla.jss.pkix.primitive.AlgorithmIdentifier)11 IOException (java.io.IOException)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 ASN1Value (org.mozilla.jss.asn1.ASN1Value)10 BMPString (org.mozilla.jss.asn1.BMPString)10 CryptoManager (org.mozilla.jss.CryptoManager)9 SET (org.mozilla.jss.asn1.SET)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)8 OBJECT_IDENTIFIER (org.mozilla.jss.asn1.OBJECT_IDENTIFIER)8 EncryptionAlgorithm (org.mozilla.jss.crypto.EncryptionAlgorithm)8 FileOutputStream (java.io.FileOutputStream)7 Cipher (org.mozilla.jss.crypto.Cipher)7 CertificateException (java.security.cert.CertificateException)6 BadPaddingException (javax.crypto.BadPaddingException)6