2 Copyright (C) 1999 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu
.java
.security
.provider
;
41 import java
.math
.BigInteger
;
42 import gnu
.java
.security
.der
.DEREncodingException
;
44 public class DERReader
49 static final int UNIVERSAL
= 1;
50 static final int APPLICATION
= 2;
51 static final int CONTEXT_SPECIFIC
= 3;
52 static final int PRIVATE
= 4;
61 public DERReader( byte source
[] )
66 public void init( String source
)
68 init( source
.getBytes() );
71 public void init( byte source
[] )
77 public BigInteger
getBigInteger() throws DEREncodingException
79 return new BigInteger( getPrimitive() );
82 //Reads Primitive, definite-length method
83 private byte[] getPrimitive() throws DEREncodingException
88 byte identifier
= source
[tmp
++];
89 if( (0x20 & identifier
) != 0)
90 throw new DEREncodingException();
91 int type
= translateLeadIdentifierByte(identifier
);
92 //System.out.println("Type: " + type);
95 int tag
= (0x1f & identifier
);
97 // tag = getIdentifier(tmp);
98 //System.out.println("Tag: " + tag);
101 byte len
= source
[tmp
]; //may be length of length parameter
102 long length
= 0x7f & len
;
104 if( (0x80 & len
) != 0 ) {
105 //System.out.println("Extra Long Length");
107 //System.out.println("Length of Length: " + len);
110 for( i
= 0; i
< len
; i
++ ) {
113 length
+= (source
[tmp
] < 0 ) ?
114 (256 + source
[tmp
]) :
116 //System.out.println("Length of Length: " + length);
122 /*System.out.println("Position: " + tmp);
123 System.out.println("Length: " + length);
124 for( i = 0; i < 10; i++)
125 System.out.print(source[tmp + i] + " ");
126 System.out.println();*/
128 byte tmpb
[] = new byte[ (int)length
];
129 System
.arraycopy( source
, tmp
, tmpb
, 0, (int)length
);
130 pos
= (int)(tmp
+ length
);
134 private int translateLeadIdentifierByte(byte b
)
136 if( (0x3f & b
) == b
)
138 else if( (0x7f & b
) == b
)
140 else if( (0xbf & b
) == b
)
141 return CONTEXT_SPECIFIC
;
146 private int getIdentifier(int tpos
)
148 while( (0x80 & source
[tpos
]) != 0)