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.keystore.legacy.impl;
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;
20 class SecurityHelperTest {
21 private final SecurityHelper helper = new SecurityHelper();
24 void testRSAKey() throws Exception {
25 assertNotNull(decodePrivateKey("rsa", ""));
29 void testRSAEncryptedKey() throws Exception {
30 assertNotNull(decodePrivateKey("rsa_encrypted", "passphrase"));
34 void testRSAWrongPassphrase() {
35 final var ex = assertThrows(EncryptionException.class, () -> decodePrivateKey("rsa_encrypted", "wrong"));
36 assertEquals("exception using cipher - please check password and data.", ex.getMessage());
40 void testDSAKey() throws Exception {
41 assertNotNull(decodePrivateKey("dsa", ""));
45 void testDSAEncryptedKey() throws Exception {
46 assertNotNull(decodePrivateKey("dsa_encrypted", "passphrase"));
50 void testDSAWrongPassphrase() {
51 final var ex = assertThrows(EncryptionException.class, () -> decodePrivateKey("dsa_encrypted", "wrong"));
52 assertEquals("exception using cipher - please check password and data.", ex.getMessage());
56 @SuppressWarnings("AbbreviationAsWordInName")
57 void testECDSAKey() throws Exception {
58 assertNotNull(decodePrivateKey("ecdsa", ""));
62 @SuppressWarnings("AbbreviationAsWordInName")
63 void testECDSAEncryptedKey() throws Exception {
64 assertNotNull(decodePrivateKey("ecdsa_encrypted", "passphrase"));
68 @SuppressWarnings("AbbreviationAsWordInName")
69 void testECDSAWrongPassphrase() {
70 final var ex = assertThrows(EncryptionException.class, () -> decodePrivateKey("ecdsa_encrypted", "wrong"));
71 assertEquals("exception using cipher - please check password and data.", ex.getMessage());
74 private KeyPair decodePrivateKey(final String resourceName, final String password) throws Exception {
75 return helper.decodePrivateKey(
76 new String(SecurityHelperTest.class.getResourceAsStream("/pki/" + resourceName).readAllBytes(),
77 StandardCharsets.UTF_8),