github.com/emmansun/gmsm@v0.29.1/pkcs7/ber_test.go (about)

     1  package pkcs7
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/asn1"
     6  	"encoding/pem"
     7  	"fmt"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestBer2Der(t *testing.T) {
    13  	// indefinite length fixture
    14  	ber := []byte{0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00}
    15  	expected := []byte{0x30, 0x03, 0x02, 0x01, 0x01}
    16  	der, err := ber2der(ber)
    17  	if err != nil {
    18  		t.Fatalf("ber2der failed with error: %v", err)
    19  	}
    20  	if !bytes.Equal(der, expected) {
    21  		t.Errorf("ber2der result did not match.\n\tExpected: % X\n\tActual: % X", expected, der)
    22  	}
    23  
    24  	if der2, err := ber2der(der); err != nil {
    25  		t.Errorf("ber2der on DER bytes failed with error: %v", err)
    26  	} else {
    27  		if !bytes.Equal(der, der2) {
    28  			t.Error("ber2der is not idempotent")
    29  		}
    30  	}
    31  	var thing struct {
    32  		Number int
    33  	}
    34  	rest, err := asn1.Unmarshal(der, &thing)
    35  	if err != nil {
    36  		t.Errorf("Cannot parse resulting DER because: %v", err)
    37  	} else if len(rest) > 0 {
    38  		t.Errorf("Resulting DER has trailing data: % X", rest)
    39  	}
    40  }
    41  
    42  func TestBer2Der_Negatives(t *testing.T) {
    43  	fixtures := []struct {
    44  		Input         []byte
    45  		ErrorContains string
    46  	}{
    47  		{[]byte{0x30, 0x85}, "tag length too long"},
    48  		{[]byte{0x30, 0x84, 0x80, 0x0, 0x0, 0x0}, "length is negative"},
    49  		{[]byte{0x30, 0x82, 0x0, 0x1}, "length has leading zero"},
    50  		{[]byte{0x30, 0x80, 0x1, 0x2, 0x1, 0x2}, "Invalid BER format"},
    51  		{[]byte{0x30, 0x80, 0x1, 0x2}, "BER tag length is more than available data"},
    52  		{[]byte{0x30, 0x03, 0x01, 0x02}, "length is more than available data"},
    53  		{[]byte{0x30}, "end of ber data reached"},
    54  	}
    55  
    56  	for _, fixture := range fixtures {
    57  		_, err := ber2der(fixture.Input)
    58  		if err == nil {
    59  			t.Errorf("No error thrown. Expected: %s", fixture.ErrorContains)
    60  		}
    61  		if !strings.Contains(err.Error(), fixture.ErrorContains) {
    62  			t.Errorf("Unexpected error thrown.\n\tExpected: /%s/\n\tActual: %s", fixture.ErrorContains, err.Error())
    63  		}
    64  	}
    65  }
    66  
    67  func TestBer2Der_NestedMultipleIndefinite(t *testing.T) {
    68  	// indefinite length fixture
    69  	ber := []byte{0x30, 0x80, 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00, 0x30, 0x80, 0x02, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00}
    70  	expected := []byte{0x30, 0x0A, 0x30, 0x03, 0x02, 0x01, 0x01, 0x30, 0x03, 0x02, 0x01, 0x02}
    71  
    72  	der, err := ber2der(ber)
    73  	if err != nil {
    74  		t.Fatalf("ber2der failed with error: %v", err)
    75  	}
    76  	if !bytes.Equal(der, expected) {
    77  		t.Errorf("ber2der result did not match.\n\tExpected: % X\n\tActual: % X", expected, der)
    78  	}
    79  
    80  	if der2, err := ber2der(der); err != nil {
    81  		t.Errorf("ber2der on DER bytes failed with error: %v", err)
    82  	} else {
    83  		if !bytes.Equal(der, der2) {
    84  			t.Error("ber2der is not idempotent")
    85  		}
    86  	}
    87  	var thing struct {
    88  		Nest1 struct {
    89  			Number int
    90  		}
    91  		Nest2 struct {
    92  			Number int
    93  		}
    94  	}
    95  	rest, err := asn1.Unmarshal(der, &thing)
    96  	if err != nil {
    97  		t.Errorf("Cannot parse resulting DER because: %v", err)
    98  	} else if len(rest) > 0 {
    99  		t.Errorf("Resulting DER has trailing data: % X", rest)
   100  	}
   101  }
   102  
   103  func TestVerifyIndefiniteLengthBer(t *testing.T) {
   104  	decoded := mustDecodePEM([]byte(testPKCS7))
   105  
   106  	_, err := ber2der(decoded)
   107  	if err != nil {
   108  		t.Errorf("cannot parse indefinite length ber: %v", err)
   109  	}
   110  }
   111  
   112  func mustDecodePEM(data []byte) []byte {
   113  	var block *pem.Block
   114  	block, rest := pem.Decode(data)
   115  	if len(rest) != 0 {
   116  		panic(fmt.Errorf("unexpected remaining PEM block during decode"))
   117  	}
   118  	return block.Bytes
   119  }
   120  
   121  const testPKCS7 = `
   122  -----BEGIN PKCS7-----
   123  MIAGCSqGSIb3DQEHAqCAMIACAQExDzANBglghkgBZQMEAgEFADCABgkqhkiG9w0B
   124  BwGggCSABIIDfXsiQWdlbnRBY3Rpb25PdmVycmlkZXMiOnsiQWdlbnRPdmVycmlk
   125  ZXMiOnsiRmlsZUV4aXN0c0JlaGF2aW9yIjoiT1ZFUldSSVRFIn19LCJBcHBsaWNh
   126  dGlvbklkIjoiZTA0NDIzZTQtN2E2Ny00ZjljLWIyOTEtOTllNjNjMWMyMTU4Iiwi
   127  QXBwbGljYXRpb25OYW1lIjoibWthbmlhLXhyZF9zYW0uY2R3c19lY2hvc2VydmVy
   128  IiwiRGVwbG95bWVudENyZWF0b3IiOiJ1c2VyIiwiRGVwbG95bWVudEdyb3VwSWQi
   129  OiJmYWI5MjEwZi1mNmM3LTQyODUtYWEyZC03Mzc2MGQ4ODE3NmEiLCJEZXBsb3lt
   130  ZW50R3JvdXBOYW1lIjoibWthbmlhLXhyZF9zYW0uY2R3c19lY2hvc2VydmVyX2Rn
   131  IiwiRGVwbG95bWVudElkIjoiZC1UREUxVTNXREEiLCJEZXBsb3ltZW50VHlwZSI6
   132  IklOX1BMQUNFIiwiR2l0SHViQWNjZXNzVG9rZW4iOm51bGwsIkluc3RhbmNlR3Jv
   133  dXBJZCI6ImZhYjkyMTBmLWY2YzctNDI4NS1hYTJkLTczNzYwZDg4MTc2YSIsIlJl
   134  dmlzaW9uIjp7IkFwcFNwZWNDb250ZW50IjpudWxsLCJDb2RlQ29tbWl0UmV2aXNp
   135  b24iOm51bGwsIkdpdEh1YlJldmlzaW9uIjpudWxsLCJHaXRSZXZpc2lvbiI6bnVs
   136  bCwiUmV2aXNpb25UeXBlIjoiUzMiLCJTM1JldmlzaW9uIjp7IkJ1Y2tldCI6Im1r
   137  YW5pYS1jZHdzLWRlcGxveS1idWNrZXQiLCJCdW5kbGVUeXBlIjoiemlwIiwiRVRh
   138  ZyI6bnVsbCwiS2V5IjoieHJkOjpzYW0uY2R3czo6ZWNob3NlcnZlcjo6MTo6Lnpp
   139  cCIsIlZlcnNpb24iOm51bGx9fSwiUzNSZXZpc2lvbiI6eyJCdWNrZXQiOiJta2Fu
   140  aWEtY2R3cy1kZXBsb3ktYnVja2V0IiwiQnVuZGxlVHlwZSI6InppcCIsIkVUYWci
   141  Om51bGwsIktleSI6InhyZDo6c2FtLmNkd3M6OmVjaG9zZXJ2ZXI6OjE6Oi56aXAi
   142  LCJWZXJzaW9uIjpudWxsfSwiVGFyZ2V0UmV2aXNpb24iOm51bGx9AAAAAAAAoIAw
   143  ggWbMIIEg6ADAgECAhAGrjFMK45t2jcNHtjY1DjEMA0GCSqGSIb3DQEBCwUAMEYx
   144  CzAJBgNVBAYTAlVTMQ8wDQYDVQQKEwZBbWF6b24xFTATBgNVBAsTDFNlcnZlciBD
   145  QSAxQjEPMA0GA1UEAxMGQW1hem9uMB4XDTIwMTExMjAwMDAwMFoXDTIxMTAxNTIz
   146  NTk1OVowNDEyMDAGA1UEAxMpY29kZWRlcGxveS1zaWduZXItdXMtZWFzdC0yLmFt
   147  YXpvbmF3cy5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDit4f+
   148  I4BSv4rBV/8bJ+f4KqBwTCt9iJeau/r9liQfMgj/C1M2E+aa++u8BtY/LQstB44v
   149  v6KqcaiOyWpkD9OsUty9qb4eNTPF2Y4jpNsi/Hfw0phsd9gLun2foppILmL4lZIG
   150  lBhTeEwv6qV4KbyXOG9abHOX32+jVFtM1rbzHNFvz90ysfZp16TBAi7IRKEZeXvd
   151  MvlJJMAJtAoblxiDIS3A1csY1G4XHYET8xIoCop3mqEZEtAxUUP2epdXXdhD2U0G
   152  7alSRS54o91QW1Dp3A13lu1A1nds9CkWlPkDTpKSUG/qN5y5+6dCCGaydgL5krMs
   153  R79bCrR1sEKm5hi1AgMBAAGjggKVMIICkTAfBgNVHSMEGDAWgBRZpGYGUqB7lZI8
   154  o5QHJ5Z0W/k90DAdBgNVHQ4EFgQUPF5qTbnTDYhmp7tGmmL/jTmLoHMwNAYDVR0R
   155  BC0wK4IpY29kZWRlcGxveS1zaWduZXItdXMtZWFzdC0yLmFtYXpvbmF3cy5jb20w
   156  DgYDVR0PAQH/BAQDAgWgMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjA7
   157  BgNVHR8ENDAyMDCgLqAshipodHRwOi8vY3JsLnNjYTFiLmFtYXpvbnRydXN0LmNv
   158  bS9zY2ExYi5jcmwwIAYDVR0gBBkwFzALBglghkgBhv1sAQIwCAYGZ4EMAQIBMHUG
   159  CCsGAQUFBwEBBGkwZzAtBggrBgEFBQcwAYYhaHR0cDovL29jc3Auc2NhMWIuYW1h
   160  em9udHJ1c3QuY29tMDYGCCsGAQUFBzAChipodHRwOi8vY3J0LnNjYTFiLmFtYXpv
   161  bnRydXN0LmNvbS9zY2ExYi5jcnQwDAYDVR0TAQH/BAIwADCCAQQGCisGAQQB1nkC
   162  BAIEgfUEgfIA8AB2APZclC/RdzAiFFQYCDCUVo7jTRMZM7/fDC8gC8xO8WTjAAAB
   163  dboejIcAAAQDAEcwRQIgeqoKXbST17TCEzM1BMWx/jjyVQVBIN3LG17U4OaV364C
   164  IQDPUSJZhJm7uqGea6+VwqeDe/vGuGSuJzkDwTIOeIXPaAB2AFzcQ5L+5qtFRLFe
   165  mtRW5hA3+9X6R9yhc5SyXub2xw7KAAABdboejNQAAAQDAEcwRQIgEKIAwwhjUcq2
   166  iwzBAagdy+fTiKnBY1Yjf6wOeRpwXfMCIQC8wM3nxiWrGgIpdzzgDvFhZZTV3N81
   167  JWcYAu+srIVOhTANBgkqhkiG9w0BAQsFAAOCAQEAer9kml53XFy4ZSVzCbdsIFYP
   168  Ohu7LDf5iffHBVZFnGOEVOmiPYYkNwi9R6EHIYaAs7G7GGLCp/6tdc+G4eF1j6wB
   169  IkmXZcxMTxk/87R+S+36yDLg1GBZvqttLfexj0TRVAfVLJc7FjLXAW2+wi7YyNe8
   170  X17lWBwHxa1r5KgweJshGzYVUsgMTSx0aJ+93ZnqplBp9x+9DSQNqqNlBgxFANxs
   171  ux+dfpduyLd8VLqtlECGC07tYE4mBaAjMiNjCZRWMp8ya/Z6J/bJZ27IDGA4dXzm
   172  l9NNnlbuUDAenAByUqE+0b78J6EmmdAVf+N8siriMg02FdP3lAXJLE8tDeZp8AAA
   173  MYICIDCCAhwCAQEwWjBGMQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRUw
   174  EwYDVQQLEwxTZXJ2ZXIgQ0EgMUIxDzANBgNVBAMTBkFtYXpvbgIQBq4xTCuObdo3
   175  DR7Y2NQ4xDANBglghkgBZQMEAgEFAKCBmDAYBgkqhkiG9w0BCQMxCwYJKoZIhvcN
   176  AQcBMBwGCSqGSIb3DQEJBTEPFw0yMTA2MjQxOTU1MzFaMC0GCSqGSIb3DQEJNDEg
   177  MB4wDQYJYIZIAWUDBAIBBQChDQYJKoZIhvcNAQELBQAwLwYJKoZIhvcNAQkEMSIE
   178  IP7gMuT2H0/AhgPgj3Eo0NWCIdQOBjJO18coNKIaOnJYMA0GCSqGSIb3DQEBCwUA
   179  BIIBAJX+e87q0YvRon9/ENTvE0FoYMzYblID2Reek6L217ZlZ6pUuRsc4ghhJ5Yh
   180  WZeOCaLwi4mrnQ5/+DGKkJ4a/w5sqFTwtJIGIIAuDCn/uDm8kIDUVkbeznSOLoPA
   181  67cxiqgIdqZ5pqUoid2YsDj20owrGDG4wUF6ZvhM9g/5va3CAhxqvTE2HwjhHTfz
   182  Cgl8Nlvalz7YxXEf2clFEiEVa1fVaGMl9pCyedAmTfd6hoivcpAsopvXfVaaaR2y
   183  iuZidpUfFhSk+Ls7TU/kB74ckfUGj5q/5HcKJgb/S+FYUV7eu0ewzTyW1uRl/d0U
   184  Tb7e7EjgDGJsjOTMdTrMfv8ho8kAAAAAAAA=
   185  -----END PKCS7-----
   186  `