1 22 package org.jboss.iiop.csiv2; 23 24 import java.net.InetAddress ; 25 import java.io.UnsupportedEncodingException ; 26 27 import org.omg.CORBA.Any ; 28 import org.omg.CORBA.BAD_PARAM ; 29 import org.omg.CORBA.MARSHAL ; 30 import org.omg.CORBA.ORB ; 31 32 import org.omg.IOP.Codec ; 33 import org.omg.IOP.CodecPackage.InvalidTypeForEncoding ; 34 import org.omg.IOP.TaggedComponent ; 35 36 import org.omg.CSI.ITTAnonymous; 37 import org.omg.CSI.ITTPrincipalName; 38 import org.omg.CSI.ITTX509CertChain; 39 import org.omg.CSI.ITTDistinguishedName; 40 41 import org.omg.CSIIOP.AS_ContextSec; 42 import org.omg.CSIIOP.CompoundSecMech; 43 import org.omg.CSIIOP.CompoundSecMechList; 44 import org.omg.CSIIOP.CompoundSecMechListHelper; 45 import org.omg.CSIIOP.Confidentiality; 46 import org.omg.CSIIOP.DetectMisordering; 47 import org.omg.CSIIOP.DetectReplay; 48 import org.omg.CSIIOP.EstablishTrustInClient; 49 import org.omg.CSIIOP.EstablishTrustInTarget; 50 import org.omg.CSIIOP.IdentityAssertion; 51 import org.omg.CSIIOP.Integrity; 52 import org.omg.CSIIOP.SAS_ContextSec; 53 import org.omg.CSIIOP.ServiceConfiguration; 54 import org.omg.CSIIOP.TAG_CSI_SEC_MECH_LIST; 55 import org.omg.CSIIOP.TAG_NULL_TAG; 56 import org.omg.CSIIOP.TAG_TLS_SEC_TRANS; 57 import org.omg.CSIIOP.TLS_SEC_TRANS; 58 import org.omg.CSIIOP.TLS_SEC_TRANSHelper; 59 import org.omg.CSIIOP.TransportAddress; 60 61 import org.omg.GSSUP.GSSUPMechOID; 62 import org.omg.GSSUP.InitialContextToken; 63 import org.omg.GSSUP.InitialContextTokenHelper; 64 65 import org.omg.PortableInterceptor.ClientRequestInfo ; 66 67 import org.omg.SSLIOP.SSL; 68 import org.omg.SSLIOP.SSLHelper; 69 import org.omg.SSLIOP.TAG_SSL_SEC_TRANS; 70 71 import org.ietf.jgss.GSSException ; 72 import org.ietf.jgss.Oid ; 73 74 import org.jboss.metadata.IorSecurityConfigMetaData; 75 import org.jboss.metadata.IorSecurityConfigMetaData.TransportConfig; 76 import org.jboss.metadata.IorSecurityConfigMetaData.AsContext; 77 import org.jboss.metadata.IorSecurityConfigMetaData.SasContext; 78 79 import org.jboss.logging.Logger; 80 81 89 public final class CSIv2Util 90 { 91 private static final Logger log = Logger.getLogger(CSIv2Util.class); 92 93 94 private static final byte[] gssUpMechOidArray = createGSSUPMechOID(); 95 96 private CSIv2Util() 97 { 98 } 100 101 104 public static TaggedComponent createCopy(TaggedComponent tc) 105 { 106 TaggedComponent copy = null; 107 108 if( tc != null ) 109 { 110 byte[] buf = new byte[tc.component_data.length]; 111 System.arraycopy(tc.component_data, 0, buf, 0, tc.component_data.length); 112 113 copy = new TaggedComponent (tc.tag, buf); 114 } 115 116 return copy; 117 } 118 119 126 public static TaggedComponent createSSLTaggedComponent( 127 IorSecurityConfigMetaData metadata, 128 Codec codec, 129 int sslPort, 130 ORB orb) 131 { 132 if( metadata == null ) 133 { 134 log.debug("createSSLTaggedComponent() called with null metadata"); 135 return null; 136 } 137 138 TaggedComponent tc = null; 139 140 try 141 { 142 int supports = createTargetSupports(metadata.getTransportConfig()); 143 int requires = createTargetRequires(metadata.getTransportConfig()); 144 SSL ssl = new SSL((short) supports, (short) requires, (short) sslPort); 145 Any any = orb.create_any(); 146 SSLHelper.insert(any, ssl); 147 byte[] componentData = codec.encode_value(any); 148 tc = new TaggedComponent (TAG_SSL_SEC_TRANS.value, componentData); 149 } 150 catch(InvalidTypeForEncoding e) 151 { 152 log.warn("Caught unexcepted exception while encoding SSL component", e); 153 throw new RuntimeException (e); 154 } 155 return tc; 156 } 157 158 166 public static TaggedComponent createSecurityTaggedComponent( 167 IorSecurityConfigMetaData metadata, 168 Codec codec, 169 int sslPort, 170 ORB orb 171 ) 172 { 173 if( metadata == null ) 174 { 175 log.debug("createSecurityTaggedComponent() called with null metadata"); 176 return null; 177 } 178 179 TaggedComponent tc = null; 180 181 CompoundSecMech[] mechList = 183 createCompoundSecMechanisms(metadata, codec, sslPort, orb); 184 185 CompoundSecMechList csmList = new CompoundSecMechList(false, 190 mechList); 191 try 193 { 194 Any any = orb.create_any(); 195 196 CompoundSecMechListHelper.insert(any, csmList); 197 byte[] b = codec.encode_value(any); 198 199 tc = new TaggedComponent (TAG_CSI_SEC_MECH_LIST.value, b); 200 } 201 catch(InvalidTypeForEncoding e) 202 { 203 log.warn("Caught unexcepted exception while encoding CompoundSecMechList", e); 204 throw new RuntimeException (e); 205 } 206 207 return tc; 208 } 209 210 214 public static CompoundSecMech[] createCompoundSecMechanisms( 215 IorSecurityConfigMetaData metadata, 216 Codec codec, 217 int sslPort, 218 ORB orb 219 ) 220 { 221 CompoundSecMech[] csmList = new CompoundSecMech[1]; 223 224 227 TaggedComponent transport_mech = createTransportMech( 231 metadata.getTransportConfig(), codec, sslPort, orb 232 ); 233 234 AS_ContextSec asContext = createAuthenticationServiceContext(metadata); 236 237 SAS_ContextSec sasContext = createSecureAttributeServiceContext(metadata); 239 240 int target_requires = 243 createTargetRequires(metadata.getTransportConfig()) | 244 asContext.target_requires | 245 sasContext.target_requires; 246 247 CompoundSecMech csm = new CompoundSecMech((short) target_requires, 249 transport_mech, 250 asContext, 251 sasContext); 252 csmList[0] = csm; 254 255 return csmList; 256 } 257 258 262 public static SAS_ContextSec createSecureAttributeServiceContext( 263 IorSecurityConfigMetaData metadata 264 ) 265 { 266 SAS_ContextSec context = null; 267 268 int support = 0; 272 int require = 0; 273 ServiceConfiguration[] privilAuth = new ServiceConfiguration[0]; 274 byte[][] supNamMechs = {}; 275 int supIdenTypes = 0; 277 SasContext sasMeta = metadata.getSasContext(); 279 280 if( sasMeta == null || !sasMeta.isCallerPropagationSupported() ) 283 { 284 context = new SAS_ContextSec((short) support, 285 (short) require, 286 privilAuth, 287 supNamMechs, 288 supIdenTypes); 289 } 290 else 291 { 292 support = IdentityAssertion.value; 293 294 byte[] upMech = createGSSUPMechOID(); 296 supNamMechs = new byte[1][upMech.length]; 297 System.arraycopy(upMech, 0, supNamMechs[0], 0, upMech.length); 298 299 supIdenTypes = ITTAnonymous.value | 302 ITTPrincipalName.value | 303 ITTX509CertChain.value | 304 ITTDistinguishedName.value; 305 context = new SAS_ContextSec((short) support, 307 (short) require, 308 privilAuth, 309 supNamMechs, 310 supIdenTypes); 311 } 312 313 return context; 314 } 315 316 320 public static AS_ContextSec createAuthenticationServiceContext( 321 IorSecurityConfigMetaData metadata 322 ) 323 { 324 AS_ContextSec context = null; 325 326 int support = EstablishTrustInClient.value; int require = 0; 329 byte[] clientAuthMech = {}; 330 byte[] targetName = {}; 331 332 AsContext asMeta = metadata.getAsContext(); 334 335 if( asMeta == null || asMeta.getAuthMethod().equals(AsContext.AUTH_METHOD_NONE) 338 || asMeta.isRequired() == false ) 339 { 340 targetName = encodeGssExportedName(targetName); 342 343 context = new AS_ContextSec((short) support, 344 (short) require, 345 clientAuthMech, 346 targetName); 347 } 348 else 349 { 350 if( asMeta.isRequired() ) 352 require = EstablishTrustInClient.value; 353 354 clientAuthMech = createGSSUPMechOID(); 356 357 String realm = asMeta.getRealm(); 361 targetName = createGSSExportedName(clientAuthMech, realm.getBytes()); 362 363 context = new AS_ContextSec((short) support, 365 (short) require, 366 clientAuthMech, 367 targetName); 368 } 369 370 return context; 371 } 372 373 388 public static TaggedComponent createTransportMech( 389 TransportConfig tconfig, 390 Codec codec, 391 int sslPort, 392 ORB orb 393 ) 394 { 395 TaggedComponent tc = null; 396 397 int support = 0; 399 int require = 0; 400 401 if( tconfig != null ) 402 { 403 require = createTargetRequires(tconfig); 404 support = createTargetSupports(tconfig); 405 } 406 407 if( tconfig == null || support == 0 || sslPort < 0 ) 408 { 409 tc = new TaggedComponent (TAG_NULL_TAG.value, new byte[0]); 411 } 412 else 413 { 414 String host; 416 try 417 { 418 host = InetAddress.getLocalHost().getHostAddress(); 419 } 420 catch(java.net.UnknownHostException e) 421 { 422 host = "127.0.0.1"; 423 } 424 425 TransportAddress[] taList = createTransportAddress(host, sslPort); 427 428 TLS_SEC_TRANS tst = new TLS_SEC_TRANS((short) support, 429 (short) require, 430 taList); 431 432 try 434 { 435 Any any = orb.create_any(); 436 437 TLS_SEC_TRANSHelper.insert(any, tst); 438 byte[] b = codec.encode_value(any); 439 440 tc = new TaggedComponent (TAG_TLS_SEC_TRANS.value, b); 441 } 442 catch(InvalidTypeForEncoding e) 443 { 444 log.warn("Caught unexcepted exception while encoding TLS_SEC_TRANS", e); 445 throw new RuntimeException (e); 446 } 447 } 448 449 return tc; 450 } 451 452 455 public static TransportAddress[] createTransportAddress( 456 String host, int port 457 ) 458 { 459 short short_port = (port > 32767) ? (short) (port - 65536) : (short) port; 461 462 TransportAddress ta = new TransportAddress(host, short_port); 463 TransportAddress[] taList = new TransportAddress[1]; 464 taList[0] = ta; 465 466 return taList; 467 } 468 469 472 public static int createTargetRequires(TransportConfig tc) 473 { 474 int requires = 0; 475 476 if( tc != null ) 477 { 478 if( tc.getIntegrity().equals(TransportConfig.INTEGRITY_REQUIRED) ) 479 requires = requires | Integrity.value; 480 481 if( tc.getConfidentiality().equals(TransportConfig.CONFIDENTIALITY_REQUIRED) ) 482 requires = requires | Confidentiality.value; 483 484 if( tc.getDetectMisordering().equalsIgnoreCase(TransportConfig.DETECT_MISORDERING_REQUIRED) ) 485 requires = requires | DetectMisordering.value; 486 487 if( tc.getDetectReplay().equalsIgnoreCase(TransportConfig.DETECT_REPLAY_REQUIRED) ) 488 requires = requires | DetectReplay.value; 489 490 492 if( tc.getEstablishTrustInClient().equals(TransportConfig.ESTABLISH_TRUST_IN_CLIENT_REQUIRED) ) 493 requires = requires | EstablishTrustInClient.value; 494 } 495 496 return requires; 497 } 498 499 502 public static int createTargetSupports(TransportConfig tc) 503 { 504 int supports = 0; 505 506 if( tc != null ) 507 { 508 if( !tc.getIntegrity().equals(TransportConfig.INTEGRITY_NONE) ) 509 supports = supports | Integrity.value; 510 511 if( !tc.getConfidentiality().equals(TransportConfig.CONFIDENTIALITY_NONE) ) 512 supports = supports | Confidentiality.value; 513 514 if( !tc.getDetectMisordering().equalsIgnoreCase(TransportConfig.DETECT_MISORDERING_NONE) ) 515 supports = supports | DetectMisordering.value; 516 517 if( !tc.getDetectReplay().equalsIgnoreCase(TransportConfig.DETECT_REPLAY_NONE) ) 518 supports = supports | DetectReplay.value; 519 520 if( !tc.getEstablishTrustInTarget().equals(TransportConfig.ESTABLISH_TRUST_IN_TARGET_NONE) ) 521 supports = supports | EstablishTrustInTarget.value; 522 523 if( !tc.getEstablishTrustInClient().equals(TransportConfig.ESTABLISH_TRUST_IN_CLIENT_NONE) ) 524 supports = supports | EstablishTrustInClient.value; 525 } 526 527 return supports; 528 } 529 530 534 public static byte[] createGSSUPMechOID() 535 { 536 539 byte[] retval = {}; 540 try 541 { 542 Oid oid = new Oid (GSSUPMechOID.value.substring(4)); 543 retval = oid.getDER(); 544 } 545 catch(GSSException e) 546 { 547 log.warn("Caught exception while encoding GSSUPMechOID", e); 548 } 549 return retval; 550 } 551 552 555 public static byte[] gssUpMechOid() 556 { 557 return (byte[])gssUpMechOidArray.clone(); 558 } 559 560 603 public static byte[] createGSSExportedName(byte[] oid, byte[] name) 604 { 605 int olen = oid.length; 606 int nlen = name.length; 607 608 int size = 2 + 2 + olen + 4 + nlen; 610 611 byte[] buf = new byte[size]; 613 int i = 0; 615 616 buf[i++] = 0x04; 618 buf[i++] = 0x01; 619 620 buf[i++] = (byte) (olen & 0xFF00); 622 buf[i++] = (byte) (olen & 0x00FF); 623 624 System.arraycopy(oid, 0, buf, i, olen); 626 i += olen; 627 628 buf[i++] = (byte) (nlen & 0xFF000000); 630 buf[i++] = (byte) (nlen & 0x00FF0000); 631 buf[i++] = (byte) (nlen & 0x0000FF00); 632 buf[i++] = (byte) (nlen & 0x000000FF); 633 634 System.arraycopy(name, 0, buf, i, nlen); 636 637 return buf; 639 } 640 641 650 public static byte[] encodeInitialContextToken(InitialContextToken authToken, 651 Codec codec) 652 { 653 byte[] out = null; 654 Any any = ORB.init().create_any(); 655 InitialContextTokenHelper.insert(any, authToken); 656 try 657 { 658 out = codec.encode_value(any); 659 } 660 catch (Exception e) 661 { 662 return new byte[0]; 664 } 665 666 int length = out.length + gssUpMechOidArray.length; 667 int n; 668 669 if (length < (1 << 7)) 670 n = 0; 671 else if (length < (1 << 8)) 672 n = 1; 673 else if (length < (1 << 16)) 674 n = 2; 675 else if (length < (1 << 24)) 676 n = 3; 677 else n = 4; 679 680 byte[] encodedToken = new byte[2 + n + length]; 681 encodedToken[0] = 0x60; 682 683 if (n == 0) 684 encodedToken[1] = (byte)length; 685 else 686 { 687 encodedToken[1] = (byte)(n | 0x80); 688 switch (n) 689 { 690 case 1: 691 encodedToken[2] = (byte)length; 692 break; 693 case 2: 694 encodedToken[2] = (byte)(length >> 8); 695 encodedToken[3] = (byte)length; 696 break; 697 case 3: 698 encodedToken[2] = (byte)(length >> 16); 699 encodedToken[3] = (byte)(length >> 8); 700 encodedToken[4] = (byte)length; 701 break; 702 default: encodedToken[2] = (byte)(length >> 24); 704 encodedToken[3] = (byte)(length >> 16); 705 encodedToken[4] = (byte)(length >> 8); 706 encodedToken[5] = (byte)length; 707 } 708 } 709 System.arraycopy(gssUpMechOidArray, 0, 710 encodedToken, 2 + n, 711 gssUpMechOidArray.length); 712 System.arraycopy(out, 0, 713 encodedToken, 2 + n + gssUpMechOidArray.length, 714 out.length); 715 716 return encodedToken; 717 } 718 719 724 public static InitialContextToken decodeInitialContextToken( 725 byte[] encodedToken, 726 Codec codec) 727 { 728 if(encodedToken[0] != 0x60) 729 return null; 730 731 int encodedLength = 0; 732 int n = 0; 733 734 if(encodedToken[1] >= 0) 735 encodedLength = encodedToken[1]; 736 else 737 { 738 n = encodedToken[1] & 0x7F; 739 for(int i = 1; i <= n; i++) 740 encodedLength += (encodedToken[1 + i] & 0xFF) << (n-i)*8; 741 } 742 743 int length = encodedLength - gssUpMechOidArray.length; 744 byte[] encodedInitialContextToken = new byte[length]; 745 746 System.arraycopy(encodedToken, 2 + n + gssUpMechOidArray.length, 747 encodedInitialContextToken, 0, 748 length); 749 Any any = null; 750 try 751 { 752 any = codec.decode_value(encodedInitialContextToken, 753 InitialContextTokenHelper.type()); 754 } 755 catch(Exception e) 756 { 757 return null; 758 } 759 760 InitialContextToken contextToken = 761 InitialContextTokenHelper.extract(any); 762 763 return contextToken; 764 765 } 766 767 771 public static byte[] encodeGssExportedName(byte[] name) 772 { 773 return createGSSExportedName(gssUpMechOidArray, name); 774 } 775 776 781 public static byte[] decodeGssExportedName(byte[] encodedName) 782 { 783 if(encodedName[0] != 0x04 || encodedName[1] != 0x01) 784 return null; 785 786 int mechOidLength = (encodedName[2] & 0xFF) << 8; mechOidLength += (encodedName[3] & 0xFF); 789 byte[] oidArray = new byte[mechOidLength]; 790 System.arraycopy(encodedName, 4, 791 oidArray, 0, 792 mechOidLength); 793 794 for(int i = 0; i < mechOidLength; i++) 795 { 796 if(gssUpMechOidArray[i] != oidArray[i]) 797 return null; 798 } 799 800 int offset = 4 + mechOidLength; 801 int nameLength = (encodedName[ offset] & 0xFF) << 24; 802 nameLength += (encodedName[++offset] & 0xFF) << 16; 803 nameLength += (encodedName[++offset] & 0xFF) << 8; 804 nameLength += (encodedName[++offset] & 0xFF); 805 806 byte[] name = new byte[nameLength]; 807 System.arraycopy(encodedName, ++offset, 808 name, 0, 809 nameLength); 810 811 return name; 812 } 813 814 834 public static CompoundSecMech getMatchingSecurityMech(ClientRequestInfo ri, 835 Codec codec, 836 short clientSupports, 837 short clientRequires) 838 { 839 CompoundSecMechList csmList = null; 840 try 841 { 842 TaggedComponent tc = 843 ri.get_effective_component(TAG_CSI_SEC_MECH_LIST.value); 844 845 Any any = codec.decode_value(tc.component_data, 846 CompoundSecMechListHelper.type()); 847 848 csmList = CompoundSecMechListHelper.extract(any); 849 850 for(int i = 0; i < csmList.mechanism_list.length; i++) 852 { 853 CompoundSecMech securityMech = csmList.mechanism_list[i]; 854 AS_ContextSec authConfig = securityMech.as_context_mech; 855 856 if( (EstablishTrustInTarget.value 857 & (clientRequires ^ authConfig.target_supports) 858 & ~authConfig.target_supports) != 0 ) 859 { 860 continue; } 864 865 if( (EstablishTrustInClient.value 866 & (authConfig.target_requires ^ clientSupports) 867 & ~clientSupports) != 0 ) 868 { 869 continue; } 873 874 SAS_ContextSec identityConfig = securityMech.sas_context_mech; 875 876 if( (IdentityAssertion.value 877 & (identityConfig.target_requires ^ clientSupports) 878 & ~clientSupports) != 0 ) 879 { 880 continue; } 884 885 return securityMech; 887 } 888 return null; 890 } 891 catch(BAD_PARAM e) 892 { 893 return null; 895 } 896 catch(org.omg.IOP.CodecPackage.TypeMismatch e) 897 { 898 throw new MARSHAL ("Unexpected exception: " + e); 900 } 901 catch(org.omg.IOP.CodecPackage.FormatMismatch e) 902 { 903 throw new MARSHAL ("Unexpected exception: " + e); 905 } 906 } 907 908 912 public static void toString(CompoundSecMech securityMech, StringBuffer buffer) 913 { 914 AS_ContextSec asMech = securityMech != null ? securityMech.as_context_mech : null; 915 SAS_ContextSec sasMech = securityMech != null ? securityMech.sas_context_mech : null; 916 if( securityMech != null ) 917 { 918 buffer.append("CompoundSecMech["); 919 buffer.append("target_requires: "); 920 buffer.append(securityMech.target_requires); 921 if( asMech != null ) 922 { 923 buffer.append("AS_ContextSec["); 924 925 buffer.append("client_authentication_mech: "); 926 try 927 { 928 buffer.append(new String (asMech.client_authentication_mech, "UTF-8")); 929 } 930 catch(UnsupportedEncodingException e) 931 { 932 buffer.append(e.getMessage()); 933 } 934 buffer.append(", target_name: "); 935 try 936 { 937 buffer.append(new String (asMech.target_name, "UTF-8")); 938 } 939 catch(UnsupportedEncodingException e) 940 { 941 buffer.append(e.getMessage()); 942 } 943 buffer.append(", target_requires: "); 944 buffer.append(asMech.target_requires); 945 buffer.append(", target_supports: "); 946 buffer.append(asMech.target_supports); 947 buffer.append("]"); 948 } 949 if( sasMech != null ) 950 { 951 buffer.append("SAS_ContextSec["); 952 buffer.append("supported_identity_types: "); 953 buffer.append(sasMech.supported_identity_types); 954 buffer.append(", target_requires: "); 955 buffer.append(sasMech.target_requires); 956 buffer.append(", target_supports: "); 957 buffer.append(sasMech.target_supports); 958 buffer.append("]"); 959 } 960 buffer.append("]"); 961 } 962 } 963 964 } 965 | Popular Tags |