|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.sshd.agent; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.security.KeyPair; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import org.apache.sshd.agent.common.AbstractAgentClient; |
| 28 | +import org.apache.sshd.agent.common.AbstractAgentProxy; |
| 29 | +import org.apache.sshd.agent.local.AgentImpl; |
| 30 | +import org.apache.sshd.common.config.keys.KeyUtils; |
| 31 | +import org.apache.sshd.common.keyprovider.KeyPairProvider; |
| 32 | +import org.apache.sshd.common.signature.BuiltinSignatures; |
| 33 | +import org.apache.sshd.common.signature.Signature; |
| 34 | +import org.apache.sshd.common.util.buffer.Buffer; |
| 35 | +import org.apache.sshd.common.util.buffer.ByteArrayBuffer; |
| 36 | +import org.apache.sshd.common.util.security.SecurityUtils; |
| 37 | +import org.apache.sshd.util.test.BaseTestSupport; |
| 38 | +import org.apache.sshd.util.test.NoIoTestCase; |
| 39 | +import org.junit.Test; |
| 40 | +import org.junit.experimental.categories.Category; |
| 41 | +import org.junit.runner.RunWith; |
| 42 | +import org.junit.runners.Parameterized; |
| 43 | + |
| 44 | +/** |
| 45 | + * Simple short-circuited test for {@link AbstractAgentClient} and {@link AbstractAgentProxy}. |
| 46 | + */ |
| 47 | +@Category(NoIoTestCase.class) |
| 48 | +@RunWith(Parameterized.class) |
| 49 | +public class AgentUnitTest extends BaseTestSupport { |
| 50 | + |
| 51 | + private String algorithm; |
| 52 | + |
| 53 | + private BuiltinSignatures factory; |
| 54 | + |
| 55 | + public AgentUnitTest(String algorithm, BuiltinSignatures factory) { |
| 56 | + this.algorithm = algorithm; |
| 57 | + this.factory = factory; |
| 58 | + } |
| 59 | + |
| 60 | + @Parameterized.Parameters(name = "{0}") |
| 61 | + public static List<Object[]> getParameters() { |
| 62 | + return Arrays.asList(new Object[] { KeyUtils.RSA_SHA512_KEY_TYPE_ALIAS, BuiltinSignatures.rsaSHA512 }, |
| 63 | + new Object[] { KeyUtils.RSA_SHA256_KEY_TYPE_ALIAS, BuiltinSignatures.rsaSHA256 }, |
| 64 | + new Object[] { KeyPairProvider.SSH_RSA, BuiltinSignatures.rsa }); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testRsaSignature() throws Exception { |
| 69 | + SshAgent agent = new AgentImpl(); |
| 70 | + KeyPair pair = SecurityUtils.getKeyPairGenerator(KeyUtils.RSA_ALGORITHM).generateKeyPair(); |
| 71 | + agent.addIdentity(pair, "test key"); |
| 72 | + Server server = new Server(agent); |
| 73 | + Client client = new Client(server); |
| 74 | + server.setClient(client); |
| 75 | + byte[] data = { 'd', 'a', 't', 'a' }; |
| 76 | + Map.Entry<String, byte[]> result = client.sign(null, pair.getPublic(), algorithm, data); |
| 77 | + assertEquals("Unexpected signature algorithm", algorithm, result.getKey()); |
| 78 | + byte[] signature = result.getValue(); |
| 79 | + Signature verifier = factory.get(); |
| 80 | + verifier.initVerifier(null, pair.getPublic()); |
| 81 | + verifier.update(null, data); |
| 82 | + assertTrue("Signature should validate", verifier.verify(null, signature)); |
| 83 | + } |
| 84 | + |
| 85 | + private static class Server extends AbstractAgentClient { |
| 86 | + |
| 87 | + private Client client; |
| 88 | + |
| 89 | + protected Server(SshAgent agent) { |
| 90 | + super(agent); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected void reply(Buffer buf) throws IOException { |
| 95 | + client.setResult(buf.getCompactData()); |
| 96 | + } |
| 97 | + |
| 98 | + void setClient(Client client) { |
| 99 | + this.client = client; |
| 100 | + } |
| 101 | + |
| 102 | + void request(byte[] data) throws IOException { |
| 103 | + messageReceived(ByteArrayBuffer.getCompactClone(data)); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static class Client extends AbstractAgentProxy { |
| 108 | + |
| 109 | + private final Server server; |
| 110 | + |
| 111 | + private byte[] result; |
| 112 | + |
| 113 | + protected Client(Server server) { |
| 114 | + super(null); |
| 115 | + this.server = server; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + protected Buffer request(Buffer buffer) throws IOException { |
| 120 | + server.request(buffer.getCompactData()); |
| 121 | + Buffer received = ByteArrayBuffer.getCompactClone(result); |
| 122 | + return new ByteArrayBuffer(received.getBytes()); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public boolean isOpen() { |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + void setResult(byte[] data) { |
| 131 | + result = data; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments