Java Code Examples for java.security.spec.PSSParameterSpec#TRAILER_FIELD_BC

The following examples show how to use java.security.spec.PSSParameterSpec#TRAILER_FIELD_BC . 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: RSAPSSSignature.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validate the specified Signature PSS parameters.
 */
private PSSParameterSpec validateSigParams(AlgorithmParameterSpec p)
        throws InvalidAlgorithmParameterException {
    if (p == null) {
        throw new InvalidAlgorithmParameterException
            ("Parameters cannot be null");
    }
    if (!(p instanceof PSSParameterSpec)) {
        throw new InvalidAlgorithmParameterException
            ("parameters must be type PSSParameterSpec");
    }
    // no need to validate again if same as current signature parameters
    PSSParameterSpec params = (PSSParameterSpec) p;
    if (params == this.sigParams) return params;

    RSAKey key = (this.privKey == null? this.pubKey : this.privKey);
    // check against keyParams if set
    if (key != null) {
        if (!isCompatible(key.getParams(), params)) {
            throw new InvalidAlgorithmParameterException
                ("Signature parameters does not match key parameters");
        }
    }
    // now sanity check the parameter values
    if (!(params.getMGFAlgorithm().equalsIgnoreCase("MGF1"))) {
        throw new InvalidAlgorithmParameterException("Only supports MGF1");

    }
    if (params.getTrailerField() != PSSParameterSpec.TRAILER_FIELD_BC) {
        throw new InvalidAlgorithmParameterException
            ("Only supports TrailerFieldBC(1)");

    }
    String digestAlgo = params.getDigestAlgorithm();
    // check key length again
    if (key != null) {
        try {
            int hLen = DIGEST_LENGTHS.get(digestAlgo);
            checkKeyLength(key, hLen, params.getSaltLength());
        } catch (SignatureException e) {
            throw new InvalidAlgorithmParameterException(e);
        }
    }
    return params;
}
 
Example 2
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 3
Source File: CSignature.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validate the specified Signature PSS parameters.
 */
private PSSParameterSpec validateSigParams(AlgorithmParameterSpec p)
        throws InvalidAlgorithmParameterException {

    if (p == null) {
        throw new InvalidAlgorithmParameterException
                ("Parameters cannot be null");
    }

    if (!(p instanceof PSSParameterSpec)) {
        throw new InvalidAlgorithmParameterException
                ("parameters must be type PSSParameterSpec");
    }

    // no need to validate again if same as current signature parameters
    PSSParameterSpec params = (PSSParameterSpec) p;
    if (params == this.pssParams) return params;

    // now sanity check the parameter values
    if (!(params.getMGFAlgorithm().equalsIgnoreCase("MGF1"))) {
        throw new InvalidAlgorithmParameterException("Only supports MGF1");

    }

    if (params.getTrailerField() != PSSParameterSpec.TRAILER_FIELD_BC) {
        throw new InvalidAlgorithmParameterException
                ("Only supports TrailerFieldBC(1)");
    }

    AlgorithmParameterSpec algSpec = params.getMGFParameters();
    if (!(algSpec instanceof MGF1ParameterSpec)) {
        throw new InvalidAlgorithmParameterException
                ("Only support MGF1ParameterSpec");
    }

    MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec)algSpec;

    String msgHashAlg = params.getDigestAlgorithm()
            .toLowerCase(Locale.ROOT).replaceAll("-", "");
    if (msgHashAlg.equals("sha")) {
        msgHashAlg = "sha1";
    }
    String mgf1HashAlg = mgfSpec.getDigestAlgorithm()
            .toLowerCase(Locale.ROOT).replaceAll("-", "");
    if (mgf1HashAlg.equals("sha")) {
        mgf1HashAlg = "sha1";
    }

    if (!mgf1HashAlg.equals(msgHashAlg)) {
        throw new InvalidAlgorithmParameterException
                ("MGF1 hash must be the same as message hash");
    }

    return params;
}
 
Example 4
Source File: RSAPSSSignature.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Validate the specified Signature PSS parameters.
 */
