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