Skip to content

Fix null check for public key parameter.#944

Open
dkocher wants to merge 1 commit intohierynomus:masterfrom
iterate-ch:issue/907-addendum
Open

Fix null check for public key parameter.#944
dkocher wants to merge 1 commit intohierynomus:masterfrom
iterate-ch:issue/907-addendum

Conversation

@dkocher
Copy link
Copy Markdown
Contributor

@dkocher dkocher commented May 14, 2024

Addendum for #908 as discussed in 1. Resolves #907.

Footnotes

  1. https://github.com/hierynomus/sshj/pull/908/files#r1600044608

@dkocher dkocher requested a review from hierynomus as a code owner May 14, 2024 17:07
@hierynomus
Copy link
Copy Markdown
Owner

Actually the null check is not for the parameter, but was once meant to only ensure that a subsequent call to init would not re-initialize. Need to check what the potential problem is.

@diamondo25
Copy link
Copy Markdown

This has been blocked for ages. Here is my dirty solution for at least 1 init() call:

package com.hierynomus.sshj.userauth.keyprovider;

import lombok.val;

import java.security.PublicKey;

/**
 * This class fixes a bug with init() checking for pubKey != null, which causes publicKey (its argument) to never initialize...
 * <a href="https://github.com/hierynomus/sshj/pull/944">This Github pull request will be merged some day...</a>
 */
public class OpenSSHKeyV1KeyFileHacked extends OpenSSHKeyV1KeyFile {
    private class DummyPublicKey implements PublicKey {
        @Override
        public String getAlgorithm() {
            return "";
        }

        @Override
        public String getFormat() {
            return "";
        }

        @Override
        public byte[] getEncoded() {
            return new byte[0];
        }
    }
    
    @Override
    public void init(String privateKey, String publicKey) {
        // Make sure we only prepare loading the publicKey when its not null, as it'd otherwise throw a NullPointerException later.
        if (publicKey != null) {
            try {
                val field = OpenSSHKeyV1KeyFile.class.getDeclaredField("pubKey");
                field.setAccessible(true);
                field.set(this, new DummyPublicKey());
            } catch (Exception e) {
                throw new RuntimeException("Unable to hack pubKey field", e);
            }
        }
        
        super.init(privateKey, publicKey);
    }
}

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

Successfully merging this pull request may close these issues.

No initialization of public key depending on constructor usage of file key provider

3 participants