|
| 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.client.auth.pubkey; |
| 20 | + |
| 21 | +import java.security.KeyPair; |
| 22 | +import java.security.KeyPairGenerator; |
| 23 | +import java.security.interfaces.ECPublicKey; |
| 24 | +import java.util.AbstractMap.SimpleImmutableEntry; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import org.apache.sshd.client.session.ClientSession; |
| 29 | +import org.apache.sshd.common.auth.AbstractUserAuthServiceFactory; |
| 30 | +import org.apache.sshd.common.config.keys.u2f.SkEcdsaPublicKey; |
| 31 | +import org.apache.sshd.common.session.SessionContext; |
| 32 | +import org.apache.sshd.common.util.ValidateUtils; |
| 33 | +import org.apache.sshd.common.util.buffer.ByteArrayBuffer; |
| 34 | +import org.apache.sshd.common.util.security.SecurityUtils; |
| 35 | +import org.apache.sshd.util.test.BaseTestSupport; |
| 36 | +import org.junit.jupiter.api.Tag; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import org.mockito.Mockito; |
| 39 | + |
| 40 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 41 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 42 | + |
| 43 | +/** |
| 44 | + * Unit tests for client-side sk-* authentication packet encoding. |
| 45 | + */ |
| 46 | +@Tag("NoIoTestCase") |
| 47 | +public class UserAuthPublicKeySkTest extends BaseTestSupport { |
| 48 | + |
| 49 | + @Test |
| 50 | + public void securityKeySignatureBlobIsNotWrapped() throws Exception { |
| 51 | + byte[] rawSignature = { 1, 2, 3, 4, 5 }; |
| 52 | + byte flags = 1; |
| 53 | + long counter = 42; |
| 54 | + |
| 55 | + KeyPairGenerator generator = SecurityUtils.getKeyPairGenerator("EC"); |
| 56 | + generator.initialize(256); |
| 57 | + KeyPair pair = generator.generateKeyPair(); |
| 58 | + ECPublicKey ecPublicKey = ValidateUtils.checkInstanceOf( |
| 59 | + pair.getPublic(), ECPublicKey.class, "Expected an ECPublicKey"); |
| 60 | + SkEcdsaPublicKey sk = new SkEcdsaPublicKey("ssh", false, false, ecPublicKey); |
| 61 | + byte[] skSignature = createSkSignature(sk, rawSignature, flags, counter); |
| 62 | + |
| 63 | + ClientSession session = Mockito.mock(ClientSession.class); |
| 64 | + Mockito.when(session.getSessionId()).thenReturn(new byte[] { 9, 8, 7, 6 }); |
| 65 | + |
| 66 | + ExposedUserAuthPublicKey auth = new ExposedUserAuthPublicKey(new StaticSignatureIdentity(sk, skSignature)); |
| 67 | + byte[] actual = auth.appendSignature( |
| 68 | + session, AbstractUserAuthServiceFactory.DEFAULT_NAME, UserAuthPublicKey.NAME, "testuser", sk.getKeyType(), sk); |
| 69 | + |
| 70 | + assertSkSignature(sk, rawSignature, flags, counter, actual); |
| 71 | + } |
| 72 | + |
| 73 | + private static byte[] createSkSignature(SkEcdsaPublicKey key, byte[] rawSignature, byte flags, long counter) { |
| 74 | + ByteArrayBuffer buffer = new ByteArrayBuffer(); |
| 75 | + buffer.putString(key.getKeyType()); |
| 76 | + buffer.putBytes(rawSignature); |
| 77 | + buffer.putByte(flags); |
| 78 | + buffer.putUInt(counter); |
| 79 | + return buffer.getCompactData(); |
| 80 | + } |
| 81 | + |
| 82 | + private static void assertSkSignature( |
| 83 | + SkEcdsaPublicKey key, byte[] rawSignature, byte flags, long counter, byte[] signature) { |
| 84 | + ByteArrayBuffer buffer = new ByteArrayBuffer(signature); |
| 85 | + assertEquals(key.getKeyType(), buffer.getString()); |
| 86 | + assertArrayEquals(rawSignature, buffer.getBytes()); |
| 87 | + assertEquals(flags, buffer.getByte()); |
| 88 | + assertEquals(counter, buffer.getUInt()); |
| 89 | + assertEquals(0, buffer.available()); |
| 90 | + } |
| 91 | + |
| 92 | + private static class ExposedUserAuthPublicKey extends UserAuthPublicKey { |
| 93 | + ExposedUserAuthPublicKey(PublicKeyIdentity identity) { |
| 94 | + super(Collections.emptyList()); |
| 95 | + current = identity; |
| 96 | + } |
| 97 | + |
| 98 | + byte[] appendSignature( |
| 99 | + ClientSession session, String service, String name, String username, String algo, SkEcdsaPublicKey key) |
| 100 | + throws Exception { |
| 101 | + ByteArrayBuffer buffer = new ByteArrayBuffer(); |
| 102 | + super.appendSignature(session, service, name, username, algo, key, null, buffer); |
| 103 | + return buffer.getBytes(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static class StaticSignatureIdentity implements PublicKeyIdentity { |
| 108 | + private final SkEcdsaPublicKey key; |
| 109 | + private final byte[] signature; |
| 110 | + |
| 111 | + StaticSignatureIdentity(SkEcdsaPublicKey key, byte[] signature) { |
| 112 | + this.key = key; |
| 113 | + this.signature = signature.clone(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public KeyPair getKeyIdentity() { |
| 118 | + return new KeyPair(key, null); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public Map.Entry<String, byte[]> sign(SessionContext session, String algo, byte[] data) { |
| 123 | + return new SimpleImmutableEntry<>(key.getKeyType(), signature.clone()); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments