Skip to content

KeySelection using Realname. #68

Open
@GiuseppeMP

Description

@GiuseppeMP

Hello everyone,

Describe the bug
I'm cannot use legacy keys that have been generated without email.

To Reproduce
Try to use keys generated without Email, just Realname. in GPG shell works, in java throws no key suitable found.

Expected behavior
Find the Key by the RealName if email is not present.

Additional context
To resolve it in my project, I implemented this strategy:

public class ByEmailKeyAndJustRealnameKeySelectionStrategy extends Rfc4880KeySelectionStrategy {

    private static final String ENTRE_BRACKETS = "<.*>";

    /**
     * @param dateOfTimestampVerification The date used for key expiration date
     *                                    checks as "now".
     */
    public ByEmailKeyAndJustRealnameKeySelectionStrategy() {
        super(Instant.now(), true, true);
    }

    /**
     * Return all keyrings that ARE valid keys for the given uid.
     *
     * If the uid does not already include '&lt;...&gt;' then wrap it in
     * "&lt;uid&gt;"
     * to filter for e-mails. E.g. "[email protected]" will be converted to
     * "&lt;[email protected]&gt;" but "Klaus &lt;[email protected]&gt;" or
     * "&lt;[email protected]&gt;" will be left untouched.
     * If the uids does not match with email format; will be left untouched.
     *
     * @param uid           the userid as passed by upstream.
     * @param keyringConfig the keyring config
     * @param purpose       what is the requested key to be used for
     *
     * @return Set with keyrings, never null.
     *
     * @throws PGPException Something with BouncyCastle went wrong
     * @throws IOException  IO is dangerous
     */
    @SuppressWarnings({ "PMD.LawOfDemeter" })
    @Override
    protected Set<PGPPublicKeyRing> publicKeyRingsForUid(final PURPOSE purpose, final String uid,
            KeyringConfig keyringConfig)
            throws IOException, PGPException {

        final Set<PGPPublicKeyRing> keyringsForUid = new HashSet<>();

        String uidQuery = uid;
        final boolean uidAlreadyInBrackets = uidAlreadyInBrackets(uid);
        final boolean isValidEmail = JMail.isValid(uid);
        
        if (!uidAlreadyInBrackets && isValidEmail) {
            uidQuery = "<" + uid + ">";
        }

        final Iterator<PGPPublicKeyRing> keyRings = keyringConfig.getPublicKeyRings()
                .getKeyRings(uidQuery, true, true);

        while (keyRings.hasNext()) {
            keyringsForUid.add(keyRings.next());
        }

        return keyringsForUid;
    }
    
    protected boolean uidAlreadyInBrackets(String uid){
        return uid.matches(ENTRE_BRACKETS);
    }

I wondering if this make sense (is it correct) and if open an Issue/Merge worths. Any thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions