github.com/emmansun/gmsm@v0.29.1/smx509/pkcs1.go (about) 1 package smx509 2 3 import ( 4 "crypto/rsa" 5 "crypto/x509" 6 "math/big" 7 ) 8 9 // pkcs1PrivateKey is a structure which mirrors the PKCS #1 ASN.1 for an RSA private key. 10 type pkcs1PrivateKey struct { 11 Version int 12 N *big.Int 13 E int 14 D *big.Int 15 P *big.Int 16 Q *big.Int 17 // We ignore these values, if present, because rsa will calculate them. 18 Dp *big.Int `asn1:"optional"` 19 Dq *big.Int `asn1:"optional"` 20 Qinv *big.Int `asn1:"optional"` 21 22 AdditionalPrimes []pkcs1AdditionalRSAPrime `asn1:"optional,omitempty"` 23 } 24 25 type pkcs1AdditionalRSAPrime struct { 26 Prime *big.Int 27 28 // We ignore these values because rsa will calculate them. 29 Exp *big.Int 30 Coeff *big.Int 31 } 32 33 // pkcs1PublicKey reflects the ASN.1 structure of a PKCS #1 public key. 34 type pkcs1PublicKey struct { 35 N *big.Int 36 E int 37 } 38 39 // ParsePKCS1PrivateKey parses an RSA private key in PKCS #1, ASN.1 DER form. 40 // 41 // This kind of key is commonly encoded in PEM blocks of type "RSA PRIVATE KEY". 42 func ParsePKCS1PrivateKey(der []byte) (*rsa.PrivateKey, error) { 43 return x509.ParsePKCS1PrivateKey(der) 44 } 45 46 // MarshalPKCS1PrivateKey converts an RSA private key to PKCS #1, ASN.1 DER form. 47 // 48 // This kind of key is commonly encoded in PEM blocks of type "RSA PRIVATE KEY". 49 // For a more flexible key format which is not RSA specific, use 50 // MarshalPKCS8PrivateKey. 51 func MarshalPKCS1PrivateKey(key *rsa.PrivateKey) []byte { 52 return x509.MarshalPKCS1PrivateKey(key) 53 } 54 55 // ParsePKCS1PublicKey parses an RSA public key in PKCS #1, ASN.1 DER form. 56 // 57 // This kind of key is commonly encoded in PEM blocks of type "RSA PUBLIC KEY". 58 func ParsePKCS1PublicKey(der []byte) (*rsa.PublicKey, error) { 59 return x509.ParsePKCS1PublicKey(der) 60 } 61 62 // MarshalPKCS1PublicKey converts an RSA public key to PKCS #1, ASN.1 DER form. 63 // 64 // This kind of key is commonly encoded in PEM blocks of type "RSA PUBLIC KEY". 65 func MarshalPKCS1PublicKey(key *rsa.PublicKey) []byte { 66 return x509.MarshalPKCS1PublicKey(key) 67 }