1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Mozilla Public License Version
5
* 1.1 (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
7
* http://www.mozilla.org/MPL/
9
* Software distributed under the License is distributed on an "AS IS" basis,
10
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
* for the specific language governing rights and limitations under the
14
* The Original Code is the Netscape security libraries.
16
* The Initial Developer of the Original Code is
17
* Netscape Communications Corporation.
18
* Portions created by the Initial Developer are Copyright (C) 1994-2000
19
* the Initial Developer. All Rights Reserved.
23
* Alternatively, the contents of this file may be used under the terms of
24
* either the GNU General Public License Version 2 or later (the "GPL"), or
25
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26
* in which case the provisions of the GPL or the LGPL are applicable instead
27
* of those above. If you wish to allow use of your version of this file only
28
* under the terms of either the GPL or the LGPL, and not to allow others to
29
* use your version of this file under the terms of the MPL, indicate your
30
* decision by deleting the provisions above and replace them with the notice
31
* and other provisions required by the GPL or the LGPL. If you do not delete
32
* the provisions above, a recipient may use your version of this file under
33
* the terms of any one of the MPL, the GPL or the LGPL.
35
* ***** END LICENSE BLOCK ***** */
36
/* $Id: secdig.c,v 1.5.28.2 2006/08/31 03:57:56 nelson%bolyard.com Exp $ */
44
* XXX OLD Template. Once all uses have been switched over to new one,
47
DERTemplate SGNDigestInfoTemplate[] = {
49
0, NULL, sizeof(SGNDigestInfo) },
51
offsetof(SGNDigestInfo,digestAlgorithm),
52
SECAlgorithmIDTemplate, },
54
offsetof(SGNDigestInfo,digest), },
58
/* XXX See comment below about SGN_DecodeDigestInfo -- keep this static! */
59
/* XXX Changed from static -- need to change name? */
60
const SEC_ASN1Template sgn_DigestInfoTemplate[] = {
62
0, NULL, sizeof(SGNDigestInfo) },
64
offsetof(SGNDigestInfo,digestAlgorithm),
65
SECOID_AlgorithmIDTemplate },
66
{ SEC_ASN1_OCTET_STRING,
67
offsetof(SGNDigestInfo,digest) },
71
SEC_ASN1_CHOOSER_IMPLEMENT(sgn_DigestInfoTemplate)
74
* XXX Want to have a SGN_DecodeDigestInfo, like:
75
* SGNDigestInfo *SGN_DecodeDigestInfo(SECItem *didata);
76
* that creates a pool and allocates from it and decodes didata into
77
* the newly allocated DigestInfo structure. Then fix secvfy.c (it
78
* will no longer need an arena itself) to call this and then call
79
* DestroyDigestInfo when it is done, then can remove the old template
80
* above and keep our new template static and "hidden".
84
* XXX It might be nice to combine the following two functions (create
85
* and encode). I think that is all anybody ever wants to do anyway.
89
SGN_EncodeDigestInfo(PRArenaPool *poolp, SECItem *dest, SGNDigestInfo *diginfo)
91
return SEC_ASN1EncodeItem (poolp, dest, diginfo, sgn_DigestInfoTemplate);
95
SGN_CreateDigestInfo(SECOidTag algorithm, unsigned char *sig, unsigned len)
112
PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
116
arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
121
di = (SGNDigestInfo *) PORT_ArenaZAlloc(arena, sizeof(SGNDigestInfo));
123
PORT_FreeArena(arena, PR_FALSE);
130
* PKCS #1 specifies that the AlgorithmID must have a NULL parameter
131
* (as opposed to no parameter at all).
133
dummy_value.data = NULL;
135
null_param = SEC_ASN1EncodeItem(NULL, NULL, &dummy_value, SEC_NullTemplate);
136
if (null_param == NULL) {
140
rv = SECOID_SetAlgorithmID(arena, &di->digestAlgorithm, algorithm,
143
SECITEM_FreeItem(null_param, PR_TRUE);
145
if (rv != SECSuccess) {
149
di->digest.data = (unsigned char *) PORT_ArenaAlloc(arena, len);
150
if (di->digest.data == NULL) {
154
di->digest.len = len;
155
PORT_Memcpy(di->digest.data, sig, len);
159
SGN_DestroyDigestInfo(di);
164
SGN_DecodeDigestInfo(SECItem *didata)
168
SECStatus rv = SECFailure;
169
SECItem diCopy = {siBuffer, NULL, 0};
171
arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
175
rv = SECITEM_CopyItem(arena, &diCopy, didata);
176
if (rv != SECSuccess) {
177
PORT_FreeArena(arena, PR_FALSE);
181
di = (SGNDigestInfo *)PORT_ArenaZAlloc(arena, sizeof(SGNDigestInfo));
184
rv = SEC_QuickDERDecodeItem(arena, di, sgn_DigestInfoTemplate, &diCopy);
187
if ((di == NULL) || (rv != SECSuccess)) {
188
PORT_FreeArena(arena, PR_FALSE);
196
SGN_DestroyDigestInfo(SGNDigestInfo *di)
198
if (di && di->arena) {
199
PORT_FreeArena(di->arena, PR_FALSE);
206
SGN_CopyDigestInfo(PRArenaPool *poolp, SGNDigestInfo *a, SGNDigestInfo *b)
211
if((poolp == NULL) || (a == NULL) || (b == NULL))
214
mark = PORT_ArenaMark(poolp);
216
rv = SECOID_CopyAlgorithmID(poolp, &a->digestAlgorithm,
217
&b->digestAlgorithm);
218
if (rv == SECSuccess)
219
rv = SECITEM_CopyItem(poolp, &a->digest, &b->digest);
221
if (rv != SECSuccess) {
222
PORT_ArenaRelease(poolp, mark);
224
PORT_ArenaUnmark(poolp, mark);
231
SGN_CompareDigestInfo(SGNDigestInfo *a, SGNDigestInfo *b)
235
/* Check signature algorithm's */
236
rv = SECOID_CompareAlgorithmID(&a->digestAlgorithm, &b->digestAlgorithm);
239
/* Compare signature block length's */
240
rv = SECITEM_CompareItem(&a->digest, &b->digest);