Skip to content

Commit

Permalink
Service Provider Interface for the Signable implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
ebourg committed Feb 2, 2024
1 parent ed0b056 commit 59aacc2
Show file tree
Hide file tree
Showing 16 changed files with 607 additions and 54 deletions.
61 changes: 8 additions & 53 deletions jsign-core/src/main/java/net/jsign/Signable.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,14 @@
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.util.List;
import java.util.ServiceLoader;

import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.cms.ContentInfo;
import org.bouncycastle.cms.CMSSignedData;

import net.jsign.appx.APPXFile;
import net.jsign.asn1.authenticode.AuthenticodeObjectIdentifiers;
import net.jsign.cat.CatalogFile;
import net.jsign.mscab.MSCabinetFile;
import net.jsign.msi.MSIFile;
import net.jsign.navx.NAVXFile;
import net.jsign.pe.PEFile;
import net.jsign.script.JScript;
import net.jsign.script.PowerShellScript;
import net.jsign.script.PowerShellXMLScript;
import net.jsign.script.VBScript;
import net.jsign.script.WindowsScript;
import net.jsign.spi.SignableProvider;

/**
* A file that can be signed with Authenticode.
Expand Down Expand Up @@ -138,48 +129,12 @@ static Signable of(File file) throws IOException {
* @throws UnsupportedOperationException if the file specified isn't supported
*/
static Signable of(File file, Charset encoding) throws IOException {
if (PEFile.isPEFile(file)) {
return new PEFile(file);

} else if (MSIFile.isMSIFile(file)) {
return new MSIFile(file);

} else if (MSCabinetFile.isMSCabinetFile(file)) {
return new MSCabinetFile(file);

} else if (CatalogFile.isCatalogFile(file)) {
return new CatalogFile(file);

} else if (NAVXFile.isNAVXFile(file)) {
return new NAVXFile(file);

} else if (file.getName().toLowerCase().endsWith(".ps1")
|| file.getName().toLowerCase().endsWith(".psd1")
|| file.getName().toLowerCase().endsWith(".psm1")) {
return new PowerShellScript(file, encoding);

} else if (file.getName().toLowerCase().endsWith(".ps1xml")) {
return new PowerShellXMLScript(file, encoding);

} else if (file.getName().toLowerCase().endsWith(".vbs")
|| file.getName().toLowerCase().endsWith(".vbe")) {
return new VBScript(file, encoding);

} else if (file.getName().toLowerCase().endsWith(".js")
|| file.getName().toLowerCase().endsWith(".jse")) {
return new JScript(file, encoding);

} else if (file.getName().toLowerCase().endsWith(".wsf")) {
return new WindowsScript(file, encoding);

} else if (file.getName().toLowerCase().endsWith(".msix")
|| file.getName().toLowerCase().endsWith(".msixbundle")
|| file.getName().toLowerCase().endsWith(".appx")
|| file.getName().toLowerCase().endsWith(".appxbundle")) {
return new APPXFile(file);

} else {
throw new UnsupportedOperationException("Unsupported file: " + file);
for (SignableProvider provider : ServiceLoader.load(SignableProvider.class)) {
if (provider.isSupported(file)) {
return provider.create(file, encoding);
}
}

throw new UnsupportedOperationException("Unsupported file: " + file);
}
}
44 changes: 44 additions & 0 deletions jsign-core/src/main/java/net/jsign/spi/APPXSignableProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright 2024 Emmanuel Bourg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.jsign.spi;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import org.kohsuke.MetaInfServices;

import net.jsign.Signable;
import net.jsign.appx.APPXFile;

/**
* {@link SignableProvider} for APPX files.
*
* @since 6.1
*/
@MetaInfServices(SignableProvider.class)
public class APPXSignableProvider extends ExtensionBasedSignableProvider {

public APPXSignableProvider() {
super("msix", "msixbundle", "appx", "appxbundle");
}

@Override
public Signable create(File file, Charset encoding) throws IOException {
return new APPXFile(file);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2024 Emmanuel Bourg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.jsign.spi;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import org.kohsuke.MetaInfServices;

import net.jsign.Signable;
import net.jsign.cat.CatalogFile;

/**
* {@link SignableProvider} for Catalog files.
*
* @since 6.1
*/
@MetaInfServices
public class CatalogSignableProvider implements SignableProvider {

@Override
public boolean isSupported(File file) {
return CatalogFile.isCatalogFile(file);
}

@Override
public Signable create(File file, Charset encoding) throws IOException {
return new CatalogFile(file);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright 2024 Emmanuel Bourg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.jsign.spi;

import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
* {@link SignableProvider} for files matching a specific extension.
*
* @since 6.1
*/
public abstract class ExtensionBasedSignableProvider implements SignableProvider {

private final Set<String> extensions;

public ExtensionBasedSignableProvider(String... extensions) {
this.extensions = new HashSet<>(Arrays.asList(extensions));
}

@Override
public boolean isSupported(File file) {
String extension = file.getName().substring(file.getName().lastIndexOf('.') + 1);
return extensions.contains(extension.toLowerCase());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright 2024 Emmanuel Bourg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.jsign.spi;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import org.kohsuke.MetaInfServices;

import net.jsign.Signable;
import net.jsign.script.JScript;

/**
* {@link SignableProvider} for JScript files.
*
* @since 6.1
*/
@MetaInfServices(SignableProvider.class)
public class JScriptSignableProvider extends ExtensionBasedSignableProvider {

public JScriptSignableProvider() {
super("js", "jse");
}

@Override
public Signable create(File file, Charset encoding) throws IOException {
return new JScript(file, encoding);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2024 Emmanuel Bourg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.jsign.spi;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import org.kohsuke.MetaInfServices;

import net.jsign.Signable;
import net.jsign.mscab.MSCabinetFile;

/**
* {@link SignableProvider} for Microsoft Cabinet files
*
* @since 6.1
*/
@MetaInfServices
public class MSCabinetSignableProvider implements SignableProvider {

@Override
public boolean isSupported(File file) throws IOException {
return MSCabinetFile.isMSCabinetFile(file);
}

@Override
public Signable create(File file, Charset encoding) throws IOException {
return new MSCabinetFile(file);
}
}
45 changes: 45 additions & 0 deletions jsign-core/src/main/java/net/jsign/spi/MSISignableProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2024 Emmanuel Bourg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.jsign.spi;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import org.kohsuke.MetaInfServices;

import net.jsign.Signable;
import net.jsign.msi.MSIFile;

/**
* {@link SignableProvider} for MSI files.
*
* @since 6.1
*/
@MetaInfServices
public class MSISignableProvider implements SignableProvider {

@Override
public boolean isSupported(File file) throws IOException {
return MSIFile.isMSIFile(file);
}

@Override
public Signable create(File file, Charset encoding) throws IOException {
return new MSIFile(file);
}
}
45 changes: 45 additions & 0 deletions jsign-core/src/main/java/net/jsign/spi/NAVXSignableProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2024 Emmanuel Bourg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.jsign.spi;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import org.kohsuke.MetaInfServices;

import net.jsign.Signable;
import net.jsign.navx.NAVXFile;

/**
* {@link SignableProvider} for NAVX files.
*
* @since 6.1
*/
@MetaInfServices
public class NAVXSignableProvider implements SignableProvider {

@Override
public boolean isSupported(File file) throws IOException {
return NAVXFile.isNAVXFile(file);
}

@Override
public Signable create(File file, Charset encoding) throws IOException {
return new NAVXFile(file);
}
}
Loading

0 comments on commit 59aacc2

Please sign in to comment.