Search in sources :

Example 16 with OBJECT_IDENTIFIER

use of org.mozilla.jss.asn1.OBJECT_IDENTIFIER in project jss by dogtagpki.

the class SignerInfo method verifyWithSignedAttributes.

/**
 * Verifies a SignerInfo with signed attributes.  If signed
 * attributes are present, then two particular attributes must
 * be present: <ul>
 * <li>PKCS #9 Content-Type, the type of content that is being signed.
 *      This must match the contentType parameter.
 * <li>PKCS #9 Message-Digest, the digest of the content that is being
 *      signed. This must match the messageDigest parameter.
 * </ul>
 * After these two attributes are verified to be both present and correct,
 * the encryptedDigest field of the SignerInfo is verified to be the
 * signature of the contents octets of the DER encoding of the
 * signedAttributes field.
 */
private void verifyWithSignedAttributes(byte[] messageDigest, OBJECT_IDENTIFIER contentType, PublicKey pubkey) throws NotInitializedException, NoSuchAlgorithmException, InvalidKeyException, TokenException, SignatureException {
    int numAttrib = signedAttributes.size();
    if (numAttrib < 2) {
        throw new SignatureException("At least two signed attributes must be present:" + " content-type and message-digest");
    }
    // go through the signed attributes, verifying the
    // interesting ones
    boolean foundContentType = false;
    boolean foundMessageDigest = false;
    for (int i = 0; i < numAttrib; i++) {
        if (!(signedAttributes.elementAt(i) instanceof Attribute)) {
            throw new SignatureException("Element of signedAttributes is not an Attribute");
        }
        Attribute attrib = (Attribute) signedAttributes.elementAt(i);
        if (attrib.getType().equals(CONTENT_TYPE)) {
            // content-type.  Compare with what was passed in.
            SET vals = attrib.getValues();
            if (vals.size() != 1) {
                throw new SignatureException("Content-Type attribute " + " does not have exactly one value");
            }
            ASN1Value val = vals.elementAt(0);
            OBJECT_IDENTIFIER ctype;
            try {
                if (val instanceof OBJECT_IDENTIFIER) {
                    ctype = (OBJECT_IDENTIFIER) val;
                } else if (val instanceof ANY) {
                    ctype = (OBJECT_IDENTIFIER) ((ANY) val).decodeWith(OBJECT_IDENTIFIER.getTemplate());
                } else {
                    // what the heck is it? not what it's supposed to be
                    throw new InvalidBERException("Content-Type signed attribute has unexpected" + " content type");
                }
            } catch (InvalidBERException e) {
                throw new SignatureException("Content-Type signed attribute does not have " + "OBJECT IDENTIFIER value");
            }
            // contentType parameter
            if (!ctype.equals(contentType)) {
                throw new SignatureException("Content-type in signed attributes does not " + "match content-type being verified");
            }
            // content type is A-OK
            foundContentType = true;
        } else if (attrib.getType().equals(MESSAGE_DIGEST)) {
            SET vals = attrib.getValues();
            if (vals.size() != 1) {
                throw new SignatureException("Message-digest attribute does not have" + " exactly one value");
            }
            ASN1Value val = vals.elementAt(0);
            byte[] mdigest;
            try {
                if (val instanceof OCTET_STRING) {
                    mdigest = ((OCTET_STRING) val).toByteArray();
                } else if (val instanceof ANY) {
                    OCTET_STRING os;
                    os = (OCTET_STRING) ((ANY) val).decodeWith(OCTET_STRING.getTemplate());
                    mdigest = os.toByteArray();
                } else {
                    // what the heck is it? not what it's supposed to be
                    throw new InvalidBERException("Content-Type signed attribute has unexpected" + " content type");
                }
            } catch (InvalidBERException e) {
                throw new SignatureException("Message-digest attribute does not" + " have OCTET STRING value");
            }
            // message digest being verified
            if (!byteArraysAreSame(mdigest, messageDigest)) {
                throw new SignatureException("Message-digest attribute does not" + " match message digest being verified");
            }
            // message digest is A-OK
            foundMessageDigest = true;
        }
    // we don't care about other attributes
    }
    if (!foundContentType) {
        throw new SignatureException("Signed attributes does not contain" + " PKCS #9 content-type attribute");
    }
    if (!foundMessageDigest) {
        throw new SignatureException("Signed attributes does not contain" + " PKCS #9 message-digest attribute");
    }
    SignatureAlgorithm sigAlg = SignatureAlgorithm.fromOID(digestEncryptionAlgorithm.getOID());
    // All the signed attributes are present and correct.
    // Now verify the signature.
    CryptoToken token = CryptoManager.getInstance().getInternalCryptoToken();
    Signature sig;
    // verify the contents octets of the DER encoded signed attribs
    byte[] encoding = ASN1Util.encode(signedAttributes);
    byte[] toBeVerified;
    if (sigAlg.getRawAlg() == SignatureAlgorithm.RSASignature) {
        // create DigestInfo structure
        SEQUENCE digestInfo = createDigestInfo(encoding, true);
        toBeVerified = ASN1Util.encode(digestInfo);
        sig = token.getSignatureContext(SignatureAlgorithm.RSASignature);
    } else {
        toBeVerified = encoding;
        sig = token.getSignatureContext(sigAlg);
    }
    sig.initVerify(pubkey);
    sig.update(toBeVerified);
    if (!sig.verify(encryptedDigest.toByteArray())) {
        // signature is invalid
        throw new SignatureException("encryptedDigest was not the correct" + " signature of the contents octets of the DER-encoded" + " signed attributes");
    }
// SUCCESSFULLY VERIFIED
}
Also used : SET(org.mozilla.jss.asn1.SET) CryptoToken(org.mozilla.jss.crypto.CryptoToken) Attribute(org.mozilla.jss.pkix.primitive.Attribute) OBJECT_IDENTIFIER(org.mozilla.jss.asn1.OBJECT_IDENTIFIER) SignatureAlgorithm(org.mozilla.jss.crypto.SignatureAlgorithm) SignatureException(java.security.SignatureException) ANY(org.mozilla.jss.asn1.ANY) InvalidBERException(org.mozilla.jss.asn1.InvalidBERException) ASN1Value(org.mozilla.jss.asn1.ASN1Value) OCTET_STRING(org.mozilla.jss.asn1.OCTET_STRING) Signature(org.mozilla.jss.crypto.Signature) SEQUENCE(org.mozilla.jss.asn1.SEQUENCE)

