summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/certsrv/base/KeyGenInfo.java
blob: 634b5d90e0f965344d4b527e0c7e2bbc9216ebac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.certsrv.base;


import java.lang.*;
import java.io.IOException;
import netscape.security.util.*;
import netscape.security.x509.*;


/**
 *
 * The <code>KeyGenInfo</code> represents the information generated by
 * the KeyGen tag of the HTML forms. It provides the parsing and accessing
 * mechanisms.<p>
 *
 * <pre>
 * SignedPublicKeyAndChallenge ::= SEQUENCE {
 *      publicKeyAndChallenge PublicKeyAndChallenge,
 *      signatureAlgorithm AlgorithmIdentifier,
 *      signature BIT STRING
 * }
 *
 * PublicKeyAndChallenge ::= SEQUENCE {
 *      spki SubjectPublicKeyInfo,
 *      challenge IA5STRING
 * }
 *</pre>
 *
 *
 * @version $Revision$, $Date$
 */

public class KeyGenInfo {

    /*==========================================================
     * variables
     *==========================================================*/
    private String mSPKACString;
    private byte mPKAC[];
    private byte mSPKAC[];
    private X509Key mSPKI;
    private DerValue mDerSPKI;
    private String mChallenge;
    private DerValue mDerChallenge;
    private byte mSignature[];
    private AlgorithmId mAlgId;

    /*==========================================================
     * constructors
     *==========================================================*/

    /**
     * Construct empty KeyGenInfo. Need to call decode function
     * later to initialize.
     */
    public KeyGenInfo() {

    }

    /**
     * Construct KeyGenInfo using the SignedPublicKeyAndChallenge
     * string representation.
     *
     * @param spkac SignedPublicKeyAndChallenge string representation
     */
    public KeyGenInfo(String spkac)
        throws IOException {
        decode(spkac);
    }

    /*==========================================================
     * public methods
     *==========================================================*/

    /**
     * Initialize using the SPKAC string
     *
     * @param spkac SPKAC string from the end user
     */
    public void decode(String spkac) throws IOException {
        mSPKACString = spkac;
        mSPKAC = base64Decode(spkac);
        derDecode(mSPKAC);
    }

    /**
     * Der encoded into buffer
     *
     * @return Der encoded buffer
     */
    public byte[] encode() {
        return mSPKAC;
    }

    /**
     * Get SPKI in DerValue form
     *
     * @return SPKI in DerValue form
     */
    public DerValue getDerSPKI() {
        return mDerSPKI;
    }

    /**
     * Get SPKI as X509Key
     *
     * @return SPKI in X509Key form
     */
    public X509Key getSPKI() {
        return mSPKI;
    }

    /**
     * Get Challenge phrase in DerValue form
     *
     * @return Challenge in DerValue form. null if none.
     */
    public DerValue getDerChallenge() {
        return mDerChallenge;
    }

    /**
     * Get Challenge phrase in string format
     *
     * @return challenge phrase. null if none.
     */
    public String getChallenge() {
        return mChallenge;
    }

    /**
     * Get Signature
     * @return signature
     */
    public byte[] getSignature() {
        return mSignature;
    }

    /**
     * Get Algorithm ID
     * @return the algorithm id
     */
    public AlgorithmId getAlgorithmId() {
        return mAlgId;
    }

    /**
     * Validate Signature and Challenge Phrase
     *
     * @param challenge phrase; null if none
     * @return true if validated; otherwise, false
     */
    public boolean validateChallenge(String challenge) {
        if (challenge != null) {
            if (!challenge.equals(mChallenge)) {
                return false;
            }
        }
        return true;
    }

    /**
     * String representation of KenGenInfo
     *
     * @return string representation of KeGenInfo
     */
    public String toString() {
        if (mSPKACString != null)
            return mSPKACString;
        return "";
    }

    /*==========================================================
     * private methods
     *==========================================================*/

    private byte[] base64Decode(String spkac)
        throws  IOException {

        return com.netscape.osutil.OSUtil.AtoB(spkac);
    }

    private void derDecode(byte spkac[])
        throws IOException {
        DerInputStream derIn = new DerInputStream(spkac);

        /* get SPKAC Algorithm & Signature */
        DerValue derSPKACContent[] = derIn.getSequence(3);

        mAlgId = AlgorithmId.parse(derSPKACContent[1]);
        mSignature = derSPKACContent[2].getBitString();

        /* get PKAC SPKI & Challenge */
        mPKAC = derSPKACContent[0].toByteArray();
        derIn = new DerInputStream(mPKAC);
        DerValue derPKACContent[] = derIn.getSequence(2);

        mDerSPKI = derPKACContent[0];
        mSPKI = X509Key.parse(derPKACContent[0]);

        mDerChallenge = derPKACContent[1];
        if (mDerChallenge.length() != 0)
            mChallenge = derPKACContent[1].getIA5String();

    }

}