Home | History | Annotate | Download | only in conscrypt
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      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 org.conscrypt;
     18 
     19 import java.math.BigInteger;
     20 import java.security.interfaces.DSAParams;
     21 import java.security.spec.AlgorithmParameterSpec;
     22 
     23 public class OpenSSLDSAParams implements DSAParams, AlgorithmParameterSpec {
     24 
     25     private OpenSSLKey key;
     26 
     27     private boolean fetchedParams;
     28 
     29     private BigInteger g;
     30 
     31     private BigInteger p;
     32 
     33     private BigInteger q;
     34 
     35     private BigInteger y;
     36 
     37     private BigInteger x;
     38 
     39     OpenSSLDSAParams(OpenSSLKey key) {
     40         this.key = key;
     41     }
     42 
     43     OpenSSLKey getOpenSSLKey() {
     44         return key;
     45     }
     46 
     47     private synchronized final void ensureReadParams() {
     48         if (fetchedParams) {
     49             return;
     50         }
     51 
     52         byte[][] params = NativeCrypto.get_DSA_params(key.getPkeyContext());
     53         if (params[0] != null) {
     54             g = new BigInteger(params[0]);
     55         }
     56         if (params[1] != null) {
     57             p = new BigInteger(params[1]);
     58         }
     59         if (params[2] != null) {
     60             q = new BigInteger(params[2]);
     61         }
     62         if (params[3] != null) {
     63             y = new BigInteger(params[3]);
     64         }
     65         if (params[4] != null) {
     66             x = new BigInteger(params[4]);
     67         }
     68 
     69         fetchedParams = true;
     70     }
     71 
     72     @Override
     73     public BigInteger getG() {
     74         ensureReadParams();
     75         return g;
     76     }
     77 
     78     @Override
     79     public BigInteger getP() {
     80         ensureReadParams();
     81         return p;
     82     }
     83 
     84     @Override
     85     public BigInteger getQ() {
     86         ensureReadParams();
     87         return q;
     88     }
     89 
     90     boolean hasParams() {
     91         ensureReadParams();
     92         return (g != null) && (p != null) && (q != null);
     93     }
     94 
     95     BigInteger getY() {
     96         ensureReadParams();
     97         return y;
     98     }
     99 
    100     BigInteger getX() {
    101         ensureReadParams();
    102         return x;
    103     }
    104 
    105     @Override
    106     public boolean equals(Object o) {
    107         if (o == this) {
    108             return true;
    109         }
    110 
    111         if (o instanceof OpenSSLDSAParams) {
    112             OpenSSLDSAParams other = (OpenSSLDSAParams) o;
    113 
    114             /*
    115              * We can shortcut the true case, but it still may be equivalent but
    116              * different copies.
    117              */
    118             if (key == other.getOpenSSLKey()) {
    119                 return true;
    120             }
    121         }
    122 
    123         if (!(o instanceof DSAParams)) {
    124             return false;
    125         }
    126 
    127         ensureReadParams();
    128 
    129         DSAParams other = (DSAParams) o;
    130         return g.equals(other.getG()) && p.equals(other.getP()) && q.equals(other.getQ());
    131     }
    132 
    133     @Override
    134     public int hashCode() {
    135         ensureReadParams();
    136 
    137         return g.hashCode() ^ p.hashCode() ^ q.hashCode();
    138     }
    139 
    140     @Override
    141     public String toString() {
    142         ensureReadParams();
    143 
    144         final StringBuilder sb = new StringBuilder("OpenSSLDSAParams{");
    145         sb.append("G=");
    146         sb.append(g.toString(16));
    147         sb.append(",P=");
    148         sb.append(p.toString(16));
    149         sb.append(",Q=");
    150         sb.append(q.toString(16));
    151         sb.append('}');
    152 
    153         return sb.toString();
    154     }
    155 }
    156