summaryrefslogtreecommitdiff
path: root/gnu/javax/crypto/keyring/EnvelopeEntry.java
blob: 2a57a23dabff6e290495b9a306da39e22d11af91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* EnvelopeEntry.java -- 
   Copyright (C) 2003, 2006 Free Software Foundation, Inc.

This file is a part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.  */


package gnu.javax.crypto.keyring;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Logger;

/**
 * An envelope entry is a generic container for some number of primitive
 * and other envelope entries.
 */
public abstract class EnvelopeEntry extends Entry
{
  // Fields.
  // ------------------------------------------------------------------------

  private static final Logger log = Logger.getLogger(EnvelopeEntry.class.getName());
  /** The envelope that contains this one (if any). */
  protected EnvelopeEntry containingEnvelope;

  /** The contained entries. */
  protected List entries;

  // Constructor.
  // ------------------------------------------------------------------------

  public EnvelopeEntry(int type, Properties properties)
  {
    super(type, properties);
    entries = new LinkedList();
    if (this.properties.get("alias-list") != null)
      {
        this.properties.remove("alias-list");
      }
  }

  protected EnvelopeEntry(int type)
  {
    super(type);
    entries = new LinkedList();
  }

  // Instance methods.
  // ------------------------------------------------------------------------

  /**
   * Adds an entry to this envelope.
   *
   * @param entry The entry to add.
   */
  public void add(Entry entry)
  {
    log.entering(this.getClass().getName(), "add", entry);
    if (! containsEntry(entry))
      {
        if (entry instanceof EnvelopeEntry)
          ((EnvelopeEntry) entry).setContainingEnvelope(this);

        entries.add(entry);
        log.finest("Payload is " + (payload == null ? "" : "not ") + "null");
        makeAliasList();
      }
    log.exiting(this.getClass().getName(), "add");
  }

  /**
   * Tests if this envelope contains a primitive entry with the
   * given alias.
   *
   * @param alias The alias to test.
   * @return True if this envelope (or one of the contained envelopes)
   *   contains a primitive entry with the given alias.
   */
  public boolean containsAlias(String alias)
  {
    log.entering(this.getClass().getName(), "containsAlias", alias);
    String aliases = getAliasList();
    log.finest("aliases = [" + aliases + "]");
    boolean result = false;
    if (aliases != null)
      {
        StringTokenizer tok = new StringTokenizer(aliases, ";");
        while (tok.hasMoreTokens())
          if (tok.nextToken().equals(alias))
            {
              result = true;
              break;
            }
      }
    log.exiting(this.getClass().getName(), "containsAlias", Boolean.valueOf(result));
    return result;
  }

  /**
   * Tests if this envelope contains the given entry.
   *
   * @param entry The entry to test.
   * @return True if this envelope contains the given entry.
   */
  public boolean containsEntry(Entry entry)
  {
    if (entry instanceof EnvelopeEntry)
      {
        return entries.contains(entry);
      }
    else if (entry instanceof PrimitiveEntry)
      {
        for (Iterator it = entries.iterator(); it.hasNext();)
          {
            Entry e = (Entry) it.next();
            if (e.equals(entry))
              return true;
            if ((e instanceof EnvelopeEntry)
                && ((EnvelopeEntry) e).containsEntry(entry))
              return true;
          }
      }
    return false;
  }

  /**
   * Returns a copy of all entries this envelope contains.
   *
   * @return All contained entries.
   */
  public List getEntries()
  {
    return new ArrayList(entries);
  }

  /**
   * Gets all primitive entries that have the given alias. If there
   * are any masked entries that contain the given alias, they will
   * be returned as well.
   *
   * @param alias The alias of the entries to get.
   * @return A list of all primitive entries that have the given alias.
   */
  public List get(String alias)
  {
    log.entering(this.getClass().getName(), "get", alias);

    List result = new LinkedList();
    for (Iterator it = entries.iterator(); it.hasNext();)
      {
        Entry e = (Entry) it.next();
        if (e instanceof EnvelopeEntry)
          {
            EnvelopeEntry ee = (EnvelopeEntry) e;
            if (! ee.containsAlias(alias))
              continue;

            if (ee instanceof MaskableEnvelopeEntry)
              {
                MaskableEnvelopeEntry mee = (MaskableEnvelopeEntry) ee;
                if (mee.isMasked())
                  {
                    log.finer("Processing masked entry: " + mee);
                    result.add(mee);
                    continue;
                  }
              }

            log.finer("Processing unmasked entry: " + ee);
            result.addAll(ee.get(alias));
          }
        else if (e instanceof PrimitiveEntry)
          {
            PrimitiveEntry pe = (PrimitiveEntry) e;
            if (pe.getAlias().equals(alias))
              result.add(e);
          }
      }

    log.exiting(this.getClass().getName(), "get", result);
    return result;
  }

  /**
   * Returns the list of all aliases contained by this envelope,
   * separated by a semicolon (';').
   *
   * @return The list of aliases.
   */
  public String getAliasList()
  {
    String list = properties.get("alias-list");
    if (list == null)
      {
        return "";
      }
    else
      {
        return list;
      }
  }

