Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014-2015 Regents of the University of California.

Author(s):
Jeff Thompson <jefft0@remap.ucla.edu>
From PyNDN certificate.py by Adeola Bannis <thecodemaiden@gmail.com>.
Originally from code in ndn-cxx by Yingdi Yu <yingdi@cs.ucla.edu> This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. A copy of the GNU Lesser General Public License is in the file COPYING.
 
 
 package net.named_data.jndn.security.certificate;
 
 import java.util.Date;
 import java.util.List;
 
 public class Certificate extends Data {
  
The default constructor.
 
   public Certificate()
   {
   }

  
Create a Certificate from the content in the data packet.

Parameters:
data The data packet with the content to decode.
 
   public Certificate(Data datathrows DerDecodingException
   {
     super(data);
     decode();
   }

  
Encode the contents of the certificate in DER format and set the Content and MetaInfo fields.
 
   public final void
   {
     DerNode root = toDer();
     setContent(root.encode());
   }

  
Override to call the base class wireDecode then populate the certificate fields.

Parameters:
input The input byte array to be decoded as an immutable Blob.
wireFormat A WireFormat object used to decode the input.
 
   public void
   wireDecode(Blob inputWireFormat wireFormat)
     throws EncodingException
   {
     super.wireDecode(inputwireFormat);
     try {
       decode();
     } catch (DerDecodingException ex) {
       throw new EncodingException(ex.getMessage());
     }
   }

  
Add a subject description.

Parameters:
description The description to be added.
 
   public final void
   {
     .add(description);
   }
  // List of CertificateSubjectDescription.
  public final List
  {
  }

  
Add a certificate extension.

Parameters:
extension the extension to be added
  public final void
  {
    .add(extension);
  }
  // List of CertificateExtension.
  public final List
  {
    return ;
  }
  public final void
  setNotBefore(double notBefore)
  {
     = notBefore;
  }
  public final double
  {
    return ;
  }
  public final void
  setNotAfter(double notAfter)
  {
     = notAfter;
  }
  public final double
  {
    return ;
  }
  public final void
  {
     = key;
  }
  public final PublicKey
  {
    return ;
  }

  
Check if the certificate is valid.

Returns:
True if the current time is earlier than notBefore.
  public final boolean
  {
    double now = Common.getNowMilliseconds();
    return now < ;
  }

  
Check if the certificate is valid.

Returns:
True if the current time is later than notAfter.
  public final boolean
  {
    double now = Common.getNowMilliseconds();
    return now > ;
  }

  
Encode the certificate fields in DER format.

Returns:
The DER encoded contents of the certificate.
  private DerSequence
  {
    DerSequence root = new DerSequence();
    DerSequence validity = new DerSequence();
    validity.addChild(notBefore);
    validity.addChild(notAfter);
    root.addChild(validity);
    DerSequence subjectList = new DerSequence();
    for (int i = 0; i < .size(); ++i)
    root.addChild(subjectList);
    root.addChild(.toDer());
    if (.size() > 0) {
      DerSequence extensionList = new DerSequence();
      for (int i = 0; i < .size(); ++i)
        extensionList.addChild(((CertificateExtension).get(i)).toDer());
      root.addChild(extensionList);
    }
    return root;
  }

  
Populate the fields by the decoding DER data from the Content.
  private void
  {
    DerNode parsedNode = DerNode.parse(getContent().buf());
    // We need to ensure that there are:
    //   validity (notBefore, notAfter)
    //   subject list
    //   public key
    //   (optional) extension list
    List rootChildren = parsedNode.getChildren();
    // 1st: validity info
    List validityChildren = DerNode.getSequence(rootChildren, 0).getChildren();
     = (Double)((DerGeneralizedTime)validityChildren.get(0)).toVal();
     = (Double)((DerGeneralizedTime)validityChildren.get(1)).toVal();
    // 2nd: subjectList
    List subjectChildren = DerNode.getSequence(rootChildren, 1).getChildren();
    for (int i = 0; i < subjectChildren.size(); ++i) {
      DerSequence sd = DerNode.getSequence(subjectChildreni);
      List descriptionChildren = sd.getChildren();
      String oidStr = (String)((DerNode)descriptionChildren.get(0)).toVal();
      String value = ((Blob)((DerNode)descriptionChildren.get(1)).toVal()).toString();
      addSubjectDescription(new CertificateSubjectDescription(oidStrvalue));
    }
    // 3rd: public key
    Blob publicKeyInfo = ((DerNode)rootChildren.get(2)).encode();
    try {
       = new PublicKey(publicKeyInfo);
    }
    catch (UnrecognizedKeyFormatException ex) {
      throw new DerDecodingException(ex.getMessage());
    }
    if (rootChildren.size() > 3) {
      List extensionChildren = DerNode.getSequence(rootChildren, 3).getChildren();
      for (int i = 0; i < extensionChildren.size(); ++i) {
        DerSequence extInfo = DerNode.getSequence(extensionChildreni);
        List children = extInfo.getChildren();
        String oidStr = (String)((DerNode)children.get(0)).toVal();
        boolean isCritical = (Boolean)((DerBoolean)children.get(1)).toVal();
        Blob value = (Blob)((DerNode)children.get(2)).toVal();
        addExtension(new CertificateExtension(oidStrisCriticalvalue));
      }
    }
  }
  public String
  {
    String s = "Certificate name:\n";
    s += "  " + getName().toUri() + "\n";
    s += "Validity:\n";
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    String notBeforeStr = dateFormat.format(new Date((long)Math.round()));
    String notAfterStr = dateFormat.format(new Date((long)Math.round()));
    s += "  NotBefore: " + notBeforeStr + "\n";
    s += "  NotAfter: " + notAfterStr + "\n";
    for (int i = 0; i < .size(); ++i) {
      s += "Subject Description:\n";
      s += "  " + sd.getOidString() + ": " + sd.getValue() + "\n";
    }
    s += "Public key bits:\n";
    Blob keyDer = .getKeyDer();
    String encodedKey = Common.base64Encode(keyDer.getImmutableArray());
    for (int i = 0; i < encodedKey.length(); i += 64)
      s += encodedKey.substring(i, Math.min(i + 64, encodedKey.length())) + "\n";
    if (.size() > 0) {
      s += "Extensions:\n";
      for (int i = 0; i < .size(); ++i) {
        s += "  OID: " + ext.getOid() + "\n";
        s += "  Is critical: " + (ext.getIsCritical() ? 'Y' : 'N') + "\n";
        s += "  Value: " + ext.getValue().toHex() + "\n" ;
      }
    }
    return s;
  }
  // Use ArrayList without generics so it works with older Java compilers.
  private final ArrayList subjectDescriptionList_ = new ArrayList(); // of CertificateSubjectDescription
  private final ArrayList extensionList_ = new ArrayList();          // of CertificateExtension
  private double notBefore_ = .// MillisecondsSince1970
  private double notAfter_ = -.// MillisecondsSince1970
  private PublicKey key_ = new PublicKey();
New to GrepCode? Check out our FAQ X