| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package internal |
| 6 |
|
| 7 |
import ( |
| 8 |
"crypto/x509" |
| 9 |
"encoding/pem" |
| 10 |
"fmt" |
| 11 |
"io/ioutil" |
| 12 |
"math/big" |
| 13 |
"os" |
| 14 |
"path/filepath" |
| 15 |
"syscall" |
| 16 |
"unsafe" |
| 17 |
) |
| 18 |
|
| 19 |
var ( |
| 20 |
FirefoxProfiles = []string{os.Getenv("USERPROFILE") + "\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles"} |
| 21 |
CertutilInstallHelp = "" |
| 22 |
NSSBrowsers = "Firefox" |
| 23 |
) |
| 24 |
|
| 25 |
var ( |
| 26 |
modcrypt32 = syscall.NewLazyDLL("crypt32.dll") |
| 27 |
procCertAddEncodedCertificateToStore = modcrypt32.NewProc("CertAddEncodedCertificateToStore") |
| 28 |
procCertCloseStore = modcrypt32.NewProc("CertCloseStore") |
| 29 |
procCertDeleteCertificateFromStore = modcrypt32.NewProc("CertDeleteCertificateFromStore") |
| 30 |
procCertDuplicateCertificateContext = modcrypt32.NewProc("CertDuplicateCertificateContext") |
| 31 |
procCertEnumCertificatesInStore = modcrypt32.NewProc("CertEnumCertificatesInStore") |
| 32 |
procCertOpenSystemStoreW = modcrypt32.NewProc("CertOpenSystemStoreW") |
| 33 |
) |
| 34 |
|
| 35 |
func (m *mkcert) installPlatform() bool { |
| 36 |
|
| 37 |
cert, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootName)) |
| 38 |
fatalIfErr(err, "failed to read root certificate") |
| 39 |
|
| 40 |
if certBlock, _ := pem.Decode(cert); certBlock == nil || certBlock.Type != "CERTIFICATE" { |
| 41 |
fatalIfErr(fmt.Errorf("invalid PEM data"), "decode pem") |
| 42 |
} else { |
| 43 |
cert = certBlock.Bytes |
| 44 |
} |
| 45 |
|
| 46 |
store, err := openWindowsRootStore() |
| 47 |
fatalIfErr(err, "open root store") |
| 48 |
defer store.close() |
| 49 |
|
| 50 |
fatalIfErr(store.addCert(cert), "add cert") |
| 51 |
return true |
| 52 |
} |
| 53 |
|
| 54 |
func (m *mkcert) uninstallPlatform() bool { |
| 55 |
|
| 56 |
|
| 57 |
store, err := openWindowsRootStore() |
| 58 |
fatalIfErr(err, "open root store") |
| 59 |
defer store.close() |
| 60 |
|
| 61 |
deletedAny, err := store.deleteCertsWithSerial(m.caCert.SerialNumber) |
| 62 |
if err == nil && !deletedAny { |
| 63 |
err = fmt.Errorf("no certs found") |
| 64 |
} |
| 65 |
fatalIfErr(err, "delete cert") |
| 66 |
return true |
| 67 |
} |
| 68 |
|
| 69 |
type windowsRootStore uintptr |
| 70 |
|
| 71 |
func openWindowsRootStore() (windowsRootStore, error) { |
| 72 |
rootStr, err := syscall.UTF16PtrFromString("ROOT") |
| 73 |
if err != nil { |
| 74 |
return 0, err |
| 75 |
} |
| 76 |
store, _, err := procCertOpenSystemStoreW.Call(0, uintptr(unsafe.Pointer(rootStr))) |
| 77 |
if store != 0 { |
| 78 |
return windowsRootStore(store), nil |
| 79 |
} |
| 80 |
return 0, fmt.Errorf("failed to open windows root store: %v", err) |
| 81 |
} |
| 82 |
|
| 83 |
func (w windowsRootStore) close() error { |
| 84 |
ret, _, err := procCertCloseStore.Call(uintptr(w), 0) |
| 85 |
if ret != 0 { |
| 86 |
return nil |
| 87 |
} |
| 88 |
return fmt.Errorf("failed to close windows root store: %v", err) |
| 89 |
} |
| 90 |
|
| 91 |
func (w windowsRootStore) addCert(cert []byte) error { |
| 92 |
|
| 93 |
ret, _, err := procCertAddEncodedCertificateToStore.Call( |
| 94 |
uintptr(w), |
| 95 |
uintptr(syscall.X509_ASN_ENCODING|syscall.PKCS_7_ASN_ENCODING), |
| 96 |
uintptr(unsafe.Pointer(&cert[0])), |
| 97 |
uintptr(len(cert)), |
| 98 |
3, |
| 99 |
0, |
| 100 |
) |
| 101 |
if ret != 0 { |
| 102 |
return nil |
| 103 |
} |
| 104 |
return fmt.Errorf("failed adding cert: %v", err) |
| 105 |
} |
| 106 |
|
| 107 |
func (w windowsRootStore) deleteCertsWithSerial(serial *big.Int) (bool, error) { |
| 108 |
|
| 109 |
var cert *syscall.CertContext |
| 110 |
deletedAny := false |
| 111 |
for { |
| 112 |
|
| 113 |
certPtr, _, err := procCertEnumCertificatesInStore.Call(uintptr(w), uintptr(unsafe.Pointer(cert))) |
| 114 |
if cert = (*syscall.CertContext)(unsafe.Pointer(certPtr)); cert == nil { |
| 115 |
if errno, ok := err.(syscall.Errno); ok && errno == 0x80092004 { |
| 116 |
break |
| 117 |
} |
| 118 |
return deletedAny, fmt.Errorf("failed enumerating certs: %v", err) |
| 119 |
} |
| 120 |
|
| 121 |
certBytes := (*[1 << 20]byte)(unsafe.Pointer(cert.EncodedCert))[:cert.Length] |
| 122 |
parsedCert, err := x509.ParseCertificate(certBytes) |
| 123 |
|
| 124 |
if err == nil && parsedCert.SerialNumber != nil && parsedCert.SerialNumber.Cmp(serial) == 0 { |
| 125 |
|
| 126 |
dupCertPtr, _, err := procCertDuplicateCertificateContext.Call(uintptr(unsafe.Pointer(cert))) |
| 127 |
if dupCertPtr == 0 { |
| 128 |
return deletedAny, fmt.Errorf("failed duplicating context: %v", err) |
| 129 |
} |
| 130 |
if ret, _, err := procCertDeleteCertificateFromStore.Call(dupCertPtr); ret == 0 { |
| 131 |
return deletedAny, fmt.Errorf("failed deleting certificate: %v", err) |
| 132 |
} |
| 133 |
deletedAny = true |
| 134 |
} |
| 135 |
} |
| 136 |
return deletedAny, nil |
| 137 |
} |