~ubuntu-branches/ubuntu/trusty/xulrunner/trusty

« back to all changes in this revision

Viewing changes to security/nss-fips/lib/util/secdig.c

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2008-08-25 13:04:18 UTC
  • mfrom: (1.1.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20080825130418-ck1i2ms384tzb9m0
Tags: 1.8.1.16+nobinonly-0ubuntu1
* New upstream release (taken from upstream CVS), LP: #254618.
* Fix MFSA 2008-35, MFSA 2008-34, MFSA 2008-33, MFSA 2008-32, MFSA 2008-31,
  MFSA 2008-30, MFSA 2008-29, MFSA 2008-28, MFSA 2008-27, MFSA 2008-25,
  MFSA 2008-24, MFSA 2008-23, MFSA 2008-22, MFSA 2008-21, MFSA 2008-26 also
  known as CVE-2008-2933, CVE-2008-2785, CVE-2008-2811, CVE-2008-2810,
  CVE-2008-2809, CVE-2008-2808, CVE-2008-2807, CVE-2008-2806, CVE-2008-2805,
  CVE-2008-2803, CVE-2008-2802, CVE-2008-2801, CVE-2008-2800, CVE-2008-2798.
* Drop 89_bz419350_attachment_306066 patch, merged upstream.
* Bump Standards-Version to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
3
 *
 
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/
 
8
 *
 
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
 
12
 * License.
 
13
 *
 
14
 * The Original Code is the Netscape security libraries.
 
15
 *
 
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.
 
20
 *
 
21
 * Contributor(s):
 
22
 *
 
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.
 
34
 *
 
35
 * ***** END LICENSE BLOCK ***** */
 
36
/* $Id: secdig.c,v 1.5.28.2 2006/08/31 03:57:56 nelson%bolyard.com Exp $ */
 
37
#include "secdig.h"
 
38
 
 
39
#include "secoid.h"
 
40
#include "secasn1.h" 
 
41
#include "secerr.h"
 
42
 
 
43
/*
 
44
 * XXX OLD Template.  Once all uses have been switched over to new one,
 
45
 * remove this.
 
46
 */
 
47
DERTemplate SGNDigestInfoTemplate[] = {
 
48
    { DER_SEQUENCE,
 
49
          0, NULL, sizeof(SGNDigestInfo) },
 
50
    { DER_INLINE,
 
51
          offsetof(SGNDigestInfo,digestAlgorithm),
 
52
          SECAlgorithmIDTemplate, },
 
53
    { DER_OCTET_STRING,
 
54
          offsetof(SGNDigestInfo,digest), },
 
55
    { 0, }
 
56
};
 
57
 
 
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[] = {
 
61
    { SEC_ASN1_SEQUENCE,
 
62
          0, NULL, sizeof(SGNDigestInfo) },
 
63
    { SEC_ASN1_INLINE,
 
64
          offsetof(SGNDigestInfo,digestAlgorithm),
 
65
          SECOID_AlgorithmIDTemplate },
 
66
    { SEC_ASN1_OCTET_STRING,
 
67
          offsetof(SGNDigestInfo,digest) },
 
68
    { 0 }
 
69
};
 
70
 
 
71
SEC_ASN1_CHOOSER_IMPLEMENT(sgn_DigestInfoTemplate)
 
72
 
 
73
/*
 
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".
 
81
 */
 
82
 
 
83
/*
 
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.
 
86
 */
 
87
 
 
88
SECItem *
 
89
SGN_EncodeDigestInfo(PRArenaPool *poolp, SECItem *dest, SGNDigestInfo *diginfo)
 
90
{
 
91
    return SEC_ASN1EncodeItem (poolp, dest, diginfo, sgn_DigestInfoTemplate);
 
92
}
 
93
 
 
94
SGNDigestInfo *
 
95
SGN_CreateDigestInfo(SECOidTag algorithm, unsigned char *sig, unsigned len)
 
96
{
 
97
    SGNDigestInfo *di;
 
98
    SECStatus rv;
 
99
    PRArenaPool *arena;
 
100
    SECItem *null_param;
 
101
    SECItem dummy_value;
 
102
 
 
103
    switch (algorithm) {
 
104
      case SEC_OID_MD2:
 
105
      case SEC_OID_MD5:
 
106
      case SEC_OID_SHA1:
 
107
      case SEC_OID_SHA256:
 
108
      case SEC_OID_SHA384:
 
109
      case SEC_OID_SHA512:
 
110
        break;
 
111
      default:
 
112
        PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
 
113
        return NULL;
 
114
    }
 
115
 
 
116
    arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
 
117
    if (arena == NULL) {
 
118
        return NULL;
 
119
    }
 
120
 
 
121
    di = (SGNDigestInfo *) PORT_ArenaZAlloc(arena, sizeof(SGNDigestInfo));
 
122
    if (di == NULL) {
 
123
        PORT_FreeArena(arena, PR_FALSE);
 
124
        return NULL;
 
125
    }
 
126
 
 
127
    di->arena = arena;
 
128
 
 
129
    /*
 
130
     * PKCS #1 specifies that the AlgorithmID must have a NULL parameter
 
131
     * (as opposed to no parameter at all).
 
132
     */
 
133
    dummy_value.data = NULL;
 
134
    dummy_value.len = 0;
 
135
    null_param = SEC_ASN1EncodeItem(NULL, NULL, &dummy_value, SEC_NullTemplate);
 
136
    if (null_param == NULL) {
 
137
        goto loser;
 
138
    }
 
139
 
 
140
    rv = SECOID_SetAlgorithmID(arena, &di->digestAlgorithm, algorithm,
 
141
                               null_param);
 
142
 
 
143
    SECITEM_FreeItem(null_param, PR_TRUE);
 
144
 
 
145
    if (rv != SECSuccess) {
 
146
        goto loser;
 
147
    }
 
148
 
 
149
    di->digest.data = (unsigned char *) PORT_ArenaAlloc(arena, len);
 
150
    if (di->digest.data == NULL) {
 
151
        goto loser;
 
152
    }
 
153
 
 
154
    di->digest.len = len;
 
155
    PORT_Memcpy(di->digest.data, sig, len);
 
156
    return di;
 
157
 
 
158
  loser:
 
159
    SGN_DestroyDigestInfo(di);
 
160
    return NULL;
 
161
}
 
162
 
 
163
SGNDigestInfo *
 
164
SGN_DecodeDigestInfo(SECItem *didata)
 
165
{
 
166
    PRArenaPool *arena;
 
167
    SGNDigestInfo *di;
 
168
    SECStatus rv = SECFailure;
 
169
    SECItem      diCopy   = {siBuffer, NULL, 0};
 
170
 
 
171
    arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE);
 
172
    if(arena == NULL)
 
173
        return NULL;
 
174
 
 
175
    rv = SECITEM_CopyItem(arena, &diCopy, didata);
 
176
    if (rv != SECSuccess) {
 
177
        PORT_FreeArena(arena, PR_FALSE);
 
178
        return NULL;
 
179
    }
 
180
 
 
181
    di = (SGNDigestInfo *)PORT_ArenaZAlloc(arena, sizeof(SGNDigestInfo));
 
182
    if (di != NULL) {
 
183
        di->arena = arena;
 
184
        rv = SEC_QuickDERDecodeItem(arena, di, sgn_DigestInfoTemplate, &diCopy);
 
185
    }
 
186
        
 
187
    if ((di == NULL) || (rv != SECSuccess)) {
 
188
        PORT_FreeArena(arena, PR_FALSE);
 
189
        di = NULL;
 
190
    }
 
191
 
 
192
    return di;
 
193
}
 
