summaryrefslogtreecommitdiffstats
path: root/base/java-tools/src/com/netscape/cmstools/CMCResponse.java
blob: 7bbba87975d4967c662e865d23b5635a273c859e (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
226
227
228
229
230
231
232
233
234
235
236
237
238
// --- 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.cmstools;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import netscape.security.util.CertPrettyPrint;
import netscape.security.x509.X509CertImpl;

import org.mozilla.jss.asn1.ASN1Util;
import org.mozilla.jss.asn1.INTEGER;
import org.mozilla.jss.asn1.OBJECT_IDENTIFIER;
import org.mozilla.jss.asn1.OCTET_STRING;
import org.mozilla.jss.asn1.SEQUENCE;
import org.mozilla.jss.asn1.SET;
import org.mozilla.jss.pkix.cert.Certificate;
import org.mozilla.jss.pkix.cmc.CMCStatusInfo;
import org.mozilla.jss.pkix.cmc.OtherInfo;
import org.mozilla.jss.pkix.cmc.PendInfo;
import org.mozilla.jss.pkix.cmc.ResponseBody;
import org.mozilla.jss.pkix.cmc.TaggedAttribute;
import org.mozilla.jss.pkix.cms.EncapsulatedContentInfo;

/**
 * Tool for parsing a CMC response
 *
 * <P>
 *
 * @version $Revision$, $Date$
 *
 */
public class CMCResponse {

    public CMCResponse() {
    }

    public static void printOutput(String path, String filename) {
        byte[] bb = new byte[10000];
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filename);
            while (fis.available() > 0)
                fis.read(bb, 0, 10000);
        } catch (Exception e) {
            System.out.println("Error reading the response. Exception: " + e.toString());
            System.exit(1);
        }

        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(bb);
            org.mozilla.jss.pkix.cms.ContentInfo cii = (org.mozilla.jss.pkix.cms.ContentInfo)
                    org.mozilla.jss.pkix.cms.ContentInfo.getTemplate().decode(bis);

            org.mozilla.jss.pkix.cms.SignedData cmcFullResp =
                    (org.mozilla.jss.pkix.cms.SignedData) cii.getInterpretedContent();

            StringBuffer content = new StringBuffer();

            if (cmcFullResp.hasCertificates()) {
                SET certs = cmcFullResp.getCertificates();
                int numCerts = certs.size();

                for (int i = 0; i < numCerts; i++) {
                    Certificate cert = (Certificate) certs.elementAt(i);
                    X509CertImpl certImpl = new X509CertImpl(ASN1Util.encode(cert));
                    CertPrettyPrint print = new CertPrettyPrint(certImpl);
                    content.append(print.toString(Locale.getDefault()));
                }
            }

            System.out.println("Certificates: ");
            System.out.println(content.toString());
            System.out.println("");
            EncapsulatedContentInfo ci = cmcFullResp.getContentInfo();
            OBJECT_IDENTIFIER id = ci.getContentType();
            OBJECT_IDENTIFIER dataid = new OBJECT_IDENTIFIER("1.2.840.113549.1.7.1");
            if (!id.equals(OBJECT_IDENTIFIER.id_cct_PKIResponse) && !id.equals(dataid)) {
                System.out.println("Invalid CMC Response Format");
            }

            if (!ci.hasContent())
                return;

            OCTET_STRING content1 = ci.getContent();
            ByteArrayInputStream bbis = new ByteArrayInputStream(content1.toByteArray());
            ResponseBody responseBody = (ResponseBody) (new ResponseBody.Template()).decode(bbis);
            SEQUENCE controlSequence = responseBody.getControlSequence();

            int numControls = controlSequence.size();
            System.out.println("Number of controls is " + numControls);

            for (int i = 0; i < numControls; i++) {
                TaggedAttribute taggedAttr = (TaggedAttribute) controlSequence.elementAt(i);
                OBJECT_IDENTIFIER type = taggedAttr.getType();

                if (type.equals(OBJECT_IDENTIFIER.id_cmc_cMCStatusInfo)) {
                    System.out.println("Control #" + i + ": CMCStatusInfo");
                    System.out.println("   OID: " + type.toString());
                    SET sts = taggedAttr.getValues();
                    int numSts = sts.size();
                    for (int j = 0; j < numSts; j++) {
                        CMCStatusInfo cst = (CMCStatusInfo) ASN1Util.decode(CMCStatusInfo.getTemplate(),
                                ASN1Util.encode(sts.elementAt(j)));
                        SEQUENCE seq = cst.getBodyList();

                        StringBuilder s = new StringBuilder("   BodyList: ");
                        for (int k = 0; k < seq.size(); k++) {
                            INTEGER n = (INTEGER) seq.elementAt(k);
                            s.append(n.toString() + " ");
                        }
                        System.out.println(s);
                        int st = cst.getStatus();
                        if (st != CMCStatusInfo.SUCCESS && st != CMCStatusInfo.CONFIRM_REQUIRED) {
                            String stString = cst.getStatusString();
                            if (stString != null)
                                System.out.println("   Status String: " + stString);
                            OtherInfo oi = cst.getOtherInfo();
                            OtherInfo.Type t = oi.getType();
                            if (t == OtherInfo.FAIL)
                                System.out.println("   OtherInfo type: FAIL");
                            else if (t == OtherInfo.PEND) {
                                System.out.println("   OtherInfo type: PEND");
                                PendInfo pi = oi.getPendInfo();
                                if (pi.getPendTime() != null) {
                                    String datePattern = "dd/MMM/yyyy:HH:mm:ss z";
                                    SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
                                    Date d = pi.getPendTime().toDate();
                                    System.out.println("   Date: " + dateFormat.format(d));
                                }
                            }
                        } else if (st == CMCStatusInfo.SUCCESS) {
                            System.out.println("   Status: SUCCESS");
                        }
                    }
                } else if (type.equals(OBJECT_IDENTIFIER.id_cmc_transactionId)) {
                    System.out.println("Control #" + i + ": CMC Transaction Id");
                    System.out.println("   OID: " + type.toString());
                    SET transIds = taggedAttr.getValues();
                    INTEGER num = (INTEGER) (ASN1Util.decode(INTEGER.getTemplate(),
                            ASN1Util.encode(transIds.elementAt(0))));
                    System.out.println("   INTEGER: " + num);
                } else if (type.equals(OBJECT_IDENTIFIER.id_cmc_recipientNonce)) {
                    System.out.println("Control #" + i + ": CMC Recipient Nonce");
                    System.out.println("   OID: " + type.toString());
                    SET recipientN = taggedAttr.getValues();
                    OCTET_STRING str =
                            (OCTET_STRING) (ASN1Util.decode(OCTET_STRING.getTemplate(),
                                    ASN1Util.encode(recipientN.elementAt(0))));
                    byte b[] = str.toByteArray();
                    StringBuilder s = new StringBuilder("   Value: ");
                    for (int m = 0; m < b.length; m++) {
                        s.append(b[m]);
                        s.append(" ");
                    }
                    System.out.println(s);
                } else if (type.equals(OBJECT_IDENTIFIER.id_cmc_senderNonce)) {
                    System.out.println("Control #" + i + ": CMC Sender Nonce");
                    System.out.println("   OID: " + type.toString());
                    SET senderN = taggedAttr.getValues();
                    OCTET_STRING str =
                            (OCTET_STRING) (ASN1Util.decode(OCTET_STRING.getTemplate(),
                                    ASN1Util.encode(senderN.elementAt(0))));
                    byte b[] = str.toByteArray();
                    StringBuilder s = new StringBuilder("   Value: ");
                    for (int m = 0; m < b.length; m++) {
                        s.append(b[m]);
                        s.append(" ");
                    }
                    System.out.println(s);
                } else if (type.equals(OBJECT_IDENTIFIER.id_cmc_dataReturn)) {
                    System.out.println("Control #" + i + ": CMC Data Return");
                    System.out.println("   OID: " + type.toString());
                    SET dataReturn = taggedAttr.getValues();
                    OCTET_STRING str =
                            (OCTET_STRING) (ASN1Util.decode(OCTET_STRING.getTemplate(),
                                    ASN1Util.encode(dataReturn.elementAt(0))));
                    byte b[] = str.toByteArray();
                    StringBuilder s = new StringBuilder("   Value: ");
                    for (int m = 0; m < b.length; m++) {
                        s.append(b[m]);
                        s.append(" ");
                    }
                    System.out.println(s);
                }
            }
        } catch (Exception e) {
            System.out.println("Error found in the response. Exception: " + e.toString());
            System.exit(1);

        }
    }

    private static void printUsage() {
        System.out.println("");
        System.out.println(
                "Usage: CMCResponse -d <pathname for cert8.db> -i <pathname for CMC response in binary format> ");
    }

    public static void main(String args[]) {
        String filename = null, path = null;
        if (args.length != 4) {
            printUsage();
            System.exit(1);
        }

        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-d"))
                path = args[i + 1];
            else if (args[i].equals("-i"))
                filename = args[i + 1];
        }

        if (filename == null || path == null) {
            printUsage();
            System.exit(1);
        }
        printOutput(path, filename);
    }
}