private PSSParameterSpec validateSigParams(AlgorithmParameterSpec p)
        throws InvalidAlgorithmParameterException {
    if (p == null) {
        throw new InvalidAlgorithmParameterException
            ("Parameters cannot be null");
    }
    if (!(p instanceof PSSParameterSpec)) {
        throw new InvalidAlgorithmParameterException
            ("parameters must be type PSSParameterSpec");
    }
    // no need to validate again if same as current signature parameters
    PSSParameterSpec params = (PSSParameterSpec) p;
    if (params == this.sigParams) return params;

    RSAKey key = (this.privKey == null? this.pubKey : this.privKey);
    // check against keyParams if set
    if (key != null) {
        if (!isCompatible(key.getParams(), params)) {
            throw new InvalidAlgorithmParameterException
                ("Signature parameters does not match key parameters");
        }
    }
    // now sanity check the parameter values
    if (!(params.getMGFAlgorithm().equalsIgnoreCase("MGF1"))) {
        throw new InvalidAlgorithmParameterException("Only supports MGF1");

    }
    if (params.getTrailerField() != PSSParameterSpec.TRAILER_FIELD_BC) {
        throw new InvalidAlgorithmParameterException
            ("Only supports TrailerFieldBC(1)");

    }
    String digestAlgo = params.getDigestAlgorithm();
    // check key length again
    if (key != null) {
        try {
            int hLen = DIGEST_LENGTHS.get(digestAlgo);
            checkKeyLength(key, hLen, params.getSaltLength());
        } catch (SignatureException e) {
            throw new InvalidAlgorithmParameterException(e);
        }
    }
    return params;
}
 
Example 5
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 6
Source File: RSAPSSSignature.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validate the specified Signature PSS parameters.
 */
private PSSParameterSpec validateSigParams(AlgorithmParameterSpec p)
        throws InvalidAlgorithmParameterException {
    if (p == null) {
        throw new InvalidAlgorithmParameterException
            ("Parameters cannot be null");
    }
    if (!(p instanceof PSSParameterSpec)) {
        throw new InvalidAlgorithmParameterException
            ("parameters must be type PSSParameterSpec");
    }
    // no need to validate again if same as current signature parameters
    PSSParameterSpec params = (PSSParameterSpec) p;
    if (params == this.sigParams) return params;

    RSAKey key = (this.privKey == null? this.pubKey : this.privKey);
    // check against keyParams if set
    if (key != null) {
        if (!isCompatible(key.getParams(), params)) {
            throw new InvalidAlgorithmParameterException
                ("Signature parameters does not match key parameters");
        }
    }
    // now sanity check the parameter values
    if (!(params.getMGFAlgorithm().equalsIgnoreCase("MGF1"))) {
        throw new InvalidAlgorithmParameterException("Only supports MGF1");

    }
    if (params.getTrailerField() != PSSParameterSpec.TRAILER_FIELD_BC) {
        throw new InvalidAlgorithmParameterException
            ("Only supports TrailerFieldBC(1)");

    }
    String digestAlgo = params.getDigestAlgorithm();
    // check key length again
    if (key != null) {
        try {
            int hLen = DIGEST_LENGTHS.get(digestAlgo);
            checkKeyLength(key, hLen, params.getSaltLength());
        } catch (SignatureException e) {
            throw new InvalidAlgorithmParameterException(e);
        }
    }
    return params;
}
 
Example 7
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();
}
 
Example 8
Source File: CSignature.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Validate the specified Signature PSS parameters.
 */
private PSSParameterSpec validateSigParams(AlgorithmParameterSpec p)
        throws InvalidAlgorithmParameterException {

    if (p == null) {
        throw new InvalidAlgorithmParameterException
                ("Parameters cannot be null");
    }

    if (!(p instanceof PSSParameterSpec)) {
        throw new InvalidAlgorithmParameterException
                ("parameters must be type PSSParameterSpec");
    }

    // no need to validate again if same as current signature parameters
    PSSParameterSpec params = (PSSParameterSpec) p;
    if (params == this.pssParams) return params;

    // now sanity check the parameter values
    if (!(params.getMGFAlgorithm().equalsIgnoreCase("MGF1"))) {
        throw new InvalidAlgorithmParameterException("Only supports MGF1");

    }

    if (params.getTrailerField() != PSSParameterSpec.TRAILER_FIELD_BC) {
        throw new InvalidAlgorithmParameterException
                ("Only supports TrailerFieldBC(1)");
    }

    AlgorithmParameterSpec algSpec = params.getMGFParameters();
    if (!(algSpec instanceof MGF1ParameterSpec)) {
        throw new InvalidAlgorithmParameterException
                ("Only support MGF1ParameterSpec");
    }

    MGF1ParameterSpec mgfSpec = (MGF1ParameterSpec)algSpec;

    String msgHashAlg = params.getDigestAlgorithm()
            .toLowerCase(Locale.ROOT).replaceAll("-", "");
    if (msgHashAlg.equals("sha")) {
        msgHashAlg = "sha1";
    }
    String mgf1HashAlg = mgfSpec.getDigestAlgorithm()
            .toLowerCase(Locale.ROOT).replaceAll("-", "");
    if (mgf1HashAlg.equals("sha")) {
        mgf1HashAlg = "sha1";
    }

    if (!mgf1HashAlg.equals(msgHashAlg)) {
        throw new InvalidAlgorithmParameterException
                ("MGF1 hash must be the same as message hash");
    }

    return params;
}