2 * Copyright (c) 2017 Brocade Communication Systems and others. All rights reserved.
3 * Copyright (c) 2024 PANTHEON.tech, s.r.o.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.topology.spi;
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13 import static org.junit.jupiter.api.Assertions.assertThrows;
15 import java.nio.charset.StandardCharsets;
16 import java.security.KeyPair;
17 import org.bouncycastle.openssl.EncryptionException;
18 import org.junit.jupiter.api.Test;
22 void testRSAKey() throws Exception {
23 assertNotNull(decodePrivateKey("rsa", ""));
27 void testRSAEncryptedKey() throws Exception {
28 assertNotNull(decodePrivateKey("rsa_encrypted", "passphrase"));
32 void testRSAWrongPassphrase() {
33 final var ex = assertThrows(EncryptionException.class, () -> decodePrivateKey("rsa_encrypted", "wrong"));
34 assertEquals("exception using cipher - please check password and data.", ex.getMessage());
38 void testDSAKey() throws Exception {
39 assertNotNull(decodePrivateKey("dsa", ""));
43 void testDSAEncryptedKey() throws Exception {
44 assertNotNull(decodePrivateKey("dsa_encrypted", "passphrase"));
48 void testDSAWrongPassphrase() {
49 final var ex = assertThrows(EncryptionException.class, () -> decodePrivateKey("dsa_encrypted", "wrong"));
50 assertEquals("exception using cipher - please check password and data.", ex.getMessage());
54 @SuppressWarnings("AbbreviationAsWordInName")
55 void testECDSAKey() throws Exception {
56 assertNotNull(decodePrivateKey("ecdsa", ""));
60 @SuppressWarnings("AbbreviationAsWordInName")
61 void testECDSAEncryptedKey() throws Exception {
62 assertNotNull(decodePrivateKey("ecdsa_encrypted", "passphrase"));
66 @SuppressWarnings("AbbreviationAsWordInName")
67 void testECDSAWrongPassphrase() {
68 final var ex = assertThrows(EncryptionException.class, () -> decodePrivateKey("ecdsa_encrypted", "wrong"));
69 assertEquals("exception using cipher - please check password and data.", ex.getMessage());
72 private static KeyPair decodePrivateKey(final String resourceName, final String password) throws Exception {
73 return NetconfClientConfigurationBuilderFactoryImpl.decodePrivateKey(
74 new String(PKIUtilTest.class.getResourceAsStream("/pki/" + resourceName).readAllBytes(),
75 StandardCharsets.UTF_8),