194
 
 
195
void
 
196
SGN_DestroyDigestInfo(SGNDigestInfo *di)
 
197
{
 
198
    if (di && di->arena) {
 
199
        PORT_FreeArena(di->arena, PR_FALSE);
 
200
    }
 
201
 
 
202
    return;
 
203
}
 
204
 
 
205
SECStatus 
 
206
SGN_CopyDigestInfo(PRArenaPool *poolp, SGNDigestInfo *a, SGNDigestInfo *b)
 
207
{
 
208
    SECStatus rv;
 
209
    void *mark;
 
210
 
 
211
    if((poolp == NULL) || (a == NULL) || (b == NULL))
 
212
        return SECFailure;
 
213
 
 
214
    mark = PORT_ArenaMark(poolp);
 
215
    a->arena = poolp;
 
216
    rv = SECOID_CopyAlgorithmID(poolp, &a->digestAlgorithm, 
 
217
        &b->digestAlgorithm);
 
218
    if (rv == SECSuccess)
 
219
        rv = SECITEM_CopyItem(poolp, &a->digest, &b->digest);
 
220
 
 
221
    if (rv != SECSuccess) {
 
222
        PORT_ArenaRelease(poolp, mark);
 
223
    } else {
 
224
        PORT_ArenaUnmark(poolp, mark);
 
225
    }
 
226
 
 
227
    return rv;
 
228
}
 
229
 
 
230
SECComparison
 
231
SGN_CompareDigestInfo(SGNDigestInfo *a, SGNDigestInfo *b)
 
232
{
 
233
    SECComparison rv;
 
234
 
 
235
    /* Check signature algorithm's */
 
236
    rv = SECOID_CompareAlgorithmID(&a->digestAlgorithm, &b->digestAlgorithm);
 
237
    if (rv) return rv;
 
238
 
 
239
    /* Compare signature block length's */
 
240
    rv = SECITEM_CompareItem(&a->digest, &b->digest);
 
241
    return rv;
 
242
}