...

Source file src/golang.org/x/crypto/openpgp/packet/signature_v3_test.go

Documentation: golang.org/x/crypto/openpgp/packet

     1  // Copyright 2013 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 packet
     6  
     7  import (
     8  	"bytes"
     9  	"crypto"
    10  	"encoding/hex"
    11  	"io"
    12  	"testing"
    13  
    14  	"golang.org/x/crypto/openpgp/armor"
    15  )
    16  
    17  func TestSignatureV3Read(t *testing.T) {
    18  	r := v3KeyReader(t)
    19  	Read(r)                // Skip public key
    20  	Read(r)                // Skip uid
    21  	packet, err := Read(r) // Signature
    22  	if err != nil {
    23  		t.Error(err)
    24  		return
    25  	}
    26  	sig, ok := packet.(*SignatureV3)
    27  	if !ok || sig.SigType != SigTypeGenericCert || sig.PubKeyAlgo != PubKeyAlgoRSA || sig.Hash != crypto.MD5 {
    28  		t.Errorf("failed to parse, got: %#v", packet)
    29  	}
    30  }
    31  
    32  func TestSignatureV3Reserialize(t *testing.T) {
    33  	r := v3KeyReader(t)
    34  	Read(r) // Skip public key
    35  	Read(r) // Skip uid
    36  	packet, err := Read(r)
    37  	if err != nil {
    38  		t.Error(err)
    39  		return
    40  	}
    41  	sig := packet.(*SignatureV3)
    42  	out := new(bytes.Buffer)
    43  	if err = sig.Serialize(out); err != nil {
    44  		t.Errorf("error reserializing: %s", err)
    45  		return
    46  	}
    47  	expected, err := io.ReadAll(v3KeyReader(t))
    48  	if err != nil {
    49  		t.Error(err)
    50  		return
    51  	}
    52  	expected = expected[4+141+4+39:] // See pgpdump offsets below, this is where the sig starts
    53  	if !bytes.Equal(expected, out.Bytes()) {
    54  		t.Errorf("output doesn't match input (got vs expected):\n%s\n%s", hex.Dump(out.Bytes()), hex.Dump(expected))
    55  	}
    56  }
    57  
    58  func v3KeyReader(t *testing.T) io.Reader {
    59  	armorBlock, err := armor.Decode(bytes.NewBufferString(keySigV3Armor))
    60  	if err != nil {
    61  		t.Fatalf("armor Decode failed: %v", err)
    62  	}
    63  	return armorBlock.Body
    64  }
    65  
    66  // keySigV3Armor is some V3 public key I found in an SKS dump.
    67  // Old: Public Key Packet(tag 6)(141 bytes)
    68  //
    69  //	Ver 4 - new
    70  //	Public key creation time - Fri Sep 16 17:13:54 CDT 1994
    71  //	Pub alg - unknown(pub 0)
    72  //	Unknown public key(pub 0)
    73  //
    74  // Old: User ID Packet(tag 13)(39 bytes)
    75  //
    76  //	User ID - Armin M. Warda <warda@nephilim.ruhr.de>
    77  //
    78  // Old: Signature Packet(tag 2)(149 bytes)
    79  //
    80  //	Ver 4 - new
    81  //	Sig type - unknown(05)
    82  //	Pub alg - ElGamal Encrypt-Only(pub 16)
    83  //	Hash alg - unknown(hash 46)
    84  //	Hashed Sub: unknown(sub 81, critical)(1988 bytes)
    85  const keySigV3Armor = `-----BEGIN PGP PUBLIC KEY BLOCK-----
    86  Version: SKS 1.0.10
    87  
    88  mI0CLnoYogAAAQQA1qwA2SuJwfQ5bCQ6u5t20ulnOtY0gykf7YjiK4LiVeRBwHjGq7v30tGV
    89  5Qti7qqRW4Ww7CDCJc4sZMFnystucR2vLkXaSoNWoFm4Fg47NiisDdhDezHwbVPW6OpCFNSi
    90  ZAamtj4QAUBu8j4LswafrJqZqR9336/V3g8Yil2l48kABRG0J0FybWluIE0uIFdhcmRhIDx3
    91  YXJkYUBuZXBoaWxpbS5ydWhyLmRlPoiVAgUQLok2xwXR6zmeWEiZAQE/DgP/WgxPQh40/Po4
    92  gSkWZCDAjNdph7zexvAb0CcUWahcwiBIgg3U5ErCx9I5CNVA9U+s8bNrDZwgSIeBzp3KhWUx
    93  524uhGgm6ZUTOAIKA6CbV6pfqoLpJnRYvXYQU5mIWsNa99wcu2qu18OeEDnztb7aLA6Ra9OF
    94  YFCbq4EjXRoOrYM=
    95  =LPjs
    96  -----END PGP PUBLIC KEY BLOCK-----`
    97  

View as plain text