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