Java Code Examples for java.security.spec.PSSParameterSpec#getSaltLength()

The following examples show how to use java.security.spec.PSSParameterSpec#getSaltLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AlgorithmParametersSpi.java    From RipplePower with Apache License 2.0 6 votes vote down vote up
/**
 * Return the PKCS#1 ASN.1 structure RSASSA-PSS-params.
 */
protected byte[] engineGetEncoded() 
    throws IOException
{
    PSSParameterSpec pssSpec = currentSpec;
    AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier(
                                        DigestFactory.getOID(pssSpec.getDigestAlgorithm()),
                                        DERNull.INSTANCE);
    MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec)pssSpec.getMGFParameters();
    AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier(
                                        PKCSObjectIdentifiers.id_mgf1,
                                        new AlgorithmIdentifier(DigestFactory.getOID(mgfSpec.getDigestAlgorithm()), DERNull.INSTANCE));
    RSASSAPSSparams pssP = new RSASSAPSSparams(hashAlgorithm, maskGenAlgorithm, new ASN1Integer(pssSpec.getSaltLength()), new ASN1Integer(pssSpec.getTrailerField()));
    
    return pssP.getEncoded("DER");
}
 
Example 2
Source File: AlgorithmParametersSpi.java    From ripple-lib-java with ISC License 6 votes vote down vote up
/**
 * Return the PKCS#1 ASN.1 structure RSASSA-PSS-params.
 */
protected byte[] engineGetEncoded() 
    throws IOException
{
    PSSParameterSpec pssSpec = currentSpec;
    AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier(
                                        DigestFactory.getOID(pssSpec.getDigestAlgorithm()),
                                        DERNull.INSTANCE);
    MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec)pssSpec.getMGFParameters();
    AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier(
                                        PKCSObjectIdentifiers.id_mgf1,
                                        new AlgorithmIdentifier(DigestFactory.getOID(mgfSpec.getDigestAlgorithm()), DERNull.INSTANCE));
    RSASSAPSSparams pssP = new RSASSAPSSparams(hashAlgorithm, maskGenAlgorithm, new ASN1Integer(pssSpec.getSaltLength()), new ASN1Integer(pssSpec.getTrailerField()));
    
    return pssP.getEncoded("DER");
}
 
Example 3
Source File: RSAPSSSignature.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Utility method for checking the key PSS parameters against signature
 * PSS parameters.
 * Returns false if any of the digest/MGF algorithms and trailerField
 * values does not match or if the salt length in key parameters is
 * larger than the value in signature parameters.
 */
private static boolean isCompatible(AlgorithmParameterSpec keyParams,
        PSSParameterSpec sigParams) {
    if (keyParams == null) {
        // key with null PSS parameters means no restriction
        return true;
    }
    if (!(keyParams instanceof PSSParameterSpec)) {
        return false;
    }
    // nothing to compare yet, defer the check to when sigParams is set
    if (sigParams == null) {
        return true;
    }
    PSSParameterSpec pssKeyParams = (PSSParameterSpec) keyParams;
    // first check the salt length requirement
    if (pssKeyParams.getSaltLength() > sigParams.getSaltLength()) {
        return false;
    }

    // compare equality of the rest of fields based on DER encoding
    PSSParameterSpec keyParams2 =
        new PSSParameterSpec(pssKeyParams.getDigestAlgorithm(),
                pssKeyParams.getMGFAlgorithm(),
                pssKeyParams.getMGFParameters(),
                sigParams.getSaltLength(),
                pssKeyParams.getTrailerField());
    PSSParameters ap = new PSSParameters();
    // skip the JCA overhead
    try {
        ap.engineInit(keyParams2);
        byte[] encoded = ap.engineGetEncoded();
        ap.engineInit(sigParams);
        byte[] encoded2 = ap.engineGetEncoded();
        return Arrays.equals(encoded, encoded2);
    } catch (Exception e) {
        if (DEBUG) {
            e.printStackTrace();
        }
        return false;
    }
}
 
