summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/certsrv/logging/event/CertRequestProcessedEvent.java
blob: d095ab62fc29d17f219706a347f37bb30b48a6ee (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
// --- 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) 2017 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.certsrv.logging.event;

import java.security.cert.CertificateEncodingException;

import com.netscape.certsrv.logging.AuditEvent;
import com.netscape.certsrv.logging.ILogger;
import com.netscape.certsrv.request.IRequest;
import com.netscape.cmsutil.util.Utils;

import netscape.security.x509.X509CertImpl;

public class CertRequestProcessedEvent extends AuditEvent {

    private static final long serialVersionUID = 1L;

    private final static String LOGGING_PROPERTY =
            "LOGGING_SIGNED_AUDIT_CERT_REQUEST_PROCESSED";

    public final static String SIGNED_AUDIT_CERT_REQUEST_REASON = "requestNotes";

    public CertRequestProcessedEvent(
            String subjectID,
            String outcome,
            String requesterID,
            String infoName,
            String infoValue) {

        super(LOGGING_PROPERTY);

        setAttribute("InfoName", infoName);
        setAttribute("InfoValue", infoValue);

        setParameters(new Object[] {
                subjectID,
                outcome,
                requesterID,
                getAttributeList()
        });
    }

    public CertRequestProcessedEvent(
            String subjectID,
            String outcome,
            String requesterID,
            String infoName,
            X509CertImpl x509cert) {

        super(LOGGING_PROPERTY);

        setAttribute("CertSerialNum", x509cert.getSerialNumber());

        setParameters(new Object[] {
                subjectID,
                outcome,
                requesterID,
                getAttributeList()
        });
    }

    public CertRequestProcessedEvent(
            String subjectID,
            String outcome,
            String requesterID,
            String infoName,
            IRequest request) {

        super(LOGGING_PROPERTY);

        setAttribute("InfoName", infoName);
        setAttribute("InfoValue", auditInfoValue(request));

        setParameters(new Object[] {
                subjectID,
                outcome,
                requesterID,
                getAttributeList()
        });
    }

    /**
     * Signed Audit Log Info Certificate Value
     *
     * This method is called to obtain the certificate from the passed in
     * "X509CertImpl" for a signed audit log message.
     * <P>
     *
     * @param x509cert an X509CertImpl
     * @return cert string containing the certificate
     */
    String auditInfoCertValue(X509CertImpl x509cert) {

        if (x509cert == null) {
            return ILogger.SIGNED_AUDIT_EMPTY_VALUE;
        }

        byte rawData[] = null;

        try {
            rawData = x509cert.getEncoded();
        } catch (CertificateEncodingException e) {
            return ILogger.SIGNED_AUDIT_EMPTY_VALUE;
        }

        String cert = null;

        // convert "rawData" into "base64Data"
        if (rawData != null) {
            String base64Data = Utils.base64encode(rawData).trim();

            // concatenate lines
            cert = base64Data.replace("\r", "").replace("\n", "");
        }

        if (cert != null) {
            cert = cert.trim();

            if (cert.equals("")) {
                return ILogger.SIGNED_AUDIT_EMPTY_VALUE;
            } else {
                return cert;
            }
        } else {
            return ILogger.SIGNED_AUDIT_EMPTY_VALUE;
        }
    }

    /**
     * Signed Audit Log Info Value
     *
     * This method is called to obtain the "reason" for
     * a signed audit log message.
     * <P>
     *
     * @param request the actual request
     * @return reason string containing the signed audit log message reason
     */
    String auditInfoValue(IRequest request) {

        String reason = ILogger.SIGNED_AUDIT_EMPTY_VALUE;

        if (request != null) {
            // overwrite "reason" if and only if "info" != null
            String info =
                    request.getExtDataInString(SIGNED_AUDIT_CERT_REQUEST_REASON);

            if (info != null) {
                reason = info.trim();

                // overwrite "reason" if and only if "reason" is empty
                if (reason.equals("")) {
                    reason = ILogger.SIGNED_AUDIT_EMPTY_VALUE;
                }
            }
        }

        return reason;
    }
}