Decrypt key credentials in keystore-legacy
[netconf.git] / keystore / keystore-legacy / src / test / java / org / opendaylight / netconf / keystore / legacy / impl / SecurityHelperTest.java
1 /*
2  * Copyright (c) 2017 Brocade Communication Systems and others.  All rights reserved.
3  * Copyright (c) 2024 PANTHEON.tech, s.r.o.
4  *
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
8  */
9 package org.opendaylight.netconf.keystore.legacy.impl;
10
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;
14
15 import java.nio.charset.StandardCharsets;
16 import java.security.KeyPair;
17 import org.bouncycastle.openssl.EncryptionException;
18 import org.junit.jupiter.api.Test;
19
20 class SecurityHelperTest {
21     private final SecurityHelper helper = new SecurityHelper();
22
23     @Test
24     void testRSAKey() throws Exception {
25         assertNotNull(decodePrivateKey("rsa", ""));
26     }
27
28     @Test
29     void testRSAEncryptedKey() throws Exception {
30         assertNotNull(decodePrivateKey("rsa_encrypted", "passphrase"));
31     }
32
33     @Test
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());
37     }
38
39     @Test
40     void testDSAKey() throws Exception {
41         assertNotNull(decodePrivateKey("dsa", ""));
42     }
43
44     @Test
45     void testDSAEncryptedKey() throws Exception {
46         assertNotNull(decodePrivateKey("dsa_encrypted", "passphrase"));
47     }
48
49     @Test
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());
53     }
54
55     @Test
56     @SuppressWarnings("AbbreviationAsWordInName")
57     void testECDSAKey() throws Exception {
58         assertNotNull(decodePrivateKey("ecdsa", ""));
59     }
60
61     @Test
62     @SuppressWarnings("AbbreviationAsWordInName")
63     void testECDSAEncryptedKey() throws Exception {
64         assertNotNull(decodePrivateKey("ecdsa_encrypted", "passphrase"));
65     }
66
67     @Test
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());
72     }
73
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),
78             password);
79     }
80 }