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