Skip to content

Commit

Permalink
Fix some sonatype warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
hierynomus committed May 8, 2023
1 parent 01d720e commit 6bf720c
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 33 deletions.
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ The implementation is based on the following specifications:
== Changelog

=== 0.12.0 (2023-??-??)
* Ensure we call flip() on Buffer to avoid Java8/9 compatibility issues (Fixes https://github.com/hierynomus/smbj/issues/705[#705])
* Do not send SNB2EncryptionCapabilities if the withEncryptData is set to false (Fixes https://github.com/hierynomus/smbj/issues/747[#747])
* Added Implementation-Version and Implementation-Name to MANIFEST.MF (Fixes https://github.com/hierynomus/smbj/issues/743[#743])
* Upgraded Gradle to 8.0.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.hierynomus.mssmb2;

import java.util.Arrays;

import com.hierynomus.protocol.commons.EnumWithValue;
import com.hierynomus.protocol.commons.buffer.Buffer;
import com.hierynomus.smb.SMBBuffer;
Expand All @@ -28,7 +30,7 @@
* This optional header is only valid for the SMB 3.1.1 dialect<73>.
*/
public class SMB2CompressionTransformHeader implements SMBHeader {
public static final byte[] COMPRESSED_PROTOCOL_ID = {(byte) 0xFC, 'S', 'M', 'B'};
private static final byte[] COMPRESSED_PROTOCOL_ID = {(byte) 0xFC, 'S', 'M', 'B'};
private int headerStartPosition;
private int originalCompressedSegmentSize;
private SMB3CompressionAlgorithm compressionAlgorithm;
Expand Down Expand Up @@ -74,4 +76,8 @@ public SMB3CompressionAlgorithm getCompressionAlgorithm() {
public int getOffset() {
return offset;
}

public static boolean isCompressed(byte[] header) {
return Arrays.equals(COMPRESSED_PROTOCOL_ID, header);
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/hierynomus/mssmb2/SMB2PacketHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import static com.hierynomus.protocol.commons.EnumWithValue.EnumUtils.isSet;

import java.util.Arrays;

/**
* [MS-SMB2] 2.2.1 SMB2 Packet Header
*/
Expand All @@ -30,7 +32,7 @@ public class SMB2PacketHeader implements SMBHeader {
public static final int STRUCTURE_SIZE = 64;
public static final int SIGNATURE_OFFSET = 48;
public static final int SIGNATURE_SIZE = 16;
public static final byte[] PROTOCOL_ID = {(byte) 0xFE, 'S', 'M', 'B'};
private static final byte[] PROTOCOL_ID = {(byte) 0xFE, 'S', 'M', 'B'};

private SMB2Dialect dialect;
private int creditCharge = 1;
Expand Down Expand Up @@ -257,4 +259,8 @@ public int getMessageEndPosition() {
public void setMessageEndPosition(int messageEndPosition) {
this.messageEndPosition = messageEndPosition;
}

public static boolean isPacketHeader(byte[] header) {
return Arrays.equals(PROTOCOL_ID, header);
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/hierynomus/mssmb2/SMB2TransformHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.hierynomus.mssmb2;

import java.util.Arrays;

import com.hierynomus.protocol.commons.buffer.Buffer;
import com.hierynomus.smb.SMBBuffer;
import com.hierynomus.smb.SMBHeader;
Expand All @@ -27,7 +29,7 @@
* The SMB2 TRANSFORM_HEADER is only valid for the SMB 3.x dialect family.
*/
public class SMB2TransformHeader implements SMBHeader {
public static final byte[] ENCRYPTED_PROTOCOL_ID = {(byte) 0xFD, 'S', 'M', 'B'};
private static final byte[] ENCRYPTED_PROTOCOL_ID = {(byte) 0xFD, 'S', 'M', 'B'};
private int headerStartPosition;
private byte[] signature;
private byte[] nonce;
Expand Down Expand Up @@ -110,4 +112,8 @@ public int getFlagsEncryptionAlgorithm() {
public long getSessionId() {
return sessionId;
}

public static boolean isEncrypted(byte[] header) {
return Arrays.equals(ENCRYPTED_PROTOCOL_ID, header);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ExtendedGSSContext {
private static final Method inquireSecContext = getInquireSecContextMethod();
private static Object krb5GetSessionKeyConst;

@SuppressWarnings("unchecked")
private static Method getInquireSecContextMethod() {
Class<?> extendedContextClass;
Class<?> inquireTypeClass;
Expand All @@ -42,6 +43,7 @@ private static Method getInquireSecContextMethod() {
throw exception;
}
}

krb5GetSessionKeyConst = Enum.valueOf(inquireTypeClass.asSubclass(Enum.class), "KRB5_GET_SESSION_KEY");
try {
return extendedContextClass.getDeclaredMethod("inquireSecContext", inquireTypeClass);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/hierynomus/smbj/common/Pooled.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Pooled<A extends Pooled<A>> {
* Takes a lease on the pooled object.
* @return <code>this</code> if the object is still valid (has at least 1 lease), else <code>null</code>
*/
@SuppressWarnings("unchecked")
public A lease() {
if (leases.getAndIncrement() > 0) {
return (A) this;
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/com/hierynomus/smbj/connection/PacketSignatory.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@
*/
package com.hierynomus.smbj.connection;

import com.hierynomus.mssmb2.SMB2Dialect;
import static com.hierynomus.mssmb2.SMB2MessageFlag.SMB2_FLAGS_SIGNED;
import static com.hierynomus.mssmb2.SMB2PacketHeader.EMPTY_SIGNATURE;
import static com.hierynomus.mssmb2.SMB2PacketHeader.SIGNATURE_OFFSET;
import static com.hierynomus.mssmb2.SMB2PacketHeader.SIGNATURE_SIZE;
import static com.hierynomus.mssmb2.SMB2PacketHeader.STRUCTURE_SIZE;

import java.util.Arrays;

import javax.crypto.SecretKey;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hierynomus.mssmb2.SMB2Packet;
import com.hierynomus.mssmb2.SMB2PacketData;
import com.hierynomus.mssmb2.SMB2PacketHeader;
import com.hierynomus.mssmb2.SMB2Packet;
import com.hierynomus.protocol.commons.buffer.Buffer;
import com.hierynomus.security.Mac;
import com.hierynomus.security.SecurityException;
import com.hierynomus.security.SecurityProvider;
import com.hierynomus.smb.SMBBuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.SecretKey;
import java.util.Arrays;

import static com.hierynomus.mssmb2.SMB2PacketHeader.*;
import static com.hierynomus.mssmb2.SMB2MessageFlag.SMB2_FLAGS_SIGNED;

public class PacketSignatory {
private static final Logger logger = LoggerFactory.getLogger(PacketSignatory.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ protected void doHandle(SMBPacketData<?> packetData) throws TransportException {
byte[] decrypted = encryptor.decrypt(data, session.getSessionContext().getDecryptionKey());

byte[] decryptedProtocolId = Arrays.copyOf(decrypted, 4);
if (Arrays.equals(decryptedProtocolId, SMB2TransformHeader.ENCRYPTED_PROTOCOL_ID)) {
if (SMB2TransformHeader.isEncrypted(decryptedProtocolId)) {
logger.error("Encountered a nested encrypted packet in packet {}, disconnecting the transport", packetData);
throw new TransportException("Cannot nest an encrypted packet in encrypted packet " + packetData);
} else if (Arrays.equals(decryptedProtocolId, SMB2CompressionTransformHeader.COMPRESSED_PROTOCOL_ID)) {
} else if (SMB2CompressionTransformHeader.isCompressed(decryptedProtocolId)) {
handleCompressedPacket(packetData, decrypted);
return;
} else if (Arrays.equals(decryptedProtocolId, SMB2PacketHeader.PROTOCOL_ID)) {
} else if (SMB2PacketHeader.isPacketHeader(decryptedProtocolId)) {
handleSMB2Packet(decrypted, data);
return;
} else {
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/hierynomus/smbj/paths/DFSPathResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,6 @@ private ReferralResult(long status) {
this.status = status;
}

private ReferralResult(ReferralCache.ReferralCacheEntry referralCacheEntry) {
this.referralCacheEntry = referralCacheEntry;
}

private ReferralResult(DomainCache.DomainCacheEntry domainCacheEntry) {
this.domainCacheEntry = domainCacheEntry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
*/
package com.hierynomus.smbj.transport.tcp.direct;

import static java.lang.String.format;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import javax.net.SocketFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hierynomus.protocol.Packet;
import com.hierynomus.protocol.PacketData;
import com.hierynomus.protocol.commons.buffer.Buffer;
Expand All @@ -23,19 +36,6 @@
import com.hierynomus.protocol.transport.TransportException;
import com.hierynomus.protocol.transport.TransportLayer;
import com.hierynomus.smbj.transport.PacketReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.SocketFactory;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import static java.lang.String.format;

/**
* A transport layer over Direct TCP/IP.
Expand Down

0 comments on commit 6bf720c

Please sign in to comment.