github.com/emmansun/gmsm@v0.29.1/smx509/example_test.go (about)

     1  package smx509_test
     2  
     3  import (
     4  	"crypto/dsa"
     5  	"crypto/ecdsa"
     6  	"crypto/ed25519"
     7  	"crypto/rsa"
     8  	"encoding/asn1"
     9  	"encoding/pem"
    10  	"fmt"
    11  	"strings"
    12  
    13  	"github.com/emmansun/gmsm/sm2"
    14  	"github.com/emmansun/gmsm/smx509"
    15  )
    16  
    17  func ExampleParsePKIXPublicKey() {
    18  	const pubPEM = `
    19  -----BEGIN PUBLIC KEY-----
    20  MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAENpoOih+9ASfmKYx5lK5mLsrUK3Am
    21  B6kLUsqHlVyglXgoMEwo8Sr8xb/Q3gDMNnd7Wyp2bJE9ksb60ansO4QaKg==
    22  -----END PUBLIC KEY-----`
    23  
    24  	block, _ := pem.Decode([]byte(pubPEM))
    25  	if block == nil {
    26  		panic("failed to parse PEM block containing the public key")
    27  	}
    28  
    29  	pub, err := smx509.ParsePKIXPublicKey(block.Bytes)
    30  	if err != nil {
    31  		panic("failed to parse DER encoded public key: " + err.Error())
    32  	}
    33  
    34  	switch pub := pub.(type) {
    35  	case *rsa.PublicKey:
    36  		fmt.Println("pub is of type RSA:", pub)
    37  	case *dsa.PublicKey:
    38  		fmt.Println("pub is of type DSA:", pub)
    39  	case *ecdsa.PublicKey:
    40  		fmt.Println("pub is of type ECDSA:", pub.Curve.Params().Name)
    41  	case ed25519.PublicKey:
    42  		fmt.Println("pub is of type Ed25519:", pub)
    43  	default:
    44  		panic("unknown type of public key")
    45  	}
    46  	isSM2 := sm2.IsSM2PublicKey(pub)
    47  	fmt.Printf("%v\n", isSM2)
    48  	// Output:
    49  	// pub is of type ECDSA: sm2p256v1
    50  	// true
    51  }
    52  
    53  func ExampleParsePKCS8PrivateKey() {
    54  	const privPEM = `
    55  -----BEGIN PRIVATE KEY-----
    56  MIGHAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBG0wawIBAQQgW+2/sIbWJ5bqzQ4D
    57  Vh8sQ2B/6I1PLGIcItXgGxAcdA6hRANCAAQ/Sx9dzxrMJwgoHmQ76X6g4EoM/2ca
    58  Cm0E4OyvrAVYYipqoI2JhFccq9ZYC5cA9cMj9JW0l5fBtSHp3dSd6wNH
    59  -----END PRIVATE KEY-----`
    60  	block, _ := pem.Decode([]byte(privPEM))
    61  	if block == nil {
    62  		panic("failed to parse PEM block containing the private key")
    63  	}
    64  	key, err := smx509.ParsePKCS8PrivateKey(block.Bytes)
    65  	if err != nil {
    66  		panic("failed to parse DER encoded private key: " + err.Error())
    67  	}
    68  	switch priv := key.(type) {
    69  	case *sm2.PrivateKey:
    70  		fmt.Println("priv is of type SM2:", priv.Params().Name)
    71  	default:
    72  		panic("unexpected type of private key")
    73  	}
    74  	// Output:
    75  	// priv is of type SM2: sm2p256v1
    76  }
    77  
    78  func ExampleParseTypedECPrivateKey() {
    79  	// Of course, you can remove EC PARAMETERS to make it simple.
    80  	// https://security.stackexchange.com/questions/29778/why-does-openssl-writes-ec-parameters-when-generating-private-key
    81  	const privPEM = `	
    82  -----BEGIN EC PARAMETERS-----
    83  BggqgRzPVQGCLQ==
    84  -----END EC PARAMETERS-----
    85  -----BEGIN EC PRIVATE KEY-----
    86  MHcCAQEEIFvtv7CG1ieW6s0OA1YfLENgf+iNTyxiHCLV4BsQHHQOoAoGCCqBHM9V
    87  AYItoUQDQgAEP0sfXc8azCcIKB5kO+l+oOBKDP9nGgptBODsr6wFWGIqaqCNiYRX
    88  HKvWWAuXAPXDI/SVtJeXwbUh6d3UnesDRw==
    89  -----END EC PRIVATE KEY-----`
    90  	var keyDERBlock *pem.Block
    91  	keyPEMBlock := []byte(privPEM)
    92  	for {
    93  		keyDERBlock, keyPEMBlock = pem.Decode(keyPEMBlock)
    94  		if keyDERBlock == nil {
    95  			break
    96  		}
    97  		if keyDERBlock.Type == "EC PARAMETERS" {
    98  			var oid asn1.ObjectIdentifier
    99  			_, err := asn1.Unmarshal(keyDERBlock.Bytes, &oid)
   100  			if err != nil {
   101  				panic("failed to parse private key ecparams")
   102  			}
   103  			fmt.Printf("%v\n", oid)
   104  		}
   105  		if keyDERBlock.Type == "EC PRIVATE KEY" || strings.HasSuffix(keyDERBlock.Type, " PRIVATE KEY") {
   106  			break
   107  		}
   108  	}
   109  	if keyDERBlock == nil {
   110  		panic("failed to parse PEM block containing the private key")
   111  	}
   112  
   113  	key, err := smx509.ParseTypedECPrivateKey(keyDERBlock.Bytes)
   114  	if err != nil {
   115  		panic("failed to parse DER encoded private key: " + err.Error())
   116  	}
   117  	switch priv := key.(type) {
   118  	case *sm2.PrivateKey:
   119  		fmt.Println("priv is of type SM2:", priv.Params().Name)
   120  	default:
   121  		panic("unexpected type of private key")
   122  	}
   123  	// Output:
   124  	// 1.2.156.10197.1.301
   125  	// priv is of type SM2: sm2p256v1
   126  }
   127  
   128  func ExampleParseSM2PrivateKey() {
   129  	// Of course, you can remove EC PARAMETERS to make it simple.
   130  	// https://security.stackexchange.com/questions/29778/why-does-openssl-writes-ec-parameters-when-generating-private-key
   131  	const privPEM = `	
   132  -----BEGIN EC PARAMETERS-----
   133  BggqgRzPVQGCLQ==
   134  -----END EC PARAMETERS-----
   135  -----BEGIN EC PRIVATE KEY-----
   136  MHcCAQEEIFvtv7CG1ieW6s0OA1YfLENgf+iNTyxiHCLV4BsQHHQOoAoGCCqBHM9V
   137  AYItoUQDQgAEP0sfXc8azCcIKB5kO+l+oOBKDP9nGgptBODsr6wFWGIqaqCNiYRX
   138  HKvWWAuXAPXDI/SVtJeXwbUh6d3UnesDRw==
   139  -----END EC PRIVATE KEY-----`
   140  	var keyDERBlock *pem.Block
   141  	keyPEMBlock := []byte(privPEM)
   142  	for {
   143  		keyDERBlock, keyPEMBlock = pem.Decode(keyPEMBlock)
   144  		if keyDERBlock == nil {
   145  			break
   146  		}
   147  		if keyDERBlock.Type == "EC PRIVATE KEY" || strings.HasSuffix(keyDERBlock.Type, " PRIVATE KEY") {
   148  			break
   149  		}
   150  	}
   151  	if keyDERBlock == nil {
   152  		panic("failed to parse PEM block containing the private key")
   153  	}
   154  
   155  	key, err := smx509.ParseSM2PrivateKey(keyDERBlock.Bytes)
   156  	if err != nil {
   157  		panic("failed to parse DER encoded private key: " + err.Error())
   158  	}
   159  	fmt.Println("priv is of type SM2:", key.Params().Name)
   160  	// Output:
   161  	// priv is of type SM2: sm2p256v1	
   162  }