1 /* -*- Mode: C; tab-width: 8 -*-*/
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 /*
7  * This file will contain all routines needed by a client that has
8  * to parse a CMMFCertRepContent structure and retirieve the appropriate
9  * data.
10  */
11 
12 #include "cmmf.h"
13 #include "cmmfi.h"
14 #include "crmf.h"
15 #include "crmfi.h"
16 #include "secitem.h"
17 #include "secder.h"
18 #include "secasn1.h"
19 
20 CMMFCertRepContent *
CMMF_CreateCertRepContentFromDER(CERTCertDBHandle * db,const char * buf,long len)21 CMMF_CreateCertRepContentFromDER(CERTCertDBHandle *db, const char *buf,
22                                  long len)
23 {
24     PLArenaPool *poolp;
25     CMMFCertRepContent *certRepContent;
26     SECStatus rv;
27     int i;
28 
29     poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
30     if (poolp == NULL) {
31         return NULL;
32     }
33     certRepContent = PORT_ArenaZNew(poolp, CMMFCertRepContent);
34     if (certRepContent == NULL) {
35         goto loser;
36     }
37     certRepContent->poolp = poolp;
38     rv = SEC_ASN1Decode(poolp, certRepContent, CMMFCertRepContentTemplate,
39                         buf, len);
40     if (rv != SECSuccess) {
41         goto loser;
42     }
43     if (certRepContent->response != NULL) {
44         for (i = 0; certRepContent->response[i] != NULL; i++) {
45             rv = cmmf_decode_process_cert_response(poolp, db,
46                                                    certRepContent->response[i]);
47             if (rv != SECSuccess) {
48                 goto loser;
49             }
50         }
51     }
52     certRepContent->isDecoded = PR_TRUE;
53     return certRepContent;
54 loser:
55     PORT_FreeArena(poolp, PR_FALSE);
56     return NULL;
57 }
58 
59 long
CMMF_CertResponseGetCertReqId(CMMFCertResponse * inCertResp)60 CMMF_CertResponseGetCertReqId(CMMFCertResponse *inCertResp)
61 {
62     PORT_Assert(inCertResp != NULL);
63     if (inCertResp == NULL) {
64         return -1;
65     }
66     return DER_GetInteger(&inCertResp->certReqId);
67 }
68 
69 PRBool
cmmf_CertRepContentIsIndexValid(CMMFCertRepContent * inCertRepContent,int inIndex)70 cmmf_CertRepContentIsIndexValid(CMMFCertRepContent *inCertRepContent,
71                                 int inIndex)
72 {
73     int numResponses;
74 
75     PORT_Assert(inCertRepContent != NULL);
76     numResponses = CMMF_CertRepContentGetNumResponses(inCertRepContent);
77     return (PRBool)(inIndex >= 0 && inIndex < numResponses);
78 }
79 
80 CMMFCertResponse *
CMMF_CertRepContentGetResponseAtIndex(CMMFCertRepContent * inCertRepContent,int inIndex)81 CMMF_CertRepContentGetResponseAtIndex(CMMFCertRepContent *inCertRepContent,
82                                       int inIndex)
83 {
84     CMMFCertResponse *certResponse;
85     SECStatus rv;
86 
87     PORT_Assert(inCertRepContent != NULL &&
88                 cmmf_CertRepContentIsIndexValid(inCertRepContent, inIndex));
89     if (inCertRepContent == NULL ||
90         !cmmf_CertRepContentIsIndexValid(inCertRepContent, inIndex)) {
91         return NULL;
92     }
93     certResponse = PORT_ZNew(CMMFCertResponse);
94     if (certResponse) {
95         rv = cmmf_CopyCertResponse(NULL, certResponse,
96                                    inCertRepContent->response[inIndex]);
97         if (rv != SECSuccess) {
98             CMMF_DestroyCertResponse(certResponse);
99             certResponse = NULL;
100         }
101     }
102     return certResponse;
103 }
104 
105 CMMFPKIStatus
CMMF_CertResponseGetPKIStatusInfoStatus(CMMFCertResponse * inCertResp)106 CMMF_CertResponseGetPKIStatusInfoStatus(CMMFCertResponse *inCertResp)
107 {
108     PORT_Assert(inCertResp != NULL);
109     if (inCertResp == NULL) {
110         return cmmfNoPKIStatus;
111     }
112     return cmmf_PKIStatusInfoGetStatus(&inCertResp->status);
113 }
114 
115 CERTCertificate *
CMMF_CertResponseGetCertificate(CMMFCertResponse * inCertResp,CERTCertDBHandle * inCertdb)116 CMMF_CertResponseGetCertificate(CMMFCertResponse *inCertResp,
117                                 CERTCertDBHandle *inCertdb)
118 {
119     PORT_Assert(inCertResp != NULL);
120     if (inCertResp == NULL || inCertResp->certifiedKeyPair == NULL) {
121         return NULL;
122     }
123 
124     return cmmf_CertOrEncCertGetCertificate(
125         &inCertResp->certifiedKeyPair->certOrEncCert, inCertdb);
126 }
127 
128 CERTCertList *
CMMF_CertRepContentGetCAPubs(CMMFCertRepContent * inCertRepContent)129 CMMF_CertRepContentGetCAPubs(CMMFCertRepContent *inCertRepContent)
130 {
131     PORT_Assert(inCertRepContent != NULL);
132     if (inCertRepContent == NULL || inCertRepContent->caPubs == NULL) {
133         return NULL;
134     }
135     return cmmf_MakeCertList(inCertRepContent->caPubs);
136 }
137