Example 4
Source File: PSSParameters.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns the encoding of a {@link PSSParameterSpec} object. This method
 * is used in this class and {@link AlgorithmId}.
 *
 * @param spec a {@code PSSParameterSpec} object
 * @return its DER encoding
 * @throws IOException if the name of a MessageDigest or MaskGenAlgorithm
 *          is unsupported
 */
public static byte[] getEncoded(PSSParameterSpec spec) throws IOException {

    AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();
    if (!(mgfSpec instanceof MGF1ParameterSpec)) {
        throw new IOException("Cannot encode " + mgfSpec);
    }

    MGF1ParameterSpec mgf1Spec = (MGF1ParameterSpec)mgfSpec;

    DerOutputStream tmp = new DerOutputStream();
    DerOutputStream tmp2, tmp3;

    // MD
    AlgorithmId mdAlgId;
    try {
        mdAlgId = AlgorithmId.get(spec.getDigestAlgorithm());
    } catch (NoSuchAlgorithmException nsae) {
        throw new IOException("AlgorithmId " + spec.getDigestAlgorithm() +
                " impl not found");
    }
    if (!mdAlgId.getOID().equals(AlgorithmId.SHA_oid)) {
        tmp2 = new DerOutputStream();
        mdAlgId.derEncode(tmp2);
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0),
                tmp2);
    }

    // MGF
    AlgorithmId mgfDigestId;
    try {
        mgfDigestId = AlgorithmId.get(mgf1Spec.getDigestAlgorithm());
    } catch (NoSuchAlgorithmException nase) {
        throw new IOException("AlgorithmId " +
                mgf1Spec.getDigestAlgorithm() + " impl not found");
    }

    if (!mgfDigestId.getOID().equals(AlgorithmId.SHA_oid)) {
        tmp2 = new DerOutputStream();
        tmp2.putOID(AlgorithmId.mgf1_oid);
        mgfDigestId.encode(tmp2);
        tmp3 = new DerOutputStream();
        tmp3.write(DerValue.tag_Sequence, tmp2);
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 1),
                tmp3);
    }

    // SaltLength
    if (spec.getSaltLength() != 20) {
        tmp2 = new DerOutputStream();
        tmp2.putInteger(spec.getSaltLength());
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 2),
                tmp2);
    }

    // TrailerField
    if (spec.getTrailerField() != PSSParameterSpec.TRAILER_FIELD_BC) {
        tmp2 = new DerOutputStream();
        tmp2.putInteger(spec.getTrailerField());
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 3),
                tmp2);
    }

    // Put all together under a SEQUENCE tag
    DerOutputStream out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, tmp);
    return out.toByteArray();
}
 
Example 5
Source File: RSAPSSSignature.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Utility method for checking the key PSS parameters against signature
 * PSS parameters.
 * Returns false if any of the digest/MGF algorithms and trailerField
 * values does not match or if the salt length in key parameters is
 * larger than the value in signature parameters.
 */
private static boolean isCompatible(AlgorithmParameterSpec keyParams,
        PSSParameterSpec sigParams) {
    if (keyParams == null) {
        // key with null PSS parameters means no restriction
        return true;
    }
    if (!(keyParams instanceof PSSParameterSpec)) {
        return false;
    }
    // nothing to compare yet, defer the check to when sigParams is set
    if (sigParams == null) {
        return true;
    }
    PSSParameterSpec pssKeyParams = (PSSParameterSpec) keyParams;
    // first check the salt length requirement
    if (pssKeyParams.getSaltLength() > sigParams.getSaltLength()) {
        return false;
    }

    // compare equality of the rest of fields based on DER encoding
    PSSParameterSpec keyParams2 =
        new PSSParameterSpec(pssKeyParams.getDigestAlgorithm(),
                pssKeyParams.getMGFAlgorithm(),
                pssKeyParams.getMGFParameters(),
                sigParams.getSaltLength(),
                pssKeyParams.getTrailerField());
    PSSParameters ap = new PSSParameters();
    // skip the JCA overhead
    try {
        ap.engineInit(keyParams2);
        byte[] encoded = ap.engineGetEncoded();
        ap.engineInit(sigParams);
        byte[] encoded2 = ap.engineGetEncoded();
        return Arrays.equals(encoded, encoded2);
    } catch (Exception e) {
        if (DEBUG) {
            e.printStackTrace();
        }
        return false;
    }
}
 
