src/java.security.jgss/share/classes/org/ietf/jgss/GSSContext.java
changeset 55599 e6c430d4d217
parent 50307 ba1b490901d4
equal deleted inserted replaced
55598:091ea1a285dc 55599:e6c430d4d217
   126  * application data, and finally context deletion.
   126  * application data, and finally context deletion.
   127  *
   127  *
   128  * <pre>
   128  * <pre>
   129  *    // Create a context using default credentials
   129  *    // Create a context using default credentials
   130  *    // and the implementation specific default mechanism
   130  *    // and the implementation specific default mechanism
   131  *    GSSManager manager ...
   131  *    GSSManager manager = ...
   132  *    GSSName targetName ...
   132  *    GSSName targetName = ...
   133  *    GSSContext context = manager.createContext(targetName, null, null,
   133  *    GSSContext context = manager.createContext(targetName, null, null,
   134  *                                           GSSContext.INDEFINITE_LIFETIME);
   134  *                                           GSSContext.INDEFINITE_LIFETIME);
   135  *
   135  *
   136  *    // set desired context options prior to context establishment
   136  *    // set desired context options prior to context establishment
   137  *    context.requestConf(true);
   137  *    context.requestConf(true);
   139  *    context.requestReplayDet(true);
   139  *    context.requestReplayDet(true);
   140  *    context.requestSequenceDet(true);
   140  *    context.requestSequenceDet(true);
   141  *
   141  *
   142  *    // establish a context between peers
   142  *    // establish a context between peers
   143  *
   143  *
   144  *    byte []inToken = new byte[0];
   144  *    byte[] inToken = new byte[0];
       
   145  *    byte[] outToken;
   145  *
   146  *
   146  *    // Loop while there still is a token to be processed
   147  *    // Loop while there still is a token to be processed
   147  *
   148  *
   148  *    while (!context.isEstablished()) {
   149  *    while (!context.isEstablished()) {
   149  *
   150  *
   150  *        byte[] outToken
   151  *        outToken = context.initSecContext(inToken, 0, inToken.length);
   151  *            = context.initSecContext(inToken, 0, inToken.length);
       
   152  *
   152  *
   153  *        // send the output token if generated
   153  *        // send the output token if generated
   154  *        if (outToken != null)
   154  *        if (outToken != null) {
   155  *            sendToken(outToken);
   155  *            sendToken(outToken);
       
   156  *        }
   156  *
   157  *
   157  *        if (!context.isEstablished()) {
   158  *        if (!context.isEstablished()) {
   158  *            inToken = readToken();
   159  *            inToken = readToken();
       
   160  *        }
   159  *    }
   161  *    }
   160  *
   162  *
   161  *     // display context information
   163  *     // display context information
   162  *     System.out.println("Remaining lifetime in seconds = "
   164  *     System.out.println("Remaining lifetime in seconds = "
   163  *                                          + context.getLifetime());
   165  *                                          + context.getLifetime());
   164  *     System.out.println("Context mechanism = " + context.getMech());
   166  *     System.out.println("Context mechanism = " + context.getMech());
   165  *     System.out.println("Initiator = " + context.getSrcName());
   167  *     System.out.println("Initiator = " + context.getSrcName());
   166  *     System.out.println("Acceptor = " + context.getTargName());
   168  *     System.out.println("Acceptor = " + context.getTargName());
   167  *
   169  *
   168  *     if (context.getConfState())
   170  *     if (context.getConfState()) {
   169  *             System.out.println("Confidentiality (i.e., privacy) is available");
   171  *         System.out.println("Confidentiality (i.e., privacy) is available");
   170  *
   172  *     }
   171  *     if (context.getIntegState())
   173  *
   172  *             System.out.println("Integrity is available");
   174  *     if (context.getIntegState()) {
       
   175  *         System.out.println("Integrity is available");
       
   176  *     }
   173  *
   177  *
   174  *     // perform wrap on an application supplied message, appMsg,
   178  *     // perform wrap on an application supplied message, appMsg,
   175  *     // using QOP = 0, and requesting privacy service
   179  *     // using QOP = 0, and requesting privacy service
   176  *     byte [] appMsg ...
   180  *     byte[] appMsg = ...
   177  *
   181  *
   178  *     MessageProp mProp = new MessageProp(0, true);
   182  *     MessageProp mProp = new MessageProp(0, true);
   179  *
   183  *
   180  *     byte []tok = context.wrap(appMsg, 0, appMsg.length, mProp);
   184  *     outToken = context.wrap(appMsg, 0, appMsg.length, mProp);
   181  *
   185  *
   182  *     sendToken(tok);
   186  *     sendToken(outToken);
       
   187  *
       
   188  *     // perform unwrap on an incoming application message, and check
       
   189  *     // its privacy state and supplementary information
       
   190  *     inToken = readToken();
       
   191  *
       
   192  *     mProp = new MessageProp(0, true);
       
   193  *
       
   194  *     appMsg = context.unwrap(inToken, 0, inToken.length, mProp);
       
   195  *
       
   196  *     System.out.println("Was it encrypted? " + mProp.getPrivacy());
       
   197  *     System.out.println("Duplicate Token? " + mProp.isDuplicateToken());
       
   198  *     System.out.println("Old Token? " + mProp.isOldToken());
       
   199  *     System.out.println("Unsequenced Token? " + mProp.isUnseqToken());
       
   200  *     System.out.println("Gap Token? " + mProp.isGapToken());
       
   201  *
       
   202  *     // the application determines if the privacy state and supplementary
       
   203  *     // information are acceptable
   183  *
   204  *
   184  *     // release the local-end of the context
   205  *     // release the local-end of the context
   185  *     context.dispose();
   206  *     context.dispose();
   186  *
   207  *
   187  * </pre>
   208  * </pre>