Restructure try/catch blocks for readability

This commit is contained in:
chubtub 2023-03-23 05:17:24 -04:00
parent 114443ff14
commit 7f840e9a35

View File

@ -578,45 +578,54 @@ public class SwidTagGateway {
} }
//Parse SoftwareIdentity id //Parse SoftwareIdentity id
String softwareIdentityId = "";
Document swidTag = null; Document swidTag = null;
Element softwareIdentity = null; DocumentBuilder db = null;
try { try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder(); dbf.setNamespaceAware(false);
swidTag = db.parse(new InputSource(new StringReader(xmlToSign))); db = dbf.newDocumentBuilder();
softwareIdentity = (Element) swidTag.getElementsByTagName(
SwidTagConstants.SOFTWARE_IDENTITY).item(0);
softwareIdentityId = softwareIdentity.getAttributes()
.getNamedItem("id").getNodeValue();
//How to sign without an Id attribute?
} catch (ParserConfigurationException e) { } catch (ParserConfigurationException e) {
System.out.println("Error instantiating DocumentBuilder object: " + e.getMessage()); System.out.println("Error instantiating DocumentBuilder object: " + e.getMessage());
System.exit(1); System.exit(1);
}
try {
swidTag = db.parse(new InputSource(new StringReader(xmlToSign)));
} catch (IOException | SAXException e) { } catch (IOException | SAXException e) {
System.out.println("Error parsing XML from " + signFile); System.out.println("Error parsing XML from " + signFile);
System.exit(1);
} }
Element softwareIdentity = (Element) swidTag.getElementsByTagName(
SwidTagConstants.SOFTWARE_IDENTITY).item(0);
String softwareIdentityId = softwareIdentity.getAttributes()
.getNamedItem("id").getNodeValue();
//Create signature with a reference to SoftwareIdentity id //Create signature with a reference to SoftwareIdentity id
System.out.println("Referencing SoftwareIdentity with id " + softwareIdentityId); System.out.println("Referencing SoftwareIdentity with id " + softwareIdentityId);
Document detachedSignature = null; XMLSignatureFactory sigFactory = null;
SignedInfo signedInfo = null;
try { try {
XMLSignatureFactory sigFactory = XMLSignatureFactory.getInstance("DOM"); sigFactory = XMLSignatureFactory.getInstance("DOM");
//ref must be distinguished from existing <Reference URI=""> //ref must be distinguished from existing <Reference URI="">
Reference ref = sigFactory.newReference("#" + softwareIdentityId, Reference ref = sigFactory.newReference("#" + softwareIdentityId,
sigFactory.newDigestMethod(DigestMethod.SHA256, null)); sigFactory.newDigestMethod(DigestMethod.SHA256, null));
SignedInfo signedInfo = sigFactory.newSignedInfo( signedInfo = sigFactory.newSignedInfo(
sigFactory.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, sigFactory.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE,
(C14NMethodParameterSpec) null), (C14NMethodParameterSpec) null),
sigFactory.newSignatureMethod(SwidTagConstants.SIGNATURE_ALGORITHM_RSA_SHA256, sigFactory.newSignatureMethod(SwidTagConstants.SIGNATURE_ALGORITHM_RSA_SHA256,
null), null),
Collections.singletonList(ref) Collections.singletonList(ref)
); );
List<XMLStructure> keyInfoElements = new ArrayList<XMLStructure>(); } catch (InvalidAlgorithmParameterException e) {
System.out.println("Digest method parameters are invalid: " + e.getMessage());
} catch (NoSuchAlgorithmException e) {
System.out.println("The digest algorithm could not be found: " + e.getMessage());
}
List<XMLStructure> keyInfoElements = new ArrayList<XMLStructure>();
KeyInfoFactory kiFactory = sigFactory.getKeyInfoFactory(); KeyInfoFactory kiFactory = sigFactory.getKeyInfoFactory();
PrivateKey privateKey; PrivateKey privateKey = null;
CredentialParser cp = new CredentialParser(); CredentialParser cp = new CredentialParser();
try {
if (defaultCredentials) { if (defaultCredentials) {
cp.parseJKSCredentials(jksTruststoreFile); cp.parseJKSCredentials(jksTruststoreFile);
privateKey = cp.getPrivateKey(); privateKey = cp.getPrivateKey();
@ -636,34 +645,36 @@ public class SwidTagGateway {
keyInfoElements.add(kiFactory.newKeyValue(certificate.getPublicKey())); keyInfoElements.add(kiFactory.newKeyValue(certificate.getPublicKey()));
} }
} }
KeyInfo keyinfo = kiFactory.newKeyInfo(keyInfoElements);
detachedSignature = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
detachedSignature.appendChild(detachedSignature.createElement("root"));
DOMSignContext context = new DOMSignContext(privateKey,
detachedSignature.getDocumentElement());
context.setIdAttributeNS(softwareIdentity, null, "id");
XMLSignature signature = sigFactory.newXMLSignature(signedInfo, keyinfo);
signature.sign(context);
System.out.println("Detached signature: " + detachedSignature);
} catch (InvalidAlgorithmParameterException e) {
System.out.println("Digest method parameters are invalid: " + e.getMessage());
} catch (NoSuchAlgorithmException e) {
System.out.println("The digest algorithm could not be found: " + e.getMessage());
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error getting SKID from signing credentials: " + e.getMessage()); System.out.println("Error getting SKID from signing credentials: " + e.getMessage());
} catch (ParserConfigurationException e) {
System.out.println("Error creating new document object: " + e.getMessage());
} catch (MarshalException | XMLSignatureException e) {
System.out.println("Error while signing SoftwareIdentity");
e.printStackTrace();
} catch (KeyException e) { } catch (KeyException e) {
System.out.println("Public key algorithm not recognized or supported: " System.out.println("Public key algorithm not recognized or supported: "
+ e.getMessage()); + e.getMessage());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
}
KeyInfo keyinfo = kiFactory.newKeyInfo(keyInfoElements);
Document detachedSignature = null;
try {
detachedSignature = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e) {
System.out.println("Error creating new document object: " + e.getMessage());
}
detachedSignature.setXmlVersion("1.0");
detachedSignature.appendChild(detachedSignature.createElement("root"));
DOMSignContext context = new DOMSignContext(privateKey,
detachedSignature.getDocumentElement());
context.setIdAttributeNS(softwareIdentity, null, "id");
XMLSignature signature = sigFactory.newXMLSignature(signedInfo, keyinfo);
try {
signature.sign(context);
} catch (MarshalException | XMLSignatureException e) {
System.out.println("Error while signing SoftwareIdentity");
e.printStackTrace(); e.printStackTrace();
} }
System.out.println("Detached signature: " + detachedSignature);
return swidTag; return swidTag;
} }