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
17package com.google.polo.pairing;
18
19import com.google.polo.exception.PoloException;
20
21import java.math.BigInteger;
22import java.security.cert.Certificate;
23
24import javax.net.ssl.SSLPeerUnverifiedException;
25import javax.net.ssl.SSLSession;
26
27/**
28 * Utility methods of general usefulness to the Polo library.
29 */
30public class PoloUtil {
31
32  /**
33   * Returns the peer {@link Certificate} for an {@link SSLSession}.
34   *
35   * @throws PoloException  if the peer certificate could not be obtained
36   *                        from the {@link SSLSession}.
37   * @return                the {@link Certificate} of the peer
38   */
39  public static Certificate getPeerCert(SSLSession session)
40      throws PoloException {
41    try {
42      // Peer certificate
43      Certificate[] certs = session.getPeerCertificates();
44      if (certs == null || certs.length < 1) {
45        throw new PoloException("No peer certificate.");
46      }
47      return certs[0];
48    } catch (SSLPeerUnverifiedException e) {
49      throw new PoloException(e);
50    }
51  }
52
53  /**
54   * Return the local {@link Certificate} for an {@link SSLSession}.
55   *
56   * @throws PoloException  if the local certificate could not be obtained
57   *                        from the {@link SSLSession}
58   * @return                the {@link Certificate} of the peer
59   */
60  public static Certificate getLocalCert(SSLSession session)
61      throws PoloException {
62    Certificate[] certs = session.getLocalCertificates();
63    if (certs == null || certs.length < 1) {
64      throw new PoloException("No local certificate.");
65    }
66    return certs[0];
67  }
68
69  /**
70   * Converts an array of bytes to a string of hexadecimal characters.
71   * Leading null bytes are preserved in the output.
72   * <p>
73   * The input byte stream is assumed to be a positive, two's complement
74   * representation of an integer.  The return value is the hexadecimal string
75   * representation of this value.
76   *
77   * @param bytes  the bytes to convert
78   * @return       the string representation
79   */
80  public static String bytesToHexString(byte[] bytes) {
81    if (bytes == null || bytes.length == 0) {
82      return "";
83    }
84    BigInteger bigint = new BigInteger(1, bytes);
85    int formatLen = bytes.length * 2;
86    return String.format("%0" + formatLen + "x", bigint);
87  }
88
89  /**
90   * Converts a string of hex characters to a byte array.
91   *
92   * @param hexstr  the string of hex characters
93   * @return        a byte array representation
94   */
95  public static byte[] hexStringToBytes(String hexstr) {
96    if (hexstr == null || hexstr.length() == 0 || (hexstr.length() % 2) != 0) {
97      throw new IllegalArgumentException("Bad input string.");
98    }
99
100    byte[] result = new byte[hexstr.length() / 2];
101    for (int i=0; i < result.length; i++) {
102      result[i] = (byte) Integer.parseInt(hexstr.substring(2 * i, 2 * (i + 1)),
103          16);
104    }
105    return result;
106  }
107
108  /**
109   * Converts an integer value to the big endian 4-byte representation.
110   */
111  public static final byte[] intToBigEndianIntBytes(int intVal) {
112    byte[] outBuf = new byte[4];
113    outBuf[0] = (byte)((intVal >> 24) & 0xff);
114    outBuf[1] = (byte)((intVal >> 16) & 0xff);
115    outBuf[2] = (byte)((intVal >> 8) & 0xff);
116    outBuf[3] = (byte)(intVal & 0xff);
117    return outBuf;
118  }
119
120  /**
121   * Converts a 4-byte array of bytes to an unsigned long value.
122   */
123  public static final long intBigEndianBytesToLong(byte[] input) {
124    assert (input.length == 4);
125    long ret = (long)(input[0]) & 0xff;
126    ret <<= 8;
127    ret |= (long)(input[1]) & 0xff;
128    ret <<= 8;
129    ret |= (long)(input[2]) & 0xff;
130    ret <<= 8;
131    ret |= (long)(input[3]) & 0xff;
132    return ret;
133  }
134
135}
136