summaryrefslogtreecommitdiffstats
path: root/net/base/asn1_util.h
blob: 563f6d1dbf893c80dd313bbafd274c2971b92fb6 (plain)
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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_BASE_ASN1_UTIL_H_
#define NET_BASE_ASN1_UTIL_H_
#pragma once

#include "base/string_piece.h"
#include "net/base/net_api.h"

namespace net {

namespace asn1 {

// These are the DER encodings of the tag byte for ASN.1 objects.
static const unsigned kINTEGER = 0x02;
static const unsigned kOID = 0x06;
static const unsigned kSEQUENCE = 0x30;

// These are flags that can be ORed with the above tag numbers.
static const unsigned kContextSpecific = 0x80;
static const unsigned kCompound = 0x20;

// ParseElement parses a DER encoded ASN1 element from |in|, requiring that
// it have the given |tag_value|. It returns true on success. The following
// limitations are imposed:
//   1) tag numbers > 31 are not permitted.
//   2) lengths > 65535 are not permitted.
// On successful return:
//   |in| is advanced over the element
//   |out| contains the element, including the tag and length bytes.
//   |out_header_len| contains the length of the tag and length bytes in |out|.
bool ParseElement(base::StringPiece* in,
                  unsigned tag_value,
                  base::StringPiece* out,
                  unsigned *out_header_len);

// GetElement performs the same actions as ParseElement, except that the header
// bytes are not included in the output.
bool GetElement(base::StringPiece* in,
                unsigned tag_value,
                base::StringPiece* out);


// ExtractSPKIFromDERCert parses the DER encoded certificate in |cert| and
// extracts the bytes of the SubjectPublicKeyInfo. On successful return,
// |spki_out| is set to contain the SPKI, pointing into |cert|.
NET_TEST bool ExtractSPKIFromDERCert(base::StringPiece cert,
                                     base::StringPiece* spki_out);

} // namespace asn1

} // namespace net

#endif // NET_BASE_ASN1_UTIL_H_