Home | History | Annotate | Download | only in pairing
      1 /*
      2  * Copyright (C) 2009 Google Inc.  All rights reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.polo.pairing;
     18 
     19 import com.google.polo.exception.PoloException;
     20 
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 import java.io.OutputStream;
     24 import java.security.cert.Certificate;
     25 
     26 import javax.net.ssl.SSLSocket;
     27 
     28 /**
     29  * Container class for various bits of state related to a pairing session.
     30  */
     31 public class PairingContext {
     32 
     33   /**
     34    * The {@link Certificate} of the local endpoint of the protocol.
     35    */
     36   private Certificate mLocalCertificate;
     37 
     38   /**
     39    * The {@link Certificate} of the remote endpoint of the protocol.
     40    */
     41   private Certificate mPeerCertificate;
     42 
     43   /**
     44    * An {@link InputStream} for the peer in the protocol.
     45    */
     46   private InputStream mPeerInputStream;
     47 
     48   /**
     49    * An {@link OutputStream} for the peer in the protocol.
     50    */
     51   private OutputStream mPeerOutputStream;
     52 
     53   /**
     54    * {@code true} if this context is for a server endpoint.
     55    */
     56   private final boolean mIsServer;
     57 
     58   /**
     59    * Constructs a new instance.
     60    *
     61    * @param localCertificate  the local endpoint's {@link Certificate}
     62    * @param peerCertificate   the remote endpoint's {@link Certificate}
     63    * @param peerInputStream   an {@link InputStream} from the peer
     64    * @param peerOutputStream  a {@link OutputStream} to the peer
     65    * @param isServer          {@code true} if this endpoint it the server
     66    */
     67   public PairingContext(Certificate localCertificate,
     68       Certificate peerCertificate, InputStream peerInputStream,
     69       OutputStream peerOutputStream, boolean isServer) {
     70     setLocalCertificate(localCertificate);
     71     setPeerCertificate(peerCertificate);
     72     setPeerInputStream(peerInputStream);
     73     setPeerOutputStream(peerOutputStream);
     74     mIsServer = isServer;
     75   }
     76 
     77   /**
     78    * Constructs a new instance from an {@link SSLSocket}.
     79    *
     80    * @param   socket          the socket to use
     81    * @param   isServer        {@code true} if this endpoint is the server
     82    * @return  the new instance
     83    * @throws PoloException  if certificates could not be obtained
     84    * @throws IOException    if the socket's streams could not be obtained
     85    */
     86   public static PairingContext fromSslSocket(SSLSocket socket, boolean isServer)
     87       throws PoloException, IOException {
     88     Certificate localCert = PoloUtil.getLocalCert(socket.getSession());
     89     Certificate peerCert = PoloUtil.getPeerCert(socket.getSession());
     90     InputStream input = socket.getInputStream();
     91     OutputStream output = socket.getOutputStream();
     92     return new PairingContext(localCert, peerCert, input, output, isServer);
     93   }
     94 
     95   public void setLocalCertificate(Certificate localCertificate) {
     96     mLocalCertificate = localCertificate;
     97   }
     98 
     99   public Certificate getClientCertificate() {
    100     if (isServer()) {
    101       return mPeerCertificate;
    102     } else {
    103       return mLocalCertificate;
    104     }
    105   }
    106 
    107   public void setPeerCertificate(Certificate peerCertificate) {
    108     mPeerCertificate = peerCertificate;
    109   }
    110 
    111   public Certificate getServerCertificate() {
    112     if (isServer()) {
    113       return mLocalCertificate;
    114     } else {
    115       return mPeerCertificate;
    116     }
    117   }
    118 
    119   public void setPeerInputStream(InputStream peerInputStream) {
    120     mPeerInputStream = peerInputStream;
    121   }
    122 
    123   public InputStream getPeerInputStream() {
    124     return mPeerInputStream;
    125   }
    126 
    127   public void setPeerOutputStream(OutputStream peerOutputStream) {
    128     mPeerOutputStream = peerOutputStream;
    129   }
    130 
    131   public OutputStream getPeerOutputStream() {
    132     return mPeerOutputStream;
    133   }
    134 
    135   public boolean isServer() {
    136     return mIsServer;
    137   }
    138 
    139   public boolean isClient() {
    140     return !(isServer());
    141   }
    142 
    143 }
    144