/src/openssl30/crypto/cmp/cmp_status.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright Nokia 2007-2019 |
4 | | * Copyright Siemens AG 2015-2019 |
5 | | * |
6 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
7 | | * this file except in compliance with the License. You can obtain a copy |
8 | | * in the file LICENSE in the source distribution or at |
9 | | * https://www.openssl.org/source/license.html |
10 | | */ |
11 | | |
12 | | /* CMP functions for PKIStatusInfo handling and PKIMessage decomposition */ |
13 | | |
14 | | #include <string.h> |
15 | | |
16 | | #include "cmp_local.h" |
17 | | |
18 | | /* explicit #includes not strictly needed since implied by the above: */ |
19 | | #include <time.h> |
20 | | #include <openssl/cmp.h> |
21 | | #include <openssl/crmf.h> |
22 | | #include <openssl/err.h> /* needed in case config no-deprecated */ |
23 | | #include <openssl/engine.h> |
24 | | #include <openssl/evp.h> |
25 | | #include <openssl/objects.h> |
26 | | #include <openssl/x509.h> |
27 | | #include <openssl/asn1err.h> /* for ASN1_R_TOO_SMALL and ASN1_R_TOO_LARGE */ |
28 | | |
29 | | /* CMP functions related to PKIStatus */ |
30 | | |
31 | | int ossl_cmp_pkisi_get_status(const OSSL_CMP_PKISI *si) |
32 | 0 | { |
33 | 0 | if (!ossl_assert(si != NULL && si->status != NULL)) |
34 | 0 | return -1; |
35 | 0 | return ossl_cmp_asn1_get_int(si->status); |
36 | 0 | } |
37 | | |
38 | | const char *ossl_cmp_PKIStatus_to_string(int status) |
39 | 0 | { |
40 | 0 | switch (status) { |
41 | 0 | case OSSL_CMP_PKISTATUS_accepted: |
42 | 0 | return "PKIStatus: accepted"; |
43 | 0 | case OSSL_CMP_PKISTATUS_grantedWithMods: |
44 | 0 | return "PKIStatus: granted with modifications"; |
45 | 0 | case OSSL_CMP_PKISTATUS_rejection: |
46 | 0 | return "PKIStatus: rejection"; |
47 | 0 | case OSSL_CMP_PKISTATUS_waiting: |
48 | 0 | return "PKIStatus: waiting"; |
49 | 0 | case OSSL_CMP_PKISTATUS_revocationWarning: |
50 | 0 | return "PKIStatus: revocation warning - a revocation of the cert is imminent"; |
51 | 0 | case OSSL_CMP_PKISTATUS_revocationNotification: |
52 | 0 | return "PKIStatus: revocation notification - a revocation of the cert has occurred"; |
53 | 0 | case OSSL_CMP_PKISTATUS_keyUpdateWarning: |
54 | 0 | return "PKIStatus: key update warning - update already done for the cert"; |
55 | 0 | default: |
56 | 0 | ERR_raise_data(ERR_LIB_CMP, CMP_R_ERROR_PARSING_PKISTATUS, |
57 | 0 | "PKIStatus: invalid=%d", status); |
58 | 0 | return NULL; |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | | OSSL_CMP_PKIFREETEXT *ossl_cmp_pkisi_get0_statusString(const OSSL_CMP_PKISI *si) |
63 | 0 | { |
64 | 0 | if (!ossl_assert(si != NULL)) |
65 | 0 | return NULL; |
66 | 0 | return si->statusString; |
67 | 0 | } |
68 | | |
69 | | int ossl_cmp_pkisi_get_pkifailureinfo(const OSSL_CMP_PKISI *si) |
70 | 0 | { |
71 | 0 | int i; |
72 | 0 | int res = 0; |
73 | |
|
74 | 0 | if (!ossl_assert(si != NULL)) |
75 | 0 | return -1; |
76 | 0 | if (si->failInfo != NULL) |
77 | 0 | for (i = 0; i <= OSSL_CMP_PKIFAILUREINFO_MAX; i++) |
78 | 0 | if (ASN1_BIT_STRING_get_bit(si->failInfo, i)) |
79 | 0 | res |= 1 << i; |
80 | 0 | return res; |
81 | 0 | } |
82 | | |
83 | | /*- |
84 | | * convert PKIFailureInfo number to human-readable string |
85 | | * returns pointer to static string, or NULL on error |
86 | | */ |
87 | | static const char *CMP_PKIFAILUREINFO_to_string(int number) |
88 | 0 | { |
89 | 0 | switch (number) { |
90 | 0 | case OSSL_CMP_PKIFAILUREINFO_badAlg: |
91 | 0 | return "badAlg"; |
92 | 0 | case OSSL_CMP_PKIFAILUREINFO_badMessageCheck: |
93 | 0 | return "badMessageCheck"; |
94 | 0 | case OSSL_CMP_PKIFAILUREINFO_badRequest: |
95 | 0 | return "badRequest"; |
96 | 0 | case OSSL_CMP_PKIFAILUREINFO_badTime: |
97 | 0 | return "badTime"; |
98 | 0 | case OSSL_CMP_PKIFAILUREINFO_badCertId: |
99 | 0 | return "badCertId"; |
100 | 0 | case OSSL_CMP_PKIFAILUREINFO_badDataFormat: |
101 | 0 | return "badDataFormat"; |
102 | 0 | case OSSL_CMP_PKIFAILUREINFO_wrongAuthority: |
103 | 0 | return "wrongAuthority"; |
104 | 0 | case OSSL_CMP_PKIFAILUREINFO_incorrectData: |
105 | 0 | return "incorrectData"; |
106 | 0 | case OSSL_CMP_PKIFAILUREINFO_missingTimeStamp: |
107 | 0 | return "missingTimeStamp"; |
108 | 0 | case OSSL_CMP_PKIFAILUREINFO_badPOP: |
109 | 0 | return "badPOP"; |
110 | 0 | case OSSL_CMP_PKIFAILUREINFO_certRevoked: |
111 | 0 | return "certRevoked"; |
112 | 0 | case OSSL_CMP_PKIFAILUREINFO_certConfirmed: |
113 | 0 | return "certConfirmed"; |
114 | 0 | case OSSL_CMP_PKIFAILUREINFO_wrongIntegrity: |
115 | 0 | return "wrongIntegrity"; |
116 | 0 | case OSSL_CMP_PKIFAILUREINFO_badRecipientNonce: |
117 | 0 | return "badRecipientNonce"; |
118 | 0 | case OSSL_CMP_PKIFAILUREINFO_timeNotAvailable: |
119 | 0 | return "timeNotAvailable"; |
120 | 0 | case OSSL_CMP_PKIFAILUREINFO_unacceptedPolicy: |
121 | 0 | return "unacceptedPolicy"; |
122 | 0 | case OSSL_CMP_PKIFAILUREINFO_unacceptedExtension: |
123 | 0 | return "unacceptedExtension"; |
124 | 0 | case OSSL_CMP_PKIFAILUREINFO_addInfoNotAvailable: |
125 | 0 | return "addInfoNotAvailable"; |
126 | 0 | case OSSL_CMP_PKIFAILUREINFO_badSenderNonce: |
127 | 0 | return "badSenderNonce"; |
128 | 0 | case OSSL_CMP_PKIFAILUREINFO_badCertTemplate: |
129 | 0 | return "badCertTemplate"; |
130 | 0 | case OSSL_CMP_PKIFAILUREINFO_signerNotTrusted: |
131 | 0 | return "signerNotTrusted"; |
132 | 0 | case OSSL_CMP_PKIFAILUREINFO_transactionIdInUse: |
133 | 0 | return "transactionIdInUse"; |
134 | 0 | case OSSL_CMP_PKIFAILUREINFO_unsupportedVersion: |
135 | 0 | return "unsupportedVersion"; |
136 | 0 | case OSSL_CMP_PKIFAILUREINFO_notAuthorized: |
137 | 0 | return "notAuthorized"; |
138 | 0 | case OSSL_CMP_PKIFAILUREINFO_systemUnavail: |
139 | 0 | return "systemUnavail"; |
140 | 0 | case OSSL_CMP_PKIFAILUREINFO_systemFailure: |
141 | 0 | return "systemFailure"; |
142 | 0 | case OSSL_CMP_PKIFAILUREINFO_duplicateCertReq: |
143 | 0 | return "duplicateCertReq"; |
144 | 0 | default: |
145 | 0 | return NULL; /* illegal failure number */ |
146 | 0 | } |
147 | 0 | } |
148 | | |
149 | | int ossl_cmp_pkisi_check_pkifailureinfo(const OSSL_CMP_PKISI *si, int bit_index) |
150 | 0 | { |
151 | 0 | if (!ossl_assert(si != NULL && si->failInfo != NULL)) |
152 | 0 | return -1; |
153 | 0 | if (bit_index < 0 || bit_index > OSSL_CMP_PKIFAILUREINFO_MAX) { |
154 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_INVALID_ARGS); |
155 | 0 | return -1; |
156 | 0 | } |
157 | | |
158 | 0 | return ASN1_BIT_STRING_get_bit(si->failInfo, bit_index); |
159 | 0 | } |
160 | | |
161 | | /*- |
162 | | * place human-readable error string created from PKIStatusInfo in given buffer |
163 | | * returns pointer to the same buffer containing the string, or NULL on error |
164 | | */ |
165 | | static |
166 | | char *snprint_PKIStatusInfo_parts(int status, int fail_info, |
167 | | const OSSL_CMP_PKIFREETEXT *status_strings, |
168 | | char *buf, size_t bufsize) |
169 | 0 | { |
170 | 0 | int failure; |
171 | 0 | const char *status_string, *failure_string; |
172 | 0 | ASN1_UTF8STRING *text; |
173 | 0 | int i; |
174 | 0 | int printed_chars; |
175 | 0 | int failinfo_found = 0; |
176 | 0 | int n_status_strings; |
177 | 0 | char *write_ptr = buf; |
178 | |
|
179 | 0 | if (buf == NULL |
180 | 0 | || status < 0 |
181 | 0 | || (status_string = ossl_cmp_PKIStatus_to_string(status)) == NULL) |
182 | 0 | return NULL; |
183 | | |
184 | 0 | #define ADVANCE_BUFFER \ |
185 | 0 | if (printed_chars < 0 || (size_t)printed_chars >= bufsize) \ |
186 | 0 | return NULL; \ |
187 | 0 | write_ptr += printed_chars; \ |
188 | 0 | bufsize -= printed_chars; |
189 | | |
190 | 0 | printed_chars = BIO_snprintf(write_ptr, bufsize, "%s", status_string); |
191 | 0 | ADVANCE_BUFFER; |
192 | | |
193 | | /* |
194 | | * failInfo is optional and may be empty; |
195 | | * if present, print failInfo before statusString because it is more concise |
196 | | */ |
197 | 0 | if (fail_info != -1 && fail_info != 0) { |
198 | 0 | printed_chars = BIO_snprintf(write_ptr, bufsize, "; PKIFailureInfo: "); |
199 | 0 | ADVANCE_BUFFER; |
200 | 0 | for (failure = 0; failure <= OSSL_CMP_PKIFAILUREINFO_MAX; failure++) { |
201 | 0 | if ((fail_info & (1 << failure)) != 0) { |
202 | 0 | failure_string = CMP_PKIFAILUREINFO_to_string(failure); |
203 | 0 | if (failure_string != NULL) { |
204 | 0 | printed_chars = BIO_snprintf(write_ptr, bufsize, "%s%s", |
205 | 0 | failinfo_found ? ", " : "", |
206 | 0 | failure_string); |
207 | 0 | ADVANCE_BUFFER; |
208 | 0 | failinfo_found = 1; |
209 | 0 | } |
210 | 0 | } |
211 | 0 | } |
212 | 0 | } |
213 | 0 | if (!failinfo_found && status != OSSL_CMP_PKISTATUS_accepted |
214 | 0 | && status != OSSL_CMP_PKISTATUS_grantedWithMods) { |
215 | 0 | printed_chars = BIO_snprintf(write_ptr, bufsize, "; <no failure info>"); |
216 | 0 | ADVANCE_BUFFER; |
217 | 0 | } |
218 | | |
219 | | /* statusString sequence is optional and may be empty */ |
220 | 0 | n_status_strings = sk_ASN1_UTF8STRING_num(status_strings); |
221 | 0 | if (n_status_strings > 0) { |
222 | 0 | printed_chars = BIO_snprintf(write_ptr, bufsize, "; StatusString%s: ", |
223 | 0 | n_status_strings > 1 ? "s" : ""); |
224 | 0 | ADVANCE_BUFFER; |
225 | 0 | for (i = 0; i < n_status_strings; i++) { |
226 | 0 | text = sk_ASN1_UTF8STRING_value(status_strings, i); |
227 | 0 | printed_chars = BIO_snprintf(write_ptr, bufsize, "\"%.*s\"%s", |
228 | 0 | ASN1_STRING_length(text), |
229 | 0 | ASN1_STRING_get0_data(text), |
230 | 0 | i < n_status_strings - 1 ? ", " : ""); |
231 | 0 | ADVANCE_BUFFER; |
232 | 0 | } |
233 | 0 | } |
234 | 0 | #undef ADVANCE_BUFFER |
235 | 0 | return buf; |
236 | 0 | } |
237 | | |
238 | | char *OSSL_CMP_snprint_PKIStatusInfo(const OSSL_CMP_PKISI *statusInfo, |
239 | | char *buf, size_t bufsize) |
240 | 0 | { |
241 | 0 | int failure_info; |
242 | |
|
243 | 0 | if (statusInfo == NULL) { |
244 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
245 | 0 | return NULL; |
246 | 0 | } |
247 | | |
248 | 0 | failure_info = ossl_cmp_pkisi_get_pkifailureinfo(statusInfo); |
249 | |
|
250 | 0 | return snprint_PKIStatusInfo_parts(ASN1_INTEGER_get(statusInfo->status), |
251 | 0 | failure_info, |
252 | 0 | statusInfo->statusString, buf, bufsize); |
253 | 0 | } |
254 | | |
255 | | char *OSSL_CMP_CTX_snprint_PKIStatus(const OSSL_CMP_CTX *ctx, char *buf, |
256 | | size_t bufsize) |
257 | 0 | { |
258 | 0 | if (ctx == NULL) { |
259 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
260 | 0 | return NULL; |
261 | 0 | } |
262 | | |
263 | 0 | return snprint_PKIStatusInfo_parts(OSSL_CMP_CTX_get_status(ctx), |
264 | 0 | OSSL_CMP_CTX_get_failInfoCode(ctx), |
265 | 0 | OSSL_CMP_CTX_get0_statusString(ctx), |
266 | 0 | buf, bufsize); |
267 | 0 | } |
268 | | |
269 | | /*- |
270 | | * Creates a new PKIStatusInfo structure and fills it in |
271 | | * returns a pointer to the structure on success, NULL on error |
272 | | * note: strongly overlaps with TS_RESP_CTX_set_status_info() |
273 | | * and TS_RESP_CTX_add_failure_info() in ../ts/ts_rsp_sign.c |
274 | | */ |
275 | | OSSL_CMP_PKISI *OSSL_CMP_STATUSINFO_new(int status, int fail_info, |
276 | | const char *text) |
277 | 6.75k | { |
278 | 6.75k | OSSL_CMP_PKISI *si = OSSL_CMP_PKISI_new(); |
279 | 6.75k | ASN1_UTF8STRING *utf8_text = NULL; |
280 | 6.75k | int failure; |
281 | | |
282 | 6.75k | if (si == NULL) |
283 | 0 | goto err; |
284 | 6.75k | if (!ASN1_INTEGER_set(si->status, status)) |
285 | 0 | goto err; |
286 | | |
287 | 6.75k | if (text != NULL) { |
288 | 6.56k | if ((utf8_text = ASN1_UTF8STRING_new()) == NULL |
289 | 6.56k | || !ASN1_STRING_set(utf8_text, text, -1)) |
290 | 0 | goto err; |
291 | 6.56k | if ((si->statusString = sk_ASN1_UTF8STRING_new_null()) == NULL) |
292 | 0 | goto err; |
293 | 6.56k | if (!sk_ASN1_UTF8STRING_push(si->statusString, utf8_text)) |
294 | 0 | goto err; |
295 | | /* Ownership is lost. */ |
296 | 6.56k | utf8_text = NULL; |
297 | 6.56k | } |
298 | | |
299 | 189k | for (failure = 0; failure <= OSSL_CMP_PKIFAILUREINFO_MAX; failure++) { |
300 | 182k | if ((fail_info & (1 << failure)) != 0) { |
301 | 6.75k | if (si->failInfo == NULL |
302 | 6.75k | && (si->failInfo = ASN1_BIT_STRING_new()) == NULL) |
303 | 0 | goto err; |
304 | 6.75k | if (!ASN1_BIT_STRING_set_bit(si->failInfo, failure, 1)) |
305 | 0 | goto err; |
306 | 6.75k | } |
307 | 182k | } |
308 | 6.75k | return si; |
309 | | |
310 | 0 | err: |
311 | 0 | OSSL_CMP_PKISI_free(si); |
312 | 0 | ASN1_UTF8STRING_free(utf8_text); |
313 | 0 | return NULL; |
314 | 6.75k | } |