...

Source file src/github.com/google/certificate-transparency-go/client/getentries.go

Documentation: github.com/google/certificate-transparency-go/client

     1  // Copyright 2016 Google LLC. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package client
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"strconv"
    21  
    22  	ct "github.com/google/certificate-transparency-go"
    23  	"github.com/google/certificate-transparency-go/x509"
    24  )
    25  
    26  // GetRawEntries exposes the /ct/v1/get-entries result with only the JSON parsing done.
    27  func (c *LogClient) GetRawEntries(ctx context.Context, start, end int64) (*ct.GetEntriesResponse, error) {
    28  	if end < 0 {
    29  		return nil, errors.New("end should be >= 0")
    30  	}
    31  	if end < start {
    32  		return nil, errors.New("start should be <= end")
    33  	}
    34  
    35  	params := map[string]string{
    36  		"start": strconv.FormatInt(start, 10),
    37  		"end":   strconv.FormatInt(end, 10),
    38  	}
    39  
    40  	var resp ct.GetEntriesResponse
    41  	if _, _, err := c.GetAndParse(ctx, ct.GetEntriesPath, params, &resp); err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	return &resp, nil
    46  }
    47  
    48  // GetEntries attempts to retrieve the entries in the sequence [start, end] from the CT log server
    49  // (RFC6962 s4.6) as parsed [pre-]certificates for convenience, held in a slice of ct.LogEntry structures.
    50  // However, this does mean that any certificate parsing failures will cause a failure of the whole
    51  // retrieval operation; for more robust retrieval of parsed certificates, use GetRawEntries() and invoke
    52  // ct.LogEntryFromLeaf() on each individual entry.
    53  func (c *LogClient) GetEntries(ctx context.Context, start, end int64) ([]ct.LogEntry, error) {
    54  	resp, err := c.GetRawEntries(ctx, start, end)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	entries := make([]ct.LogEntry, len(resp.Entries))
    59  	for i, entry := range resp.Entries {
    60  		index := start + int64(i)
    61  		logEntry, err := ct.LogEntryFromLeaf(index, &entry)
    62  		if x509.IsFatal(err) {
    63  			return nil, err
    64  		}
    65  		entries[i] = *logEntry
    66  	}
    67  	return entries, nil
    68  }
    69  

View as plain text