1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_BASE_ASN1_UTIL_H_
6#define NET_BASE_ASN1_UTIL_H_
7#pragma once
8
9#include "base/string_piece.h"
10
11namespace net {
12
13namespace asn1 {
14
15// These are the DER encodings of the tag byte for ASN.1 objects.
16static const unsigned kINTEGER = 0x02;
17static const unsigned kOID = 0x06;
18static const unsigned kSEQUENCE = 0x30;
19
20// These are flags that can be ORed with the above tag numbers.
21static const unsigned kContextSpecific = 0x80;
22static const unsigned kCompound = 0x20;
23
24// ParseElement parses a DER encoded ASN1 element from |in|, requiring that
25// it have the given |tag_value|. It returns true on success. The following
26// limitations are imposed:
27//   1) tag numbers > 31 are not permitted.
28//   2) lengths > 65535 are not permitted.
29// On successful return:
30//   |in| is advanced over the element
31//   |out| contains the element, including the tag and length bytes.
32//   |out_header_len| contains the length of the tag and length bytes in |out|.
33bool ParseElement(base::StringPiece* in,
34                  unsigned tag_value,
35                  base::StringPiece* out,
36                  unsigned *out_header_len);
37
38// GetElement performs the same actions as ParseElement, except that the header
39// bytes are not included in the output.
40bool GetElement(base::StringPiece* in,
41                unsigned tag_value,
42                base::StringPiece* out);
43
44
45// ExtractSPKIFromDERCert parses the DER encoded certificate in |cert| and
46// extracts the bytes of the SubjectPublicKeyInfo. On successful return,
47// |spki_out| is set to contain the SPKI, pointing into |cert|.
48bool ExtractSPKIFromDERCert(base::StringPiece cert,
49                            base::StringPiece* spki_out);
50
51} // namespace asn1
52
53} // namespace net
54
55#endif // NET_BASE_ASN1_UTIL_H_
56