gitee.com/lh-her-team/common@v1.5.1/crypto/tls/gm_handshake_messages.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package tls
     6  
     7  type certificateRequestMsgGM struct {
     8  	raw                    []byte
     9  	certificateTypes       []byte
    10  	certificateAuthorities [][]byte
    11  }
    12  
    13  func (m *certificateRequestMsgGM) marshal() (x []byte) {
    14  	if m.raw != nil {
    15  		return m.raw
    16  	}
    17  	// See https://tools.ietf.org/html/rfc4346#section-7.4.4
    18  	length := 1 + len(m.certificateTypes) + 2
    19  	casLength := 0
    20  	for _, ca := range m.certificateAuthorities {
    21  		casLength += 2 + len(ca)
    22  	}
    23  	length += casLength
    24  	x = make([]byte, 4+length)
    25  	x[0] = typeCertificateRequest
    26  	x[1] = uint8(length >> 16)
    27  	x[2] = uint8(length >> 8)
    28  	x[3] = uint8(length)
    29  	x[4] = uint8(len(m.certificateTypes))
    30  	copy(x[5:], m.certificateTypes)
    31  	y := x[5+len(m.certificateTypes):]
    32  	y[0] = uint8(casLength >> 8)
    33  	y[1] = uint8(casLength)
    34  	y = y[2:]
    35  	for _, ca := range m.certificateAuthorities {
    36  		y[0] = uint8(len(ca) >> 8)
    37  		y[1] = uint8(len(ca))
    38  		y = y[2:]
    39  		copy(y, ca)
    40  		y = y[len(ca):]
    41  	}
    42  	m.raw = x
    43  	return
    44  }
    45  
    46  func (m *certificateRequestMsgGM) unmarshal(data []byte) bool {
    47  	m.raw = data
    48  	if len(data) < 5 {
    49  		return false
    50  	}
    51  	length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
    52  	if uint32(len(data))-4 != length {
    53  		return false
    54  	}
    55  	numCertTypes := int(data[4])
    56  	data = data[5:]
    57  	if numCertTypes == 0 || len(data) <= numCertTypes {
    58  		return false
    59  	}
    60  	m.certificateTypes = make([]byte, numCertTypes)
    61  	if copy(m.certificateTypes, data) != numCertTypes {
    62  		return false
    63  	}
    64  	data = data[numCertTypes:]
    65  	if len(data) < 2 {
    66  		return false
    67  	}
    68  	casLength := uint16(data[0])<<8 | uint16(data[1])
    69  	data = data[2:]
    70  	if len(data) < int(casLength) {
    71  		return false
    72  	}
    73  	cas := make([]byte, casLength)
    74  	copy(cas, data)
    75  	data = data[casLength:]
    76  	m.certificateAuthorities = nil
    77  	for len(cas) > 0 {
    78  		if len(cas) < 2 {
    79  			return false
    80  		}
    81  		caLen := uint16(cas[0])<<8 | uint16(cas[1])
    82  		cas = cas[2:]
    83  		if len(cas) < int(caLen) {
    84  			return false
    85  		}
    86  		m.certificateAuthorities = append(m.certificateAuthorities, cas[:caLen])
    87  		cas = cas[caLen:]
    88  	}
    89  	return len(data) == 0
    90  }