  /**
   * Removes the specified entry.
   *
   * @param entry The entry.
   * @return True if an entry was removed.
   */
  public boolean remove(Entry entry)
  {
    log.entering(this.getClass().getName(), "remove", entry);
    boolean ret = false;
    for (Iterator it = entries.iterator(); it.hasNext();)
      {
        Entry e = (Entry) it.next();
        if (e instanceof EnvelopeEntry)
          {
            if (e == entry)
              {
                it.remove();
                ret = true;
                break;
              }
            if (((EnvelopeEntry) e).remove(entry))
              {
                ret = true;
                break;
              }
          }
        else if (e instanceof PrimitiveEntry)
          {
            if (((PrimitiveEntry) e).equals(entry))
              {
                it.remove();
                ret = true;
                break;
              }
          }
      }
    if (ret)
      {
        log.finest("State before: " + this);
        payload = null;
        makeAliasList();
        log.finest("State after: " + this);
      }
    log.exiting(this.getClass().getName(), "remove", Boolean.valueOf(ret));
    return ret;
  }

  /**
   * Removes all primitive entries that have the specified alias.
   * 
   * @param alias The alias of the entries to remove.
   * @return <code>true</code> if <code>alias</code> was present and was
   *         successfully trmoved. Returns <code>false</code> if
   *         <code>alias</code> was not present in the list of aliases in this
   *         envelope.
   */
  public boolean remove(String alias)
  {
    log.entering(this.getClass().getName(), "remove", alias);
    boolean result = false;
    for (Iterator it = entries.iterator(); it.hasNext();)
      {
        Entry e = (Entry) it.next();
        if (e instanceof EnvelopeEntry)
          {
            EnvelopeEntry ee = (EnvelopeEntry) e;
            result = ee.remove(alias) || result;
          }
        else if (e instanceof PrimitiveEntry)
          {
            PrimitiveEntry pe = (PrimitiveEntry) e;
            if (pe.getAlias().equals(alias))
              {
                it.remove();
                result = true;
              }
          }
      }
    if (result)
      {
        log.finest("State before: " + this);
        payload = null;
        makeAliasList();
        log.finest("State after: " + this);
      }
    log.exiting(this.getClass().getName(), "remove", Boolean.valueOf(result));
    return result;
  }

  public String toString()
  {
    return new StringBuilder("Envelope{")
        .append(super.toString())
        .append(", entries=").append(entries)
        .append("}").toString();
  }

  // Protected methods.
  // ------------------------------------------------------------------------

  protected void encodePayload() throws IOException
  {
    ByteArrayOutputStream bout = new ByteArrayOutputStream(1024);
    DataOutputStream out = new DataOutputStream(bout);
    for (Iterator it = entries.iterator(); it.hasNext();)
      {
        ((Entry) it.next()).encode(out);
      }
  }

  protected void setContainingEnvelope(EnvelopeEntry e)
  {
    if (containingEnvelope != null)
      {
        throw new IllegalArgumentException("envelopes may not be shared");
      }
    containingEnvelope = e;
  }

  protected void decodeEnvelope(DataInputStream in) throws IOException
  {
    this.entries.clear();
    while (true)
      {
        int type = in.read();
        switch (type)
          {
          case EncryptedEntry.TYPE:
            add(EncryptedEntry.decode(in));
            break;
          case PasswordEncryptedEntry.TYPE:
            add(PasswordEncryptedEntry.decode(in));
            break;
          case PasswordAuthenticatedEntry.TYPE:
            add(PasswordAuthenticatedEntry.decode(in));
            break;
          case AuthenticatedEntry.TYPE:
            add(AuthenticatedEntry.decode(in));
            break;
          case CompressedEntry.TYPE:
            add(CompressedEntry.decode(in));
            break;
          case CertificateEntry.TYPE:
            add(CertificateEntry.decode(in));
            break;
          case PublicKeyEntry.TYPE:
            add(PublicKeyEntry.decode(in));
            break;
          case PrivateKeyEntry.TYPE:
            add(PrivateKeyEntry.decode(in));
            break;
          case CertPathEntry.TYPE:
            add(CertPathEntry.decode(in));
            break;
          case BinaryDataEntry.TYPE:
            add(BinaryDataEntry.decode(in));
            break;
          case -1:
            return;
          default:
            throw new MalformedKeyringException("unknown type " + type);
          }
      }
  }

  // Own methods.
  // ------------------------------------------------------------------------

  private void makeAliasList()
  {
    log.entering(this.getClass().getName(), "makeAliasList");
    if (! entries.isEmpty())
      {
        StringBuilder buf = new StringBuilder();
        String aliasOrList;
        for (Iterator it = entries.iterator(); it.hasNext();)
          {
            Entry entry = (Entry) it.next();
            aliasOrList = null;
            if (entry instanceof EnvelopeEntry)
              aliasOrList = ((EnvelopeEntry) entry).getAliasList();
            else if (entry instanceof PrimitiveEntry)
              aliasOrList = ((PrimitiveEntry) entry).getAlias();
            else
              log.fine("Entry with no Alias. Ignored: " + entry);

            if (aliasOrList != null)
              {
                aliasOrList = aliasOrList.trim();
                if (aliasOrList.trim().length() > 0)
                {
                  buf.append(aliasOrList);
                  if (it.hasNext())
                    buf.append(';');
                }
              }
          }
        String aliasList = buf.toString();
        properties.put("alias-list", aliasList);
        log.finer("alias-list=[" + aliasList + "]");
        if (containingEnvelope != null)
          containingEnvelope.makeAliasList();
      }
    log.exiting(this.getClass().getName(), "makeAliasList");
  }
}