Remove function name from errors
[openssl.git] / crypto / crmf / crmf_lib.c
1 /*-
2  * Copyright 2007-2019 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright Nokia 2007-2018
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  * CRMF implementation by Martin Peylo, Miikka Viljanen, and David von Oheimb.
12  */
13
14 /*
15  * This file contains the functions that handle the individual items inside
16  * the CRMF structures
17  */
18
19 /*
20  * NAMING
21  *
22  * The 0 functions use the supplied structure pointer directly in the parent and
23  * it will be freed up when the parent is freed.
24  *
25  * The 1 functions use a copy of the supplied structure pointer (or in some
26  * cases increases its link count) in the parent and so both should be freed up.
27  */
28
29 #include <openssl/asn1t.h>
30
31 #include "crmf_int.h"
32
33 /* explicit #includes not strictly needed since implied by the above: */
34 #include <openssl/crmf.h>
35 #include <openssl/err.h>
36 #include <openssl/evp.h>
37
38 /*-
39  * atyp = Attribute Type
40  * valt = Value Type
41  * ctrlinf = "regCtrl" or "regInfo"
42  */
43 #define IMPLEMENT_CRMF_CTRL_FUNC(atyp, valt, ctrlinf)                     \
44 int OSSL_CRMF_MSG_set1_##ctrlinf##_##atyp(OSSL_CRMF_MSG *msg,             \
45                                           const valt *in)                 \
46 {                                                                         \
47     OSSL_CRMF_ATTRIBUTETYPEANDVALUE *atav = NULL;                         \
48                                                                           \
49     if (msg == NULL || in  == NULL)                                       \
50         goto err;                                                         \
51     if ((atav = OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new()) == NULL)           \
52         goto err;                                                         \
53     if ((atav->type = OBJ_nid2obj(NID_id_##ctrlinf##_##atyp)) == NULL)    \
54         goto err;                                                         \
55     if ((atav->value.atyp = valt##_dup(in)) == NULL)                      \
56         goto err;                                                         \
57     if (!OSSL_CRMF_MSG_push0_##ctrlinf(msg, atav))                        \
58         goto err;                                                         \
59     return 1;                                                             \
60  err:                                                                     \
61     OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(atav);                           \
62     return 0;                                                             \
63 }
64
65
66 /*-
67  * Pushes the given control attribute into the controls stack of a CertRequest
68  * (section 6)
69  * returns 1 on success, 0 on error
70  */
71 static int OSSL_CRMF_MSG_push0_regCtrl(OSSL_CRMF_MSG *crm,
72                                        OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ctrl)
73 {
74     int new = 0;
75
76     if (crm == NULL || crm->certReq == NULL || ctrl == NULL) {
77         CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGCTRL, CRMF_R_NULL_ARGUMENT);
78         return 0;
79     }
80
81     if (crm->certReq->controls == NULL) {
82         crm->certReq->controls = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
83         if (crm->certReq->controls == NULL)
84             goto oom;
85         new = 1;
86     }
87     if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->certReq->controls, ctrl))
88         goto oom;
89
90     return 1;
91  oom:
92     CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGCTRL, ERR_R_MALLOC_FAILURE);
93
94     if (new != 0) {
95         sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(crm->certReq->controls);
96         crm->certReq->controls = NULL;
97     }
98     return 0;
99 }
100
101  /* id-regCtrl-regToken Control (section 6.1) */
102 IMPLEMENT_CRMF_CTRL_FUNC(regToken, ASN1_STRING, regCtrl)
103
104  /* id-regCtrl-authenticator Control (section 6.2) */
105 #define ASN1_UTF8STRING_dup ASN1_STRING_dup
106 IMPLEMENT_CRMF_CTRL_FUNC(authenticator, ASN1_UTF8STRING, regCtrl)
107
108 int OSSL_CRMF_MSG_set0_SinglePubInfo(OSSL_CRMF_SINGLEPUBINFO *spi,
109                                      int method, GENERAL_NAME *nm)
110 {
111     if (spi == NULL
112             || method < OSSL_CRMF_PUB_METHOD_DONTCARE
113             || method > OSSL_CRMF_PUB_METHOD_LDAP) {
114         CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_SINGLEPUBINFO,
115                 ERR_R_PASSED_INVALID_ARGUMENT);
116         return 0;
117     }
118
119     if (!ASN1_INTEGER_set(spi->pubMethod, method))
120         return 0;
121     GENERAL_NAME_free(spi->pubLocation);
122     spi->pubLocation = nm;
123     return 1;
124 }
125
126 int OSSL_CRMF_MSG_PKIPublicationInfo_push0_SinglePubInfo(
127                                  OSSL_CRMF_PKIPUBLICATIONINFO *pi,
128                                  OSSL_CRMF_SINGLEPUBINFO *spi)
129 {
130     if (pi == NULL || spi == NULL) {
131         CRMFerr(CRMF_F_OSSL_CRMF_MSG_PKIPUBLICATIONINFO_PUSH0_SINGLEPUBINFO,
132                 CRMF_R_NULL_ARGUMENT);
133         return 0;
134     }
135     if (pi->pubInfos == NULL)
136         pi->pubInfos = sk_OSSL_CRMF_SINGLEPUBINFO_new_null();
137     if (pi->pubInfos == NULL)
138         goto oom;
139
140     if (!sk_OSSL_CRMF_SINGLEPUBINFO_push(pi->pubInfos, spi))
141         goto oom;
142     return 1;
143
144  oom:
145     CRMFerr(CRMF_F_OSSL_CRMF_MSG_PKIPUBLICATIONINFO_PUSH0_SINGLEPUBINFO,
146             ERR_R_MALLOC_FAILURE);
147     return 0;
148 }
149
150 int OSSL_CRMF_MSG_set_PKIPublicationInfo_action(
151                                  OSSL_CRMF_PKIPUBLICATIONINFO *pi, int action)
152 {
153     if (pi == NULL
154             || action < OSSL_CRMF_PUB_ACTION_DONTPUBLISH
155             || action > OSSL_CRMF_PUB_ACTION_PLEASEPUBLISH) {
156         CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_PKIPUBLICATIONINFO_ACTION,
157                 ERR_R_PASSED_INVALID_ARGUMENT);
158         return 0;
159     }
160
161     return ASN1_INTEGER_set(pi->action, action);
162 }
163
164  /* id-regCtrl-pkiPublicationInfo Control (section 6.3) */
165 IMPLEMENT_CRMF_CTRL_FUNC(pkiPublicationInfo, OSSL_CRMF_PKIPUBLICATIONINFO,
166                          regCtrl)
167
168  /* id-regCtrl-oldCertID Control (section 6.5) from the given */
169 IMPLEMENT_CRMF_CTRL_FUNC(oldCertID, OSSL_CRMF_CERTID, regCtrl)
170
171 OSSL_CRMF_CERTID *OSSL_CRMF_CERTID_gen(const X509_NAME *issuer,
172                                        const ASN1_INTEGER *serial)
173 {
174     OSSL_CRMF_CERTID *cid = NULL;
175
176     if (issuer == NULL || serial == NULL) {
177         CRMFerr(CRMF_F_OSSL_CRMF_CERTID_GEN, CRMF_R_NULL_ARGUMENT);
178         return NULL;
179     }
180
181     if ((cid = OSSL_CRMF_CERTID_new()) == NULL)
182         goto oom;
183
184     if (!X509_NAME_set(&cid->issuer->d.directoryName, issuer))
185         goto oom;
186     cid->issuer->type = GEN_DIRNAME;
187
188     ASN1_INTEGER_free(cid->serialNumber);
189     if ((cid->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
190         goto oom;
191
192     return cid;
193
194  oom:
195     CRMFerr(CRMF_F_OSSL_CRMF_CERTID_GEN, ERR_R_MALLOC_FAILURE);
196     OSSL_CRMF_CERTID_free(cid);
197     return NULL;
198 }
199
200  /*
201   * id-regCtrl-protocolEncrKey Control (section 6.6)
202   *
203   * For some reason X509_PUBKEY_dup() is not implemented in OpenSSL X509
204   * TODO: check whether that should go elsewhere
205   */
206 static IMPLEMENT_ASN1_DUP_FUNCTION(X509_PUBKEY)
207 IMPLEMENT_CRMF_CTRL_FUNC(protocolEncrKey, X509_PUBKEY, regCtrl)
208
209 /*-
210  * Pushes the attribute given in regInfo in to the CertReqMsg->regInfo stack.
211  * (section 7)
212  * returns 1 on success, 0 on error
213  */
214 static int OSSL_CRMF_MSG_push0_regInfo(OSSL_CRMF_MSG *crm,
215                                        OSSL_CRMF_ATTRIBUTETYPEANDVALUE *ri)
216 {
217     STACK_OF(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) *info = NULL;
218
219     if (crm == NULL || ri == NULL) {
220         CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGINFO, CRMF_R_NULL_ARGUMENT);
221         return 0;
222     }
223
224     if (crm->regInfo == NULL)
225         crm->regInfo = info = sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_new_null();
226     if (crm->regInfo == NULL)
227         goto oom;
228     if (!sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_push(crm->regInfo, ri))
229         goto oom;
230     return 1;
231
232  oom:
233     CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_REGINFO, ERR_R_MALLOC_FAILURE);
234     if (info != NULL)
235         crm->regInfo = NULL;
236     sk_OSSL_CRMF_ATTRIBUTETYPEANDVALUE_free(info);
237     return 0;
238 }
239
240 /* id-regInfo-utf8Pairs to regInfo (section 7.1) */
241 IMPLEMENT_CRMF_CTRL_FUNC(utf8Pairs, ASN1_UTF8STRING, regInfo)
242
243 /* id-regInfo-certReq to regInfo (section 7.2) */
244 IMPLEMENT_CRMF_CTRL_FUNC(certReq, OSSL_CRMF_CERTREQUEST, regInfo)
245
246
247 /* retrieves the certificate template of crm */
248 OSSL_CRMF_CERTTEMPLATE *OSSL_CRMF_MSG_get0_tmpl(const OSSL_CRMF_MSG *crm)
249 {
250     if (crm == NULL || crm->certReq == NULL) {
251         CRMFerr(CRMF_F_OSSL_CRMF_MSG_GET0_TMPL, CRMF_R_NULL_ARGUMENT);
252         return NULL;
253     }
254     return crm->certReq->certTemplate;
255 }
256
257
258 int OSSL_CRMF_MSG_set_validity(OSSL_CRMF_MSG *crm, time_t from, time_t to)
259 {
260     OSSL_CRMF_OPTIONALVALIDITY *vld = NULL;
261     ASN1_TIME *from_asn = NULL;
262     ASN1_TIME *to_asn = NULL;
263     OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
264
265     if (tmpl == NULL) { /* also crm == NULL implies this */
266         CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_VALIDITY, CRMF_R_NULL_ARGUMENT);
267         return 0;
268     }
269
270     if (from != 0 && ((from_asn = ASN1_TIME_set(NULL, from)) == NULL))
271         goto oom;
272     if (to != 0 && ((to_asn = ASN1_TIME_set(NULL, to)) == NULL))
273         goto oom;
274     if ((vld = OSSL_CRMF_OPTIONALVALIDITY_new()) == NULL)
275         goto oom;
276
277     vld->notBefore = from_asn;
278     vld->notAfter = to_asn;
279
280     tmpl->validity = vld;
281
282     return 1;
283  oom:
284     CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_VALIDITY, ERR_R_MALLOC_FAILURE);
285     ASN1_TIME_free(from_asn);
286     ASN1_TIME_free(to_asn);
287     return 0;
288 }
289
290
291 int OSSL_CRMF_MSG_set_certReqId(OSSL_CRMF_MSG *crm, int rid)
292 {
293     if (crm == NULL || crm->certReq == NULL || crm->certReq->certReqId == NULL) {
294         CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET_CERTREQID, CRMF_R_NULL_ARGUMENT);
295         return 0;
296     }
297
298     return ASN1_INTEGER_set(crm->certReq->certReqId, rid);
299 }
300
301 /* get ASN.1 encoded integer, return -1 on error */
302 static int crmf_asn1_get_int(const ASN1_INTEGER *a)
303 {
304     int64_t res;
305
306     if (!ASN1_INTEGER_get_int64(&res, a)) {
307         CRMFerr(0, ASN1_R_INVALID_NUMBER);
308         return -1;
309     }
310     if (res < INT_MIN) {
311         CRMFerr(0, ASN1_R_TOO_SMALL);
312         return -1;
313     }
314     if (res > INT_MAX) {
315         CRMFerr(0, ASN1_R_TOO_LARGE);
316         return -1;
317     }
318     return (int)res;
319 }
320
321 int OSSL_CRMF_MSG_get_certReqId(OSSL_CRMF_MSG *crm)
322 {
323     if (crm == NULL || /* not really needed: */ crm->certReq == NULL) {
324         CRMFerr(CRMF_F_OSSL_CRMF_MSG_GET_CERTREQID, CRMF_R_NULL_ARGUMENT);
325         return -1;
326     }
327     return crmf_asn1_get_int(crm->certReq->certReqId);
328 }
329
330
331 int OSSL_CRMF_MSG_set0_extensions(OSSL_CRMF_MSG *crm,
332                                   X509_EXTENSIONS *exts)
333 {
334     OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
335
336     if (tmpl == NULL) { /* also crm == NULL implies this */
337         CRMFerr(CRMF_F_OSSL_CRMF_MSG_SET0_EXTENSIONS, CRMF_R_NULL_ARGUMENT);
338         return 0;
339     }
340
341     if (sk_X509_EXTENSION_num(exts) == 0) {
342         sk_X509_EXTENSION_free(exts);
343         exts = NULL; /* do not include empty extensions list */
344     }
345
346     sk_X509_EXTENSION_pop_free(tmpl->extensions, X509_EXTENSION_free);
347     tmpl->extensions = exts;
348     return 1;
349 }
350
351
352 int OSSL_CRMF_MSG_push0_extension(OSSL_CRMF_MSG *crm,
353                                   const X509_EXTENSION *ext)
354 {
355     int new = 0;
356     OSSL_CRMF_CERTTEMPLATE *tmpl = OSSL_CRMF_MSG_get0_tmpl(crm);
357
358     if (tmpl == NULL || ext == NULL) { /* also crm == NULL implies this */
359         CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_EXTENSION, CRMF_R_NULL_ARGUMENT);
360         return 0;
361     }
362
363     if (tmpl->extensions == NULL) {
364         if ((tmpl->extensions = sk_X509_EXTENSION_new_null()) == NULL)
365             goto oom;
366         new = 1;
367     }
368
369     if (!sk_X509_EXTENSION_push(tmpl->extensions, (X509_EXTENSION *)ext))
370         goto oom;
371     return 1;
372  oom:
373     CRMFerr(CRMF_F_OSSL_CRMF_MSG_PUSH0_EXTENSION, ERR_R_MALLOC_FAILURE);
374
375     if (new != 0) {
376         sk_X509_EXTENSION_free(tmpl->extensions);
377         tmpl->extensions = NULL;
378     }
379     return 0;
380 }
381
382 /* TODO: support cases 1+2 (besides case 3) defined in RFC 4211, section 4.1. */
383 static int CRMF_poposigningkey_init(OSSL_CRMF_POPOSIGNINGKEY *ps,
384                                     OSSL_CRMF_CERTREQUEST *cr,
385                                     EVP_PKEY *pkey, int dgst)
386 {
387     int len;
388     size_t crlen;
389     size_t siglen;
390     unsigned char *crder = NULL, *sig = NULL;
391     int alg_nid = 0;
392     int md_nid = 0;
393     const EVP_MD *alg = NULL;
394     EVP_MD_CTX *ctx = NULL;
395     int ret = 0;
396
397     if (ps == NULL || cr == NULL || pkey == NULL) {
398         CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, CRMF_R_NULL_ARGUMENT);
399         return 0;
400     }
401
402     /* OpenSSL defaults all bit strings to be encoded as ASN.1 NamedBitList */
403     ps->signature->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
404     ps->signature->flags |= ASN1_STRING_FLAG_BITS_LEFT;
405
406     len = i2d_OSSL_CRMF_CERTREQUEST(cr, &crder);
407     if (len < 0 || crder == NULL) {
408         CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, CRMF_R_ERROR);
409         goto err;
410     }
411     crlen = (size_t)len;
412
413     if (!OBJ_find_sigid_by_algs(&alg_nid, dgst, EVP_PKEY_id(pkey))) {
414         CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT,
415                 CRMF_R_UNSUPPORTED_ALG_FOR_POPSIGNINGKEY);
416         goto err;
417     }
418     if (!OBJ_find_sigid_algs(alg_nid, &md_nid, NULL)
419             || (alg = EVP_get_digestbynid(md_nid)) == NULL) {
420         CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT,
421                 CRMF_R_UNSUPPORTED_ALG_FOR_POPSIGNINGKEY);
422         goto err;
423     }
424     if (!X509_ALGOR_set0(ps->algorithmIdentifier, OBJ_nid2obj(alg_nid),
425                          V_ASN1_NULL, NULL)
426             || (ctx = EVP_MD_CTX_new()) == NULL
427             || EVP_DigestSignInit(ctx, NULL, alg, NULL, pkey) <= 0
428             || EVP_DigestSignUpdate(ctx, crder, crlen) <= 0
429             || EVP_DigestSignFinal(ctx, NULL, &siglen) <= 0) {
430         CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, CRMF_R_ERROR);
431         goto err;
432     }
433     if ((sig = OPENSSL_malloc(siglen)) == NULL) {
434         CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, ERR_R_MALLOC_FAILURE);
435         goto err;
436     }
437     if (EVP_DigestSignFinal(ctx, sig, &siglen) <= 0
438             || !ASN1_BIT_STRING_set(ps->signature, sig, siglen)) {
439         CRMFerr(CRMF_F_CRMF_POPOSIGNINGKEY_INIT, CRMF_R_ERROR);
440         goto err;
441     }
442     ret = 1;
443
444  err:
445     OPENSSL_free(crder);
446     EVP_MD_CTX_free(ctx);
447     OPENSSL_free(sig);
448     return ret;
449 }
450
451
452 int OSSL_CRMF_MSG_create_popo(OSSL_CRMF_MSG *crm, EVP_PKEY *pkey,
453                               int dgst, int ppmtd)
454 {
455     OSSL_CRMF_POPO *pp = NULL;
456     ASN1_INTEGER *tag = NULL;
457
458     if (crm == NULL || (ppmtd == OSSL_CRMF_POPO_SIGNATURE && pkey == NULL)) {
459         CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO, CRMF_R_NULL_ARGUMENT);
460         return 0;
461     }
462
463     if (ppmtd == OSSL_CRMF_POPO_NONE)
464         goto end;
465     if ((pp = OSSL_CRMF_POPO_new()) == NULL)
466         goto oom;
467     pp->type = ppmtd;
468
469     switch (ppmtd) {
470     case OSSL_CRMF_POPO_RAVERIFIED:
471         if ((pp->value.raVerified = ASN1_NULL_new()) == NULL)
472             goto oom;
473         break;
474
475     case OSSL_CRMF_POPO_SIGNATURE:
476         {
477             OSSL_CRMF_POPOSIGNINGKEY *ps = OSSL_CRMF_POPOSIGNINGKEY_new();
478             if (ps == NULL
479                     || !CRMF_poposigningkey_init(ps, crm->certReq, pkey, dgst)){
480                 OSSL_CRMF_POPOSIGNINGKEY_free(ps);
481                 goto err;
482             }
483             pp->value.signature = ps;
484         }
485         break;
486
487     case OSSL_CRMF_POPO_KEYENC:
488         if ((pp->value.keyEncipherment = OSSL_CRMF_POPOPRIVKEY_new()) == NULL)
489             goto oom;
490         tag = ASN1_INTEGER_new();
491         pp->value.keyEncipherment->type =
492             OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE;
493         pp->value.keyEncipherment->value.subsequentMessage = tag;
494         if (tag == NULL
495                 || !ASN1_INTEGER_set(tag, OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT))
496             goto oom;
497         break;
498
499     default:
500         CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO,
501                 CRMF_R_UNSUPPORTED_METHOD_FOR_CREATING_POPO);
502         goto err;
503     }
504
505  end:
506     OSSL_CRMF_POPO_free(crm->popo);
507     crm->popo = pp;
508
509     return 1;
510  oom:
511     CRMFerr(CRMF_F_OSSL_CRMF_MSG_CREATE_POPO, ERR_R_MALLOC_FAILURE);
512  err:
513     OSSL_CRMF_POPO_free(pp);
514     return 0;
515 }
516
517 /* returns 0 for equal, -1 for a < b or error on a, 1 for a > b or error on b */
518 static int X509_PUBKEY_cmp(X509_PUBKEY *a, X509_PUBKEY *b)
519 {
520     X509_ALGOR *algA = NULL, *algB = NULL;
521     int res = 0;
522
523     if (a == b)
524         return 0;
525     if (a == NULL || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a)
526             || algA == NULL)
527         return -1;
528     if (b == NULL || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b)
529             || algB == NULL)
530         return 1;
531     if ((res = X509_ALGOR_cmp(algA, algB)) != 0)
532         return res;
533     return EVP_PKEY_cmp(X509_PUBKEY_get0(a), X509_PUBKEY_get0(b));
534 }
535
536 /* verifies the Proof-of-Possession of the request with the given rid in reqs */
537 int OSSL_CRMF_MSGS_verify_popo(const OSSL_CRMF_MSGS *reqs,
538                                int rid, int acceptRAVerified)
539 {
540     OSSL_CRMF_MSG *req = NULL;
541     X509_PUBKEY *pubkey = NULL;
542     OSSL_CRMF_POPOSIGNINGKEY *sig = NULL;
543
544     if (reqs == NULL
545             || (req = sk_OSSL_CRMF_MSG_value(reqs, rid)) == NULL
546             || req->popo == NULL) {
547         CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO,
548                 CRMF_R_NULL_ARGUMENT);
549         return 0;
550     }
551
552     switch (req->popo->type) {
553     case OSSL_CRMF_POPO_RAVERIFIED:
554         if (acceptRAVerified)
555             return 1;
556         break;
557     case OSSL_CRMF_POPO_SIGNATURE:
558         pubkey = req->certReq->certTemplate->publicKey;
559         sig = req->popo->value.signature;
560         if (sig->poposkInput != NULL) {
561             /*
562              * According to RFC 4211: publicKey contains a copy of
563              * the public key from the certificate template. This MUST be
564              * exactly the same value as contained in the certificate template.
565              */
566             if (pubkey == NULL
567                     || sig->poposkInput->publicKey == NULL
568                     || X509_PUBKEY_cmp(pubkey, sig->poposkInput->publicKey)
569                     || ASN1_item_verify(
570                            ASN1_ITEM_rptr(OSSL_CRMF_POPOSIGNINGKEYINPUT),
571                            sig->algorithmIdentifier, sig->signature,
572                            sig->poposkInput, X509_PUBKEY_get0(pubkey)) < 1)
573                 break;
574         } else {
575             if (pubkey == NULL
576                     || req->certReq->certTemplate->subject == NULL
577                     || ASN1_item_verify(ASN1_ITEM_rptr(OSSL_CRMF_CERTREQUEST),
578                                     sig->algorithmIdentifier, sig->signature,
579                                     req->certReq,
580                                     X509_PUBKEY_get0(pubkey)) < 1)
581                 break;
582         }
583         return 1;
584     case OSSL_CRMF_POPO_KEYENC:
585         /*
586          * TODO: when OSSL_CMP_certrep_new() supports encrypted certs,
587          * return 1 if the type of req->popo->value.keyEncipherment
588          * is OSSL_CRMF_POPOPRIVKEY_SUBSEQUENTMESSAGE and
589          * its value.subsequentMessage == OSSL_CRMF_SUBSEQUENTMESSAGE_ENCRCERT
590         */
591     case OSSL_CRMF_POPO_KEYAGREE:
592     default:
593         CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO,
594                 CRMF_R_UNSUPPORTED_POPO_METHOD);
595         return 0;
596     }
597     CRMFerr(CRMF_F_OSSL_CRMF_MSGS_VERIFY_POPO,
598             CRMF_R_UNSUPPORTED_POPO_NOT_ACCEPTED);
599     return 0;
600 }
601
602 /* retrieves the serialNumber of the given cert template or NULL on error */
603 ASN1_INTEGER *OSSL_CRMF_CERTTEMPLATE_get0_serialNumber(OSSL_CRMF_CERTTEMPLATE *tmpl)
604 {
605     return tmpl != NULL ? tmpl->serialNumber : NULL;
606 }
607
608 /* retrieves the issuer name of the given cert template or NULL on error */
609 X509_NAME *OSSL_CRMF_CERTTEMPLATE_get0_issuer(OSSL_CRMF_CERTTEMPLATE *tmpl)
610 {
611     return tmpl != NULL ? tmpl->issuer : NULL;
612 }
613
614 /*
615  * fill in certificate template.
616  * Any value argument that is NULL will leave the respective field unchanged.
617  */
618 int OSSL_CRMF_CERTTEMPLATE_fill(OSSL_CRMF_CERTTEMPLATE *tmpl,
619                                 EVP_PKEY *pubkey,
620                                 const X509_NAME *subject,
621                                 const X509_NAME *issuer,
622                                 const ASN1_INTEGER *serial)
623 {
624     if (tmpl == NULL) {
625         CRMFerr(CRMF_F_OSSL_CRMF_CERTTEMPLATE_FILL, CRMF_R_NULL_ARGUMENT);
626         return 0;
627     }
628     if (subject != NULL && !X509_NAME_set(&tmpl->subject, subject))
629         goto oom;
630     if (issuer != NULL && !X509_NAME_set(&tmpl->issuer, issuer))
631         goto oom;
632     if (serial != NULL) {
633         ASN1_INTEGER_free(tmpl->serialNumber);
634         if ((tmpl->serialNumber = ASN1_INTEGER_dup(serial)) == NULL)
635             goto oom;
636     }
637     if (pubkey != NULL && !X509_PUBKEY_set(&tmpl->publicKey, pubkey))
638         goto oom;
639     return 1;
640
641  oom:
642     CRMFerr(CRMF_F_OSSL_CRMF_CERTTEMPLATE_FILL, ERR_R_MALLOC_FAILURE);
643     return 0;
644 }
645
646
647 /*-
648  * Decrypts the certificate in the given encryptedValue
649  * this is needed for the indirect PoP method as in RFC 4210 section 5.2.8.2
650  *
651  * returns a pointer to the decrypted certificate
652  * returns NULL on error or if no certificate available
653  */
654 X509 *OSSL_CRMF_ENCRYPTEDVALUE_get1_encCert(OSSL_CRMF_ENCRYPTEDVALUE *ecert,
655                                             EVP_PKEY *pkey)
656 {
657     X509 *cert = NULL; /* decrypted certificate */
658     EVP_CIPHER_CTX *evp_ctx = NULL; /* context for symmetric encryption */
659     unsigned char *ek = NULL; /* decrypted symmetric encryption key */
660     const EVP_CIPHER *cipher = NULL; /* used cipher */
661     unsigned char *iv = NULL; /* initial vector for symmetric encryption */
662     unsigned char *outbuf = NULL; /* decryption output buffer */
663     const unsigned char *p = NULL; /* needed for decoding ASN1 */
664     int symmAlg = 0; /* NIDs for symmetric algorithm */
665     int n, outlen = 0;
666     EVP_PKEY_CTX *pkctx = NULL; /* private key context */
667
668     if (ecert == NULL || ecert->symmAlg == NULL || ecert->encSymmKey == NULL
669             || ecert->encValue == NULL || pkey == NULL) {
670         CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
671                 CRMF_R_NULL_ARGUMENT);
672         return NULL;
673     }
674     if ((symmAlg = OBJ_obj2nid(ecert->symmAlg->algorithm)) == 0) {
675         CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
676                 CRMF_R_UNSUPPORTED_CIPHER);
677         return NULL;
678     }
679
680     /* first the symmetric key needs to be decrypted */
681     pkctx = EVP_PKEY_CTX_new(pkey, NULL);
682     if (pkctx != NULL && EVP_PKEY_decrypt_init(pkctx)) {
683         ASN1_BIT_STRING *encKey = ecert->encSymmKey;
684         size_t eksize = 0;
685
686         if (EVP_PKEY_decrypt(pkctx, NULL, &eksize, encKey->data, encKey->length)
687                 <= 0
688                 || (ek = OPENSSL_malloc(eksize)) == NULL
689                 || EVP_PKEY_decrypt(pkctx, ek, &eksize, encKey->data,
690                                     encKey->length) <= 0) {
691             CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
692                     CRMF_R_ERROR_DECRYPTING_SYMMETRIC_KEY);
693             goto end;
694         }
695     } else {
696         goto oom;
697     }
698
699     /* select symmetric cipher based on algorithm given in message */
700     if ((cipher = EVP_get_cipherbynid(symmAlg)) == NULL) {
701         CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
702                 CRMF_R_UNSUPPORTED_CIPHER);
703         goto end;
704     }
705     if ((iv = OPENSSL_malloc(EVP_CIPHER_iv_length(cipher))) == NULL)
706         goto oom;
707     if (ASN1_TYPE_get_octetstring(ecert->symmAlg->parameter, iv,
708                                    EVP_CIPHER_iv_length(cipher))
709         != EVP_CIPHER_iv_length(cipher)) {
710         CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
711                 CRMF_R_MALFORMED_IV);
712         goto end;
713     }
714
715     /*
716      * d2i_X509 changes the given pointer, so use p for decoding the message and
717      * keep the original pointer in outbuf so the memory can be freed later
718      */
719     if ((p = outbuf = OPENSSL_malloc(ecert->encValue->length +
720                                      EVP_CIPHER_block_size(cipher))) == NULL
721             || (evp_ctx = EVP_CIPHER_CTX_new()) == NULL)
722         goto oom;
723     EVP_CIPHER_CTX_set_padding(evp_ctx, 0);
724
725     if (!EVP_DecryptInit(evp_ctx, cipher, ek, iv)
726             || !EVP_DecryptUpdate(evp_ctx, outbuf, &outlen,
727                                   ecert->encValue->data,
728                                   ecert->encValue->length)
729             || !EVP_DecryptFinal(evp_ctx, outbuf + outlen, &n)) {
730         CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
731                 CRMF_R_ERROR_DECRYPTING_CERTIFICATE);
732         goto end;
733     }
734     outlen += n;
735
736     /* convert decrypted certificate from DER to internal ASN.1 structure */
737     if ((cert = d2i_X509(NULL, &p, outlen)) == NULL) {
738         CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT,
739                 CRMF_R_ERROR_DECODING_CERTIFICATE);
740     }
741     goto end;
742
743  oom:
744     CRMFerr(CRMF_F_OSSL_CRMF_ENCRYPTEDVALUE_GET1_ENCCERT, ERR_R_MALLOC_FAILURE);
745  end:
746     EVP_PKEY_CTX_free(pkctx);
747     OPENSSL_free(outbuf);
748     EVP_CIPHER_CTX_free(evp_ctx);
749     OPENSSL_free(ek);
750     OPENSSL_free(iv);
751     return cert;
752 }