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
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.
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 ***** */
45 CMMFPOPODecKeyChallContent
*
46 CMMF_CreatePOPODecKeyChallContentFromDER(const char *buf
, long len
)
49 CMMFPOPODecKeyChallContent
*challContent
;
52 poolp
= PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE
);
56 challContent
= PORT_ArenaZNew(poolp
, CMMFPOPODecKeyChallContent
);
57 if (challContent
== NULL
) {
60 challContent
->poolp
= poolp
;
61 rv
= SEC_ASN1Decode(poolp
, challContent
,
62 CMMFPOPODecKeyChallContentTemplate
, buf
, len
);
63 if (rv
!= SECSuccess
) {
66 if (challContent
->challenges
) {
67 while (challContent
->challenges
[challContent
->numChallenges
] != NULL
) {
68 challContent
->numChallenges
++;
70 challContent
->numAllocated
= challContent
->numChallenges
;
75 PORT_FreeArena(poolp
, PR_FALSE
);
81 CMMF_POPODecKeyChallContentGetNumChallenges
82 (CMMFPOPODecKeyChallContent
*inKeyChallCont
)
84 PORT_Assert(inKeyChallCont
!= NULL
);
85 if (inKeyChallCont
== NULL
) {
88 return inKeyChallCont
->numChallenges
;
92 CMMF_POPODecKeyChallContentGetPublicValue
93 (CMMFPOPODecKeyChallContent
*inKeyChallCont
,
96 PORT_Assert(inKeyChallCont
!= NULL
);
97 if (inKeyChallCont
== NULL
|| (inIndex
> inKeyChallCont
->numChallenges
-1)||
101 return SECITEM_DupItem(&inKeyChallCont
->challenges
[inIndex
]->key
);
104 static SECAlgorithmID
*
105 cmmf_get_owf(CMMFPOPODecKeyChallContent
*inChalCont
,
110 for (i
=inIndex
; i
>= 0; i
--) {
111 if (inChalCont
->challenges
[i
]->owf
!= NULL
) {
112 return inChalCont
->challenges
[i
]->owf
;
119 CMMF_POPODecKeyChallContDecryptChallenge(CMMFPOPODecKeyChallContent
*inChalCont
,
121 SECKEYPrivateKey
*inPrivKey
)
123 CMMFChallenge
*challenge
;
124 SECItem
*decryptedRand
=NULL
;
125 PRArenaPool
*poolp
= NULL
;
127 SECStatus rv
= SECFailure
;
131 unsigned char hash
[HASH_LENGTH_MAX
];
133 PORT_Assert(inChalCont
!= NULL
&& inPrivKey
!= NULL
);
134 if (inChalCont
== NULL
|| inIndex
<0 || inIndex
> inChalCont
->numChallenges
135 || inPrivKey
== NULL
){
139 poolp
= PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE
);
144 challenge
= inChalCont
->challenges
[inIndex
];
145 decryptedRand
= SECITEM_AllocItem(poolp
, NULL
, challenge
->challenge
.len
);
146 if (decryptedRand
== NULL
) {
149 rv
= PK11_PrivDecryptPKCS1(inPrivKey
, decryptedRand
->data
,
150 &decryptedRand
->len
, decryptedRand
->len
,
151 challenge
->challenge
.data
, challenge
->challenge
.len
);
152 if (rv
!= SECSuccess
) {
156 rv
= SEC_ASN1DecodeItem(poolp
, &randStr
, CMMFRandTemplate
,
158 if (rv
!= SECSuccess
) {
161 rv
= SECFailure
; /* Just so that when we do go to loser,
162 * I won't have to set it again.
164 owf
= cmmf_get_owf(inChalCont
, inIndex
);
166 /* No hashing algorithm came with the challenges. Can't verify */
169 /* Verify the hashes in the challenge */
170 tag
= SECOID_FindOIDTag(&owf
->algorithm
);
171 hashItem
.len
= HASH_ResultLenByOidTag(tag
);
173 goto loser
; /* error code has been set */
175 rv
= PK11_HashBuf(tag
, hash
, randStr
.integer
.data
, randStr
.integer
.len
);
176 if (rv
!= SECSuccess
) {
179 hashItem
.data
= hash
;
180 if (SECITEM_CompareItem(&hashItem
, &challenge
->witness
) != SECEqual
) {
181 /* The hash for the data we decrypted doesn't match the hash provided
182 * in the challenge. Bail out.
184 PORT_SetError(SEC_ERROR_BAD_DATA
);
188 rv
= PK11_HashBuf(tag
, hash
, challenge
->senderDER
.data
,
189 challenge
->senderDER
.len
);
190 if (rv
!= SECSuccess
) {
193 if (SECITEM_CompareItem(&hashItem
, &randStr
.senderHash
) != SECEqual
) {
194 /* The hash for the data we decrypted doesn't match the hash provided
195 * in the challenge. Bail out.
197 PORT_SetError(SEC_ERROR_BAD_DATA
);
201 /* All of the hashes have verified, so we can now store the integer away.*/
202 rv
= SECITEM_CopyItem(inChalCont
->poolp
, &challenge
->randomNumber
,
206 PORT_FreeArena(poolp
, PR_FALSE
);
212 CMMF_POPODecKeyChallContentGetRandomNumber
213 (CMMFPOPODecKeyChallContent
*inKeyChallCont
,
217 CMMFChallenge
*challenge
;
219 PORT_Assert(inKeyChallCont
!= NULL
);
220 if (inKeyChallCont
== NULL
|| inIndex
> 0 || inIndex
>=
221 inKeyChallCont
->numChallenges
) {
224 challenge
= inKeyChallCont
->challenges
[inIndex
];
225 if (challenge
->randomNumber
.data
== NULL
) {
226 /* There is no random number here, nothing to see. */
229 *inDest
= DER_GetInteger(&challenge
->randomNumber
);
230 return (*inDest
== -1) ? SECFailure
: SECSuccess
;
234 CMMF_EncodePOPODecKeyRespContent(long *inDecodedRand
,
236 CRMFEncoderOutputCallback inCallback
,
240 CMMFPOPODecKeyRespContent
*response
;
242 SECStatus rv
=SECFailure
;
245 poolp
= PORT_NewArena(CRMF_DEFAULT_ARENA_SIZE
);
249 response
= PORT_ArenaZNew(poolp
, CMMFPOPODecKeyRespContent
);
250 if (response
== NULL
) {
253 response
->responses
= PORT_ArenaZNewArray(poolp
, SECItem
*, inNumRand
+1);
254 if (response
->responses
== NULL
) {
257 for (i
=0; i
<inNumRand
; i
++) {
258 currItem
= response
->responses
[i
] = PORT_ArenaZNew(poolp
,SECItem
);
259 if (currItem
== NULL
) {
262 currItem
= SEC_ASN1EncodeInteger(poolp
, currItem
, inDecodedRand
[i
]);
263 if (currItem
== NULL
) {
267 rv
= cmmf_user_encode(response
, inCallback
, inArg
,
268 CMMFPOPODecKeyRespContentTemplate
);
271 PORT_FreeArena(poolp
, PR_FALSE
);