/** * This method returns the public algorithm extracted from public key infrastructure. (ex: RSA) * * @param publicKey * @return */ public static String getPublicKeyEncryptionAlgo(final Key publicKey) { String publicKeyAlgorithm = "?"; // (List of different public key implementations with instanceOf test removed) publicKeyAlgorithm = publicKey.getAlgorithm(); if (!"?".equals(publicKeyAlgorithm)) { try { publicKeyAlgorithm = EncryptionAlgorithm.forName(publicKeyAlgorithm).getName(); } catch (DSSException e) { LOG.error(e.getMessage()); } } return publicKeyAlgorithm; }
/** * Returns the encryption algorithm associated to the given JCE name. * * @param name * @param defaultValue * @return */ public static EncryptionAlgorithm forName(final String name, final EncryptionAlgorithm defaultValue) { // To be checked if ECC exists also . if ("EC".equals(name) || "ECC".equals(name)) { return ECDSA; } try { final EncryptionAlgorithm encryptionAlgorithm = valueOf(name); return encryptionAlgorithm; } catch (Exception e) { return defaultValue; } }
private static Map<String, EncryptionAlgorithm> registerOIDAlgorithms() { Map<String, EncryptionAlgorithm> map = new HashMap<String, EncryptionAlgorithm>(); for (EncryptionAlgorithm encryptionAlgorithm : values()) { map.put(encryptionAlgorithm.oid, encryptionAlgorithm); } return map; } }
/** * For given encryption algorithm & digest algorithm this function returns the signature algorithm. * * @param encryptionAlgorithm * @param digestAlgorithm * @return */ public static SignatureAlgorithm getAlgorithm(final EncryptionAlgorithm encryptionAlgorithm, final DigestAlgorithm digestAlgorithm) { String digestAlgorithm_ = digestAlgorithm.getName(); digestAlgorithm_ = digestAlgorithm_.replace("-", ""); final String javaName = digestAlgorithm_ + "with" + encryptionAlgorithm.getName(); return JAVA_ALGORITHMS.get(javaName); }
/** * This method returns the {@code EncryptionAlgorithm} of the first signature. * * @return The {@code EncryptionAlgorithm} of the first signature */ public EncryptionAlgorithm getSignatureEncryptionAlgorithm() { final String signatureEncryptionAlgorithmName = getValue("/DiagnosticData/Signature[1]/BasicSignature/EncryptionAlgoUsedToSignThisToken/text()"); final EncryptionAlgorithm signatureEncryptionAlgorithm = EncryptionAlgorithm.forName(signatureEncryptionAlgorithmName, null); return signatureEncryptionAlgorithm; }
return EncryptionAlgorithm.forOID(oid); } catch (RuntimeException e) {
private static void prepareKeyParameters(SignatureParameters parameters, WsParameters wsParameters) { final String signatureLevelString = parameters.getSignatureLevel().name(); final SignatureLevel signatureLevel = SignatureLevel.fromValue(signatureLevelString); wsParameters.setSignatureLevel(signatureLevel); final String signaturePackagingString = parameters.getSignaturePackaging().name(); final SignaturePackaging signaturePackaging = SignaturePackaging.valueOf(signaturePackagingString); wsParameters.setSignaturePackaging(signaturePackaging); final String encryptionAlgorithmString = parameters.getEncryptionAlgorithm().name(); final EncryptionAlgorithm encryptionAlgorithm = EncryptionAlgorithm.fromValue(encryptionAlgorithmString); wsParameters.setEncryptionAlgorithm(encryptionAlgorithm); // System.out.println("####>: " + parameters.getDigestAlgorithm()); final String digestAlgorithmString = parameters.getDigestAlgorithm().name(); final DigestAlgorithm digestAlgorithm = DigestAlgorithm.fromValue(digestAlgorithmString); wsParameters.setDigestAlgorithm(digestAlgorithm); final XMLGregorianCalendar xmlGregorianCalendar = DSSXMLUtils.createXMLGregorianCalendar(new Date()); wsParameters.setSigningDate(xmlGregorianCalendar); final byte[] encoded = DSSUtils.getEncoded(parameters.getSigningCertificate()); wsParameters.setSigningCertificateBytes(encoded); }
@Override public EncryptionAlgorithm getEncryptionAlgorithm() throws DSSException { if (privateKey instanceof RSAPrivateKey) { return EncryptionAlgorithm.RSA; } else if (privateKey instanceof DSAPrivateKey) { return EncryptionAlgorithm.DSA; } else if (privateKey instanceof ECPrivateKey) { return EncryptionAlgorithm.ECDSA; } else if (EncryptionAlgorithm.RSA.getName().equals(privateKey.getAlgorithm())) { return EncryptionAlgorithm.RSA; } else if (EncryptionAlgorithm.DSA.getName().equals(privateKey.getAlgorithm())) { return EncryptionAlgorithm.DSA; } else if (EncryptionAlgorithm.ECDSA.getName().equals(privateKey.getAlgorithm())) { return EncryptionAlgorithm.ECDSA; } else { throw new DSSException("Don't find algorithm for PrivateKey of type " + privateKey.getClass()); } } }
/** * This method returns the {@code DigestAlgorithm} for the given signature. * * @param signatureId The identifier of the signature, for which the algorithm is sought. * @return The {@code DigestAlgorithm} for the given signature */ public EncryptionAlgorithm getSignatureEncryptionAlgorithm(final String signatureId) { final String signatureEncryptionAlgorithmName = getValue("/DiagnosticData/Signature[@Id='%s']/BasicSignature/EncryptionAlgoUsedToSignThisToken/text()", signatureId); final EncryptionAlgorithm signatureEncryptionAlgorithm = EncryptionAlgorithm.forName(signatureEncryptionAlgorithmName); return signatureEncryptionAlgorithm; }
final SignatureAlgorithm revocationSignatureAlgo = revocationToken.getSignatureAlgorithm(); final boolean unknownAlgorithm = revocationSignatureAlgo == null; final String encryptionAlgorithmName = unknownAlgorithm ? "?" : revocationSignatureAlgo.getEncryptionAlgorithm().getName(); xmlBasicSignatureType.setEncryptionAlgoUsedToSignThisToken(encryptionAlgorithmName); final String keyLength = revocationToken.getKeyLength();
/** * This method sets the private key entry used to create the signature. Note that the certificate chain is reset, the encryption algorithm is set and the signature algorithm * is updated. * * @param privateKeyEntry the private key entry used to sign? */ public void setPrivateKeyEntry(final DSSPrivateKeyEntry privateKeyEntry) { this.privateKeyEntry = privateKeyEntry; // When the private key entry is set the certificate chain is reset certificateChain.clear(); setSigningCertificate(privateKeyEntry.getCertificate()); setCertificateChain(privateKeyEntry.getCertificateChain()); final String encryptionAlgorithmName = this.signingCertificate.getPublicKey().getAlgorithm(); this.encryptionAlgorithm = EncryptionAlgorithm.forName(encryptionAlgorithmName); this.signatureAlgorithm = SignatureAlgorithm.getAlgorithm(this.encryptionAlgorithm, this.digestAlgorithm); }
/** * Returns the encryption algorithm associated to the given JCE name. * * @param name * @return */ public static EncryptionAlgorithm forName(final String name) { // To be checked if ECC exists also . if ("EC".equals(name) || "ECC".equals(name)) { return ECDSA; } try { return valueOf(name); } catch (Exception e) { } throw new DSSException("Unsupported algorithm: " + name); }
xmlBasicSignatureType.setEncryptionAlgoUsedToSignThisToken(signatureAlgorithm.getEncryptionAlgorithm().getName()); final String keyLength = certToken.getKeyLength(); xmlBasicSignatureType.setKeyLengthUsedToSignThisToken(keyLength);
@Override public boolean isSignedBy(final CertificateToken issuerToken) { if (this.issuerToken != null) { return this.issuerToken.equals(issuerToken); } final TimestampValidation timestampValidation = validateTimestampToken(timeStamp, issuerToken); final TimestampValidity timestampValidity = timestampValidation.getValidity(); signatureInvalidityReason = timestampValidity.name(); signatureValid = timestampValidation.isValid(); if (signatureValid) { this.issuerToken = issuerToken; issuerX500Principal = issuerToken.getSubjectX500Principal(); final String algorithm = issuerToken.getPublicKey().getAlgorithm(); final EncryptionAlgorithm encryptionAlgorithm = EncryptionAlgorithm.forName(algorithm); final AlgorithmIdentifier hashAlgorithm = timeStamp.getTimeStampInfo().getHashAlgorithm(); final DigestAlgorithm digestAlgorithm = DigestAlgorithm.forOID(hashAlgorithm.getAlgorithm()); algorithmUsedToSignToken = SignatureAlgorithm.getAlgorithm(encryptionAlgorithm, digestAlgorithm); } return signatureValid; }
final String encryptionAlgorithmString = encryptionAlgorithm == null ? "?" : encryptionAlgorithm.getName(); xmlBasicSignature.setEncryptionAlgoUsedToSignThisToken(encryptionAlgorithmString);
if (signatureAlgorithm != null) { xmlBasicSignatureType.setEncryptionAlgoUsedToSignThisToken(signatureAlgorithm.getEncryptionAlgorithm().getName()); xmlBasicSignatureType.setDigestAlgoUsedToSignThisToken(signatureAlgorithm.getDigestAlgorithm().getName());