001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package org.apache.geronimo.util.asn1.sec;
020
021 import java.math.BigInteger;
022 import org.apache.geronimo.util.asn1.*;
023
024 /**
025 * the elliptic curve private key object from SEC 1
026 */
027 public class ECPrivateKeyStructure
028 extends ASN1Encodable
029 {
030 private ASN1Sequence seq;
031
032 public ECPrivateKeyStructure(
033 ASN1Sequence seq)
034 {
035 this.seq = seq;
036 }
037
038 public ECPrivateKeyStructure(
039 BigInteger key)
040 {
041 byte[] bytes = key.toByteArray();
042
043 if (bytes[0] == 0)
044 {
045 byte[] tmp = new byte[bytes.length - 1];
046
047 System.arraycopy(bytes, 1, tmp, 0, tmp.length);
048 bytes = tmp;
049 }
050
051 ASN1EncodableVector v = new ASN1EncodableVector();
052
053 v.add(new DERInteger(1));
054 v.add(new DEROctetString(bytes));
055
056 seq = new DERSequence(v);
057 }
058
059 public BigInteger getKey()
060 {
061 ASN1OctetString octs = (ASN1OctetString)seq.getObjectAt(1);
062
063 BigInteger k = new BigInteger(1, octs.getOctets());
064
065 return k;
066 }
067
068 public DERObject toASN1Object()
069 {
070 return seq;
071 }
072 }