Example 6
Source File: PSSParameters.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the encoding of a {@link PSSParameterSpec} object. This method
 * is used in this class and {@link AlgorithmId}.
 *
 * @param spec a {@code PSSParameterSpec} object
 * @return its DER encoding
 * @throws IOException if the name of a MessageDigest or MaskGenAlgorithm
 *          is unsupported
 */
public static byte[] getEncoded(PSSParameterSpec spec) throws IOException {

    AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();
    if (!(mgfSpec instanceof MGF1ParameterSpec)) {
        throw new IOException("Cannot encode " + mgfSpec);
    }

    MGF1ParameterSpec mgf1Spec = (MGF1ParameterSpec)mgfSpec;

    DerOutputStream tmp = new DerOutputStream();
    DerOutputStream tmp2, tmp3;

    // MD
    AlgorithmId mdAlgId;
    try {
        mdAlgId = AlgorithmId.get(spec.getDigestAlgorithm());
    } catch (NoSuchAlgorithmException nsae) {
        throw new IOException("AlgorithmId " + spec.getDigestAlgorithm() +
                " impl not found");
    }
    if (!mdAlgId.getOID().equals(AlgorithmId.SHA_oid)) {
        tmp2 = new DerOutputStream();
        mdAlgId.derEncode(tmp2);
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0),
                tmp2);
    }

    // MGF
    AlgorithmId mgfDigestId;
    try {
        mgfDigestId = AlgorithmId.get(mgf1Spec.getDigestAlgorithm());
    } catch (NoSuchAlgorithmException nase) {
        throw new IOException("AlgorithmId " +
                mgf1Spec.getDigestAlgorithm() + " impl not found");
    }

    if (!mgfDigestId.getOID().equals(AlgorithmId.SHA_oid)) {
        tmp2 = new DerOutputStream();
        tmp2.putOID(AlgorithmId.mgf1_oid);
        mgfDigestId.encode(tmp2);
        tmp3 = new DerOutputStream();
        tmp3.write(DerValue.tag_Sequence, tmp2);
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 1),
                tmp3);
    }

    // SaltLength
    if (spec.getSaltLength() != 20) {
        tmp2 = new DerOutputStream();
        tmp2.putInteger(spec.getSaltLength());
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 2),
                tmp2);
    }

    // TrailerField
    if (spec.getTrailerField() != PSSParameterSpec.TRAILER_FIELD_BC) {
        tmp2 = new DerOutputStream();
        tmp2.putInteger(spec.getTrailerField());
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 3),
                tmp2);
    }

    // Put all together under a SEQUENCE tag
    DerOutputStream out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, tmp);
    return out.toByteArray();
}
 
Example 7
Source File: RSAPSSSignature.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Utility method for checking the key PSS parameters against signature
 * PSS parameters.
 * Returns false if any of the digest/MGF algorithms and trailerField
 * values does not match or if the salt length in key parameters is
 * larger than the value in signature parameters.
 */
