| 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 PKIHeader handling */ |
| 13 |
|
| 14 |
#include "cmp_local.h" |
| 15 |
#include <openssl/rand.h> /* for RAND_bytes_ex() */ |
| 16 |
|
| 17 |
int ossl_cmp_hdr_set_pvno(OSSL_CMP_PKIHEADER *hdr, int pvno) |
| 18 |
{ |
| 19 |
if (!ossl_assert(hdr != NULL)) |
| 20 |
return 0; |
| 21 |
return ASN1_INTEGER_set(hdr->pvno, pvno); |
| 22 |
} |
| 23 |
|
| 24 |
int ossl_cmp_hdr_get_pvno(const OSSL_CMP_PKIHEADER *hdr) |
| 25 |
{ |
| 26 |
int64_t pvno; |
| 27 |
|
| 28 |
if (!ossl_assert(hdr != NULL)) |
| 29 |
return -1; |
| 30 |
if (!ASN1_INTEGER_get_int64(&pvno, hdr->pvno) || pvno < 0 || pvno > INT_MAX) |
| 31 |
return -1; |
| 32 |
return (int)pvno; |
| 33 |
} |
| 34 |
|
| 35 |
int ossl_cmp_hdr_get_protection_nid(const OSSL_CMP_PKIHEADER *hdr) |
| 36 |
{ |
| 37 |
if (!ossl_assert(hdr != NULL) |
| 38 |
|| hdr->protectionAlg == NULL) |
| 39 |
return NID_undef; |
| 40 |
return OBJ_obj2nid(hdr->protectionAlg->algorithm); |
| 41 |
} |
| 42 |
|
| 43 |
ASN1_OCTET_STRING *OSSL_CMP_HDR_get0_transactionID(const OSSL_CMP_PKIHEADER *hdr) |
| 44 |
{ |
| 45 |
if (hdr == NULL) { |
| 46 |
ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
| 47 |
return NULL; |
| 48 |
} |
| 49 |
return hdr->transactionID; |
| 50 |
} |
| 51 |
|
| 52 |
ASN1_OCTET_STRING *ossl_cmp_hdr_get0_senderNonce(const OSSL_CMP_PKIHEADER *hdr) |
| 53 |
{ |
| 54 |
if (!ossl_assert(hdr != NULL)) |
| 55 |
return NULL; |
| 56 |
return hdr->senderNonce; |
| 57 |
} |
| 58 |
|
| 59 |
ASN1_OCTET_STRING *OSSL_CMP_HDR_get0_recipNonce(const OSSL_CMP_PKIHEADER *hdr) |
| 60 |
{ |
| 61 |
if (hdr == NULL) { |
| 62 |
ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
return hdr->recipNonce; |
| 66 |
} |
| 67 |
|
| 68 |
STACK_OF(OSSL_CMP_ITAV) |
| 69 |
*OSSL_CMP_HDR_get0_geninfo_ITAVs(const OSSL_CMP_PKIHEADER *hdr) |
| 70 |
{ |
| 71 |
if (hdr == NULL) { |
| 72 |
ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
| 73 |
return NULL; |
| 74 |
} |
| 75 |
return hdr->generalInfo; |
| 76 |
} |
| 77 |
|
| 78 |
/* a NULL-DN as an empty sequence of RDNs */ |
| 79 |
int ossl_cmp_general_name_is_NULL_DN(GENERAL_NAME *name) |
| 80 |
{ |
| 81 |
return name == NULL |
| 82 |
|| (name->type == GEN_DIRNAME && IS_NULL_DN(name->d.directoryName)); |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* Set the sender name in PKIHeader. |
| 87 |
* when nm is NULL, sender is set to an empty string |
| 88 |
* returns 1 on success, 0 on error |
| 89 |
*/ |
| 90 |
int ossl_cmp_hdr_set1_sender(OSSL_CMP_PKIHEADER *hdr, const X509_NAME *nm) |
| 91 |
{ |
| 92 |
if (!ossl_assert(hdr != NULL)) |
| 93 |
return 0; |
| 94 |
return GENERAL_NAME_set1_X509_NAME(&hdr->sender, nm); |
| 95 |
} |
| 96 |
|
| 97 |
int ossl_cmp_hdr_set1_recipient(OSSL_CMP_PKIHEADER *hdr, const X509_NAME *nm) |
| 98 |
{ |
| 99 |
if (!ossl_assert(hdr != NULL)) |
| 100 |
return 0; |
| 101 |
return GENERAL_NAME_set1_X509_NAME(&hdr->recipient, nm); |
| 102 |
} |
| 103 |
|
| 104 |
int ossl_cmp_hdr_update_messageTime(OSSL_CMP_PKIHEADER *hdr) |
| 105 |
{ |
| 106 |
if (!ossl_assert(hdr != NULL)) |
| 107 |
return 0; |
| 108 |
if (hdr->messageTime == NULL |
| 109 |
&& (hdr->messageTime = ASN1_GENERALIZEDTIME_new()) == NULL) |
| 110 |
return 0; |
| 111 |
return ASN1_GENERALIZEDTIME_set(hdr->messageTime, time(NULL)) != NULL; |
| 112 |
} |
| 113 |
|
| 114 |
/* assign to *tgt a random byte array of given length */ |
| 115 |
static int set_random(ASN1_OCTET_STRING **tgt, OSSL_CMP_CTX *ctx, int len) |
| 116 |
{ |
| 117 |
unsigned char *bytes = OPENSSL_malloc(len); |
| 118 |
int res = 0; |
| 119 |
|
| 120 |
if (bytes == NULL || RAND_bytes_ex(ctx->libctx, bytes, len, 0) <= 0) |
| 121 |
ERR_raise(ERR_LIB_CMP, CMP_R_FAILURE_OBTAINING_RANDOM); |
| 122 |
else |
| 123 |
res = ossl_cmp_asn1_octet_string_set1_bytes(tgt, bytes, len); |
| 124 |
OPENSSL_free(bytes); |
| 125 |
return res; |
| 126 |
} |
| 127 |
|
| 128 |
int ossl_cmp_hdr_set1_senderKID(OSSL_CMP_PKIHEADER *hdr, |
| 129 |
const ASN1_OCTET_STRING *senderKID) |
| 130 |
{ |
| 131 |
if (!ossl_assert(hdr != NULL)) |
| 132 |
return 0; |
| 133 |
return ossl_cmp_asn1_octet_string_set1(&hdr->senderKID, senderKID); |
| 134 |
} |
| 135 |
|
| 136 |
/* push the given text string to the given PKIFREETEXT ft */ |
| 137 |
int ossl_cmp_hdr_push0_freeText(OSSL_CMP_PKIHEADER *hdr, ASN1_UTF8STRING *text) |
| 138 |
{ |
| 139 |
if (!ossl_assert(hdr != NULL && text != NULL)) |
| 140 |
return 0; |
| 141 |
|
| 142 |
if (hdr->freeText == NULL |
| 143 |
&& (hdr->freeText = sk_ASN1_UTF8STRING_new_null()) == NULL) |
| 144 |
return 0; |
| 145 |
|
| 146 |
return sk_ASN1_UTF8STRING_push(hdr->freeText, text); |
| 147 |
} |
| 148 |
|
| 149 |
int ossl_cmp_hdr_push1_freeText(OSSL_CMP_PKIHEADER *hdr, ASN1_UTF8STRING *text) |
| 150 |
{ |
| 151 |
if (!ossl_assert(hdr != NULL && text != NULL)) |
| 152 |
return 0; |
| 153 |
|
| 154 |
if (hdr->freeText == NULL |
| 155 |
&& (hdr->freeText = sk_ASN1_UTF8STRING_new_null()) == NULL) |
| 156 |
return 0; |
| 157 |
|
| 158 |
return ossl_cmp_sk_ASN1_UTF8STRING_push_str(hdr->freeText, (char *)text->data, |
| 159 |
text->length); |
| 160 |
} |
| 161 |
|
| 162 |
int ossl_cmp_hdr_generalInfo_push0_item(OSSL_CMP_PKIHEADER *hdr, |
| 163 |
OSSL_CMP_ITAV *itav) |
| 164 |
{ |
| 165 |
if (!ossl_assert(hdr != NULL && itav != NULL)) |
| 166 |
return 0; |
| 167 |
return OSSL_CMP_ITAV_push0_stack_item(&hdr->generalInfo, itav); |
| 168 |
} |
| 169 |
|
| 170 |
int ossl_cmp_hdr_generalInfo_push1_items(OSSL_CMP_PKIHEADER *hdr, |
| 171 |
const STACK_OF(OSSL_CMP_ITAV) *itavs) |
| 172 |
{ |
| 173 |
int i; |
| 174 |
OSSL_CMP_ITAV *itav; |
| 175 |
|
| 176 |
if (!ossl_assert(hdr != NULL)) |
| 177 |
return 0; |
| 178 |
|
| 179 |
for (i = 0; i < sk_OSSL_CMP_ITAV_num(itavs); i++) { |
| 180 |
itav = OSSL_CMP_ITAV_dup(sk_OSSL_CMP_ITAV_value(itavs, i)); |
| 181 |
if (itav == NULL) |
| 182 |
return 0; |
| 183 |
|
| 184 |
if (!ossl_cmp_hdr_generalInfo_push0_item(hdr, itav)) { |
| 185 |
OSSL_CMP_ITAV_free(itav); |
| 186 |
return 0; |
| 187 |
} |
| 188 |
} |
| 189 |
return 1; |
| 190 |
} |
| 191 |
|
| 192 |
int ossl_cmp_hdr_set_implicitConfirm(OSSL_CMP_PKIHEADER *hdr) |
| 193 |
{ |
| 194 |
OSSL_CMP_ITAV *itav; |
| 195 |
ASN1_TYPE *asn1null; |
| 196 |
|
| 197 |
if (!ossl_assert(hdr != NULL)) |
| 198 |
return 0; |
| 199 |
asn1null = (ASN1_TYPE *)ASN1_NULL_new(); |
| 200 |
if (asn1null == NULL) |
| 201 |
return 0; |
| 202 |
if ((itav = OSSL_CMP_ITAV_create(OBJ_nid2obj(NID_id_it_implicitConfirm), |
| 203 |
asn1null)) |
| 204 |
== NULL) |
| 205 |
goto err; |
| 206 |
if (!ossl_cmp_hdr_generalInfo_push0_item(hdr, itav)) |
| 207 |
goto err; |
| 208 |
return 1; |
| 209 |
|
| 210 |
err: |
| 211 |
ASN1_TYPE_free(asn1null); |
| 212 |
OSSL_CMP_ITAV_free(itav); |
| 213 |
return 0; |
| 214 |
} |
| 215 |
|
| 216 |
/* return 1 if implicitConfirm in the generalInfo field of the header is set */ |
| 217 |
int ossl_cmp_hdr_has_implicitConfirm(const OSSL_CMP_PKIHEADER *hdr) |
| 218 |
{ |
| 219 |
int itavCount; |
| 220 |
int i; |
| 221 |
OSSL_CMP_ITAV *itav; |
| 222 |
|
| 223 |
if (!ossl_assert(hdr != NULL)) |
| 224 |
return 0; |
| 225 |
|
| 226 |
itavCount = sk_OSSL_CMP_ITAV_num(hdr->generalInfo); |
| 227 |
for (i = 0; i < itavCount; i++) { |
| 228 |
itav = sk_OSSL_CMP_ITAV_value(hdr->generalInfo, i); |
| 229 |
if (itav != NULL |
| 230 |
&& OBJ_obj2nid(itav->infoType) == NID_id_it_implicitConfirm) |
| 231 |
return 1; |
| 232 |
} |
| 233 |
|
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
/* |
| 238 |
* set ctx->transactionID in CMP header |
| 239 |
* if ctx->transactionID is NULL, a random one is created with 128 bit |
| 240 |
* according to section 5.1.1: |
| 241 |
* |
| 242 |
* It is RECOMMENDED that the clients fill the transactionID field with |
| 243 |
* 128 bits of (pseudo-) random data for the start of a transaction to |
| 244 |
* reduce the probability of having the transactionID in use at the server. |
| 245 |
*/ |
| 246 |
int ossl_cmp_hdr_set_transactionID(OSSL_CMP_CTX *ctx, OSSL_CMP_PKIHEADER *hdr) |
| 247 |
{ |
| 248 |
if (ctx->transactionID == NULL) { |
| 249 |
char *tid; |
| 250 |
|
| 251 |
if (!set_random(&ctx->transactionID, ctx, |
| 252 |
OSSL_CMP_TRANSACTIONID_LENGTH)) |
| 253 |
return 0; |
| 254 |
tid = i2s_ASN1_OCTET_STRING(NULL, ctx->transactionID); |
| 255 |
if (tid != NULL) |
| 256 |
ossl_cmp_log1(DEBUG, ctx, |
| 257 |
"Starting new transaction with ID=%s", tid); |
| 258 |
OPENSSL_free(tid); |
| 259 |
} |
| 260 |
|
| 261 |
return ossl_cmp_asn1_octet_string_set1(&hdr->transactionID, |
| 262 |
ctx->transactionID); |
| 263 |
} |
| 264 |
|
| 265 |
/* fill in all fields of the hdr according to the info given in ctx */ |
| 266 |
int ossl_cmp_hdr_init(OSSL_CMP_CTX *ctx, OSSL_CMP_PKIHEADER *hdr) |
| 267 |
{ |
| 268 |
const X509_NAME *sender; |
| 269 |
const X509_NAME *rcp = NULL; |
| 270 |
|
| 271 |
if (!ossl_assert(ctx != NULL && hdr != NULL)) |
| 272 |
return 0; |
| 273 |
|
| 274 |
/* set the CMP version */ |
| 275 |
if (!ossl_cmp_hdr_set_pvno(hdr, OSSL_CMP_PVNO)) |
| 276 |
return 0; |
| 277 |
|
| 278 |
/* |
| 279 |
* If no protection cert nor oldCert nor CSR nor subject is given, |
| 280 |
* sender name is not known to the client and thus set to NULL-DN |
| 281 |
*/ |
| 282 |
sender = ctx->cert != NULL ? X509_get_subject_name(ctx->cert) : ctx->oldCert != NULL ? X509_get_subject_name(ctx->oldCert) |
| 283 |
: ctx->p10CSR != NULL ? X509_REQ_get_subject_name(ctx->p10CSR) |
| 284 |
: ctx->subjectName; |
| 285 |
if (!ossl_cmp_hdr_set1_sender(hdr, sender)) |
| 286 |
return 0; |
| 287 |
|
| 288 |
/* determine recipient entry in PKIHeader */ |
| 289 |
if (ctx->recipient != NULL) |
| 290 |
rcp = ctx->recipient; |
| 291 |
else if (ctx->srvCert != NULL) |
| 292 |
rcp = X509_get_subject_name(ctx->srvCert); |
| 293 |
else if (ctx->issuer != NULL) |
| 294 |
rcp = ctx->issuer; |
| 295 |
else if (ctx->oldCert != NULL) |
| 296 |
rcp = X509_get_issuer_name(ctx->oldCert); |
| 297 |
else if (ctx->cert != NULL) |
| 298 |
rcp = X509_get_issuer_name(ctx->cert); |
| 299 |
if (!ossl_cmp_hdr_set1_recipient(hdr, rcp)) |
| 300 |
return 0; |
| 301 |
|
| 302 |
/* set current time as message time */ |
| 303 |
if (!ossl_cmp_hdr_update_messageTime(hdr)) |
| 304 |
return 0; |
| 305 |
|
| 306 |
if (ctx->recipNonce != NULL |
| 307 |
&& !ossl_cmp_asn1_octet_string_set1(&hdr->recipNonce, |
| 308 |
ctx->recipNonce)) |
| 309 |
return 0; |
| 310 |
|
| 311 |
if (!ossl_cmp_hdr_set_transactionID(ctx, hdr)) |
| 312 |
return 0; |
| 313 |
|
| 314 |
/*- |
| 315 |
* set random senderNonce |
| 316 |
* according to section 5.1.1: |
| 317 |
* |
| 318 |
* senderNonce present |
| 319 |
* -- 128 (pseudo-)random bits |
| 320 |
* The senderNonce and recipNonce fields protect the PKIMessage against |
| 321 |
* replay attacks. The senderNonce will typically be 128 bits of |
| 322 |
* (pseudo-) random data generated by the sender, whereas the recipNonce |
| 323 |
* is copied from the senderNonce of the previous message in the |
| 324 |
* transaction. |
| 325 |
*/ |
| 326 |
if (!set_random(&hdr->senderNonce, ctx, OSSL_CMP_SENDERNONCE_LENGTH)) |
| 327 |
return 0; |
| 328 |
|
| 329 |
/* store senderNonce - for cmp with recipNonce in next outgoing msg */ |
| 330 |
if (!OSSL_CMP_CTX_set1_senderNonce(ctx, hdr->senderNonce)) |
| 331 |
return 0; |
| 332 |
|
| 333 |
/*- |
| 334 |
* freeText [7] PKIFreeText OPTIONAL, |
| 335 |
* -- this may be used to indicate context-specific instructions |
| 336 |
* -- (this field is intended for human consumption) |
| 337 |
*/ |
| 338 |
if (ctx->freeText != NULL |
| 339 |
&& !ossl_cmp_hdr_push1_freeText(hdr, ctx->freeText)) |
| 340 |
return 0; |
| 341 |
|
| 342 |
return 1; |
| 343 |
} |