Aggregations

OBJECT_IDENTIFIER (org.mozilla.jss.asn1.OBJECT_IDENTIFIER)14 SEQUENCE (org.mozilla.jss.asn1.SEQUENCE)10 OCTET_STRING (org.mozilla.jss.asn1.OCTET_STRING)8 ByteArrayInputStream (java.io.ByteArrayInputStream)4 SignatureException (java.security.SignatureException)4 ANY (org.mozilla.jss.asn1.ANY)4 SET (org.mozilla.jss.asn1.SET)4 CryptoToken (org.mozilla.jss.crypto.CryptoToken)4 Signature (org.mozilla.jss.crypto.Signature)4 SignatureAlgorithm (org.mozilla.jss.crypto.SignatureAlgorithm)4 ASN1Value (org.mozilla.jss.asn1.ASN1Value)3 InvalidBERException (org.mozilla.jss.asn1.InvalidBERException)3 Extension (org.mozilla.jss.pkix.cert.Extension)3 AlgorithmIdentifier (org.mozilla.jss.pkix.primitive.AlgorithmIdentifier)3 Attribute (org.mozilla.jss.pkix.primitive.Attribute)3 BMPString (org.mozilla.jss.asn1.BMPString)2 BOOLEAN (org.mozilla.jss.asn1.BOOLEAN)2 PrintableString (org.mozilla.jss.asn1.PrintableString)2 AuthenticatedSafes (org.mozilla.jss.pkcs12.AuthenticatedSafes)2 SafeBag (org.mozilla.jss.pkcs12.SafeBag)2