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