Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / crmf / cmmfasn1.c
blob8f080f08cdb143af6cbee0b9c7ff1949bab55600
1 /* -*- Mode: C; tab-width: 8 -*-*/
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Netscape security libraries.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1994-2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "cmmf.h"
39 #include "cmmfi.h"
40 #include "secasn1.h"
41 #include "secitem.h"
43 SEC_ASN1_MKSUB(SEC_SignedCertificateTemplate)
45 static const SEC_ASN1Template CMMFSequenceOfCertifiedKeyPairsTemplate[] = {
46 { SEC_ASN1_SEQUENCE_OF, 0, CMMFCertifiedKeyPairTemplate}
49 static const SEC_ASN1Template CMMFKeyRecRepContentTemplate[] = {
50 { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(CMMFKeyRecRepContent)},
51 { SEC_ASN1_INLINE, offsetof(CMMFKeyRecRepContent, status),
52 CMMFPKIStatusInfoTemplate},
53 { SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_POINTER |
54 SEC_ASN1_XTRN | 0,
55 offsetof(CMMFKeyRecRepContent, newSigCert),
56 SEC_ASN1_SUB(SEC_SignedCertificateTemplate)},
57 { SEC_ASN1_CONSTRUCTED | SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | 1,
58 offsetof(CMMFKeyRecRepContent, caCerts),
59 CMMFSequenceOfCertsTemplate},
60 { SEC_ASN1_CONSTRUCTED | SEC_ASN1_OPTIONAL | SEC_ASN1_CONTEXT_SPECIFIC | 2,
61 offsetof(CMMFKeyRecRepContent, keyPairHist),
62 CMMFSequenceOfCertifiedKeyPairsTemplate},
63 { 0 }
66 SECStatus
67 CMMF_EncodeCertRepContent (CMMFCertRepContent *inCertRepContent,
68 CRMFEncoderOutputCallback inCallback,
69 void *inArg)
71 return cmmf_user_encode(inCertRepContent, inCallback, inArg,
72 CMMFCertRepContentTemplate);
75 SECStatus
76 CMMF_EncodePOPODecKeyChallContent(CMMFPOPODecKeyChallContent *inDecKeyChall,
77 CRMFEncoderOutputCallback inCallback,
78 void *inArg)
80 return cmmf_user_encode(inDecKeyChall, inCallback, inArg,
81 CMMFPOPODecKeyChallContentTemplate);
84 CMMFPOPODecKeyRespContent*
85 CMMF_CreatePOPODecKeyRespContentFromDER(const char *buf, long len)
87 PRArenaPool *poolp;
88 CMMFPOPODecKeyRespContent *decKeyResp;
89 SECStatus rv;
91 poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
92 if (poolp == NULL) {
93 return NULL;
95 decKeyResp = PORT_ArenaZNew(poolp, CMMFPOPODecKeyRespContent);
96 if (decKeyResp == NULL) {
97 goto loser;
99 decKeyResp->poolp = poolp;
100 rv = SEC_ASN1Decode(poolp, decKeyResp, CMMFPOPODecKeyRespContentTemplate,
101 buf, len);
102 if (rv != SECSuccess) {
103 goto loser;
105 return decKeyResp;
107 loser:
108 if (poolp != NULL) {
109 PORT_FreeArena(poolp, PR_FALSE);
111 return NULL;
114 SECStatus
115 CMMF_EncodeKeyRecRepContent(CMMFKeyRecRepContent *inKeyRecRep,
116 CRMFEncoderOutputCallback inCallback,
117 void *inArg)
119 return cmmf_user_encode(inKeyRecRep, inCallback, inArg,
120 CMMFKeyRecRepContentTemplate);
123 CMMFKeyRecRepContent*
124 CMMF_CreateKeyRecRepContentFromDER(CERTCertDBHandle *db, const char *buf,
125 long len)
127 PRArenaPool *poolp;
128 CMMFKeyRecRepContent *keyRecContent;
129 SECStatus rv;
131 poolp = PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE);
132 if (poolp == NULL) {
133 return NULL;
135 keyRecContent = PORT_ArenaZNew(poolp, CMMFKeyRecRepContent);
136 if (keyRecContent == NULL) {
137 goto loser;
139 keyRecContent->poolp = poolp;
140 rv = SEC_ASN1Decode(poolp, keyRecContent, CMMFKeyRecRepContentTemplate,
141 buf, len);
142 if (rv != SECSuccess) {
143 goto loser;
145 if (keyRecContent->keyPairHist != NULL) {
146 while(keyRecContent->keyPairHist[keyRecContent->numKeyPairs] != NULL) {
147 rv = cmmf_decode_process_certified_key_pair(poolp, db,
148 keyRecContent->keyPairHist[keyRecContent->numKeyPairs]);
149 if (rv != SECSuccess) {
150 goto loser;
152 keyRecContent->numKeyPairs++;
154 keyRecContent->allocKeyPairs = keyRecContent->numKeyPairs;
156 keyRecContent->isDecoded = PR_TRUE;
157 return keyRecContent;
158 loser:
159 if (poolp != NULL) {
160 PORT_FreeArena(poolp, PR_FALSE);
162 return NULL;