DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (5f0b392beadb)

VCS Links

Line Code
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsNSSASN1Object.h"

#include "nsArray.h"
#include "nsArrayUtils.h"
#include "nsIComponentManager.h"
#include "nsReadableUtils.h"
#include "nsXPCOMCID.h"
#include "secasn1.h"

NS_IMPL_ISUPPORTS(nsNSSASN1Sequence, nsIASN1Sequence, nsIASN1Object)
NS_IMPL_ISUPPORTS(nsNSSASN1PrintableItem, nsIASN1PrintableItem, nsIASN1Object)

// This function is used to interpret an integer that
// was encoded in a DER buffer. This function is used
// when converting a DER buffer into a nsIASN1Object
// structure.  This interprets the buffer in data
// as defined by the DER (Distinguised Encoding Rules) of
// ASN1.
static int getInteger256(unsigned char* data, unsigned int nb) {
  int val;

  switch (nb) {
    case 1:
      val = data[0];
      break;
    case 2:
      val = (data[0] << 8) | data[1];
      break;
    case 3:
      val = (data[0] << 16) | (data[1] << 8) | data[2];
      break;
    case 4:
      val = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
      break;
    default:
      return -1;
  }

  return val;
}

// This function is used to retrieve the lenght of a DER encoded
// item.  It looks to see if this a multibyte length and then
// interprets the buffer accordingly to get the actual length value.
// This funciton is used mostly while parsing the DER headers.
//
// A DER encoded item has the following structure:
//
//  <tag><length<data consisting of lenght bytes>
static int32_t getDERItemLength(unsigned char* data, unsigned char* end,
                                unsigned long* bytesUsed, bool* indefinite) {
  unsigned char lbyte = *data++;
  int32_t length = -1;

  *indefinite = false;
  if (lbyte >= 0x80) {
    // Multibyte length
    unsigned nb = (unsigned)(lbyte & 0x7f);
    if (nb > 4) {
      return -1;
    }
    if (nb > 0) {
      if ((data + nb) > end) {
        return -1;
      }
      length = getInteger256(data, nb);
      if (length < 0) return -1;
    } else {
      *indefinite = true;
      length = 0;
    }
    *bytesUsed = nb + 1;
  } else {
    length = lbyte;
    *bytesUsed = 1;
  }
  return length;
}

static nsresult buildASN1ObjectFromDER(unsigned char* data, unsigned char* end,
                                       nsIASN1Sequence* parent) {
  nsresult rv;
  nsCOMPtr<nsIASN1Sequence> sequence;
  nsCOMPtr<nsIASN1PrintableItem> printableItem;
  nsCOMPtr<nsIASN1Object> asn1Obj;
  nsCOMPtr<nsIMutableArray> parentObjects;

  NS_ENSURE_ARG_POINTER(parent);
  if (data >= end) return NS_OK;

  unsigned char code, tagnum;

  // A DER item has the form of |tag|len|data
  // tag is one byte and describes the type of element
  //     we are dealing with.
  // len is a DER encoded int telling us how long the data is
  // data is a buffer that is len bytes long and has to be
  //      interpreted according to its type.
  unsigned long bytesUsed;
  bool indefinite;
  int32_t len;
  uint32_t type;

  rv = parent->GetASN1Objects(getter_AddRefs(parentObjects));
  if (NS_FAILED(rv) || !parentObjects) return NS_ERROR_FAILURE;
  while (data < end) {
    code = *data;
    tagnum = code & SEC_ASN1_TAGNUM_MASK;

    /*
     * NOTE: This code does not (yet) handle the high-tag-number form!
     */
    if (tagnum == SEC_ASN1_HIGH_TAG_NUMBER) {
      return NS_ERROR_FAILURE;
    }

    data++;
    len = getDERItemLength(data, end, &bytesUsed, &indefinite);
    if (len < 0) {
      return NS_ERROR_FAILURE;
    }

    data += bytesUsed;
    if (data + len > end) {
      return NS_ERROR_FAILURE;
    }

    if (code & SEC_ASN1_CONSTRUCTED) {
      if (len > 0 || indefinite) {
        sequence = new nsNSSASN1Sequence();
        switch (code & SEC_ASN1_CLASS_MASK) {
          case SEC_ASN1_UNIVERSAL:
            type = tagnum;
            break;
          case SEC_ASN1_APPLICATION:
            type = nsIASN1Object::ASN1_APPLICATION;
            break;
          case SEC_ASN1_CONTEXT_SPECIFIC:
            type = nsIASN1Object::ASN1_CONTEXT_SPECIFIC;
            break;
          case SEC_ASN1_PRIVATE:
            type = nsIASN1Object::ASN1_PRIVATE;
            break;
          default:
            NS_ERROR("Bad DER");
            return NS_ERROR_FAILURE;
        }
        sequence->SetTag(tagnum);
        sequence->SetType(type);
        rv = buildASN1ObjectFromDER(data, (len == 0) ? end : data + len,
                                    sequence);
        asn1Obj = sequence;
      }
    } else {
      printableItem = new nsNSSASN1PrintableItem();

      asn1Obj = printableItem;
      asn1Obj->SetType(tagnum);
      asn1Obj->SetTag(tagnum);
      printableItem->SetData((char*)data, len);
    }
    data += len;
    parentObjects->AppendElement(asn1Obj);
  }

  return NS_OK;
}

