|
| 1 | +/* |
| 2 | + * SonarQube Cryptography Plugin |
| 3 | + * Copyright (C) 2024 IBM |
| 4 | + * |
| 5 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 6 | + * contributor license agreements. See the NOTICE file distributed with |
| 7 | + * this work for additional information regarding copyright ownership. |
| 8 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 9 | + * (the "License"); you may not use this file except in compliance with |
| 10 | + * the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | +package com.ibm.plugin.rules.issues; |
| 21 | + |
| 22 | +import com.ibm.engine.detection.DetectionStore; |
| 23 | +import com.ibm.engine.model.Algorithm; |
| 24 | +import com.ibm.engine.model.CipherAction; |
| 25 | +import com.ibm.engine.model.IValue; |
| 26 | +import com.ibm.engine.model.OperationMode; |
| 27 | +import com.ibm.engine.model.context.CipherContext; |
| 28 | +import com.ibm.mapper.model.INode; |
| 29 | +import com.ibm.mapper.model.KeyLength; |
| 30 | +import com.ibm.mapper.model.Mode; |
| 31 | +import com.ibm.mapper.model.Oid; |
| 32 | +import com.ibm.mapper.model.Padding; |
| 33 | +import com.ibm.mapper.model.PublicKeyEncryption; |
| 34 | +import com.ibm.mapper.model.functionality.Decapsulate; |
| 35 | +import com.ibm.mapper.model.functionality.Encapsulate; |
| 36 | +import com.ibm.plugin.TestBase; |
| 37 | +import org.junit.jupiter.api.Disabled; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | +import org.sonar.java.checks.verifier.CheckVerifier; |
| 40 | +import org.sonar.plugins.java.api.JavaCheck; |
| 41 | +import org.sonar.plugins.java.api.JavaFileScannerContext; |
| 42 | +import org.sonar.plugins.java.api.semantic.Symbol; |
| 43 | +import org.sonar.plugins.java.api.tree.Tree; |
| 44 | + |
| 45 | +import javax.annotation.Nonnull; |
| 46 | +import java.util.List; |
| 47 | + |
| 48 | +import static org.assertj.core.api.Assertions.assertThat; |
| 49 | + |
| 50 | +class ClientEncryptionJavaDuplicatedRSADetectionTest extends TestBase { |
| 51 | + |
| 52 | + @Test |
| 53 | + @Disabled |
| 54 | + void test() { |
| 55 | + CheckVerifier.newVerifier() |
| 56 | + .onFile( |
| 57 | + "src/test/files/rules/issues/ClientEncryptionJavaDuplicatedRSADetectionTestFile.java") |
| 58 | + .withChecks(this) |
| 59 | + .verifyIssues(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void asserts( |
| 64 | + int findingId, |
| 65 | + @Nonnull DetectionStore<JavaCheck, Tree, Symbol, JavaFileScannerContext> detectionStore, |
| 66 | + @Nonnull List<INode> nodes) { |
| 67 | + |
| 68 | + if (findingId == 0) { |
| 69 | + /* |
| 70 | + * Detection Store |
| 71 | + */ |
| 72 | + |
| 73 | + assertThat(detectionStore.getDetectionValues()).hasSize(1); |
| 74 | + assertThat(detectionStore.getDetectionValueContext()).isInstanceOf(CipherContext.class); |
| 75 | + IValue<Tree> value0 = detectionStore.getDetectionValues().get(0); |
| 76 | + assertThat(value0).isInstanceOf(Algorithm.class); |
| 77 | + assertThat(value0.asString()).isEqualTo("RSA/ECB/OAEPWith{ALG}AndMGF1Padding"); |
| 78 | + |
| 79 | + DetectionStore<JavaCheck, Tree, Symbol, JavaFileScannerContext> store_1 = |
| 80 | + getStoreOfValueType(OperationMode.class, detectionStore.getChildren()); |
| 81 | + assertThat(store_1.getDetectionValues()).hasSize(1); |
| 82 | + assertThat(store_1.getDetectionValueContext()).isInstanceOf(CipherContext.class); |
| 83 | + IValue<Tree> value0_1 = store_1.getDetectionValues().get(0); |
| 84 | + assertThat(value0_1).isInstanceOf(OperationMode.class); |
| 85 | + assertThat(value0_1.asString()).isEqualTo("3"); |
| 86 | + |
| 87 | + DetectionStore<JavaCheck, Tree, Symbol, JavaFileScannerContext> store_2 = |
| 88 | + getStoreOfValueType(CipherAction.class, detectionStore.getChildren()); |
| 89 | + assertThat(store_2.getDetectionValues()).hasSize(1); |
| 90 | + assertThat(store_2.getDetectionValueContext()).isInstanceOf(CipherContext.class); |
| 91 | + IValue<Tree> value0_2 = store_2.getDetectionValues().get(0); |
| 92 | + assertThat(value0_2).isInstanceOf(CipherAction.class); |
| 93 | + assertThat(value0_2.asString()).isEqualTo("WRAP"); |
| 94 | + |
| 95 | + /* |
| 96 | + * Translation |
| 97 | + */ |
| 98 | + |
| 99 | + assertThat(nodes).hasSize(1); |
| 100 | + |
| 101 | + // PublicKeyEncryption |
| 102 | + INode publicKeyEncryptionNode = nodes.get(0); |
| 103 | + assertThat(publicKeyEncryptionNode.getKind()).isEqualTo(PublicKeyEncryption.class); |
| 104 | + assertThat(publicKeyEncryptionNode.getChildren()).hasSize(5); |
| 105 | + assertThat(publicKeyEncryptionNode.asString()).isEqualTo("RSA-OAEP"); |
| 106 | + |
| 107 | + // KeyLength under PublicKeyEncryption |
| 108 | + INode keyLengthNode = publicKeyEncryptionNode.getChildren().get(KeyLength.class); |
| 109 | + assertThat(keyLengthNode).isNotNull(); |
| 110 | + assertThat(keyLengthNode.getChildren()).isEmpty(); |
| 111 | + assertThat(keyLengthNode.asString()).isEqualTo("2048"); |
| 112 | + |
| 113 | + // Oid under PublicKeyEncryption |
| 114 | + INode oidNode = publicKeyEncryptionNode.getChildren().get(Oid.class); |
| 115 | + assertThat(oidNode).isNotNull(); |
| 116 | + assertThat(oidNode.getChildren()).isEmpty(); |
| 117 | + assertThat(oidNode.asString()).isEqualTo("1.2.840.113549.1.1.7"); |
| 118 | + |
| 119 | + // Padding under PublicKeyEncryption |
| 120 | + INode paddingNode = publicKeyEncryptionNode.getChildren().get(Padding.class); |
| 121 | + assertThat(paddingNode).isNotNull(); |
| 122 | + assertThat(paddingNode.getChildren()).isEmpty(); |
| 123 | + assertThat(paddingNode.asString()).isEqualTo("OAEP"); |
| 124 | + |
| 125 | + // Encapsulate under PublicKeyEncryption |
| 126 | + INode encapsulateNode = publicKeyEncryptionNode.getChildren().get(Encapsulate.class); |
| 127 | + assertThat(encapsulateNode).isNotNull(); |
| 128 | + assertThat(encapsulateNode.getChildren()).isEmpty(); |
| 129 | + assertThat(encapsulateNode.asString()).isEqualTo("ENCAPSULATE"); |
| 130 | + |
| 131 | + // Mode under PublicKeyEncryption |
| 132 | + INode modeNode = publicKeyEncryptionNode.getChildren().get(Mode.class); |
| 133 | + assertThat(modeNode).isNotNull(); |
| 134 | + assertThat(modeNode.getChildren()).isEmpty(); |
| 135 | + assertThat(modeNode.asString()).isEqualTo("ECB"); |
| 136 | + |
| 137 | + } else { |
| 138 | + /* |
| 139 | + * Detection Store |
| 140 | + */ |
| 141 | + |
| 142 | + assertThat(detectionStore.getDetectionValues()).hasSize(1); |
| 143 | + assertThat(detectionStore.getDetectionValueContext()).isInstanceOf(CipherContext.class); |
| 144 | + IValue<Tree> value0 = detectionStore.getDetectionValues().get(0); |
| 145 | + assertThat(value0).isInstanceOf(Algorithm.class); |
| 146 | + assertThat(value0.asString()).isEqualTo("RSA/ECB/OAEPWith{ALG}AndMGF1Padding"); |
| 147 | + |
| 148 | + DetectionStore<JavaCheck, Tree, Symbol, JavaFileScannerContext> store_1 = |
| 149 | + getStoreOfValueType(OperationMode.class, detectionStore.getChildren()); |
| 150 | + assertThat(store_1.getDetectionValues()).hasSize(1); |
| 151 | + assertThat(store_1.getDetectionValueContext()).isInstanceOf(CipherContext.class); |
| 152 | + IValue<Tree> value0_1 = store_1.getDetectionValues().get(0); |
| 153 | + assertThat(value0_1).isInstanceOf(OperationMode.class); |
| 154 | + assertThat(value0_1.asString()).isEqualTo("4"); |
| 155 | + |
| 156 | + /* |
| 157 | + * Translation |
| 158 | + */ |
| 159 | + assertThat(nodes).hasSize(1); |
| 160 | + |
| 161 | + // PublicKeyEncryption |
| 162 | + INode publicKeyEncryptionNode1 = nodes.get(0); |
| 163 | + assertThat(publicKeyEncryptionNode1.getKind()).isEqualTo(PublicKeyEncryption.class); |
| 164 | + assertThat(publicKeyEncryptionNode1.getChildren()).hasSize(5); |
| 165 | + assertThat(publicKeyEncryptionNode1.asString()).isEqualTo("RSA-OAEP"); |
| 166 | + |
| 167 | + // Mode under PublicKeyEncryption |
| 168 | + INode modeNode1 = publicKeyEncryptionNode1.getChildren().get(Mode.class); |
| 169 | + assertThat(modeNode1).isNotNull(); |
| 170 | + assertThat(modeNode1.getChildren()).isEmpty(); |
| 171 | + assertThat(modeNode1.asString()).isEqualTo("ECB"); |
| 172 | + |
| 173 | + // Decapsulate under PublicKeyEncryption |
| 174 | + INode decapsulateNode = publicKeyEncryptionNode1.getChildren().get(Decapsulate.class); |
| 175 | + assertThat(decapsulateNode).isNotNull(); |
| 176 | + assertThat(decapsulateNode.getChildren()).isEmpty(); |
| 177 | + assertThat(decapsulateNode.asString()).isEqualTo("DECAPSULATE"); |
| 178 | + |
| 179 | + // Oid under PublicKeyEncryption |
| 180 | + INode oidNode1 = publicKeyEncryptionNode1.getChildren().get(Oid.class); |
| 181 | + assertThat(oidNode1).isNotNull(); |
| 182 | + assertThat(oidNode1.getChildren()).isEmpty(); |
| 183 | + assertThat(oidNode1.asString()).isEqualTo("1.2.840.113549.1.1.7"); |
| 184 | + |
| 185 | + // Padding under PublicKeyEncryption |
| 186 | + INode paddingNode1 = publicKeyEncryptionNode1.getChildren().get(Padding.class); |
| 187 | + assertThat(paddingNode1).isNotNull(); |
| 188 | + assertThat(paddingNode1.getChildren()).isEmpty(); |
| 189 | + assertThat(paddingNode1.asString()).isEqualTo("OAEP"); |
| 190 | + |
| 191 | + // KeyLength under PublicKeyEncryption |
| 192 | + INode keyLengthNode1 = publicKeyEncryptionNode1.getChildren().get(KeyLength.class); |
| 193 | + assertThat(keyLengthNode1).isNotNull(); |
| 194 | + assertThat(keyLengthNode1.getChildren()).isEmpty(); |
| 195 | + assertThat(keyLengthNode1.asString()).isEqualTo("2048"); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments