use of de.carne.check.Nullable in project certmgr by hdecarne.
the class CertReaders method readFile.
/**
* Read all available certificate objects from a file.
* <p>
* All registered {@link CertReader}s are considered for reading certificate object until one recognizes the file
* data.
*
* @param file The file to read from.
* @param password The callback to use for querying passwords (if needed).
* @return The read certificate objects, or {@code null} if no certificate data was recognized.
* @throws IOException if an I/O error occurs during reading/decoding.
*/
@Nullable
public static CertObjectStore readFile(Path file, PasswordCallback password) throws IOException {
Deque<CertReader> certReaders = new ArrayDeque<>();
Path fileName = file.getFileName();
for (CertReader reader : REGISTERED.providers()) {
if (matchFileName(reader, fileName)) {
certReaders.addFirst(reader);
} else {
certReaders.addLast(reader);
}
}
CertObjectStore certObjects = null;
for (CertReader reader : certReaders) {
try (IOResource<InputStream> in = IOResource.newInputStream(file.toString(), file, StandardOpenOption.READ)) {
certObjects = reader.readBinary(in, password);
if (certObjects != null) {
break;
}
}
}
return certObjects;
}
use of de.carne.check.Nullable in project certmgr by hdecarne.
the class KeyHelper method getKeySize.
/**
* Get the public key's key size.
*
* @param publicKey The public key to get the key size for.
* @return The public key's key size or {@code null} if the key size is indeterminable.
*/
@Nullable
public static Integer getKeySize(PublicKey publicKey) {
Integer keySize = null;
if (publicKey instanceof RSAPublicKey) {
RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
keySize = rsaPublicKey.getModulus().bitLength();
} else if (publicKey instanceof ECPublicKey) {
ECPublicKey ecPublicKey = (ECPublicKey) publicKey;
keySize = ecPublicKey.getParams().getCurve().getField().getFieldSize();
}
return keySize;
}
use of de.carne.check.Nullable in project certmgr by hdecarne.
the class CertExportController method onCmdExport.
@SuppressWarnings("unused")
@FXML
void onCmdExport(ActionEvent evt) {
try {
CertWriter exportFormat = validateAndGetFormat();
boolean encrypt = this.ctlEncryptOption.isSelected();
boolean exportCert = this.ctlExportCertOption.isSelected();
boolean exportChain = this.ctlExportChainOption.isSelected();
boolean exportChainRoot = this.ctlExportChainRootOption.isSelected();
boolean exportKey = this.ctlExportKeyOption.isSelected();
boolean exportCSR = this.ctlExportCSROption.isSelected();
boolean exportCRL = this.ctlExportCRLOption.isSelected();
if (this.ctlFileDestinationOption.isSelected()) {
Path exportFile = validateFileDestinationInput();
getExecutorService().submit(new ExportTask<Path>(exportCert, exportChain, exportChainRoot, exportKey, exportCSR, exportCRL, exportFormat, exportFile, encrypt) {
@Override
protected void export(CertWriter format, @Nullable Path param, CertObjectStore exportObjects, boolean encryptExport) throws IOException {
exportToFile(format, Check.notNull(param), exportObjects, encryptExport);
}
});
} else if (this.ctlDirectoryDestinationOption.isSelected()) {
Path exportDirectory = validateDirectoryDestinationInput();
getExecutorService().submit(new ExportTask<Path>(exportCert, exportChain, exportChainRoot, exportKey, exportCSR, exportCRL, exportFormat, exportDirectory, encrypt) {
@Override
protected void export(CertWriter format, @Nullable Path param, CertObjectStore exportObjects, boolean encryptExport) throws IOException {
exportToDirectory(format, Check.notNull(param), exportObjects, encryptExport);
}
});
} else if (this.ctlClipboardDestinationOption.isSelected()) {
getExecutorService().submit(new ExportTask<Void>(exportCert, exportChain, exportChainRoot, exportKey, exportCSR, exportCRL, exportFormat, null, encrypt) {
@Override
protected void export(CertWriter format, @Nullable Void param, CertObjectStore exportObjects, boolean encryptExport) throws IOException {
exportToClipboard(format, exportObjects, encryptExport);
}
});
}
} catch (ValidationException e) {
ValidationAlerts.error(e).showAndWait();
}
}
use of de.carne.check.Nullable in project certmgr by hdecarne.
the class SignatureAlgorithm method getDefaultSet.
/**
* Get the available signature algorithms.
*
* @param keyPairAlgorithm The key pair algorithm to get the signature algorithms for.
* @param defaultHint The default to return (may be {@code null}). If this algorithm is contained in the default
* set, it is also set as the default.
* @param expertMode Whether only standard algorithms are considered ({@code false}) or all algorithms available on
* the current platform ({@code true}).
* @return The available signature algorithms
*/
public static DefaultSet<SignatureAlgorithm> getDefaultSet(String keyPairAlgorithm, @Nullable String defaultHint, boolean expertMode) {
DefaultSet<SignatureAlgorithm> signatureAlgorithms = new DefaultSet<>();
DefaultSet<String> defaultNames = SecurityDefaults.getSignatureAlgorithmNames(keyPairAlgorithm);
@Nullable String defaultName = (defaultHint != null && defaultNames.contains(defaultHint) ? defaultHint : defaultNames.getDefault());
if (defaultName != null) {
defaultName = defaultName.toUpperCase();
}
for (Provider provider : SecurityDefaults.getProviders(expertMode)) {
for (Provider.Service service : provider.getServices()) {
if (!SERVICE_TYPE_SIGNATURE.equals(service.getType())) {
continue;
}
String upperCaseAlgorithm = service.getAlgorithm().toUpperCase();
if (!expertMode && !defaultNames.contains(upperCaseAlgorithm)) {
continue;
}
SignatureAlgorithm signatureAlgorithm = (expertMode ? new ExpertKeyPairAlgorithm(service) : new StandardKeyPairAlgorithm(service));
if (upperCaseAlgorithm.equals(defaultName)) {
signatureAlgorithms.addDefault(signatureAlgorithm);
} else {
signatureAlgorithms.add(signatureAlgorithm);
}
}
}
return signatureAlgorithms;
}
Aggregations