nsresult CreateFromDER(unsigned char* data, unsigned int len,
                       nsIASN1Object** retval) {
  nsCOMPtr<nsIASN1Sequence> sequence = new nsNSSASN1Sequence;
  *retval = nullptr;

  nsresult rv = buildASN1ObjectFromDER(data, data + len, sequence);

  if (NS_SUCCEEDED(rv)) {
    // The actual object will be the first element inserted
    // into the sequence of the sequence variable we created.
    nsCOMPtr<nsIMutableArray> elements;

    sequence->GetASN1Objects(getter_AddRefs(elements));
    nsCOMPtr<nsIASN1Object> asn1Obj = do_QueryElementAt(elements, 0);
    if (!asn1Obj) {
      return NS_ERROR_FAILURE;
    }

    asn1Obj.forget(retval);
  }
  return rv;
}

nsNSSASN1Sequence::nsNSSASN1Sequence()
    : mType(0), mTag(0), mIsValidContainer(true), mIsExpanded(true) {
  /* member initializers and constructor code */
}

nsNSSASN1Sequence::~nsNSSASN1Sequence() { /* destructor code */
}

NS_IMETHODIMP
nsNSSASN1Sequence::GetASN1Objects(nsIMutableArray** aASN1Objects) {
  if (!mASN1Objects) {
    mASN1Objects = nsArrayBase::Create();
  }
  *aASN1Objects = mASN1Objects;
  NS_IF_ADDREF(*aASN1Objects);
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1Sequence::SetASN1Objects(nsIMutableArray* aASN1Objects) {
  mASN1Objects = aASN1Objects;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1Sequence::GetTag(uint32_t* aTag) {
  *aTag = mTag;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1Sequence::SetTag(uint32_t aTag) {
  mTag = aTag;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1Sequence::GetType(uint32_t* aType) {
  *aType = mType;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1Sequence::SetType(uint32_t aType) {
  mType = aType;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1Sequence::GetDisplayName(nsAString& aDisplayName) {
  aDisplayName = mDisplayName;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1Sequence::SetDisplayName(const nsAString& aDisplayName) {
  mDisplayName = aDisplayName;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1Sequence::GetDisplayValue(nsAString& aDisplayValue) {
  aDisplayValue = mDisplayValue;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1Sequence::SetDisplayValue(const nsAString& aDisplayValue) {
  mDisplayValue = aDisplayValue;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1Sequence::GetIsValidContainer(bool* aIsValidContainer) {
  NS_ENSURE_ARG_POINTER(aIsValidContainer);
  *aIsValidContainer = mIsValidContainer;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1Sequence::SetIsValidContainer(bool aIsValidContainer) {
  mIsValidContainer = aIsValidContainer;
  SetIsExpanded(mIsValidContainer);
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1Sequence::GetIsExpanded(bool* aIsExpanded) {
  NS_ENSURE_ARG_POINTER(aIsExpanded);
  *aIsExpanded = mIsExpanded;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1Sequence::SetIsExpanded(bool aIsExpanded) {
  mIsExpanded = aIsExpanded;
  return NS_OK;
}

nsNSSASN1PrintableItem::nsNSSASN1PrintableItem()
    : mType(0), mTag(0), mData(nullptr), mLen(0) {
  /* member initializers and constructor code */
}

nsNSSASN1PrintableItem::~nsNSSASN1PrintableItem() {
  /* destructor code */
  if (mData) free(mData);
}

NS_IMETHODIMP
nsNSSASN1PrintableItem::GetDisplayValue(nsAString& aValue) {
  aValue = mValue;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1PrintableItem::SetDisplayValue(const nsAString& aValue) {
  mValue = aValue;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1PrintableItem::GetTag(uint32_t* aTag) {
  *aTag = mTag;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1PrintableItem::SetTag(uint32_t aTag) {
  mTag = aTag;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1PrintableItem::GetType(uint32_t* aType) {
  *aType = mType;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1PrintableItem::SetType(uint32_t aType) {
  mType = aType;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1PrintableItem::SetData(char* data, uint32_t len) {
  if (len > 0) {
    if (mLen < len) {
      mData = (unsigned char*)moz_xrealloc(mData, len);
    }

    memcpy(mData, data, len);
  } else if (len == 0) {
    if (mData) {
      free(mData);
      mData = nullptr;
    }
  }
  mLen = len;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1PrintableItem::GetData(char** outData, uint32_t* outLen) {
  NS_ENSURE_ARG_POINTER(outData);
  NS_ENSURE_ARG_POINTER(outLen);

  *outData = (char*)mData;
  *outLen = mLen;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1PrintableItem::GetDisplayName(nsAString& aDisplayName) {
  aDisplayName = mDisplayName;
  return NS_OK;
}

NS_IMETHODIMP
nsNSSASN1PrintableItem::SetDisplayName(const nsAString& aDisplayName) {
  mDisplayName = aDisplayName;
  return NS_OK;
}