Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeySelection using Realname. #68

Open
GiuseppeMP opened this issue Feb 17, 2022 · 0 comments
Open

KeySelection using Realname. #68

GiuseppeMP opened this issue Feb 17, 2022 · 0 comments

Comments

@GiuseppeMP
Copy link

GiuseppeMP commented Feb 17, 2022

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. "peter@example.com" will be converted to
     * "&lt;peter@example.com&gt;" but "Klaus &lt;klaus@example.com&gt;" or
     * "&lt;klaus@example.com&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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant