SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref)); KeyInfoFactory kif = fac.getKeyInfoFactory(); KeyValue kv = kif.newKeyValue(pub); KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));
/** * * @return KeyInfoFactory */ public KeyInfoFactory getKeyInfoFactory() { try { return getSignatureFactory().getKeyInfoFactory(); }catch(Exception ex) { logger.log(Level.SEVERE,"WSS1323.dsig.keyinfo.factory",ex); throw new RuntimeException(ex); } }
@Nonnull public KeyInfo createKeyInfo (@Nonnull final X509Certificate aCertificate) throws KeyException { // Create the KeyInfo containing the X509Data. final KeyInfoFactory aKeyInfoFactory = m_aSignatureFactory.getKeyInfoFactory (); // The X509 certificate subject name and the certificate itself final ICommonsList <Object> aX509Content = new CommonsArrayList <> (aCertificate.getSubjectX500Principal () .getName (), aCertificate); final X509Data aX509Data = aKeyInfoFactory.newX509Data (aX509Content); // The public key itself final KeyValue aKeyValue = aKeyInfoFactory.newKeyValue (aCertificate.getPublicKey ()); // Collect certificate and key value in key info return aKeyInfoFactory.newKeyInfo (new CommonsArrayList <> (aX509Data, aKeyValue)); }
private KeyInfo newKeyInfo(final X509Certificate[] certChain, final String keyInfoId) throws KeyException { final KeyInfoFactory keyInfoFactory = getXMLSignatureFactory().getKeyInfoFactory(); final List<X509Certificate> x509DataList = new ArrayList<>(); if (!XmlWrappedKeyInfo.PUBLIC_KEY.equals(getXmlWrappedKeyInfo())) { for (final X509Certificate cert : certChain) { x509DataList.add(cert); } } final List<XMLStructure> newList = new ArrayList<>(); newList.add(keyInfoFactory.newKeyValue(certChain[0].getPublicKey())); newList.add(keyInfoFactory.newX509Data(x509DataList)); return keyInfoFactory.newKeyInfo(newList, keyInfoId); }
protected KeyInfo newKeyInfo(X509Certificate certificate, String keyInfoId) throws KeyException { KeyInfoFactory keyInfoFactory = getXMLSignatureFactory().getKeyInfoFactory(); KeyValue keyValue = keyInfoFactory.newKeyValue(certificate.getPublicKey()); List<Object> x509DataList = new ArrayList<Object>(); if (!XmlWrappedKeyInfo.PUBLIC_KEY.equals(getXmlWrappedKeyInfo())) { x509DataList.add(certificate); } X509IssuerSerial x509IssuerSerial = keyInfoFactory.newX509IssuerSerial(certificate .getIssuerDN().getName(), certificate.getSerialNumber()); x509DataList.add(certificate.getSubjectX500Principal().getName("RFC1779")); x509DataList.add(x509IssuerSerial); X509Data x509Data = keyInfoFactory.newX509Data(x509DataList); List<Object> newList = new ArrayList<Object>(); newList.add(keyValue); newList.add(x509Data); return keyInfoFactory.newKeyInfo(newList, keyInfoId); }
private static KeyInfo createKeyInfo(PublicKey publicKey, X509Certificate x509Certificate) throws KeyException { KeyInfoFactory keyInfoFactory = fac.getKeyInfoFactory(); KeyInfo keyInfo = null; KeyValue keyValue = null; //Just with public key if (publicKey != null) { keyValue = keyInfoFactory.newKeyValue(publicKey); keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(keyValue)); } if (x509Certificate != null) { List x509list = new ArrayList(); x509list.add(x509Certificate); X509Data x509Data = keyInfoFactory.newX509Data(x509list); List items = new ArrayList(); items.add(x509Data); if (keyValue != null) { items.add(keyValue); } keyInfo = keyInfoFactory.newKeyInfo(items); } return keyInfo; } }
/** * Get the KeyValue object from the KeyInfo DOM element if it exists */ public static KeyValue getKeyValue(Element keyInfoElement, XMLSignatureFactory signatureFactory) throws MarshalException { XMLStructure keyInfoStructure = new DOMStructure(keyInfoElement); KeyInfoFactory keyInfoFactory = signatureFactory.getKeyInfoFactory(); KeyInfo keyInfo = keyInfoFactory.unmarshalKeyInfo(keyInfoStructure); List<?> list = keyInfo.getContent(); for (int i = 0; i < list.size(); i++) { XMLStructure xmlStructure = (XMLStructure) list.get(i); if (xmlStructure instanceof KeyValue) { return (KeyValue)xmlStructure; } } return null; }
private static KeyInfo createKeyInfo(PublicKey publicKey, X509Certificate x509Certificate) throws KeyException { KeyInfoFactory keyInfoFactory = fac.getKeyInfoFactory(); KeyInfo keyInfo = null; KeyValue keyValue = null; //Just with public key if (publicKey != null) { keyValue = keyInfoFactory.newKeyValue(publicKey); keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(keyValue)); } if (x509Certificate != null) { List x509list = new ArrayList(); x509list.add(x509Certificate); X509Data x509Data = keyInfoFactory.newX509Data(x509list); List items = new ArrayList(); items.add(x509Data); if (keyValue != null) { items.add(keyValue); } keyInfo = keyInfoFactory.newKeyInfo(items); } return keyInfo; } }
private static KeyInfo createKeyInfo(PublicKey publicKey, X509Certificate x509Certificate) throws KeyException { KeyInfoFactory keyInfoFactory = fac.getKeyInfoFactory(); KeyInfo keyInfo = null; KeyValue keyValue = null; //Just with public key if(publicKey != null){ keyValue = keyInfoFactory.newKeyValue(publicKey); keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(keyValue)); } if(x509Certificate != null){ List x509list = new ArrayList(); x509list.add(x509Certificate); X509Data x509Data = keyInfoFactory.newX509Data(x509list); List items = new ArrayList(); items.add(x509Data); if(keyValue != null){ items.add(keyValue); } keyInfo = keyInfoFactory.newKeyInfo(items); } return keyInfo; } }
private Document assinar(String xml, char[] senha, FileInputStream keyStoreStream) throws ParserConfigurationException, SAXException, IOException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, KeyStoreException, CertificateException, UnrecoverableEntryException { DocumentBuilder builder = getDocumentBuilder(); Document doc = builder.parse(new InputSource(new StringReader(xml))); XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM"); ArrayList<Transform> transformList = getTransformList(fac); KeyStore ks = KeyStore.getInstance("pkcs12"); ks.load(keyStoreStream, senha); String alias = ks.aliases().nextElement(); KeyStore.PrivateKeyEntry keyEntry = (KeyStore.PrivateKeyEntry) ks.getEntry(alias, new KeyStore.PasswordProtection(senha)); X509Certificate cert = (X509Certificate) keyEntry.getCertificate(); List<Serializable> x509Content = new ArrayList<>(); x509Content.add(cert); KeyInfoFactory kif = fac.getKeyInfoFactory(); X509Data xd = kif.newX509Data(x509Content); KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd)); assinarTag(fac, transformList, keyEntry.getPrivateKey(), ki, doc, 0); return doc; }
protected void marshalKeyInfo(WSDocInfo wsDocInfo) throws WSSecurityException { List<XMLStructure> kiChildren = null; if (customKeyInfoElement == null) { XMLStructure structure = new DOMStructure(secRef.getElement()); wsDocInfo.addTokenElement(secRef.getElement(), false); kiChildren = Collections.singletonList(structure); } else { Node kiChild = customKeyInfoElement.getFirstChild(); kiChildren = new ArrayList<>(); while (kiChild != null) { kiChildren.add(new DOMStructure(kiChild)); kiChild = kiChild.getNextSibling(); } } KeyInfoFactory keyInfoFactory = signatureFactory.getKeyInfoFactory(); keyInfo = keyInfoFactory.newKeyInfo(kiChildren, keyInfoUri); }
private static void loadCertificates(ConfiguracoesNfe config, XMLSignatureFactory signatureFactory) throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableEntryException, CertificadoException { Certificado certificado = config.getCertificado(); KeyStore keyStore = CertificadoService.getKeyStore(certificado); KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(certificado.getNome(), new KeyStore.PasswordProtection(certificado.getSenha().toCharArray())); privateKey = pkEntry.getPrivateKey(); KeyInfoFactory keyInfoFactory = signatureFactory.getKeyInfoFactory(); List<X509Certificate> x509Content = new ArrayList<X509Certificate>(); x509Content.add(CertificadoService.getCertificate(certificado, keyStore)); X509Data x509Data = keyInfoFactory.newX509Data(x509Content); keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(x509Data)); }
public KeyInfo newKeyInfo(X509Certificate certificate, String keyInfoId) throws KeyException { KeyInfoFactory kif = getXMLSignatureFactory().getKeyInfoFactory(); if(XmlWrappedKeyInfo.PUBLIC_KEY.equals(getXmlWrappedKeyInfo())) { KeyValue kv = kif.newKeyValue(certificate.getPublicKey()); return kif.newKeyInfo(Collections.singletonList(kv), keyInfoId); } else { X509Data certData = kif.newX509Data(Collections.singletonList(certificate)); return kif.newKeyInfo(Collections.singletonList(certData), keyInfoId); } }
/** * 创建KeyInfo * * @param config * @return * @throws Exception */ protected KeyInfo createKeyInfo(XmlSignatureConfig config) throws Exception { KeyPair keyPair = getXmlSignatureManager().getKeyPair(config.getUserId()); KeyInfoFactory keyInfoFac = xmlSignatureFactory.getKeyInfoFactory(); KeyValue keyValue = keyInfoFac.newKeyValue(keyPair.getPublic()); KeyInfo keyInfo = keyInfoFac.newKeyInfo(Collections .singletonList(keyValue)); return keyInfo; }
public static void signDOM(Node node, PrivateKey privateKey, Certificate origCert) { XMLSignatureFactory fac = initXMLSigFactory(); X509Certificate cert = (X509Certificate) origCert; // Create the KeyInfo containing the X509Data. KeyInfoFactory kif = fac.getKeyInfoFactory(); List<Object> x509Content = new ArrayList<Object>(); x509Content.add(cert.getSubjectX500Principal().getName()); x509Content.add(cert); X509Data xd = kif.newX509Data(x509Content); KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd)); // Create a DOMSignContext and specify the RSA PrivateKey and // location of the resulting XMLSignature's parent element. DOMSignContext dsc = new DOMSignContext(privateKey, node); dsc.putNamespacePrefix("http://www.w3.org/2000/09/xmldsig#", "ns2"); // Create the XMLSignature, but don't sign it yet. try { SignedInfo si = initSignedInfo(fac); XMLSignature signature = fac.newXMLSignature(si, ki); // Marshal, generate, and sign the enveloped signature. signature.sign(dsc); } catch (Exception e) { throw new RuntimeException(e); } }
transforms.add(signatureFactory.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)); transforms.add(signatureFactory.newTransform(AssinaturaDigital.C14N_TRANSFORM_METHOD, (TransformParameterSpec) null)); final KeyInfoFactory keyInfoFactory = signatureFactory.getKeyInfoFactory(); final X509Data x509Data = keyInfoFactory.newX509Data(Collections.singletonList((X509Certificate) keyEntry.getCertificate())); final KeyInfo keyInfo = keyInfoFactory.newKeyInfo(Collections.singletonList(x509Data));
public RequestSigner ( final Configuration configuration ) throws Exception { this.fac = XMLSignatureFactory.getInstance ( "DOM" ); this.md = this.fac.newDigestMethod ( configuration.getDigestMethod (), null ); this.kif = this.fac.getKeyInfoFactory (); this.t = this.fac.newTransform ( Transform.ENVELOPED, (TransformParameterSpec)null ); this.ref = this.fac.newReference ( "", this.md, Collections.singletonList ( this.t ), null, null ); this.cm = this.fac.newCanonicalizationMethod ( CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec)null ); }
public RequestSigner ( final Configuration configuration ) throws Exception { this.fac = XMLSignatureFactory.getInstance ( "DOM" ); this.md = this.fac.newDigestMethod ( configuration.getDigestMethod (), null ); this.kif = this.fac.getKeyInfoFactory (); this.t = this.fac.newTransform ( Transform.ENVELOPED, (TransformParameterSpec)null ); this.ref = this.fac.newReference ( "", this.md, Collections.singletonList ( this.t ), null, null ); this.cm = this.fac.newCanonicalizationMethod ( CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec)null ); }
SignedInfo si = fac.newSignedInfo(fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref)); KeyInfoFactory kif = fac.getKeyInfoFactory(); KeyValue kv = kif.newKeyValue(pub); KeyInfo ki = kif.newKeyInfo(Collections.singletonList(kv));
/** * Sign the XML document using xmldsig. * * @param document * the document to sign; it will be modified by the method. * @param publicKey * the public key from the key pair to sign the document. * @param privateKey * the private key from the key pair to sign the document. * @return the signed document for chaining. */ public static Document sign(Document document, RSAPublicKey publicKey, RSAPrivateKey privateKey) { XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM"); KeyInfoFactory keyInfoFactory = fac.getKeyInfoFactory(); try { Reference ref = fac.newReference("", fac.newDigestMethod(DigestMethod.SHA1, null), Collections.singletonList(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)), null, null); SignedInfo si = fac.newSignedInfo( fac.newCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE, (C14NMethodParameterSpec) null), fac.newSignatureMethod(SignatureMethod.RSA_SHA1, null), Collections.singletonList(ref)); DOMSignContext dsc = new DOMSignContext(privateKey, document.getDocumentElement()); KeyValue keyValue = keyInfoFactory.newKeyValue(publicKey); KeyInfo ki = keyInfoFactory.newKeyInfo(Collections.singletonList(keyValue)); XMLSignature signature = fac.newXMLSignature(si, ki); signature.sign(dsc); } catch (Exception e) { Logger.warn("Error while signing an XML document.", e); } return document; }