github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/azure/pkcs12/safebags.go (about) 1 package pkcs12 2 3 import ( 4 "crypto/x509/pkix" 5 "encoding/asn1" 6 "errors" 7 ) 8 9 //see https://tools.ietf.org/html/rfc7292#appendix-D 10 var ( 11 oidPkcs8ShroudedKeyBagType = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 12, 10, 1, 2} 12 oidCertBagType = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 12, 10, 1, 3} 13 14 oidCertTypeX509Certificate = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 22, 1} 15 ) 16 17 type certBag struct { 18 Id asn1.ObjectIdentifier 19 Data []byte `asn1:"tag:0,explicit"` 20 } 21 22 func getAlgorithmParams(salt []byte, iterations int) (asn1.RawValue, error) { 23 params := pbeParams{ 24 Salt: salt, 25 Iterations: iterations, 26 } 27 28 return convertToRawVal(params) 29 } 30 31 func encodePkcs8ShroudedKeyBag(privateKey interface{}, password []byte) (bytes []byte, err error) { 32 privateKeyBytes, err := marshalPKCS8PrivateKey(privateKey) 33 34 if err != nil { 35 return nil, errors.New("pkcs12: error encoding PKCS#8 private key: " + err.Error()) 36 } 37 38 salt, err := makeSalt(pbeSaltSizeBytes) 39 if err != nil { 40 return nil, errors.New("pkcs12: error creating PKCS#8 salt: " + err.Error()) 41 } 42 43 pkData, err := pbEncrypt(privateKeyBytes, salt, password, pbeIterationCount) 44 if err != nil { 45 return nil, errors.New("pkcs12: error encoding PKCS#8 shrouded key bag when encrypting cert bag: " + err.Error()) 46 } 47 48 params, err := getAlgorithmParams(salt, pbeIterationCount) 49 if err != nil { 50 return nil, errors.New("pkcs12: error encoding PKCS#8 shrouded key bag algorithm's parameters: " + err.Error()) 51 } 52 53 pkinfo := encryptedPrivateKeyInfo{ 54 AlgorithmIdentifier: pkix.AlgorithmIdentifier{ 55 Algorithm: oidPbeWithSHAAnd3KeyTripleDESCBC, 56 Parameters: params, 57 }, 58 EncryptedData: pkData, 59 } 60 61 bytes, err = asn1.Marshal(pkinfo) 62 if err != nil { 63 return nil, errors.New("pkcs12: error encoding PKCS#8 shrouded key bag: " + err.Error()) 64 } 65 66 return bytes, err 67 }