private static boolean isCompatible(AlgorithmParameterSpec keyParams,
        PSSParameterSpec sigParams) {
    if (keyParams == null) {
        // key with null PSS parameters means no restriction
        return true;
    }
    if (!(keyParams instanceof PSSParameterSpec)) {
        return false;
    }
    // nothing to compare yet, defer the check to when sigParams is set
    if (sigParams == null) {
        return true;
    }
    PSSParameterSpec pssKeyParams = (PSSParameterSpec) keyParams;
    // first check the salt length requirement
    if (pssKeyParams.getSaltLength() > sigParams.getSaltLength()) {
        return false;
    }

    // compare equality of the rest of fields based on DER encoding
    PSSParameterSpec keyParams2 =
        new PSSParameterSpec(pssKeyParams.getDigestAlgorithm(),
                pssKeyParams.getMGFAlgorithm(),
                pssKeyParams.getMGFParameters(),
                sigParams.getSaltLength(),
                pssKeyParams.getTrailerField());
    PSSParameters ap = new PSSParameters();
    // skip the JCA overhead
    try {
        ap.engineInit(keyParams2);
        byte[] encoded = ap.engineGetEncoded();
        ap.engineInit(sigParams);
        byte[] encoded2 = ap.engineGetEncoded();
        return Arrays.equals(encoded, encoded2);
    } catch (Exception e) {
        if (DEBUG) {
            e.printStackTrace();
        }
        return false;
    }
}
 
Example 8
Source File: PSSParameters.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns the encoding of a {@link PSSParameterSpec} object. This method
 * is used in this class and {@link AlgorithmId}.
 *
 * @param spec a {@code PSSParameterSpec} object
 * @return its DER encoding
 * @throws IOException if the name of a MessageDigest or MaskGenAlgorithm
 *          is unsupported
 */
public static byte[] getEncoded(PSSParameterSpec spec) throws IOException {

    AlgorithmParameterSpec mgfSpec = spec.getMGFParameters();
    if (!(mgfSpec instanceof MGF1ParameterSpec)) {
        throw new IOException("Cannot encode " + mgfSpec);
    }

    MGF1ParameterSpec mgf1Spec = (MGF1ParameterSpec)mgfSpec;

    DerOutputStream tmp = new DerOutputStream();
    DerOutputStream tmp2, tmp3;

    // MD
    AlgorithmId mdAlgId;
    try {
        mdAlgId = AlgorithmId.get(spec.getDigestAlgorithm());
    } catch (NoSuchAlgorithmException nsae) {
        throw new IOException("AlgorithmId " + spec.getDigestAlgorithm() +
                " impl not found");
    }
    if (!mdAlgId.getOID().equals(AlgorithmId.SHA_oid)) {
        tmp2 = new DerOutputStream();
        mdAlgId.derEncode(tmp2);
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0),
                tmp2);
    }

    // MGF
    AlgorithmId mgfDigestId;
    try {
        mgfDigestId = AlgorithmId.get(mgf1Spec.getDigestAlgorithm());
    } catch (NoSuchAlgorithmException nase) {
        throw new IOException("AlgorithmId " +
                mgf1Spec.getDigestAlgorithm() + " impl not found");
    }

    if (!mgfDigestId.getOID().equals(AlgorithmId.SHA_oid)) {
        tmp2 = new DerOutputStream();
        tmp2.putOID(AlgorithmId.mgf1_oid);
        mgfDigestId.encode(tmp2);
        tmp3 = new DerOutputStream();
        tmp3.write(DerValue.tag_Sequence, tmp2);
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 1),
                tmp3);
    }

    // SaltLength
    if (spec.getSaltLength() != 20) {
        tmp2 = new DerOutputStream();
        tmp2.putInteger(spec.getSaltLength());
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 2),
                tmp2);
    }

    // TrailerField
    if (spec.getTrailerField() != PSSParameterSpec.TRAILER_FIELD_BC) {
        tmp2 = new DerOutputStream();
        tmp2.putInteger(spec.getTrailerField());
        tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 3),
                tmp2);
    }

    // Put all together under a SEQUENCE tag
    DerOutputStream out = new DerOutputStream();
    out.write(DerValue.tag_Sequence, tmp);
    return out.toByteArray();
}