Example usage for org.bouncycastle.openpgp PGPSignatureSubpacketVector getKeyFlags

List of usage examples for org.bouncycastle.openpgp PGPSignatureSubpacketVector getKeyFlags

Introduction

In this page you can find the example usage for org.bouncycastle.openpgp PGPSignatureSubpacketVector getKeyFlags.

Prototype

public int getKeyFlags() 

Source Link

Usage

From source file:org.sufficientlysecure.keychain.pgp.UncachedPublicKey.java

License:Open Source License

/** Get all key usage flags.
 * If at least one key flag subpacket is present return these. If no
 * subpacket is present it returns null.
 *
 * Note that this method has package visiblity because it is used in test
 * cases. Certificates of UncachedPublicKey instances can NOT be assumed to
 * be verified or even by the correct key, so the result of this method
 * should never be used in other places!
 *//*from ww w .j  ava 2s  .  c o m*/
@SuppressWarnings("unchecked")
Integer getKeyUsage() {
    if (mCacheUsage == null) {
        PGPSignature mostRecentSig = null;
        for (PGPSignature sig : new IterableIterator<PGPSignature>(mPublicKey.getSignatures())) {
            if (mPublicKey.isMasterKey() && sig.getKeyID() != mPublicKey.getKeyID()) {
                continue;
            }

            switch (sig.getSignatureType()) {
            case PGPSignature.DEFAULT_CERTIFICATION:
            case PGPSignature.POSITIVE_CERTIFICATION:
            case PGPSignature.CASUAL_CERTIFICATION:
            case PGPSignature.NO_CERTIFICATION:
            case PGPSignature.SUBKEY_BINDING:
                break;
            // if this is not one of the above types, don't care
            default:
                continue;
            }

            // If we have no sig yet, take the first we can get
            if (mostRecentSig == null) {
                mostRecentSig = sig;
                continue;
            }

            // If the new sig is less recent, skip it
            if (mostRecentSig.getCreationTime().after(sig.getCreationTime())) {
                continue;
            }

            // Otherwise, note it down as the new "most recent" one
            mostRecentSig = sig;
        }

        // Initialize to 0 as cached but empty value, if there is no sig (can't happen
        // for canonicalized keyring), or there is no KEY_FLAGS packet in the sig
        mCacheUsage = 0;
        if (mostRecentSig != null) {
            // If a mostRecentSig has been found, (cache and) return its flags
            PGPSignatureSubpacketVector hashed = mostRecentSig.getHashedSubPackets();
            if (hashed != null && hashed.getSubpacket(SignatureSubpacketTags.KEY_FLAGS) != null) {
                mCacheUsage = hashed.getKeyFlags();
            }
        }

    }
    return mCacheUsage;
}