security/pkix/lib/pkixder.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/pkix/lib/pkixder.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,82 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* Copyright 2013 Mozilla Foundation
     1.7 + *
     1.8 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.9 + * you may not use this file except in compliance with the License.
    1.10 + * You may obtain a copy of the License at
    1.11 + *
    1.12 + *     http://www.apache.org/licenses/LICENSE-2.0
    1.13 + *
    1.14 + * Unless required by applicable law or agreed to in writing, software
    1.15 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.16 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.17 + * See the License for the specific language governing permissions and
    1.18 + * limitations under the License.
    1.19 + */
    1.20 +
    1.21 +#include "pkixder.h"
    1.22 +
    1.23 +namespace mozilla { namespace pkix { namespace der {
    1.24 +
    1.25 +// not inline
    1.26 +Result
    1.27 +Fail(PRErrorCode errorCode)
    1.28 +{
    1.29 +  PR_SetError(errorCode, 0);
    1.30 +  return Failure;
    1.31 +}
    1.32 +
    1.33 +// Too complicated to be inline
    1.34 +Result
    1.35 +ExpectTagAndGetLength(Input& input, uint8_t expectedTag, uint16_t& length)
    1.36 +{
    1.37 +  PR_ASSERT((expectedTag & 0x1F) != 0x1F); // high tag number form not allowed
    1.38 +
    1.39 +  uint8_t tag;
    1.40 +  if (input.Read(tag) != Success) {
    1.41 +    return Failure;
    1.42 +  }
    1.43 +
    1.44 +  if (tag != expectedTag) {
    1.45 +    return Fail(SEC_ERROR_BAD_DER);
    1.46 +  }
    1.47 +
    1.48 +  // The short form of length is a single byte with the high order bit set
    1.49 +  // to zero. The long form of length is one byte with the high order bit
    1.50 +  // set, followed by N bytes, where N is encoded in the lowest 7 bits of
    1.51 +  // the first byte.
    1.52 +  uint8_t length1;
    1.53 +  if (input.Read(length1) != Success) {
    1.54 +    return Failure;
    1.55 +  }
    1.56 +  if (!(length1 & 0x80)) {
    1.57 +    length = length1;
    1.58 +  } else if (length1 == 0x81) {
    1.59 +    uint8_t length2;
    1.60 +    if (input.Read(length2) != Success) {
    1.61 +      return Failure;
    1.62 +    }
    1.63 +    if (length2 < 128) {
    1.64 +      // Not shortest possible encoding
    1.65 +      return Fail(SEC_ERROR_BAD_DER);
    1.66 +    }
    1.67 +    length = length2;
    1.68 +  } else if (length1 == 0x82) {
    1.69 +    if (input.Read(length) != Success) {
    1.70 +      return Failure;
    1.71 +    }
    1.72 +    if (length < 256) {
    1.73 +      // Not shortest possible encoding
    1.74 +      return Fail(SEC_ERROR_BAD_DER);
    1.75 +    }
    1.76 +  } else {
    1.77 +    // We don't support lengths larger than 2^16 - 1.
    1.78 +    return Fail(SEC_ERROR_BAD_DER);
    1.79 +  }
    1.80 +
    1.81 +  // Ensure the input is long enough for the length it says it has.
    1.82 +  return input.EnsureLength(length);
    1.83 +}
    1.84 +
    1.85 +} } } // namespace mozilla::pkix::der

mercurial