1 17 package com.sun.org.apache.xml.internal.security.encryption; 18 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.io.StringReader ; 23 import java.io.UnsupportedEncodingException ; 24 import java.security.InvalidAlgorithmParameterException ; 25 import java.security.InvalidKeyException ; 26 import java.security.Key ; 27 import java.security.NoSuchAlgorithmException ; 28 import java.security.NoSuchProviderException ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 33 import javax.crypto.BadPaddingException; 34 import javax.crypto.Cipher; 35 import javax.crypto.IllegalBlockSizeException; 36 import javax.crypto.NoSuchPaddingException; 37 import javax.crypto.spec.IvParameterSpec; 38 import javax.xml.parsers.DocumentBuilder ; 39 import javax.xml.parsers.DocumentBuilderFactory ; 40 import javax.xml.parsers.ParserConfigurationException ; 41 42 import com.sun.org.apache.xml.internal.security.algorithms.JCEMapper; 43 import com.sun.org.apache.xml.internal.security.algorithms.MessageDigestAlgorithm; 44 import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer; 45 import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException; 46 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; 47 import com.sun.org.apache.xml.internal.security.keys.KeyInfo; 48 import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverException; 49 import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.EncryptedKeyResolver; 50 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException; 51 import com.sun.org.apache.xml.internal.security.transforms.InvalidTransformException; 52 import com.sun.org.apache.xml.internal.security.transforms.TransformationException; 53 import com.sun.org.apache.xml.internal.security.utils.Base64; 54 import com.sun.org.apache.xml.internal.security.utils.Constants; 55 import com.sun.org.apache.xml.internal.security.utils.ElementProxy; 56 import com.sun.org.apache.xml.internal.security.utils.EncryptionConstants; 57 import com.sun.org.apache.xml.internal.security.utils.XMLUtils; 58 import com.sun.org.apache.xml.internal.utils.URI; 59 import org.w3c.dom.Attr ; 60 import org.w3c.dom.Document ; 61 import org.w3c.dom.DocumentFragment ; 62 import org.w3c.dom.Element ; 63 import org.w3c.dom.NamedNodeMap ; 64 import org.w3c.dom.Node ; 65 import org.w3c.dom.NodeList ; 66 import org.xml.sax.InputSource ; 67 import org.xml.sax.SAXException ; 68 69 70 79 public class XMLCipher { 80 81 private static java.util.logging.Logger logger = 82 java.util.logging.Logger.getLogger(XMLCipher.class.getName()); 83 84 86 public static final String TRIPLEDES = 87 EncryptionConstants.ALGO_ID_BLOCKCIPHER_TRIPLEDES; 88 89 public static final String AES_128 = 90 EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128; 91 92 public static final String AES_256 = 93 EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES256; 94 95 public static final String AES_192 = 96 EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES192; 97 98 public static final String RSA_v1dot5 = 99 EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSA15; 100 101 public static final String RSA_OAEP = 102 EncryptionConstants.ALGO_ID_KEYTRANSPORT_RSAOAEP; 103 104 public static final String DIFFIE_HELLMAN = 105 EncryptionConstants.ALGO_ID_KEYAGREEMENT_DH; 106 107 public static final String TRIPLEDES_KeyWrap = 108 EncryptionConstants.ALGO_ID_KEYWRAP_TRIPLEDES; 109 110 public static final String AES_128_KeyWrap = 111 EncryptionConstants.ALGO_ID_KEYWRAP_AES128; 112 113 public static final String AES_256_KeyWrap = 114 EncryptionConstants.ALGO_ID_KEYWRAP_AES256; 115 116 public static final String AES_192_KeyWrap = 117 EncryptionConstants.ALGO_ID_KEYWRAP_AES192; 118 119 public static final String SHA1 = 120 Constants.ALGO_ID_DIGEST_SHA1; 121 122 public static final String SHA256 = 123 MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA256; 124 125 public static final String SHA512 = 126 MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA512; 127 128 public static final String RIPEMD_160 = 129 MessageDigestAlgorithm.ALGO_ID_DIGEST_RIPEMD160; 130 131 public static final String XML_DSIG = 132 Constants.SignatureSpecNS; 133 134 public static final String N14C_XML = 135 Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS; 136 137 public static final String N14C_XML_WITH_COMMENTS = 138 Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS; 139 140 public static final String EXCL_XML_N14C = 141 Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS; 142 143 public static final String EXCL_XML_N14C_WITH_COMMENTS = 144 Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS; 145 146 public static final String BASE64_ENCODING = 147 com.sun.org.apache.xml.internal.security.transforms.Transforms.TRANSFORM_BASE64_DECODE; 148 150 151 public static final int ENCRYPT_MODE = Cipher.ENCRYPT_MODE; 152 153 public static final int DECRYPT_MODE = Cipher.DECRYPT_MODE; 154 155 public static final int UNWRAP_MODE = Cipher.UNWRAP_MODE; 156 157 public static final int WRAP_MODE = Cipher.WRAP_MODE; 158 159 private static final String ENC_ALGORITHMS = TRIPLEDES + "\n" + 160 AES_128 + "\n" + AES_256 + "\n" + AES_192 + "\n" + RSA_v1dot5 + "\n" + 161 RSA_OAEP + "\n" + TRIPLEDES_KeyWrap + "\n" + AES_128_KeyWrap + "\n" + 162 AES_256_KeyWrap + "\n" + AES_192_KeyWrap+ "\n"; 163 164 165 private Cipher _contextCipher; 166 167 private int _cipherMode = Integer.MIN_VALUE; 168 169 private String _algorithm = null; 170 171 private String _requestedJCEProvider = null; 172 173 private Canonicalizer _canon; 174 175 private Document _contextDocument; 176 177 private Factory _factory; 178 179 private Serializer _serializer; 180 181 182 private Key _key; 183 185 private Key _kek; 186 187 190 private EncryptedKey _ek; 191 192 195 private EncryptedData _ed; 196 197 202 private XMLCipher() { 203 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Constructing XMLCipher..."); 204 205 _factory = new Factory (); 206 _serializer = new Serializer(); 207 208 } 209 210 217 private static boolean isValidEncryptionAlgorithm(String algorithm) { 218 boolean result = ( 219 algorithm.equals(TRIPLEDES) || 220 algorithm.equals(AES_128) || 221 algorithm.equals(AES_256) || 222 algorithm.equals(AES_192) || 223 algorithm.equals(RSA_v1dot5) || 224 algorithm.equals(RSA_OAEP) || 225 algorithm.equals(TRIPLEDES_KeyWrap) || 226 algorithm.equals(AES_128_KeyWrap) || 227 algorithm.equals(AES_256_KeyWrap) || 228 algorithm.equals(AES_192_KeyWrap) 229 ); 230 231 return (result); 232 } 233 234 262 public static XMLCipher getInstance(String transformation) throws 263 XMLEncryptionException { 264 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Getting XMLCipher..."); 266 if (null == transformation) 267 logger.log(java.util.logging.Level.SEVERE, "Transformation unexpectedly null..."); 268 if(!isValidEncryptionAlgorithm(transformation)) 269 logger.log(java.util.logging.Level.WARNING, "Algorithm non-standard, expected one of " + ENC_ALGORITHMS); 270 271 XMLCipher instance = new XMLCipher(); 272 273 instance._algorithm = transformation; 274 instance._key = null; 275 instance._kek = null; 276 277 278 280 281 try { 282 instance._canon = Canonicalizer.getInstance 283 (Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS); 284 285 } catch (InvalidCanonicalizerException ice) { 286 throw new XMLEncryptionException("empty", ice); 287 } 288 289 String jceAlgorithm = JCEMapper.translateURItoJCEID(transformation); 290 291 try { 292 instance._contextCipher = Cipher.getInstance(jceAlgorithm); 293 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "cihper.algoritm = " + 294 instance._contextCipher.getAlgorithm()); 295 } catch (NoSuchAlgorithmException nsae) { 296 throw new XMLEncryptionException("empty", nsae); 297 } catch (NoSuchPaddingException nspe) { 298 throw new XMLEncryptionException("empty", nspe); 299 } 300 301 return (instance); 302 } 303 304 public static XMLCipher getInstance(String transformation,Cipher cipher) throws 305 XMLEncryptionException { 306 logger.log(java.util.logging.Level.FINE, "Getting XMLCipher..."); 308 if (null == transformation) 309 logger.log(java.util.logging.Level.SEVERE, "Transformation unexpectedly null..."); 310 if(!isValidEncryptionAlgorithm(transformation)) 311 logger.log(java.util.logging.Level.WARNING, "Algorithm non-standard, expected one of " + ENC_ALGORITHMS); 312 313 XMLCipher instance = new XMLCipher(); 314 315 instance._algorithm = transformation; 316 instance._key = null; 317 instance._kek = null; 318 319 320 322 323 try { 324 instance._canon = Canonicalizer.getInstance 325 (Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS); 326 327 } catch (InvalidCanonicalizerException ice) { 328 throw new XMLEncryptionException("empty", ice); 329 } 330 331 String jceAlgorithm = JCEMapper.translateURItoJCEID(transformation); 332 333 try { 334 instance._contextCipher = cipher; 335 logger.log(java.util.logging.Level.FINE, "cihper.algoritm = " + 337 instance._contextCipher.getAlgorithm()); 338 }catch(Exception ex) { 339 throw new XMLEncryptionException("empty", ex); 340 } 341 342 return (instance); 343 } 344 345 346 347 363 364 public static XMLCipher getInstance(String transformation, String canon) 365 throws XMLEncryptionException { 366 XMLCipher instance = XMLCipher.getInstance(transformation); 367 368 if (canon != null) { 369 try { 370 instance._canon = Canonicalizer.getInstance(canon); 371 } catch (InvalidCanonicalizerException ice) { 372 throw new XMLEncryptionException("empty", ice); 373 } 374 } 375 376 return instance; 377 } 378 379 380 391 392 public static XMLCipher getProviderInstance(String transformation, String provider) 393 throws XMLEncryptionException { 394 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Getting XMLCipher..."); 396 if (null == transformation) 397 logger.log(java.util.logging.Level.SEVERE, "Transformation unexpectedly null..."); 398 if(null == provider) 399 logger.log(java.util.logging.Level.SEVERE, "Provider unexpectedly null.."); 400 if("" == provider) 401 logger.log(java.util.logging.Level.SEVERE, "Provider's value unexpectedly not specified..."); 402 if(!isValidEncryptionAlgorithm(transformation)) 403 logger.log(java.util.logging.Level.WARNING, "Algorithm non-standard, expected one of " + ENC_ALGORITHMS); 404 405 XMLCipher instance = new XMLCipher(); 406 407 instance._algorithm = transformation; 408 instance._requestedJCEProvider = provider; 409 instance._key = null; 410 instance._kek = null; 411 412 414 415 try { 416 instance._canon = Canonicalizer.getInstance 417 (Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS); 418 } catch (InvalidCanonicalizerException ice) { 419 throw new XMLEncryptionException("empty", ice); 420 } 421 422 try { 423 String jceAlgorithm = 424 JCEMapper.translateURItoJCEID(transformation); 425 426 instance._contextCipher = Cipher.getInstance(jceAlgorithm, provider); 427 428 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "cipher._algorithm = " + 429 instance._contextCipher.getAlgorithm()); 430 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "provider.name = " + provider); 431 } catch (NoSuchAlgorithmException nsae) { 432 throw new XMLEncryptionException("empty", nsae); 433 } catch (NoSuchProviderException nspre) { 434 throw new XMLEncryptionException("empty", nspre); 435 } catch (NoSuchPaddingException nspe) { 436 throw new XMLEncryptionException("empty", nspe); 437 } 438 439 return (instance); 440 } 441 442 459 public static XMLCipher getProviderInstance( 460 String transformation, 461 String provider, 462 String canon) 463 throws XMLEncryptionException { 464 465 XMLCipher instance = XMLCipher.getProviderInstance(transformation, provider); 466 if (canon != null) { 467 try { 468 instance._canon = Canonicalizer.getInstance(canon); 469 } catch (InvalidCanonicalizerException ice) { 470 throw new XMLEncryptionException("empty", ice); 471 } 472 } 473 return instance; 474 } 475 476 485 486 public static XMLCipher getInstance() 487 throws XMLEncryptionException { 488 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Getting XMLCipher for no transformation..."); 490 491 XMLCipher instance = new XMLCipher(); 492 493 instance._algorithm = null; 494 instance._requestedJCEProvider = null; 495 instance._key = null; 496 instance._kek = null; 497 instance._contextCipher = null; 498 499 501 502 try { 503 instance._canon = Canonicalizer.getInstance 504 (Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS); 505 } catch (InvalidCanonicalizerException ice) { 506 throw new XMLEncryptionException("empty", ice); 507 } 508 509 return (instance); 510 } 511 512 526 527 public static XMLCipher getProviderInstance(String provider) 528 throws XMLEncryptionException { 529 531 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Getting XMLCipher, provider but no transformation"); 532 if(null == provider) 533 logger.log(java.util.logging.Level.SEVERE, "Provider unexpectedly null.."); 534 if("" == provider) 535 logger.log(java.util.logging.Level.SEVERE, "Provider's value unexpectedly not specified..."); 536 537 XMLCipher instance = new XMLCipher(); 538 539 instance._algorithm = null; 540 instance._requestedJCEProvider = provider; 541 instance._key = null; 542 instance._kek = null; 543 instance._contextCipher = null; 544 545 try { 546 instance._canon = Canonicalizer.getInstance 547 (Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS); 548 } catch (InvalidCanonicalizerException ice) { 549 throw new XMLEncryptionException("empty", ice); 550 } 551 552 return (instance); 553 } 554 555 575 public void init(int opmode, Key key) throws XMLEncryptionException { 576 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Initializing XMLCipher..."); 578 579 _ek = null; 580 _ed = null; 581 582 switch (opmode) { 583 584 case ENCRYPT_MODE : 585 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "opmode = ENCRYPT_MODE"); 586 _ed = createEncryptedData(CipherData.VALUE_TYPE, "NO VALUE YET"); 587 break; 588 case DECRYPT_MODE : 589 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "opmode = DECRYPT_MODE"); 590 break; 591 case WRAP_MODE : 592 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "opmode = WRAP_MODE"); 593 _ek = createEncryptedKey(CipherData.VALUE_TYPE, "NO VALUE YET"); 594 break; 595 case UNWRAP_MODE : 596 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "opmode = UNWRAP_MODE"); 597 break; 598 default : 599 logger.log(java.util.logging.Level.SEVERE, "Mode unexpectedly invalid"); 600 throw new XMLEncryptionException("Invalid mode in init"); 601 } 602 603 _cipherMode = opmode; 604 _key = key; 605 606 } 607 608 617 618 public EncryptedData getEncryptedData() { 619 620 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Returning EncryptedData"); 622 return _ed; 623 624 } 625 626 635 636 public EncryptedKey getEncryptedKey() { 637 638 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Returning EncryptedKey"); 640 return _ek; 641 } 642 643 653 654 public void setKEK(Key kek) { 655 656 _kek = kek; 657 658 } 659 660 673 674 public Element martial(EncryptedData encryptedData) { 675 676 return (_factory.toElement (encryptedData)); 677 678 } 679 680 693 694 public Element martial(EncryptedKey encryptedKey) { 695 696 return (_factory.toElement (encryptedKey)); 697 698 } 699 700 710 711 public Element martial(Document context, EncryptedData encryptedData) { 712 713 _contextDocument = context; 714 return (_factory.toElement (encryptedData)); 715 716 } 717 718 728 729 public Element martial(Document context, EncryptedKey encryptedKey) { 730 731 _contextDocument = context; 732 return (_factory.toElement (encryptedKey)); 733 734 } 735 736 747 748 private Document encryptElement(Element element) throws Exception { 749 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Encrypting element..."); 750 if(null == element) 751 logger.log(java.util.logging.Level.SEVERE, "Element unexpectedly null..."); 752 if(_cipherMode != ENCRYPT_MODE) 753 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "XMLCipher unexpectedly not in ENCRYPT_MODE..."); 754 755 if (_algorithm == null) { 756 throw new XMLEncryptionException("XMLCipher instance without transformation specified"); 757 } 758 encryptData(_contextDocument, element, false); 759 760 Element encryptedElement = _factory.toElement(_ed); 761 762 Node sourceParent = element.getParentNode(); 763 sourceParent.replaceChild(encryptedElement, element); 764 765 return (_contextDocument); 766 } 767 768 782 private Document encryptElementContent(Element element) throws 783 Exception { 784 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Encrypting element content..."); 785 if(null == element) 786 logger.log(java.util.logging.Level.SEVERE, "Element unexpectedly null..."); 787 if(_cipherMode != ENCRYPT_MODE) 788 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "XMLCipher unexpectedly not in ENCRYPT_MODE..."); 789 790 if (_algorithm == null) { 791 throw new XMLEncryptionException("XMLCipher instance without transformation specified"); 792 } 793 encryptData(_contextDocument, element, true); 794 795 Element encryptedElement = _factory.toElement(_ed); 796 797 removeContent(element); 798 element.appendChild(encryptedElement); 799 800 return (_contextDocument); 801 } 802 803 812 public Document doFinal(Document context, Document source) throws 813 Exception { 814 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Processing source document..."); 815 if(null == context) 816 logger.log(java.util.logging.Level.SEVERE, "Context document unexpectedly null..."); 817 if(null == source) 818 logger.log(java.util.logging.Level.SEVERE, "Source document unexpectedly null..."); 819 820 _contextDocument = context; 821 822 Document result = null; 823 824 switch (_cipherMode) { 825 case DECRYPT_MODE: 826 result = decryptElement(source.getDocumentElement()); 827 break; 828 case ENCRYPT_MODE: 829 result = encryptElement(source.getDocumentElement()); 830 break; 831 case UNWRAP_MODE: 832 break; 833 case WRAP_MODE: 834 break; 835 default: 836 throw new XMLEncryptionException( 837 "empty", new IllegalStateException ()); 838 } 839 840 return (result); 841 } 842 843 852 public Document doFinal(Document context, Element element) throws 853 Exception { 854 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Processing source element..."); 855 if(null == context) 856 logger.log(java.util.logging.Level.SEVERE, "Context document unexpectedly null..."); 857 if(null == element) 858 logger.log(java.util.logging.Level.SEVERE, "Source element unexpectedly null..."); 859 860 _contextDocument = context; 861 862 Document result = null; 863 864 switch (_cipherMode) { 865 case DECRYPT_MODE: 866 result = decryptElement(element); 867 break; 868 case ENCRYPT_MODE: 869 result = encryptElement(element); 870 break; 871 case UNWRAP_MODE: 872 break; 873 case WRAP_MODE: 874 break; 875 default: 876 throw new XMLEncryptionException( 877 "empty", new IllegalStateException ()); 878 } 879 880 return (result); 881 } 882 883 895 public Document doFinal(Document context, Element element, boolean content) 896 throws Exception { 897 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Processing source element..."); 898 if(null == context) 899 logger.log(java.util.logging.Level.SEVERE, "Context document unexpectedly null..."); 900 if(null == element) 901 logger.log(java.util.logging.Level.SEVERE, "Source element unexpectedly null..."); 902 903 _contextDocument = context; 904 905 Document result = null; 906 907 switch (_cipherMode) { 908 case DECRYPT_MODE: 909 if (content) { 910 result = decryptElementContent(element); 911 } else { 912 result = decryptElement(element); 913 } 914 break; 915 case ENCRYPT_MODE: 916 if (content) { 917 result = encryptElementContent(element); 918 } else { 919 result = encryptElement(element); 920 } 921 break; 922 case UNWRAP_MODE: 923 break; 924 case WRAP_MODE: 925 break; 926 default: 927 throw new XMLEncryptionException( 928 "empty", new IllegalStateException ()); 929 } 930 931 return (result); 932 } 933 934 946 public EncryptedData encryptData(Document context, Element element) throws 947 Exception { 948 return encryptData(context, element, false); 949 } 950 951 965 public EncryptedData encryptData(Document context, Element element, boolean contentMode) throws 966 Exception { 967 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Encrypting element..."); 968 if (null == context) 969 logger.log(java.util.logging.Level.SEVERE, "Context document unexpectedly null..."); 970 if (null == element) 971 logger.log(java.util.logging.Level.SEVERE, "Element unexpectedly null..."); 972 if (_cipherMode != ENCRYPT_MODE) 973 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "XMLCipher unexpectedly not in ENCRYPT_MODE..."); 974 975 _contextDocument = context; 976 977 if (_algorithm == null) { 978 throw new XMLEncryptionException("XMLCipher instance without transformation specified"); 979 } 980 981 String serializedOctets = null; 982 if (contentMode) { 983 NodeList children = element.getChildNodes(); 984 if ((null != children)) { 985 serializedOctets = _serializer.serialize(children); 986 } else { 987 Object exArgs[] = { "Element has no content." }; 988 throw new XMLEncryptionException("empty", exArgs); 989 } 990 } else { 991 serializedOctets = _serializer.serialize(element); 992 } 993 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Serialized octets:\n" + serializedOctets); 994 995 byte[] encryptedBytes = null; 996 997 Cipher c; 999 if (_contextCipher == null) { 1000 String jceAlgorithm = 1001 JCEMapper.translateURItoJCEID(_algorithm); 1002 1003 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "alg = " + jceAlgorithm); 1004 1005 try { 1006 if (_requestedJCEProvider == null) 1007 c = Cipher.getInstance(jceAlgorithm); 1008 else 1009 c = Cipher.getInstance(jceAlgorithm, _requestedJCEProvider); 1010 } catch (NoSuchAlgorithmException nsae) { 1011 throw new XMLEncryptionException("empty", nsae); 1012 } catch (NoSuchProviderException nspre) { 1013 throw new XMLEncryptionException("empty", nspre); 1014 } catch (NoSuchPaddingException nspae) { 1015 throw new XMLEncryptionException("empty", nspae); 1016 } 1017 } 1018 else { 1019 c = _contextCipher; 1020 } 1021 1023 try { 1024 c.init(_cipherMode, _key); 1027 } catch (InvalidKeyException ike) { 1028 throw new XMLEncryptionException("empty", ike); 1029 } 1030 1031 try { 1032 encryptedBytes = 1033 c.doFinal(serializedOctets.getBytes("UTF-8")); 1034 1035 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Expected cipher.outputSize = " + 1036 Integer.toString(c.getOutputSize( 1037 serializedOctets.getBytes().length))); 1038 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Actual cipher.outputSize = " + 1039 Integer.toString(encryptedBytes.length)); 1040 } catch (IllegalStateException ise) { 1041 throw new XMLEncryptionException("empty", ise); 1042 } catch (IllegalBlockSizeException ibse) { 1043 throw new XMLEncryptionException("empty", ibse); 1044 } catch (BadPaddingException bpe) { 1045 throw new XMLEncryptionException("empty", bpe); 1046 } catch (UnsupportedEncodingException uee) { 1047 throw new XMLEncryptionException("empty", uee); 1048 } 1049 1050 1053 byte[] iv = c.getIV(); 1054 byte[] finalEncryptedBytes = 1055 new byte[iv.length + encryptedBytes.length]; 1056 System.arraycopy(iv, 0, finalEncryptedBytes, 0, 1057 iv.length); 1058 System.arraycopy(encryptedBytes, 0, finalEncryptedBytes, 1059 iv.length, 1060 encryptedBytes.length); 1061 1062 String base64EncodedEncryptedOctets = Base64.encode(finalEncryptedBytes); 1063 1064 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Encrypted octets:\n" + base64EncodedEncryptedOctets); 1065 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Encrypted octets length = " + 1066 base64EncodedEncryptedOctets.length()); 1067 1068 try { 1069 CipherData cd = _ed.getCipherData(); 1070 CipherValue cv = cd.getCipherValue(); 1071 cv.setValue(base64EncodedEncryptedOctets); 1073 1074 if (contentMode) { 1075 _ed.setType( 1076 new URI(EncryptionConstants.TYPE_CONTENT).toString()); 1077 } else { 1078 _ed.setType( 1079 new URI(EncryptionConstants.TYPE_ELEMENT).toString()); 1080 } 1081 EncryptionMethod method = 1082 _factory.newEncryptionMethod(new URI(_algorithm).toString()); 1083 _ed.setEncryptionMethod(method); 1084 } catch (URI.MalformedURIException mfue) { 1085 throw new XMLEncryptionException("empty", mfue); 1086 } 1087 return (_ed); 1088 } 1089 1090 1091 1092 public EncryptedData encryptData(Document context, byte [] serializedOctets, boolean contentMode) throws 1093 Exception { 1094 logger.log(java.util.logging.Level.FINE, "Encrypting element..."); 1095 if (null == context) 1096 logger.log(java.util.logging.Level.SEVERE, "Context document unexpectedly null..."); 1097 if (null == serializedOctets) 1098 logger.log(java.util.logging.Level.SEVERE, "Canonicalized Data is unexpectedly null..."); 1099 if (_cipherMode != ENCRYPT_MODE) 1100 logger.log(java.util.logging.Level.FINE, "XMLCipher unexpectedly not in ENCRYPT_MODE..."); 1101 1102 _contextDocument = context; 1103 1104 if (_algorithm == null) { 1105 throw new XMLEncryptionException("XMLCipher instance without transformation specified"); 1106 } 1107 1108 1109 logger.log(java.util.logging.Level.FINE, "Serialized octets:\n" + serializedOctets); 1110 1111 byte[] encryptedBytes = null; 1112 1113 Cipher c; 1115 if (_contextCipher == null) { 1116 String jceAlgorithm = 1117 JCEMapper.translateURItoJCEID(_algorithm); 1118 1119 logger.log(java.util.logging.Level.FINE, "alg = " + jceAlgorithm); 1120 1121 try { 1122 if (_requestedJCEProvider == null) 1123 c = Cipher.getInstance(jceAlgorithm); 1124 else 1125 c = Cipher.getInstance(jceAlgorithm, _requestedJCEProvider); 1126 } catch (NoSuchAlgorithmException nsae) { 1127 throw new XMLEncryptionException("empty", nsae); 1128 } catch (NoSuchProviderException nspre) { 1129 throw new XMLEncryptionException("empty", nspre); 1130 } catch (NoSuchPaddingException nspae) { 1131 throw new XMLEncryptionException("empty", nspae); 1132 } 1133 } else { 1134 c = _contextCipher; 1135 } 1136 1138 try { 1139 c.init(_cipherMode, _key); 1142 } catch (InvalidKeyException ike) { 1143 throw new XMLEncryptionException("empty", ike); 1144 } 1145 1146 try { 1147 encryptedBytes = 1148 c.doFinal(serializedOctets); 1149 1150 logger.log(java.util.logging.Level.FINE, "Expected cipher.outputSize = " + 1151 Integer.toString(c.getOutputSize( 1152 serializedOctets.length))); 1153 logger.log(java.util.logging.Level.FINE, "Actual cipher.outputSize = " + 1154 Integer.toString(encryptedBytes.length)); 1155 } catch (IllegalStateException ise) { 1156 throw new XMLEncryptionException("empty", ise); 1157 } catch (IllegalBlockSizeException ibse) { 1158 throw new XMLEncryptionException("empty", ibse); 1159 } catch (BadPaddingException bpe) { 1160 throw new XMLEncryptionException("empty", bpe); 1161 } catch (Exception uee) { 1162 throw new XMLEncryptionException("empty", uee); 1163 } 1164 1165 1168 byte[] iv = c.getIV(); 1169 byte[] finalEncryptedBytes = 1170 new byte[iv.length + encryptedBytes.length]; 1171 System.arraycopy(iv, 0, finalEncryptedBytes, 0, 1172 iv.length); 1173 System.arraycopy(encryptedBytes, 0, finalEncryptedBytes, 1174 iv.length, 1175 encryptedBytes.length); 1176 1177 String base64EncodedEncryptedOctets = Base64.encode(finalEncryptedBytes); 1178 1179 logger.log(java.util.logging.Level.FINE, "Encrypted octets:\n" + base64EncodedEncryptedOctets); 1180 logger.log(java.util.logging.Level.FINE, "Encrypted octets length = " + 1181 base64EncodedEncryptedOctets.length()); 1182 1183 try { 1184 CipherData cd = _ed.getCipherData(); 1185 CipherValue cv = cd.getCipherValue(); 1186 cv.setValue(base64EncodedEncryptedOctets); 1188 1189 if (contentMode) { 1190 _ed.setType( 1191 new URI(EncryptionConstants.TYPE_CONTENT).toString()); 1192 } else { 1193 _ed.setType( 1194 new URI(EncryptionConstants.TYPE_ELEMENT).toString()); 1195 } 1196 EncryptionMethod method = 1197 _factory.newEncryptionMethod(new URI(_algorithm).toString()); 1198 _ed.setEncryptionMethod(method); 1199 } catch (URI.MalformedURIException mfue) { 1200 throw new XMLEncryptionException("empty", mfue); 1201 } 1202 return (_ed); 1203 } 1204 1205 1206 1216 public EncryptedData loadEncryptedData(Document context, Element element) 1217 throws XMLEncryptionException { 1218 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Loading encrypted element..."); 1219 if(null == context) 1220 logger.log(java.util.logging.Level.SEVERE, "Context document unexpectedly null..."); 1221 if(null == element) 1222 logger.log(java.util.logging.Level.SEVERE, "Element unexpectedly null..."); 1223 if(_cipherMode != DECRYPT_MODE) 1224 logger.log(java.util.logging.Level.SEVERE, "XMLCipher unexpectedly not in DECRYPT_MODE..."); 1225 1226 _contextDocument = context; 1227 _ed = _factory.newEncryptedData(element); 1228 1229 return (_ed); 1230 } 1231 1232 1242 1243 public EncryptedKey loadEncryptedKey(Document context, Element element) 1244 throws XMLEncryptionException { 1245 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Loading encrypted key..."); 1246 if(null == context) 1247 logger.log(java.util.logging.Level.SEVERE, "Context document unexpectedly null..."); 1248 if(null == element) 1249 logger.log(java.util.logging.Level.SEVERE, "Element unexpectedly null..."); 1250 if(_cipherMode != UNWRAP_MODE && _cipherMode != DECRYPT_MODE) 1251 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "XMLCipher unexpectedly not in UNWRAP_MODE or DECRYPT_MODE..."); 1252 1253 _contextDocument = context; 1254 _ek = _factory.newEncryptedKey(element); 1255 return (_ek); 1256 } 1257 1258 1269 1270 public EncryptedKey loadEncryptedKey(Element element) 1271 throws XMLEncryptionException { 1272 1273 return (loadEncryptedKey(element.getOwnerDocument(), element)); 1274 } 1275 1276 1285 1286 public EncryptedKey encryptKey(Document doc, Key key) throws 1287 XMLEncryptionException { 1288 1289 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Encrypting key ..."); 1290 1291 if(null == key) 1292 logger.log(java.util.logging.Level.SEVERE, "Key unexpectedly null..."); 1293 if(_cipherMode != WRAP_MODE) 1294 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "XMLCipher unexpectedly not in WRAP_MODE..."); 1295 1296 if (_algorithm == null) { 1297 1298 throw new XMLEncryptionException("XMLCipher instance without transformation specified"); 1299 } 1300 1301 _contextDocument = doc; 1302 1303 byte[] encryptedBytes = null; 1304 Cipher c; 1305 1306 if (_contextCipher == null) { 1307 1309 String jceAlgorithm = 1310 JCEMapper.translateURItoJCEID(_algorithm); 1311 1312 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "alg = " + jceAlgorithm); 1313 1314 try { 1315 if (_requestedJCEProvider == null) 1316 c = Cipher.getInstance(jceAlgorithm); 1317 else 1318 c = Cipher.getInstance(jceAlgorithm, _requestedJCEProvider); 1319 } catch (NoSuchAlgorithmException nsae) { 1320 throw new XMLEncryptionException("empty", nsae); 1321 } catch (NoSuchProviderException nspre) { 1322 throw new XMLEncryptionException("empty", nspre); 1323 } catch (NoSuchPaddingException nspae) { 1324 throw new XMLEncryptionException("empty", nspae); 1325 } 1326 } else { 1327 c = _contextCipher; 1328 } 1329 1331 try { 1332 c.init(Cipher.WRAP_MODE, _key); 1335 encryptedBytes = c.wrap(key); 1336 } catch (InvalidKeyException ike) { 1337 throw new XMLEncryptionException("empty", ike); 1338 } catch (IllegalBlockSizeException ibse) { 1339 throw new XMLEncryptionException("empty", ibse); 1340 } 1341 1342 String base64EncodedEncryptedOctets = Base64.encode(encryptedBytes); 1343 1344 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Encrypted key octets:\n" + base64EncodedEncryptedOctets); 1345 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Encrypted key octets length = " + 1346 base64EncodedEncryptedOctets.length()); 1347 1348 CipherValue cv = _ek.getCipherData().getCipherValue(); 1349 cv.setValue(base64EncodedEncryptedOctets); 1350 1351 try { 1352 EncryptionMethod method = _factory.newEncryptionMethod( 1353 new URI(_algorithm).toString()); 1354 _ek.setEncryptionMethod(method); 1355 } catch (URI.MalformedURIException mfue) { 1356 throw new XMLEncryptionException("empty", mfue); 1357 } 1358 return _ek; 1359 1360 } 1361 1362 1371 1372 public Key decryptKey(EncryptedKey encryptedKey, String algorithm) throws 1373 XMLEncryptionException { 1374 1375 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Decrypting key from previously loaded EncryptedKey..."); 1376 1377 if(_cipherMode != UNWRAP_MODE) 1378 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "XMLCipher unexpectedly not in UNWRAP_MODE..."); 1379 1380 if (algorithm == null) { 1381 throw new XMLEncryptionException("Cannot decrypt a key without knowing the algorithm"); 1382 } 1383 1384 if (_key == null) { 1385 1386 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Trying to find a KEK via key resolvers"); 1387 1388 KeyInfo ki = encryptedKey.getKeyInfo(); 1389 if (ki != null) { 1390 try { 1391 _key = ki.getSecretKey(); 1392 } 1393 catch (Exception e) { 1394 } 1395 } 1396 if (_key == null) { 1397 logger.log(java.util.logging.Level.SEVERE, "XMLCipher::decryptKey called without a KEK and cannot resolve"); 1398 throw new XMLEncryptionException("Unable to decrypt without a KEK"); 1399 } 1400 } 1401 1402 XMLCipherInput cipherInput = new XMLCipherInput(encryptedKey); 1404 byte [] encryptedBytes = cipherInput.getBytes(); 1405 1406 String jceKeyAlgorithm = 1407 JCEMapper.getJCEKeyAlgorithmFromURI(algorithm); 1408 1409 Cipher c; 1410 if (_contextCipher == null) { 1411 1413 String jceAlgorithm = 1414 JCEMapper.translateURItoJCEID( 1415 encryptedKey.getEncryptionMethod().getAlgorithm()); 1416 1417 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "JCE Algorithm = " + jceAlgorithm); 1418 1419 try { 1420 if (_requestedJCEProvider == null) 1421 c = Cipher.getInstance(jceAlgorithm); 1422 else 1423 c = Cipher.getInstance(jceAlgorithm, _requestedJCEProvider); 1424 } catch (NoSuchAlgorithmException nsae) { 1425 throw new XMLEncryptionException("empty", nsae); 1426 } catch (NoSuchProviderException nspre) { 1427 throw new XMLEncryptionException("empty", nspre); 1428 } catch (NoSuchPaddingException nspae) { 1429 throw new XMLEncryptionException("empty", nspae); 1430 } 1431 } else { 1432 c = _contextCipher; 1433 } 1434 1435 Key ret; 1436 1437 try { 1438 c.init(Cipher.UNWRAP_MODE, _key); 1439 ret = c.unwrap(encryptedBytes, jceKeyAlgorithm, Cipher.SECRET_KEY); 1440 1441 } catch (InvalidKeyException ike) { 1442 throw new XMLEncryptionException("empty", ike); 1443 } catch (NoSuchAlgorithmException nsae) { 1444 throw new XMLEncryptionException("empty", nsae); 1445 } 1446 1447 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Decryption of key type " + algorithm + " OK"); 1448 1449 return ret; 1450 1451 } 1452 1453 1464 1465 public Key decryptKey(EncryptedKey encryptedKey) throws 1466 XMLEncryptionException { 1467 1468 return decryptKey(encryptedKey, _ed.getEncryptionMethod().getAlgorithm()); 1469 1470 } 1471 1472 1477 private void removeContent(Node node) { 1478 NodeList list = node.getChildNodes(); 1479 if (list.getLength() > 0) { 1480 Node n = list.item(0); 1481 if (null != n) { 1482 n.getParentNode().removeChild(n); 1483 } 1484 removeContent(node); 1485 } 1486 } 1487 1488 1495 private Document decryptElement(Element element) throws 1496 XMLEncryptionException { 1497 1498 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Decrypting element..."); 1499 1500 if(_cipherMode != DECRYPT_MODE) 1501 logger.log(java.util.logging.Level.SEVERE, "XMLCipher unexpectedly not in DECRYPT_MODE..."); 1502 1503 String octets; 1504 try { 1505 octets = new String (decryptToByteArray(element), "UTF-8"); 1506 } catch (UnsupportedEncodingException uee) { 1507 throw new XMLEncryptionException("empty", uee); 1508 } 1509 1510 1511 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Decrypted octets:\n" + octets); 1512 1513 Node sourceParent = element.getParentNode(); 1514 1515 DocumentFragment decryptedFragment = 1516 _serializer.deserialize(octets, sourceParent); 1517 1518 1519 1522 if (sourceParent instanceof Document ) { 1523 1524 1526 _contextDocument.removeChild(_contextDocument.getDocumentElement()); 1527 _contextDocument.appendChild(decryptedFragment); 1528 } 1529 else { 1530 sourceParent.replaceChild(decryptedFragment, element); 1531 1532 } 1533 1534 return (_contextDocument); 1535 } 1536 1537 1538 1544 private Document decryptElementContent(Element element) throws 1545 XMLEncryptionException { 1546 Element e = (Element ) element.getElementsByTagNameNS( 1547 EncryptionConstants.EncryptionSpecNS, 1548 EncryptionConstants._TAG_ENCRYPTEDDATA).item(0); 1549 1550 if (null == e) { 1551 throw new XMLEncryptionException("No EncryptedData child element."); 1552 } 1553 1554 return (decryptElement(e)); 1555 } 1556 1557 1568 1569 public byte[] decryptToByteArray(Element element) 1570 throws XMLEncryptionException { 1571 1572 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Decrypting to ByteArray..."); 1573 1574 if(_cipherMode != DECRYPT_MODE) 1575 logger.log(java.util.logging.Level.SEVERE, "XMLCipher unexpectedly not in DECRYPT_MODE..."); 1576 1577 EncryptedData encryptedData = _factory.newEncryptedData(element); 1578 1579 if (_key == null) { 1580 1581 KeyInfo ki = encryptedData.getKeyInfo(); 1582 1583 if (ki != null) { 1584 try { 1585 ki.registerInternalKeyResolver( 1587 new EncryptedKeyResolver(encryptedData. 1588 getEncryptionMethod(). 1589 getAlgorithm(), 1590 _kek)); 1591 _key = ki.getSecretKey(); 1592 } catch (KeyResolverException kre) { 1593 } 1595 } 1596 1597 if (_key == null) { 1598 logger.log(java.util.logging.Level.SEVERE, "XMLCipher::decryptElement called without a key and unable to resolve"); 1599 1600 throw new XMLEncryptionException("encryption.nokey"); 1601 } 1602 } 1603 1604 XMLCipherInput cipherInput = new XMLCipherInput(encryptedData); 1606 byte [] encryptedBytes = cipherInput.getBytes(); 1607 1608 1610 String jceAlgorithm = 1611 JCEMapper.translateURItoJCEID(encryptedData.getEncryptionMethod().getAlgorithm()); 1612 1613 Cipher c; 1614 try { 1615 if (_requestedJCEProvider == null) 1616 c = Cipher.getInstance(jceAlgorithm); 1617 else 1618 c = Cipher.getInstance(jceAlgorithm, _requestedJCEProvider); 1619 } catch (NoSuchAlgorithmException nsae) { 1620 throw new XMLEncryptionException("empty", nsae); 1621 } catch (NoSuchProviderException nspre) { 1622 throw new XMLEncryptionException("empty", nspre); 1623 } catch (NoSuchPaddingException nspae) { 1624 throw new XMLEncryptionException("empty", nspae); 1625 } 1626 1627 1629 1632 int ivLen = c.getBlockSize(); 1633 byte[] ivBytes = new byte[ivLen]; 1634 1635 1640 System.arraycopy(encryptedBytes, 0, ivBytes, 0, ivLen); 1641 IvParameterSpec iv = new IvParameterSpec(ivBytes); 1642 1643 try { 1644 c.init(_cipherMode, _key, iv); 1645 } catch (InvalidKeyException ike) { 1646 throw new XMLEncryptionException("empty", ike); 1647 } catch (InvalidAlgorithmParameterException iape) { 1648 throw new XMLEncryptionException("empty", iape); 1649 } 1650 1651 byte[] plainBytes; 1652 1653 try { 1654 plainBytes = c.doFinal(encryptedBytes, 1655 ivLen, 1656 encryptedBytes.length - ivLen); 1657 1658 } catch (IllegalBlockSizeException ibse) { 1659 throw new XMLEncryptionException("empty", ibse); 1660 } catch (BadPaddingException bpe) { 1661 throw new XMLEncryptionException("empty", bpe); 1662 } 1663 1664 return (plainBytes); 1665 } 1666 1667 1670 1671 1705 1706 public EncryptedData createEncryptedData(int type, String value) throws 1707 XMLEncryptionException { 1708 EncryptedData result = null; 1709 CipherData data = null; 1710 1711 switch (type) { 1712 case CipherData.REFERENCE_TYPE: 1713 CipherReference cipherReference = _factory.newCipherReference( 1714 value); 1715 data = _factory.newCipherData(type); 1716 data.setCipherReference(cipherReference); 1717 result = _factory.newEncryptedData(data); 1718 break; 1719 case CipherData.VALUE_TYPE: 1720 CipherValue cipherValue = _factory.newCipherValue(value); 1721 data = _factory.newCipherData(type); 1722 data.setCipherValue(cipherValue); 1723 result = _factory.newEncryptedData(data); 1724 } 1725 1726 return (result); 1727 } 1728 1729 1763 1764 public EncryptedKey createEncryptedKey(int type, String value) throws 1765 XMLEncryptionException { 1766 EncryptedKey result = null; 1767 CipherData data = null; 1768 1769 switch (type) { 1770 case CipherData.REFERENCE_TYPE: 1771 CipherReference cipherReference = _factory.newCipherReference( 1772 value); 1773 data = _factory.newCipherData(type); 1774 data.setCipherReference(cipherReference); 1775 result = _factory.newEncryptedKey(data); 1776 break; 1777 case CipherData.VALUE_TYPE: 1778 CipherValue cipherValue = _factory.newCipherValue(value); 1779 data = _factory.newCipherData(type); 1780 data.setCipherValue(cipherValue); 1781 result = _factory.newEncryptedKey(data); 1782 } 1783 1784 return (result); 1785 } 1786 1787 1793 1794 public AgreementMethod createAgreementMethod(String algorithm) { 1795 return (_factory.newAgreementMethod(algorithm)); 1796 } 1797 1798 1805 1806 public CipherData createCipherData(int type) { 1807 return (_factory.newCipherData(type)); 1808 } 1809 1810 1816 1817 public CipherReference createCipherReference(String uri) { 1818 return (_factory.newCipherReference(uri)); 1819 } 1820 1821 1827 1828 public CipherValue createCipherValue(String value) { 1829 return (_factory.newCipherValue(value)); 1830 } 1831 1832 1838 public EncryptionMethod createEncryptionMethod(String algorithm) { 1839 return (_factory.newEncryptionMethod(algorithm)); 1840 } 1841 1842 1846 public EncryptionProperties createEncryptionProperties() { 1847 return (_factory.newEncryptionProperties()); 1848 } 1849 1850 1854 public EncryptionProperty createEncryptionProperty() { 1855 return (_factory.newEncryptionProperty()); 1856 } 1857 1858 1863 public ReferenceList createReferenceList(int type) { 1864 return (_factory.newReferenceList(type)); 1865 } 1866 1867 1875 1876 public Transforms createTransforms() { 1877 return (_factory.newTransforms()); 1878 } 1879 1880 1890 public Transforms createTransforms(Document doc) { 1891 return (_factory.newTransforms(doc)); 1892 } 1893 1894 1901 1902 private class Serializer { 1903 1914 Serializer() { 1915 } 1916 1917 1928 String serialize(Document document) throws Exception { 1929 return canonSerialize(document); 1930 } 1931 1932 1943 String serialize(Element element) throws Exception { 1944 return canonSerialize(element); 1945 } 1946 1947 1969 String serialize(NodeList content) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream (); 1971 _canon.setWriter(baos); 1972 _canon.notReset(); 1973 for (int i = 0; i < content.getLength(); i++) { 1974 _canon.canonicalizeSubtree(content.item(i)); 1975 } 1976 baos.close(); 1977 return baos.toString("UTF-8"); 1978 } 1979 1980 1986 String canonSerialize(Node node) throws Exception { 1987 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 1988 _canon.setWriter(baos); 1989 _canon.notReset(); 1990 _canon.canonicalizeSubtree(node); 1991 baos.close(); 1992 return baos.toString("UTF-8"); 1993 } 1994 2001 DocumentFragment deserialize(String source, Node ctx) throws XMLEncryptionException { 2002 DocumentFragment result; 2003 final String tagname = "fragment"; 2004 2005 StringBuffer sb; 2007 2008 sb = new StringBuffer (); 2009 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><"+tagname); 2010 2011 2014 Node wk = ctx; 2015 2016 while (wk != null) { 2017 2018 NamedNodeMap atts = wk.getAttributes(); 2019 int length; 2020 if (atts != null) 2021 length = atts.getLength(); 2022 else 2023 length = 0; 2024 2025 for (int i = 0 ; i < length ; ++i) { 2026 Node att = atts.item(i); 2027 if (att.getNodeName().startsWith("xmlns:") || 2028 att.getNodeName().equals("xmlns")) { 2029 2030 Node p = ctx; 2032 boolean found = false; 2033 while (p != wk) { 2034 NamedNodeMap tstAtts = p.getAttributes(); 2035 if (tstAtts != null && 2036 tstAtts.getNamedItem(att.getNodeName()) != null) { 2037 found = true; 2038 break; 2039 } 2040 p = p.getParentNode(); 2041 } 2042 if (found == false) { 2043 2044 sb.append(" " + att.getNodeName() + "=\"" + 2046 att.getNodeValue() + "\""); 2047 } 2048 } 2049 } 2050 wk = wk.getParentNode(); 2051 } 2052 sb.append(">" + source + "</" + tagname + ">"); 2053 String fragment = sb.toString(); 2054 2055 try { 2056 DocumentBuilderFactory dbf = 2057 DocumentBuilderFactory.newInstance(); 2058 dbf.setNamespaceAware(true); 2059 dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE); 2060 DocumentBuilder db = dbf.newDocumentBuilder(); 2061 Document d = db.parse( 2062 new InputSource (new StringReader (fragment))); 2063 2064 Element fragElt = (Element ) _contextDocument.importNode( 2065 d.getDocumentElement(), true); 2066 result = _contextDocument.createDocumentFragment(); 2067 Node child = fragElt.getFirstChild(); 2068 while (child != null) { 2069 fragElt.removeChild(child); 2070 result.appendChild(child); 2071 child = fragElt.getFirstChild(); 2072 } 2073 2075 } catch (SAXException se) { 2076 throw new XMLEncryptionException("empty", se); 2077 } catch (ParserConfigurationException pce) { 2078 throw new XMLEncryptionException("empty", pce); 2079 } catch (IOException ioe) { 2080 throw new XMLEncryptionException("empty", ioe); 2081 } 2082 2083 return (result); 2084 } 2085 } 2086 2087 2088 2092 private class Factory { 2093 2098 AgreementMethod newAgreementMethod(String algorithm) { 2099 return (new AgreementMethodImpl(algorithm)); 2100 } 2101 2102 2107 CipherData newCipherData(int type) { 2108 return (new CipherDataImpl(type)); 2109 } 2110 2111 2116 CipherReference newCipherReference(String uri) { 2117 return (new CipherReferenceImpl(uri)); 2118 } 2119 2120 2125 CipherValue newCipherValue(String value) { 2126 return (new CipherValueImpl(value)); 2127 } 2128 2129 2136 2141 EncryptedData newEncryptedData(CipherData data) { 2142 return (new EncryptedDataImpl(data)); 2143 } 2144 2145 2150 EncryptedKey newEncryptedKey(CipherData data) { 2151 return (new EncryptedKeyImpl(data)); 2152 } 2153 2154 2159 EncryptionMethod newEncryptionMethod(String algorithm) { 2160 return (new EncryptionMethodImpl(algorithm)); 2161 } 2162 2163 2167 EncryptionProperties newEncryptionProperties() { 2168 return (new EncryptionPropertiesImpl()); 2169 } 2170 2171 2175 EncryptionProperty newEncryptionProperty() { 2176 return (new EncryptionPropertyImpl()); 2177 } 2178 2179 2184 ReferenceList newReferenceList(int type) { 2185 return (new ReferenceListImpl(type)); 2186 } 2187 2188 2192 Transforms newTransforms() { 2193 return (new TransformsImpl()); 2194 } 2195 2196 2201 Transforms newTransforms(Document doc) { 2202 return (new TransformsImpl(doc)); 2203 } 2204 2205 2211 AgreementMethod newAgreementMethod(Element element) throws 2223 XMLEncryptionException { 2224 if (null == element) { 2225 } 2227 2228 String algorithm = element.getAttributeNS(null, 2229 EncryptionConstants._ATT_ALGORITHM); 2230 AgreementMethod result = newAgreementMethod(algorithm); 2231 2232 Element kaNonceElement = (Element ) element.getElementsByTagNameNS( 2233 EncryptionConstants.EncryptionSpecNS, 2234 EncryptionConstants._TAG_KA_NONCE).item(0); 2235 if (null != kaNonceElement) { 2236 result.setKANonce(kaNonceElement.getNodeValue().getBytes()); 2237 } 2238 2242 2244 Element originatorKeyInfoElement = 2245 (Element ) element.getElementsByTagNameNS( 2246 EncryptionConstants.EncryptionSpecNS, 2247 EncryptionConstants._TAG_ORIGINATORKEYINFO).item(0); 2248 if (null != originatorKeyInfoElement) { 2249 try { 2250 result.setOriginatorKeyInfo( 2251 new KeyInfo(originatorKeyInfoElement, null)); 2252 } catch (XMLSecurityException xse) { 2253 throw new XMLEncryptionException("empty", xse); 2254 } 2255 } 2256 2257 2259 Element recipientKeyInfoElement = 2260 (Element ) element.getElementsByTagNameNS( 2261 EncryptionConstants.EncryptionSpecNS, 2262 EncryptionConstants._TAG_RECIPIENTKEYINFO).item(0); 2263 if (null != recipientKeyInfoElement) { 2264 try { 2265 result.setRecipientKeyInfo( 2266 new KeyInfo(recipientKeyInfoElement, null)); 2267 } catch (XMLSecurityException xse) { 2268 throw new XMLEncryptionException("empty", xse); 2269 } 2270 } 2271 2272 return (result); 2273 } 2274 2275 2281 CipherData newCipherData(Element element) throws 2289 XMLEncryptionException { 2290 if (null == element) { 2291 } 2293 2294 int type = 0; 2295 Element e = null; 2296 if (element.getElementsByTagNameNS( 2297 EncryptionConstants.EncryptionSpecNS, 2298 EncryptionConstants._TAG_CIPHERVALUE).getLength() > 0) { 2299 type = CipherData.VALUE_TYPE; 2300 e = (Element ) element.getElementsByTagNameNS( 2301 EncryptionConstants.EncryptionSpecNS, 2302 EncryptionConstants._TAG_CIPHERVALUE).item(0); 2303 } else if (element.getElementsByTagNameNS( 2304 EncryptionConstants.EncryptionSpecNS, 2305 EncryptionConstants._TAG_CIPHERREFERENCE).getLength() > 0) { 2306 type = CipherData.REFERENCE_TYPE; 2307 e = (Element ) element.getElementsByTagNameNS( 2308 EncryptionConstants.EncryptionSpecNS, 2309 EncryptionConstants._TAG_CIPHERREFERENCE).item(0); 2310 } 2311 2312 CipherData result = newCipherData(type); 2313 if (type == CipherData.VALUE_TYPE) { 2314 result.setCipherValue(newCipherValue(e)); 2315 } else if (type == CipherData.REFERENCE_TYPE) { 2316 result.setCipherReference(newCipherReference(e)); 2317 } 2318 2319 return (result); 2320 } 2321 2322 2328 CipherReference newCipherReference(Element element) throws 2336 XMLEncryptionException { 2337 2338 Attr URIAttr = 2339 element.getAttributeNodeNS(null, EncryptionConstants._ATT_URI); 2340 CipherReference result = new CipherReferenceImpl(URIAttr); 2341 2342 2344 NodeList transformsElements = element.getElementsByTagNameNS( 2345 EncryptionConstants.EncryptionSpecNS, 2346 EncryptionConstants._TAG_TRANSFORMS); 2347 Element transformsElement = 2348 (Element ) transformsElements.item(0); 2349 2350 if (transformsElement != null) { 2351 if (logger.isLoggable(java.util.logging.Level.FINE)) logger.log(java.util.logging.Level.FINE, "Creating a DSIG based Transforms element"); 2352 try { 2353 result.setTransforms(new TransformsImpl(transformsElement)); 2354 } 2355 catch (XMLSignatureException xse) { 2356 throw new XMLEncryptionException("empty", xse); 2357 } catch (InvalidTransformException ite) { 2358 throw new XMLEncryptionException("empty", ite); 2359 } catch (XMLSecurityException xse) { 2360 throw new XMLEncryptionException("empty", xse); 2361 } 2362 2363 } 2364 2365 return result; 2366 } 2367 2368 2373 CipherValue newCipherValue(Element element) { 2374 String value = XMLUtils.getFullTextChildrenFromElement(element); 2375 2376 CipherValue result = newCipherValue(value); 2377 2378 return (result); 2379 } 2380 2381 2387 EncryptedData newEncryptedData(Element element) throws 2407 XMLEncryptionException { 2408 EncryptedData result = null; 2409 2410 NodeList dataElements = element.getElementsByTagNameNS( 2411 EncryptionConstants.EncryptionSpecNS, 2412 EncryptionConstants._TAG_CIPHERDATA); 2413 2414 2417 Element dataElement = 2418 (Element ) dataElements.item(dataElements.getLength() - 1); 2419 2420 CipherData data = newCipherData(dataElement); 2421 2422 result = newEncryptedData(data); 2423 2424 try { 2425 result.setId(element.getAttributeNS( 2426 null, EncryptionConstants._ATT_ID)); 2427 result.setType(new URI( 2428 element.getAttributeNS( 2429 null, EncryptionConstants._ATT_TYPE)).toString()); 2430 result.setMimeType(element.getAttributeNS( 2431 null, EncryptionConstants._ATT_MIMETYPE)); 2432 result.setEncoding(new URI( 2433 element.getAttributeNS( 2434 null, Constants._ATT_ENCODING)).toString()); 2435 } catch (URI.MalformedURIException mfue) { 2436 } 2438 2439 Element encryptionMethodElement = 2440 (Element ) element.getElementsByTagNameNS( 2441 EncryptionConstants.EncryptionSpecNS, 2442 EncryptionConstants._TAG_ENCRYPTIONMETHOD).item(0); 2443 if (null != encryptionMethodElement) { 2444 result.setEncryptionMethod(newEncryptionMethod( 2445 encryptionMethodElement)); 2446 } 2447 2448 2451 Element keyInfoElement = 2452 (Element ) element.getElementsByTagNameNS( 2453 Constants.SignatureSpecNS, Constants._TAG_KEYINFO).item(0); 2454 if (null != keyInfoElement) { 2455 try { 2456 result.setKeyInfo(new KeyInfo(keyInfoElement, null)); 2457 } catch (XMLSecurityException xse) { 2458 throw new XMLEncryptionException("Error loading Key Info", 2459 xse); 2460 } 2461 } 2462 2463 Element encryptionPropertiesElement = 2465 (Element ) element.getElementsByTagNameNS( 2466 EncryptionConstants.EncryptionSpecNS, 2467 EncryptionConstants._TAG_ENCRYPTIONPROPERTIES).item(0); 2468 if (null != encryptionPropertiesElement) { 2469 result.setEncryptionProperties( 2470 newEncryptionProperties(encryptionPropertiesElement)); 2471 } 2472 2473 return (result); 2474 } 2475 2476 2482 EncryptedKey newEncryptedKey(Element element) throws 2508 XMLEncryptionException { 2509 EncryptedKey result = null; 2510 NodeList dataElements = element.getElementsByTagNameNS( 2511 EncryptionConstants.EncryptionSpecNS, 2512 EncryptionConstants._TAG_CIPHERDATA); 2513 Element dataElement = 2514 (Element ) dataElements.item(dataElements.getLength() - 1); 2515 2516 CipherData data = newCipherData(dataElement); 2517 result = newEncryptedKey(data); 2518 2519 try { 2520 result.setId(element.getAttributeNS( 2521 null, EncryptionConstants._ATT_ID)); 2522 result.setType(new URI( 2523 element.getAttributeNS( 2524 null, EncryptionConstants._ATT_TYPE)).toString()); 2525 result.setMimeType(element.getAttributeNS( 2526 null, EncryptionConstants._ATT_MIMETYPE)); 2527 result.setEncoding(new URI( 2528 element.getAttributeNS( 2529 null, Constants._ATT_ENCODING)).toString()); 2530 result.setRecipient(element.getAttributeNS( 2531 null, EncryptionConstants._ATT_RECIPIENT)); 2532 } catch (URI.MalformedURIException mfue) { 2533 } 2535 2536 Element encryptionMethodElement = 2537 (Element ) element.getElementsByTagNameNS( 2538 EncryptionConstants.EncryptionSpecNS, 2539 EncryptionConstants._TAG_ENCRYPTIONMETHOD).item(0); 2540 if (null != encryptionMethodElement) { 2541 result.setEncryptionMethod(newEncryptionMethod( 2542 encryptionMethodElement)); 2543 } 2544 2545 Element keyInfoElement = 2546 (Element ) element.getElementsByTagNameNS( 2547 Constants.SignatureSpecNS, Constants._TAG_KEYINFO).item(0); 2548 if (null != keyInfoElement) { 2549 try { 2550 result.setKeyInfo(new KeyInfo(keyInfoElement, null)); 2551 } catch (XMLSecurityException xse) { 2552 throw new XMLEncryptionException("Error loading Key Info", 2553 xse); 2554 } 2555 } 2556 2557 Element encryptionPropertiesElement = 2559 (Element ) element.getElementsByTagNameNS( 2560 EncryptionConstants.EncryptionSpecNS, 2561 EncryptionConstants._TAG_ENCRYPTIONPROPERTIES).item(0); 2562 if (null != encryptionPropertiesElement) { 2563 result.setEncryptionProperties( 2564 newEncryptionProperties(encryptionPropertiesElement)); 2565 } 2566 2567 Element referenceListElement = 2568 (Element ) element.getElementsByTagNameNS( 2569 EncryptionConstants.EncryptionSpecNS, 2570 EncryptionConstants._TAG_REFERENCELIST).item(0); 2571 if (null != referenceListElement) { 2572 result.setReferenceList(newReferenceList(referenceListElement)); 2573 } 2574 2575 Element carriedNameElement = 2576 (Element ) element.getElementsByTagNameNS( 2577 EncryptionConstants.EncryptionSpecNS, 2578 EncryptionConstants._TAG_CARRIEDKEYNAME).item(0); 2579 if (null != carriedNameElement) { 2580 result.setCarriedName(carriedNameElement.getNodeValue()); 2581 } 2582 2583 return (result); 2584 } 2585 2586 2591 EncryptionMethod newEncryptionMethod(Element element) { 2600 String algorithm = element.getAttributeNS( 2601 null, EncryptionConstants._ATT_ALGORITHM); 2602 EncryptionMethod result = newEncryptionMethod(algorithm); 2603 2604 Element keySizeElement = 2605 (Element ) element.getElementsByTagNameNS( 2606 EncryptionConstants.EncryptionSpecNS, 2607 EncryptionConstants._TAG_KEYSIZE).item(0); 2608 if (null != keySizeElement) { 2609 result.setKeySize( 2610 Integer.valueOf( 2611 keySizeElement.getFirstChild().getNodeValue()).intValue()); 2612 } 2613 2614 Element oaepParamsElement = 2615 (Element ) element.getElementsByTagNameNS( 2616 EncryptionConstants.EncryptionSpecNS, 2617 EncryptionConstants._TAG_OAEPPARAMS).item(0); 2618 if (null != oaepParamsElement) { 2619 result.setOAEPparams( 2620 oaepParamsElement.getNodeValue().getBytes()); 2621 } 2622 2623 2626 return (result); 2627 } 2628 2629 2634 EncryptionProperties newEncryptionProperties(Element element) { 2642 EncryptionProperties result = newEncryptionProperties(); 2643 2644 result.setId(element.getAttributeNS( 2645 null, EncryptionConstants._ATT_ID)); 2646 2647 NodeList encryptionPropertyList = 2648 element.getElementsByTagNameNS( 2649 EncryptionConstants.EncryptionSpecNS, 2650 EncryptionConstants._TAG_ENCRYPTIONPROPERTY); 2651 for(int i = 0; i < encryptionPropertyList.getLength(); i++) { 2652 Node n = encryptionPropertyList.item(i); 2653 if (null != n) { 2654 result.addEncryptionProperty( 2655 newEncryptionProperty((Element ) n)); 2656 } 2657 } 2658 2659 return (result); 2660 } 2661 2662 2667 EncryptionProperty newEncryptionProperty(Element element) { 2677 EncryptionProperty result = newEncryptionProperty(); 2678 2679 try { 2680 result.setTarget(new URI( 2681 element.getAttributeNS( 2682 null, EncryptionConstants._ATT_TARGET)).toString()); 2683 } catch (URI.MalformedURIException mfue) { 2684 } 2686 result.setId(element.getAttributeNS( 2687 null, EncryptionConstants._ATT_ID)); 2688 2691 2694 return (result); 2695 } 2696 2697 2702 ReferenceList newReferenceList(Element element) { 2711 int type = 0; 2712 if (null != element.getElementsByTagNameNS( 2713 EncryptionConstants.EncryptionSpecNS, 2714 EncryptionConstants._TAG_DATAREFERENCE).item(0)) { 2715 type = ReferenceList.DATA_REFERENCE; 2716 } else if (null != element.getElementsByTagNameNS( 2717 EncryptionConstants.EncryptionSpecNS, 2718 EncryptionConstants._TAG_KEYREFERENCE).item(0)) { 2719 type = ReferenceList.KEY_REFERENCE; 2720 } else { 2721 } 2723 2724 ReferenceList result = new ReferenceListImpl(type); 2725 NodeList list = null; 2726 switch (type) { 2727 case ReferenceList.DATA_REFERENCE: 2728 list = element.getElementsByTagNameNS( 2729 EncryptionConstants.EncryptionSpecNS, 2730 EncryptionConstants._TAG_DATAREFERENCE); 2731 for (int i = 0; i < list.getLength() ; i++) { 2732 String uri = ((Element ) list.item(i)).getAttribute("URI"); 2733 result.add(result.newDataReference(uri)); 2734 } 2735 break; 2736 case ReferenceList.KEY_REFERENCE: 2737 list = element.getElementsByTagNameNS( 2738 EncryptionConstants.EncryptionSpecNS, 2739 EncryptionConstants._TAG_KEYREFERENCE); 2740 for (int i = 0; i < list.getLength() ; i++) { 2741 String uri = ((Element ) list.item(i)).getAttribute("URI"); 2742 result.add(result.newKeyReference(uri)); 2743 } 2744 } 2745 2746 return (result); 2747 } 2748 2749 2754 Transforms newTransforms(Element element) { 2755 return (null); 2756 } 2757 2758 2763 Element toElement(AgreementMethod agreementMethod) { 2764 return ((AgreementMethodImpl) agreementMethod).toElement(); 2765 } 2766 2767 2772 Element toElement(CipherData cipherData) { 2773 return ((CipherDataImpl) cipherData).toElement(); 2774 } 2775 2776 2781 Element toElement(CipherReference cipherReference) { 2782 return ((CipherReferenceImpl) cipherReference).toElement(); 2783 } 2784 2785 2790 Element toElement(CipherValue cipherValue) { 2791 return ((CipherValueImpl) cipherValue).toElement(); 2792 } 2793 2794 2799 Element toElement(EncryptedData encryptedData) { 2800 return ((EncryptedDataImpl) encryptedData).toElement(); 2801 } 2802 2803 2808 Element toElement(EncryptedKey encryptedKey) { 2809 return ((EncryptedKeyImpl) encryptedKey).toElement(); 2810 } 2811 2812 2817 Element toElement(EncryptionMethod encryptionMethod) { 2818 return ((EncryptionMethodImpl) encryptionMethod).toElement(); 2819 } 2820 2821 2826 Element toElement(EncryptionProperties encryptionProperties) { 2827 return ((EncryptionPropertiesImpl) encryptionProperties).toElement(); 2828 } 2829 2830 2835 Element toElement(EncryptionProperty encryptionProperty) { 2836 return ((EncryptionPropertyImpl) encryptionProperty).toElement(); 2837 } 2838 2839 Element toElement(ReferenceList referenceList) { 2840 return ((ReferenceListImpl) referenceList).toElement(); 2841 } 2842 2843 2848 Element toElement(Transforms transforms) { 2849 return ((TransformsImpl) transforms).toElement(); 2850 } 2851 2852 private class AgreementMethodImpl implements AgreementMethod { 2864 private byte[] kaNonce = null; 2865 private List agreementMethodInformation = null; 2866 private KeyInfo originatorKeyInfo = null; 2867 private KeyInfo recipientKeyInfo = null; 2868 private String algorithmURI = null; 2869 2870 2873 public AgreementMethodImpl(String algorithm) { 2874 agreementMethodInformation = new LinkedList (); 2875 URI tmpAlgorithm = null; 2876 try { 2877 tmpAlgorithm = new URI(algorithm); 2878 } catch (URI.MalformedURIException fmue) { 2879 } 2881 algorithmURI = tmpAlgorithm.toString(); 2882 } 2883 2884 2885 public byte[] getKANonce() { 2886 return (kaNonce); 2887 } 2888 2889 2890 public void setKANonce(byte[] kanonce) { 2891 kaNonce = kanonce; 2892 } 2893 2894 2895 public Iterator getAgreementMethodInformation() { 2896 return (agreementMethodInformation.iterator()); 2897 } 2898 2899 2900 public void addAgreementMethodInformation(Element info) { 2901 agreementMethodInformation.add(info); 2902 } 2903 2904 2905 public void revoveAgreementMethodInformation(Element info) { 2906 agreementMethodInformation.remove(info); 2907 } 2908 2909 2910 public KeyInfo getOriginatorKeyInfo() { 2911 return (originatorKeyInfo); 2912 } 2913 2914 2915 public void setOriginatorKeyInfo(KeyInfo keyInfo) { 2916 originatorKeyInfo = keyInfo; 2917 } 2918 2919 2920 public KeyInfo getRecipientKeyInfo() { 2921 return (recipientKeyInfo); 2922 } 2923 2924 2925 public void setRecipientKeyInfo(KeyInfo keyInfo) { 2926 recipientKeyInfo = keyInfo; 2927 } 2928 2929 2930 public String getAlgorithm() { 2931 return (algorithmURI); 2932 } 2933 2934 2935 public void setAlgorithm(String algorithm) { 2936 URI tmpAlgorithm = null; 2937 try { 2938 tmpAlgorithm = new URI(algorithm); 2939 } catch (URI.MalformedURIException mfue) { 2940 } 2942 algorithm = tmpAlgorithm.toString(); 2943 } 2944 2945 Element toElement() { 2957 Element result = ElementProxy.createElementForFamily( 2958 _contextDocument, 2959 EncryptionConstants.EncryptionSpecNS, 2960 EncryptionConstants._TAG_AGREEMENTMETHOD); 2961 result.setAttributeNS( 2962 null, EncryptionConstants._ATT_ALGORITHM, algorithmURI); 2963 if (null != kaNonce) { 2964 result.appendChild( 2965 ElementProxy.createElementForFamily( 2966 _contextDocument, 2967 EncryptionConstants.EncryptionSpecNS, 2968 EncryptionConstants._TAG_KA_NONCE)).appendChild( 2969 _contextDocument.createTextNode(new String (kaNonce))); 2970 } 2971 if (!agreementMethodInformation.isEmpty()) { 2972 Iterator itr = agreementMethodInformation.iterator(); 2973 while (itr.hasNext()) { 2974 result.appendChild((Element ) itr.next()); 2975 } 2976 } 2977 if (null != originatorKeyInfo) { 2978 result.appendChild(originatorKeyInfo.getElement()); 2979 } 2980 if (null != recipientKeyInfo) { 2981 result.appendChild(recipientKeyInfo.getElement()); 2982 } 2983 2984 return (result); 2985 } 2986 } 2987 2988 private class CipherDataImpl implements CipherData { 2996 private static final String valueMessage = 2997 "Data type is reference type."; 2998 private static final String referenceMessage = 2999 "Data type is value type."; 3000 private CipherValue cipherValue = null; 3001 private CipherReference cipherReference = null; 3002 private int cipherType = Integer.MIN_VALUE; 3003 3004 3007 public CipherDataImpl(int type) { 3008 cipherType = type; 3009 } 3010 3011 3012 public CipherValue getCipherValue() { 3013 return (cipherValue); 3014 } 3015 3016 3017 public void setCipherValue(CipherValue value) throws 3018 XMLEncryptionException { 3019 3020 if (cipherType == REFERENCE_TYPE) { 3021 throw new XMLEncryptionException("empty", 3022 new UnsupportedOperationException (valueMessage)); 3023 } 3024 3025 cipherValue = value; 3026 } 3027 3028 3029 public CipherReference getCipherReference() { 3030 return (cipherReference); 3031 } 3032 3033 3034 public void setCipherReference(CipherReference reference) throws 3035 XMLEncryptionException { 3036 if (cipherType == VALUE_TYPE) { 3037 throw new XMLEncryptionException("empty", 3038 new UnsupportedOperationException (referenceMessage)); 3039 } 3040 3041 cipherReference = reference; 3042 } 3043 3044 3045 public int getDataType() { 3046 return (cipherType); 3047 } 3048 3049 Element toElement() { 3057 Element result = ElementProxy.createElementForFamily( 3058 _contextDocument, 3059 EncryptionConstants.EncryptionSpecNS, 3060 EncryptionConstants._TAG_CIPHERDATA); 3061 if (cipherType == VALUE_TYPE) { 3062 result.appendChild( 3063 ((CipherValueImpl) cipherValue).toElement()); 3064 } else if (cipherType == REFERENCE_TYPE) { 3065 result.appendChild( 3066 ((CipherReferenceImpl) cipherReference).toElement()); 3067 } else { 3068 } 3070 3071 return (result); 3072 } 3073 } 3074 3075 private class CipherReferenceImpl implements CipherReference { 3083 private String referenceURI = null; 3084 private Transforms referenceTransforms = null; 3085 private Attr referenceNode = null; 3086 3087 3090 public CipherReferenceImpl(String uri) { 3091 3092 referenceURI = uri; 3093 referenceNode = null; 3094 } 3095 3096 3099 public CipherReferenceImpl(Attr uri) { 3100 referenceURI = uri.getNodeValue(); 3101 referenceNode = uri; 3102 } 3103 3104 3105 public String getURI() { 3106 return (referenceURI); 3107 } 3108 3109 3110 public Attr getURIAsAttr() { 3111 return (referenceNode); 3112 } 3113 3114 3115 public Transforms getTransforms() { 3116 return (referenceTransforms); 3117 } 3118 3119 3120 public void setTransforms(Transforms transforms) { 3121 referenceTransforms = transforms; 3122 } 3123 3124 Element toElement() { 3132 Element result = ElementProxy.createElementForFamily( 3133 _contextDocument, 3134 EncryptionConstants.EncryptionSpecNS, 3135 EncryptionConstants._TAG_CIPHERREFERENCE); 3136 result.setAttributeNS( 3137 null, EncryptionConstants._ATT_URI, referenceURI); 3138 if (null != referenceTransforms) { 3139 result.appendChild( 3140 ((TransformsImpl) referenceTransforms).toElement()); 3141 } 3142 3143 return (result); 3144 } 3145 } 3146 3147 private class CipherValueImpl implements CipherValue { 3148 private String cipherValue = null; 3149 3150 3154 3157 public CipherValueImpl(String value) { 3158 cipherValue = value; 3160 } 3161 3162 3163 public String getValue() { 3164 return (cipherValue); 3165 } 3166 3167 3172 public void setValue(String value) { 3173 cipherValue = value; 3175 } 3176 3177 Element toElement() { 3178 Element result = ElementProxy.createElementForFamily( 3179 _contextDocument, EncryptionConstants.EncryptionSpecNS, 3180 EncryptionConstants._TAG_CIPHERVALUE); 3181 result.appendChild(_contextDocument.createTextNode( 3182 new String (cipherValue))); 3183 3184 return (result); 3185 } 3186 } 3187 3188 private class EncryptedDataImpl extends EncryptedTypeImpl implements 3208 EncryptedData { 3209 3212 public EncryptedDataImpl(CipherData data) { 3213 super(data); 3214 } 3215 3216 Element toElement() { 3236 Element result = ElementProxy.createElementForFamily( 3237 _contextDocument, EncryptionConstants.EncryptionSpecNS, 3238 EncryptionConstants._TAG_ENCRYPTEDDATA); 3239 3240 if (null != super.getId()) { 3241 result.setAttributeNS( 3242 null, EncryptionConstants._ATT_ID, super.getId()); 3243 } 3244 if (null != super.getType()) { 3245 result.setAttributeNS( 3246 null, EncryptionConstants._ATT_TYPE, 3247 super.getType().toString()); 3248 } 3249 if (null != super.getMimeType()) { 3250 result.setAttributeNS( 3251 null, EncryptionConstants._ATT_MIMETYPE, 3252 super.getMimeType()); 3253 } 3254 if (null != super.getEncoding()) { 3255 result.setAttributeNS( 3256 null, EncryptionConstants._ATT_ENCODING, 3257 super.getEncoding().toString()); 3258 } 3259 if (null != super.getEncryptionMethod()) { 3260 result.appendChild(((EncryptionMethodImpl) 3261 super.getEncryptionMethod()).toElement()); 3262 } 3263 if (null != super.getKeyInfo()) { 3264 result.appendChild(super.getKeyInfo().getElement()); 3265 } 3266 3267 result.appendChild( 3268 ((CipherDataImpl) super.getCipherData()).toElement()); 3269 if (null != super.getEncryptionProperties()) { 3270 result.appendChild(((EncryptionPropertiesImpl) 3271 super.getEncryptionProperties()).toElement()); 3272 } 3273 3274 return (result); 3275 } 3276 } 3277 3278 private class EncryptedKeyImpl extends EncryptedTypeImpl implements 3304 EncryptedKey { 3305 private String keyRecipient = null; 3306 private ReferenceList referenceList = null; 3307 private String carriedName = null; 3308 3309 3312 public EncryptedKeyImpl(CipherData data) { 3313 super(data); 3314 } 3315 3316 3317 public String getRecipient() { 3318 return (keyRecipient); 3319 } 3320 3321 3322 public void setRecipient(String recipient) { 3323 keyRecipient = recipient; 3324 } 3325 3326 3327 public ReferenceList getReferenceList() { 3328 return (referenceList); 3329 } 3330 3331 3332 public void setReferenceList(ReferenceList list) { 3333 referenceList = list; 3334 } 3335 3336 3337 public String getCarriedName() { 3338 return (carriedName); 3339 } 3340 3341 3342 public void setCarriedName(String name) { 3343 carriedName = name; 3344 } 3345 3346 Element toElement() { 3372 Element result = ElementProxy.createElementForFamily( 3373 _contextDocument, EncryptionConstants.EncryptionSpecNS, 3374 EncryptionConstants._TAG_ENCRYPTEDKEY); 3375 3376 if (null != super.getId()) { 3377 result.setAttributeNS( 3378 null, EncryptionConstants._ATT_ID, super.getId()); 3379 } 3380 if (null != super.getType()) { 3381 result.setAttributeNS( 3382 null, EncryptionConstants._ATT_TYPE, 3383 super.getType().toString()); 3384 } 3385 if (null != super.getMimeType()) { 3386 result.setAttributeNS(null, 3387 EncryptionConstants._ATT_MIMETYPE, super.getMimeType()); 3388 } 3389 if (null != super.getEncoding()) { 3390 result.setAttributeNS(null, Constants._ATT_ENCODING, 3391 super.getEncoding().toString()); 3392 } 3393 if (null != getRecipient()) { 3394 result.setAttributeNS(null, 3395 EncryptionConstants._ATT_RECIPIENT, getRecipient()); 3396 } 3397 if (null != super.getEncryptionMethod()) { 3398 result.appendChild(((EncryptionMethodImpl) 3399 super.getEncryptionMethod()).toElement()); 3400 } 3401 if (null != super.getKeyInfo()) { 3402 result.appendChild(super.getKeyInfo().getElement()); 3403 } 3404 result.appendChild( 3405 ((CipherDataImpl) super.getCipherData()).toElement()); 3406 if (null != super.getEncryptionProperties()) { 3407 result.appendChild(((EncryptionPropertiesImpl) 3408 super.getEncryptionProperties()).toElement()); 3409 } 3410 if (referenceList != null && !referenceList.isEmpty()) { 3411 result.appendChild(((ReferenceListImpl) 3412 getReferenceList()).toElement()); 3413 } 3414 if (null != carriedName) { 3415 Element element = ElementProxy.createElementForFamily( 3416 _contextDocument, 3417 EncryptionConstants.EncryptionSpecNS, 3418 EncryptionConstants._TAG_CARRIEDKEYNAME); 3419 Node node = _contextDocument.createTextNode(carriedName); 3420 element.appendChild(node); 3421 result.appendChild(element); 3422 } 3423 3424 return (result); 3425 } 3426 } 3427 3428 private abstract class EncryptedTypeImpl { 3429 private String id = null; 3430 private String type = null; 3431 private String mimeType = null; 3432 private String encoding = null; 3433 private EncryptionMethod encryptionMethod = null; 3434 private KeyInfo keyInfo = null; 3435 private CipherData cipherData = null; 3436 private EncryptionProperties encryptionProperties = null; 3437 3438 protected EncryptedTypeImpl(CipherData data) { 3439 cipherData = data; 3440 } 3441 3445 public String getId() { 3446 return (id); 3447 } 3448 3452 public void setId(String id) { 3453 this.id = id; 3454 } 3455 3459 public String getType() { 3460 return (type); 3461 } 3462 3466 public void setType(String type) { 3467 URI tmpType = null; 3468 try { 3469 tmpType = new URI(type); 3470 } catch (URI.MalformedURIException mfue) { 3471 } 3473 this.type = tmpType.toString(); 3474 } 3475 3479 public String getMimeType() { 3480 return (mimeType); 3481 } 3482 3486 public void setMimeType(String type) { 3487 mimeType = type; 3488 } 3489 3493 public String getEncoding() { 3494 return (encoding); 3495 } 3496 3500 public void setEncoding(String encoding) { 3501 URI tmpEncoding = null; 3502 try { 3503 tmpEncoding = new URI(encoding); 3504 } catch (URI.MalformedURIException mfue) { 3505 } 3507 this.encoding = tmpEncoding.toString(); 3508 } 3509 3513 public EncryptionMethod getEncryptionMethod() { 3514 return (encryptionMethod); 3515 } 3516 3520 public void setEncryptionMethod(EncryptionMethod method) { 3521 encryptionMethod = method; 3522 } 3523 3527 public KeyInfo getKeyInfo() { 3528 return (keyInfo); 3529 } 3530 3534 public void setKeyInfo(KeyInfo info) { 3535 keyInfo = info; 3536 } 3537 3541 public CipherData getCipherData() { 3542 return (cipherData); 3543 } 3544 3548 public EncryptionProperties getEncryptionProperties() { 3549 return (encryptionProperties); 3550 } 3551 3555 public void setEncryptionProperties( 3556 EncryptionProperties properties) { 3557 encryptionProperties = properties; 3558 } 3559 } 3560 3561 private class EncryptionMethodImpl implements EncryptionMethod { 3570 private String algorithm = null; 3571 private int keySize = Integer.MIN_VALUE; 3572 private byte[] oaepParams = null; 3573 private List encryptionMethodInformation = null; 3574 3578 public EncryptionMethodImpl(String algorithm) { 3579 URI tmpAlgorithm = null; 3580 try { 3581 tmpAlgorithm = new URI(algorithm); 3582 } catch (URI.MalformedURIException mfue) { 3583 } 3585 this.algorithm = tmpAlgorithm.toString(); 3586 encryptionMethodInformation = new LinkedList (); 3587 } 3588 3589 public String getAlgorithm() { 3590 return (algorithm); 3591 } 3592 3593 public int getKeySize() { 3594 return (keySize); 3595 } 3596 3597 public void setKeySize(int size) { 3598 keySize = size; 3599 } 3600 3601 public byte[] getOAEPparams() { 3602 return (oaepParams); 3603 } 3604 3605 public void setOAEPparams(byte[] params) { 3606 oaepParams = params; 3607 } 3608 3609 public Iterator getEncryptionMethodInformation() { 3610 return (encryptionMethodInformation.iterator()); 3611 } 3612 3613 public void addEncryptionMethodInformation(Element info) { 3614 encryptionMethodInformation.add(info); 3615 } 3616 3617 public void removeEncryptionMethodInformation(Element info) { 3618 encryptionMethodInformation.remove(info); 3619 } 3620 3621 Element toElement() { 3630 Element result = ElementProxy.createElementForFamily( 3631 _contextDocument, EncryptionConstants.EncryptionSpecNS, 3632 EncryptionConstants._TAG_ENCRYPTIONMETHOD); 3633 result.setAttributeNS(null, EncryptionConstants._ATT_ALGORITHM, 3634 algorithm.toString()); 3635 if (keySize > 0) { 3636 result.appendChild( 3637 ElementProxy.createElementForFamily(_contextDocument, 3638 EncryptionConstants.EncryptionSpecNS, 3639 EncryptionConstants._TAG_KEYSIZE).appendChild( 3640 _contextDocument.createTextNode( 3641 String.valueOf(keySize)))); 3642 } 3643 if (null != oaepParams) { 3644 result.appendChild( 3645 ElementProxy.createElementForFamily(_contextDocument, 3646 EncryptionConstants.EncryptionSpecNS, 3647 EncryptionConstants._TAG_OAEPPARAMS).appendChild( 3648 _contextDocument.createTextNode( 3649 new String (oaepParams)))); 3650 } 3651 if (!encryptionMethodInformation.isEmpty()) { 3652 Iterator itr = encryptionMethodInformation.iterator(); 3653 result.appendChild((Element ) itr.next()); 3654 } 3655 3656 return (result); 3657 } 3658 } 3659 3660 private class EncryptionPropertiesImpl implements EncryptionProperties { 3668 private String id = null; 3669 private List encryptionProperties = null; 3670 3674 public EncryptionPropertiesImpl() { 3675 encryptionProperties = new LinkedList (); 3676 } 3677 3678 public String getId() { 3679 return (id); 3680 } 3681 3682 public void setId(String id) { 3683 this.id = id; 3684 } 3685 3686 public Iterator getEncryptionProperties() { 3687 return (encryptionProperties.iterator()); 3688 } 3689 3690 public void addEncryptionProperty(EncryptionProperty property) { 3691 encryptionProperties.add(property); 3692 } 3693 3694 public void removeEncryptionProperty(EncryptionProperty property) { 3695 encryptionProperties.remove(property); 3696 } 3697 3698 Element toElement() { 3706 Element result = ElementProxy.createElementForFamily( 3707 _contextDocument, EncryptionConstants.EncryptionSpecNS, 3708 EncryptionConstants._TAG_ENCRYPTIONPROPERTIES); 3709 if (null != id) { 3710 result.setAttributeNS(null, EncryptionConstants._ATT_ID, id); 3711 } 3712 Iterator itr = getEncryptionProperties(); 3713 while (itr.hasNext()) { 3714 result.appendChild(((EncryptionPropertyImpl) 3715 itr.next()).toElement()); 3716 } 3717 3718 return (result); 3719 } 3720 } 3721 3722 private class EncryptionPropertyImpl implements EncryptionProperty { 3732 private String target = null; 3733 private String id = null; 3734 private String attributeName = null; 3735 private String attributeValue = null; 3736 private List encryptionInformation = null; 3737 3738 3742 public EncryptionPropertyImpl() { 3743 encryptionInformation = new LinkedList (); 3744 } 3745 3746 public String getTarget() { 3747 return (target); 3748 } 3749 3750 public void setTarget(String target) { 3751 URI tmpTarget = null; 3752 try { 3753 tmpTarget = new URI(target); 3754 } catch (URI.MalformedURIException mfue) { 3755 } 3757 this.target = tmpTarget.toString(); 3758 } 3759 3760 public String getId() { 3761 return (id); 3762 } 3763 3764 public void setId(String id) { 3765 this.id = id; 3766 } 3767 3768 public String getAttribute(String attribute) { 3769 return (attributeValue); 3770 } 3771 3772 public void setAttribute(String attribute, String value) { 3773 attributeName = attribute; 3774 attributeValue = value; 3775 } 3776 3777 public Iterator getEncryptionInformation() { 3778 return (encryptionInformation.iterator()); 3779 } 3780 3781 public void addEncryptionInformation(Element info) { 3782 encryptionInformation.add(info); 3783 } 3784 3785 public void removeEncryptionInformation(Element info) { 3786 encryptionInformation.remove(info); 3787 } 3788 3789 Element toElement() { 3799 Element result = ElementProxy.createElementForFamily( 3800 _contextDocument, EncryptionConstants.EncryptionSpecNS, 3801 EncryptionConstants._TAG_ENCRYPTIONPROPERTY); 3802 if (null != target) { 3803 result.setAttributeNS(null, EncryptionConstants._ATT_TARGET, 3804 target.toString()); 3805 } 3806 if (null != id) { 3807 result.setAttributeNS(null, EncryptionConstants._ATT_ID, 3808 id); 3809 } 3810 3813 return (result); 3814 } 3815 } 3816 3817 private class TransformsImpl extends 3823 com.sun.org.apache.xml.internal.security.transforms.Transforms 3824 implements Transforms { 3825 3826 3829 3830 public TransformsImpl() { 3831 super(_contextDocument); 3832 } 3833 3837 public TransformsImpl(Document doc) { 3838 super(doc); 3839 } 3840 3848 public TransformsImpl(Element element) 3849 throws XMLSignatureException, 3850 InvalidTransformException, 3851 XMLSecurityException, 3852 TransformationException { 3853 3854 super(element, ""); 3855 3856 } 3857 3858 3862 public Element toElement() { 3863 3864 if (_doc == null) 3865 _doc = _contextDocument; 3866 3867 return getElement(); 3868 } 3869 3870 3871 public com.sun.org.apache.xml.internal.security.transforms.Transforms getDSTransforms() { 3872 return (this); 3873 } 3874 3875 3876 3878 public String getBaseNamespace() { 3879 return EncryptionConstants.EncryptionSpecNS; 3880 } 3881 3882 } 3883 3884 private class ReferenceListImpl implements ReferenceList { 3893 private Class sentry; 3894 private List references; 3895 3899 public ReferenceListImpl(int type) { 3900 if (type == ReferenceList.DATA_REFERENCE) { 3901 sentry = DataReference.class; 3902 } else if (type == ReferenceList.KEY_REFERENCE) { 3903 sentry = KeyReference.class; 3904 } else { 3905 throw new IllegalArgumentException (); 3906 } 3907 references = new LinkedList (); 3908 } 3909 3910 public void add(Reference reference) { 3911 if (!reference.getClass().equals(sentry)) { 3912 throw new IllegalArgumentException (); 3913 } 3914 references.add(reference); 3915 } 3916 3917 public void remove(Reference reference) { 3918 if (!reference.getClass().equals(sentry)) { 3919 throw new IllegalArgumentException (); 3920 } 3921 references.remove(reference); 3922 } 3923 3924 public int size() { 3925 return (references.size()); 3926 } 3927 3928 public boolean isEmpty() { 3929 return (references.isEmpty()); 3930 } 3931 3932 public Iterator getReferences() { 3933 return (references.iterator()); 3934 } 3935 3936 Element toElement() { 3937 Element result = ElementProxy.createElementForFamily( 3938 _contextDocument, 3939 EncryptionConstants.EncryptionSpecNS, 3940 EncryptionConstants._TAG_REFERENCELIST); 3941 Iterator eachReference = references.iterator(); 3942 while (eachReference.hasNext()) { 3943 Reference reference = (Reference) eachReference.next(); 3944 result.appendChild( 3945 ((ReferenceImpl) reference).toElement()); 3946 } 3947 return (result); 3948 } 3949 3950 public Reference newDataReference(String uri) { 3951 return (new DataReference(uri)); 3952 } 3953 3954 public Reference newKeyReference(String uri) { 3955 return (new KeyReference(uri)); 3956 } 3957 3958 3964 private abstract class ReferenceImpl implements Reference { 3965 private String uri; 3966 private List referenceInformation; 3967 3968 ReferenceImpl(String _uri) { 3969 this.uri = _uri; 3970 referenceInformation = new LinkedList (); 3971 } 3972 3973 public String getURI() { 3974 return (uri); 3975 } 3976 3977 public Iterator getElementRetrievalInformation() { 3978 return (referenceInformation.iterator()); 3979 } 3980 3981 public void setURI(String _uri) { 3982 this.uri = _uri; 3983 } 3984 3985 public void removeElementRetrievalInformation(Element node) { 3986 referenceInformation.remove(node); 3987 } 3988 3989 public void addElementRetrievalInformation(Element node) { 3990 referenceInformation.add(node); 3991 } 3992 3996 public abstract Element toElement(); 3997 3998 Element toElement(String tagName) { 3999 Element result = ElementProxy.createElementForFamily( 4000 _contextDocument, 4001 EncryptionConstants.EncryptionSpecNS, 4002 tagName); 4003 result.setAttribute(EncryptionConstants._ATT_URI, uri); 4004 4005 4009 return (result); 4010 } 4011 } 4012 4013 private class DataReference extends ReferenceImpl { 4014 DataReference(String uri) { 4015 super(uri); 4016 } 4017 4018 public Element toElement() { 4019 return super.toElement(EncryptionConstants._TAG_DATAREFERENCE); 4020 } 4021 } 4022 4023 private class KeyReference extends ReferenceImpl { 4024 KeyReference(String uri) { 4025 super (uri); 4026 } 4027 4028 public Element toElement() { 4029 return super.toElement(EncryptionConstants._TAG_KEYREFERENCE); 4030 } 4031 } 4032 } 4033 } 4034} 4035 | Popular Tags |